{ config, pkgs, lib, ... }: let 
  launch_emacs = pkgs.writeScript "emacsclient-or-start" ''
    #!${pkgs.bash}/bin/bash
    emacsclient --create-frame --alternate-editor=""
  '';
in {
  imports = [
    ./wayland.nix
  ];
  services.displayManager.ly = {
    enable = true;
  };

  services.displayManager.sessionPackages = [ pkgs.dwl ];
  #services.gnome.gnome-keyring.enable = lib.mkDefault true;
  systemd.packages = [ pkgs.dwl ];

  nixpkgs.overlays = [
    (final: prev: {
      # wlroots_0_19 = prev.wlroots_0_18.overrideAttrs (old: {
      #   version = "0.20.0-dev";
      #   src = final.fetchFromGitLab {
      #     domain = "gitlab.freedesktop.org";
      #     owner = "wlroots";
      #     repo = "wlroots";
      #     rev = "106f0f950639924d706df21f48545b09db8197ed"; # 0.20.0-dev # 2025-11-14
      #     hash = "sha256-Xybhb4hdvrfLCa0HopnKa6HP4b5gaepK5Y54YUg/TKM=";
      #   };
      # });
      dwl = (prev.dwl.override { wlroots = final.wlroots_0_19; }).overrideAttrs (old: let
        dwl-patches = pkgs.fetchgit {
          url = "https://codeberg.org/dwl/dwl-patches.git";
          rev = "46021333a10ecb9179c8477588fafd75c7cc7a05";
          hash = "sha256-Q39EYfGNTSsfViTxW9ZFLqaw/udqkBc1hb7hEfjmsgY=";
        };
        patches = let
          dwl-patch = p: "${dwl-patches}/patches/${p}/${p}.patch";
        in [
          (dwl-patch "ipc")
          (dwl-patch "restore-monitor")
          (dwl-patch "regexrules")
          (dwl-patch "autostart")
        ];
      in {
        version = "28-09-2025";
        
        src = final.fetchFromGitea {
          domain = "codeberg.org";
          owner = "dwl";
          repo = "dwl";
          rev = "6cd26568d5b8be2252ac0def36cd194b4fb2d7c3";
          hash = "sha256-ihxF9Z4uT0K3omO4mbzkeICY/RyqvuD+C5JSGWIf6MI=";
        };
 
        patches = (old.patches or []) ++ patches;
        postPatch = (old.postPatch or "") + ''
          export termcmd=${pkgs.foot}/bin/foot
          export editcmd=${launch_emacs}
          export menucmd=${pkgs.fuzzel}/bin/fuzzel
          export barcmd=${pkgs.waybar}/bin/waybar
          export playcmd=${pkgs.writeScript "playerctl-play-pause" ''
            #!${pkgs.bash}/bin/bash
            ${pkgs.playerctl}/bin/playerctl play-pause
          ''}
          export scrncmd=${pkgs.writeScript "screenshot-region" ''
           #!${pkgs.bash}/bin/bash
           ${pkgs.slurp}/bin/slurp | ${pkgs.grim}/bin/grim -g - - | ${pkgs.wl-clipboard}/bin/wl-copy
          ''}
          substituteAll ${./dwl/config.def.h} ./config.def.h
        '';
        postInstall = ''
          chmod ugo+x $out/share/wayland-sessions/dwl.desktop
        '';
        passthru.providedSessions = ["dwl"];
      });
    })
  ];

  environment.sessionVariables = {
    # Breaks WebCord
    # NIXOS_OZONE_WL=1;
    WLR_NO_HARDWARE_CURSORS=1;
    SDL_VIDEODRIVER="wayland";
    QT_QPA_PLATFORM="wayland-egl";
    QT_WAYLAND_DISABLE_WINDOW_DECORATIONS="1";
    __JAVA_AWT_WM_NONREPARENTING=1;
    GTK_THEME="Adwaita-dark";
  };
  
  xdg.portal = {
    enable = lib.mkDefault true;
    
    configPackages = [ pkgs.dwl ];
    
    # Recommended by upstream, required for screencast support
    # https://github.com/YaLTeR/niri/wiki/Important-Software#portals
    extraPortals = [
      pkgs.xdg-desktop-portal-gnome
      pkgs.xdg-desktop-portal-gtk
    ];

    wlr.enable = true;
  };
  
  security = {
    polkit.enable = true;
    pam.services.swaylock = { };
  };

  services.graphical-desktop.enable = true;

  services.xserver.desktopManager.runXdgAutostartIfNone = lib.mkDefault true;

  environment.systemPackages = with pkgs; [
    dwl
    waybar
    foot
    pwvucontrol
  ];
}
