{ ... }: {
  flake.nixosModules.desktopOptions =
    { config, options, lib, pkgs, self, inputs, ... }:
    let
      cfg = config.chiasson.desktop;
      hmAvailable = lib.hasAttrByPath [ "home-manager" "sharedModules" ] options;
      guiEnabled = cfg.hyprland.enable || cfg.niri.enable || cfg.plasma.enable;
      dmsEnabled = cfg.shell == "dms";
      sddmIni = pkgs.formats.ini { };
      # Pixie SDDM theme — Qt6 main; upstream has a qt5 branch if you need it.
      pixieSddm = pkgs.stdenvNoCC.mkDerivation {
        pname = "pixie-sddm";
        version = "0-unstable-2026-03-29";
        src = pkgs.fetchFromGitHub {
          owner = "xCaptaiN09";
          repo = "pixie-sddm";
          rev = "12a5f459ebd6d699be42c188c10976c8bb7076d7";
          hash = "sha256-lmE/49ySuAZDh5xLochWqfSw9qWrIV+fYaK5T2Ckck8=";
        };
        dontConfigure = true;
        dontBuild = true;
        installPhase = ''
          mkdir -p "$out/share/sddm/themes/pixie"
          cp -r "$src"/. "$out/share/sddm/themes/pixie/"
        '';
        meta = {
          description = "Pixel / Material 3 inspired SDDM theme (Qt6)";
          homepage = "https://github.com/xCaptaiN09/pixie-sddm";
          license = lib.licenses.mit;
          platforms = lib.platforms.linux;
        };
      };
    in
    {
      options.chiasson.desktop = {
        defaultSession = lib.mkOption {
          type = lib.types.nullOr (lib.types.enum [
            "hyprland"
            "niri"
            "plasma"
          ]);
          default = null;
          example = "niri";
          description = ''
            DM session preference; `null` picks from which compositor flags are enabled. Turn on only
            the compositor you use so SDDM does not drag in extras.
          '';
        };

        shell = lib.mkOption {
          type = lib.types.nullOr (lib.types.enum [ "dms" ]);
          default = null;
          example = "dms";
          description = "Desktop shell overlay (e.g. DMS). Extend the enum when you add another.";
        };

        displayManager = {
          variant = lib.mkOption {
            type = lib.types.nullOr (lib.types.enum [
              "sddm"
              "dankgreeter"
            ]);
            default = null;
            description = ''
              SDDM vs DankGreeter (greetd + DMS — [docs](https://danklinux.com/docs/dankgreeter/nixos-flake)).
              `null`: Plasma-only → SDDM; Hyprland/Niri + `desktop.shell = "dms"` → DankGreeter; else SDDM.
            '';
          };

          greeter = {
            configHome = lib.mkOption {
              type = lib.types.nullOr lib.types.str;
              default = null;
              example = "/home/olivier";
              description = ''
                Whose DMS files to mirror into the greeter cache. `null` → first entry in `chiasson.users.enabled`.
              '';
            };
          };
        };

        displayManager.sddm = {
          wayland.enable = lib.mkEnableOption ''
            SDDM greeter on Wayland (nicer on HiDPI; turn off if the greeter glitches on your GPU).
          '' // {
            default = true;
          };

          enableHidpi = lib.mkOption {
            type = lib.types.bool;
            default = true;
            description = "Passed through to `services.displayManager.sddm.enableHidpi`.";
          };

          theme = {
            package = lib.mkOption {
              type = lib.types.nullOr lib.types.package;
              default = pixieSddm;
              description = ''
                Package providing `share/sddm/themes/<id>`. Default: bundled [Pixie](https://github.com/xCaptaiN09/pixie-sddm).
                `null` → Breeze. Other nixpkgs examples: `catppuccin-sddm`, `sddm-sugar-dark`.
              '';
            };

            id = lib.mkOption {
              type = lib.types.str;
              default = "pixie";
              description = ''
                Subdir under `share/sddm/themes/` (default `pixie`). Match your theme package (e.g. catppuccin ids).
              '';
            };
          };

          settings = lib.mkOption {
            type = sddmIni.type;
            default = { };
            description = "Extra `services.displayManager.sddm.settings` (INI).";
          };
        };

        plasma.enable = lib.mkOption {
          type = lib.types.bool;
          default = false;
          description = "Plasma 6 session bits for this flake.";
        };

        defaultPackages = {
          enabled = lib.mkOption {
            type = lib.types.bool;
            default = true;
            description = "Shared desktop utility packages (ntfs3g, cifs-utils, …).";
          };
          packages = lib.mkOption {
            type = lib.types.listOf lib.types.package;
            default = with pkgs; [
              ntfs3g
              cifs-utils
              usbutils
              xhost
            ];
            description = "Packages merged when `defaultPackages.enabled` is true.";
          };
        };

        extraPackages = lib.mkOption {
          type = lib.types.listOf lib.types.package;
          default = [ ];
          description = "Extra packages on GUI hosts.";
        };

        homeManager = {
          bundleWisdom = lib.mkOption {
            type = lib.types.bool;
            default = true;
            description = ''
              Add `wisdom` (baseline + bash) to HM `sharedModules`. Per-user `extraModules` should use `lib.wisdomCatalogExtraModules self` once and set `chiasson.home.*.enable`.
            '';
          };
        };

        keyring = {
          enable = lib.mkEnableOption ''
            gnome-keyring + pam (login + sddm or greetd) + HM user service + `gcr` + `services.xserver.updateDbusEnvironment`.
            niri/hyprland: `dbus-update-activation-environment` at compositor start so libsecret/Electron see `WAYLAND_DISPLAY`.
          '' // {
            default = true;
          };
        };
      };

      config = lib.mkMerge [
        {
          assertions = [
            {
              assertion = cfg.defaultSession != "hyprland" || cfg.hyprland.enable;
              message = "chiasson.desktop.defaultSession = \"hyprland\" requires chiasson.desktop.hyprland.enable = true.";
            }
            {
              assertion = cfg.defaultSession != "niri" || cfg.niri.enable;
              message = "chiasson.desktop.defaultSession = \"niri\" requires chiasson.desktop.niri.enable = true.";
            }
            {
              assertion = cfg.defaultSession != "plasma" || cfg.plasma.enable;
              message = "chiasson.desktop.defaultSession = \"plasma\" requires chiasson.desktop.plasma.enable = true.";
            }
            {
              assertion =
                cfg.displayManager.variant != "dankgreeter" || cfg.hyprland.enable || cfg.niri.enable;
              message = "chiasson.desktop.displayManager.variant = \"dankgreeter\" requires chiasson.desktop.hyprland or chiasson.desktop.niri.";
            }
            {
              assertion = cfg.displayManager.variant != "dankgreeter" || cfg.shell == "dms";
              message = "DankGreeter expects chiasson.desktop.shell = \"dms\" for DMS/matugen sync; use SDDM or enable DMS.";
            }
          ];
        }
        (lib.mkIf guiEnabled {
          chiasson.desktop.displayManager.variant = lib.mkDefault (
            if cfg.plasma.enable && !(cfg.hyprland.enable || cfg.niri.enable) then
              "sddm"
            else if (cfg.hyprland.enable || cfg.niri.enable) && cfg.shell == "dms" then
              "dankgreeter"
            else
              "sddm"
          );
        })
        (lib.mkIf (guiEnabled && hmAvailable && cfg.homeManager.bundleWisdom) {
          "home-manager".sharedModules = [ self.homeManagerModules.wisdom ];
        })
        (lib.mkIf (dmsEnabled && hmAvailable) {
          "home-manager".sharedModules = [ self.homeManagerModules.desktopShellDms ];
        })
        (lib.mkIf (hmAvailable && (dmsEnabled || cfg.niri.enable)) {
          "home-manager".extraSpecialArgs = { inherit inputs; };
        })
      ];
    };
}
