{
  lib,
  config,
  ...
}: let
  cfg = config.marleyos.wayland.niri;
in {
  options.marleyos.wayland.niri = {
    enable = lib.mkEnableOption "niri";

    monitors = lib.mkOption {
      type = with lib.types; nullOr (listOf str);
      default = null;
      description = "The monitors to configure";
    };
  };

  config = lib.mkIf cfg.enable {
    marleyos = {
      wayland.enable = true;

      programs = {
        waybar.enable = true;
        wlogout.enable = true;
        fuzzel.enable = true;
        xwayland-satellite.enable = true;
      };
      services = {
        swaybg.enable = true;
        swaync.enable = true;
      };
    };

    programs.niri = {
      # Auto-enabled by the nixos module
      # enable = true;

      settings = let
        cmd = command: {command = [command];};
      in {
        prefer-no-csd = true;
        hotkey-overlay.skip-at-startup = true;

        environment = {
          # For some reason this won't set properly from home.sessionVariables.
          DISPLAY = ":0";
        };

        ### Startup Programs ###
        spawn-at-startup = let
          inherit (config.marleyos) apps;
        in [
          {command = ["sh" "-c" "${lib.getExe apps.terminal} -e tmux new -s main -A"];}
          (cmd (lib.getExe apps.browser))
          (cmd (lib.getExe apps.email))
        ];

        ### Layout ###
        layout = let
          gradient = {
            gradient = with config.lib.stylix.colors.withHashtag; {
              from = base08;
              to = base0D;
              angle = 90;
            };
          };
        in {
          gaps = 16;
          center-focused-column = "never";

          preset-column-widths = [
            {proportion = 0.33333;}
            {proportion = 0.5;}
            {proportion = 0.66667;}
          ];

          default-column-width = {proportion = 0.5;};

          border = {
            width = 4;
            active = gradient;
          };

          shadow = {
            softness = 30;
            spread = 5;

            offset = {
              x = 0;
              y = 5;
            };

            color = "#0007";
          };
        };

        ### Screenshots ###
        screenshot-path = "~/pictures/screenshots/%Y-%m-%d %H-%M-%S.png";

        ### Workspaces ###
        workspaces = {
          "01-main" = {
            open-on-output = builtins.elemAt cfg.monitors 0;
            name = "main";
          };
          "02-web" = {
            open-on-output = builtins.elemAt cfg.monitors 1;
            name = "web";
          };
          "03-email" = {
            open-on-output = builtins.elemAt cfg.monitors 1;
            name = "email";
          };
        };

        ### Window Rules ###
        window-rules =
          [
            {
              matches = [{app-id = "^org\.wezfurlong\.wezterm$";}];
              default-column-width.proportion = 1.0;
            }

            {
              matches = [
                {
                  app-id = "^floorp$";
                  title = "^Picture-in-Picture$";
                }
                {title = "^Picture in picture$";}
              ];
              open-floating = true;
            }
          ]
          ++ (lib.optionals (cfg.monitors != null) [
            {
              matches = [
                {
                  app-id = "^floorp$";
                  at-startup = true;
                }
              ];
              open-on-output = builtins.elemAt cfg.monitors 1;
              open-on-workspace = "web";
              default-column-width.proportion = 1.0;
            }
            {
              matches = [
                {
                  app-id = "^thunderbird$";
                  at-startup = true;
                }
              ];
              open-on-output = builtins.elemAt cfg.monitors 1;
              open-on-workspace = "email";
              default-column-width.proportion = 1.0;
            }
          ]);

        ### Keybinds ###
        binds = with config.lib.niri.actions; {
          "Mod+Q".action = close-window;
          "Mod+Shift+E".action = quit;
          "Ctrl+Alt+Delete".action = quit;

          "Mod+Shift+Slash".action = show-hotkey-overlay;
          "Mod+Return".action = spawn (lib.getExe config.marleyos.apps.terminal);
          "Mod+R".action = spawn (lib.getExe config.programs.fuzzel.package);

          "Mod+H".action = focus-column-or-monitor-left;
          "Mod+J".action = focus-window-down;
          "Mod+K".action = focus-window-up;
          "Mod+L".action = focus-column-or-monitor-right;
          "Mod+Home".action = focus-column-first;
          "Mod+End".action = focus-column-last;

          "Mod+Shift+H".action = move-column-left-or-to-monitor-left;
          "Mod+Shift+J".action = move-window-down;
          "Mod+Shift+K".action = move-window-up;
          "Mod+Shift+L".action = move-column-right-or-to-monitor-right;
          "Mod+Shift+Home".action = move-column-to-first;
          "Mod+Shift+End".action = move-column-to-last;

          "Mod+Page_Down".action = focus-workspace-down;
          "Mod+Page_Up".action = focus-workspace-up;
          "Mod+Ctrl+Page_Down".action = move-workspace-up;
          "Mod+Ctrl+Page_Up".action = move-workspace-down;
          "Mod+Shift+Page_Down".action = move-column-to-workspace-down;
          "Mod+Shift+Page_Up".action = move-column-to-workspace-up;

          "Mod+Ctrl+H".action = focus-monitor-left;
          "Mod+Ctrl+L".action = focus-monitor-right;

          "Mod+BracketLeft".action = consume-or-expel-window-left;
          "Mod+BracketRight".action = consume-or-expel-window-right;
          "Mod+Comma".action = consume-window-into-column;
          "Mod+Period".action = expel-window-from-column;

          "Mod+P".action = switch-preset-column-width;
          "Mod+Shift+P".action = switch-preset-window-height;
          "Mod+Ctrl+P".action = reset-window-height;
          "Mod+F".action = maximize-column;
          "Mod+Shift+F".action = fullscreen-window;
          "Mod+Ctrl+F".action = expand-column-to-available-width;
          "Mod+C".action = center-column;
          "Mod+Minus".action = set-column-width "-10%";
          "Mod+Equal".action = set-column-width "+10%";
          "Mod+Shift+Minus".action = set-window-height "-10%";
          "Mod+Shift+Equal".action = set-window-height "+10%";

          "Mod+G".action = toggle-window-floating;
          "Mod+Shift+G".action = switch-focus-between-floating-and-tiling;
          "Mod+T".action = toggle-column-tabbed-display;

          "Print".action = screenshot;
          "Ctrl+Print".action = screenshot-screen;
          "Alt+Print".action = screenshot-window;

          "Mod+Escape".action = toggle-keyboard-shortcuts-inhibit;
        };
      };
    };
  };
}
