{ 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.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: {
        src = final.fetchFromGitLab {
          domain = "gitlab.freedesktop.org";
          owner = "wlroots";
          repo = "wlroots";
          rev = "d305934ebe6852785a1f425ee96861f0b7280d76"; # 0.19-prev # 25-01-25
          hash = "sha256-NnPSC5p/phTFe+nWp9vl8LUbmBO/RXSSUuDZ2boucXY=";
        };
      });
      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 = "4a869141fa643d9be792a9aa44a42cf7f7f882ad";
          hash = "sha256-xlaK9xMrc+KDNecsyByRIxXmPEZ2dmP4FQ0bMojsyws=";
        };
        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 = "21-09-2024";
        
        src = final.fetchFromGitea {
          domain = "codeberg.org";
          owner = "dwl";
          repo = "dwl";
          rev = "d1c2f434983562bd7d2ace15ab0c05155be603bc";
          hash = "sha256-SpfjQfzvbpDiihziyKGPoBwvp3evgCQQhhjldAE/MwY=";
        };
 
        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"];
      });
    })
  ];


  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
  ];
}
{ config, pkgs, lib, ... }: {
  programs.niri.enable = true;

  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";
  };

}
