main
  1{inputs, ...}: {
  2  den.aspects.desktop.wm.niri = {
  3    nixos = {
  4      # See https://github.com/NixOS/nixpkgs/blob/bcb52fa87abbc7ec046c4817d85950c35dae92b7/nixos/modules/services/misc/graphical-desktop.nix
  5      services.graphical-desktop.enable = true;
  6
  7      programs.niri = {
  8        enable = true;
  9        useNautilus = true;
 10      };
 11    };
 12
 13    provides.to-users = {
 14      hjem = {
 15        user,
 16        pkgs,
 17        lib,
 18        config,
 19        ...
 20      }: let
 21        niriKeybindsKDLGenerator = binds: ''
 22          binds {
 23            ${lib.concatMapAttrsStringSep "\n  " (key: bind: ''${key} { ${bind}; }'') binds}
 24          }
 25        '';
 26
 27        ix-screenshot = pkgs.writeShellApplication {
 28          name = "ix-screenshot";
 29          meta.mainProgram = "ix-screenshot";
 30          runtimeInputs = with pkgs; [
 31            grim
 32            satty
 33            slurp
 34          ];
 35          text = ''
 36            MODE="''${1:-region}"
 37            case "''${MODE}" in
 38            region)
 39              grim -g "$(slurp -d)" -
 40              ;;
 41            window)
 42              niri msg action screenshot-window
 43              sleep 0.5
 44              wl-paste --type image/png
 45              ;;
 46            monitor-focused)
 47              grim -o "$(niri msg --json focused-output | jq --raw-output .name)" -
 48              ;;
 49            monitor-all)
 50              grim -
 51              ;;
 52            *)
 53              echo "\"''${MODE}\" is not a supported." >&2
 54              exit 1
 55              ;;
 56            esac | satty --filename - --output-filename "${config.xdg.user-dirs.directories.screenshots}/%Y-%m-%d_%H-%M-%S.png"
 57          '';
 58        };
 59      in {
 60        xdg.config.files =
 61          lib.mergeAttrsList
 62          (inputs.import-tree.withLib lib
 63            (i: i.initFilter (lib.hasSuffix ".kdl"))
 64            (i: i.map (x: {"niri/${lib.lists.last (lib.path.subpath.components (lib.path.splitRoot x).subpath)}".source = x;}))
 65            (i: i.leaves ./config))
 66          // {
 67            "niri/nix/pre.kdl".text = "";
 68            "niri/nix/post.kdl".text = ''
 69              include "keybinds.kdl"
 70
 71              xwayland-satellite { path "${lib.getExe pkgs.xwayland-satellite}"; }
 72            '';
 73            "niri/nix/keybinds.kdl" = {
 74              generator = niriKeybindsKDLGenerator;
 75              value = {
 76                "Mod+Grave" =
 77                  lib.mkIf (user.system.settings.desktop.app.term.default != null)
 78                  ''spawn "${user.system.settings.desktop.app.term.default}"'';
 79                "Print" = ''spawn-sh "${lib.getExe ix-screenshot}"'';
 80                "Mod+Shift+S" = ''spawn-sh "${lib.getExe ix-screenshot}"'';
 81                "Ctrl+Print" = ''spawn-sh "${lib.getExe ix-screenshot} monitor-all"'';
 82                "Alt+Print" = ''spawn-sh "${lib.getExe ix-screenshot} window"'';
 83              };
 84            };
 85          };
 86      };
 87    };
 88  };
 89
 90  den.aspects.desktop.wm.niri.themes = {
 91    catppuccin = {
 92      hjem.xdg.config.files."niri/nix/pre.kdl".text = ''
 93        layout {
 94        	focus-ring {
 95        		active-color "#cba6f7"
 96        	}
 97        }
 98        recent-windows {
 99        	highlight {
100        		active-color "#cba6f7"
101        		urgent-color "#f2cdcd"
102        	}
103        }
104      '';
105    };
106  };
107}