summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/display-managers/dms-greeter.nix
blob: 53996b3f064f9e4663bf9052266cd77f21465cb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
{
  config,
  lib,
  pkgs,
  ...
}:
let
  inherit (lib)
    types
    mkEnableOption
    mkOption
    mkIf
    mkDefault
    literalExpression
    getExe
    makeBinPath
    optionalString
    escapeShellArgs
    ;

  cfg = config.services.displayManager.dms-greeter;
  cfgDms = config.programs.dms-shell;
  cfgAutoLogin = config.services.displayManager.autoLogin;
  sessionData = config.services.displayManager.sessionData;

  # Miracle WM is nested under `programs.wayland.miracle-wm` unlike the rest
  compositorOption =
    if cfg.compositor.name == "miracle-wm" then "wayland.miracle-wm" else "${cfg.compositor.name}";
  cacheDir = "/var/lib/dms-greeter";

  # Not all compositor packages match the name they use and Miracle does not have a package option
  compositorPkg =
    if cfg.compositor.name == "miracle-wm" then
      pkgs.miracle-wm
    else
      lib.attrByPath [
        "programs"
        cfg.compositor.name
        "package"
      ] null config;

  greeterScript = pkgs.writeShellScriptBin "dms-greeter-start" ''
    export PATH=$PATH:${
      makeBinPath [
        cfg.quickshell.package
        compositorPkg
        pkgs.glib # provides gdbus, used by the fprintd hardware probe and portal reads
      ]
    }
    ${
      escapeShellArgs (
        [
          "${cfg.package}/bin/dms-greeter"
          "--cache-dir"
          cacheDir
          "--command"
          cfg.compositor.name
        ]
        ++ lib.optionals (cfg.compositor.customConfig != "") [
          "-C"
          "${pkgs.writeText "dmsgreeter-compositor-config" cfg.compositor.customConfig}"
        ]
      )
    } ${optionalString cfg.logs.save "> ${cfg.logs.path} 2>&1"}
  '';

  autoLoginCommand =
    pkgs.runCommand "dms-greeter-autologin-command"
      {
        nativeBuildInputs = [
          pkgs.gnugrep
          pkgs.coreutils
        ];
      }
      ''
        set -euo pipefail

        session="${sessionData.autologinSession}"
        desktops="${sessionData.desktops}"

        for sessionFile in \
          "$desktops/share/wayland-sessions/$session.desktop" \
          "$desktops/share/xsessions/$session.desktop"
        do
          if [ -f "$sessionFile" ]; then
            command="$(grep -m1 '^Exec=' "$sessionFile" | cut -d= -f2- || true)"

            if [ -n "$command" ]; then
              printf '%s\n' "$command" > "$out"
              exit 0
            fi
          fi
        done

        echo "dms-greeter autologin: could not resolve Exec for session '$session'" >&2
        exit 1
      '';

  jq = getExe pkgs.jq;

  configFilesFromHome =
    if cfg.configHome != null then
      [
        "${cfg.configHome}/.config/DankMaterialShell/settings.json"
        "${cfg.configHome}/.local/state/DankMaterialShell/session.json"
        "${cfg.configHome}/.cache/DankMaterialShell/dms-colors.json"
      ]
    else
      [ ];
in
{
  options.services.displayManager.dms-greeter = {
    enable = mkEnableOption "DankMaterialShell greeter";

    package = lib.mkPackageOption pkgs "dms-greeter" { };

    compositor = {
      name = mkOption {
        type = types.enum [
          "niri"
          "hyprland"
          "sway"
          "mangowc"
          "miracle-wm"
          "labwc"
        ];
        example = "niri";
        description = ''
          The Wayland compositor to run the greeter in.

          The specified compositor must be enabled via its corresponding
          `programs.<compositor>.enable` option.

          Supported compositors:
          - niri: A scrollable-tiling Wayland compositor
          - hyprland: A dynamic tiling Wayland compositor
          - sway: An i3-compatible Wayland compositor
          - mango: A dwm-inspired Wayland compositor with modern config options and multiple layouts
          - miracle-wm: A keyboard-driven Wayland compositor with smooth animations and extensibility
          - labwc: Lightweight stacking Wayland compositor inspired by Openbox
        '';
      };

      customConfig = mkOption {
        type = types.lines;
        default = "";
        example = ''
          # Niri example
          input {
              keyboard {
                  xkb {
                      layout "us"
                  }
              }
          }
        '';
        description = ''
          Custom compositor configuration to use for the greeter session.

          This configuration is written to a file and passed to the compositor
          when launching the greeter. The format and available options depend
          on the selected compositor.

          Leave empty to use the system's default compositor configuration.
        '';
      };
    };

    configFiles = mkOption {
      type = types.listOf types.path;
      default = [ ];
      example = literalExpression ''
        [
          "/home/user/.config/DankMaterialShell/settings.json"
          "/home/user/.local/state/DankMaterialShell/session.json"
        ]
      '';
      description = ''
        List of DankMaterialShell configuration files to copy into the greeter
        data directory at `/var/lib/dms-greeter`.

        This is useful for preserving user preferences like wallpapers, themes,
        and other settings in the greeter screen.

        ::: {.tip}
        Use {option}`configHome` instead if your configuration files are in
        standard XDG locations.
        :::
      '';
    };

    configHome = mkOption {
      type = types.nullOr types.str;
      default = null;
      example = "/home/alice";
      description = ''
        Path to a user's home directory from which to copy DankMaterialShell
        configuration files.

        When set, the following files will be automatically copied to the greeter:
        - `~/.config/DankMaterialShell/settings.json`
        - `~/.local/state/DankMaterialShell/session.json`
        - `~/.cache/DankMaterialShell/dms-colors.json`

        If your configuration files are in non-standard locations, use the
        {option}`configFiles` option instead.
      '';
    };

    quickshell = {
      package = mkOption {
        type = types.package;
        default = if cfgDms.enable then cfgDms.quickshell.package else pkgs.quickshell;
        defaultText = literalExpression ''
          if config.programs.dms-shell.enable
          then config.programs.dms-shell.quickshell.package
          else pkgs.quickshell;
        '';
        description = ''
          The Quickshell package to use for the greeter.

          Defaults to the quickshell package from `programs.dms-shell` if it is enabled,
          otherwise defaults to `pkgs.quickshell`.
        '';
      };
    };

    logs = {
      save = mkEnableOption "saving logs from the DMS greeter to a file";

      path = mkOption {
        type = types.path;
        default = "/tmp/dms-greeter.log";
        example = "/var/log/dms-greeter.log";
        description = ''
          File path where DMS greeter logs will be saved.

          This is useful for debugging greeter issues. Logs will include
          output from both the greeter and the compositor.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion =
          # Assemble the full attribute structure because miracle-wm is not nested in the same location as the others
          lib.attrByPath (
            [ "programs" ] ++ lib.splitString "." compositorOption ++ [ "enable" ]
          ) false config;
        message = ''
          DankMaterialShell greeter: The compositor "${cfg.compositor.name}" is not enabled.

          Please enable the compositor via:
            programs.${compositorOption}.enable = true;
        '';
      }
      {
        assertion = cfgAutoLogin.enable -> sessionData.autologinSession != null;
        message = ''
          dms-greeter auto-login requires services.displayManager.defaultSession to be set,
          or at least one session in services.displayManager.sessionPackages.
        '';
      }
    ];

    services.greetd = {
      enable = mkDefault true;
      settings = {
        default_session = {
          user = "dms-greeter";
          command = getExe greeterScript;
        };
        initial_session = mkIf (cfgAutoLogin.enable && (cfgAutoLogin.user != null)) {
          inherit (cfgAutoLogin) user;
          command = ''${getExe pkgs.bash} -lc "${config.systemd.package}/bin/systemd-cat $(<${autoLoginCommand})"'';
        };
      };
    };

    environment.systemPackages = [ cfg.package ];

    fonts.packages = with pkgs; [
      fira-code
      inter
      material-symbols
    ];

    systemd.tmpfiles.settings."10-dms-greeter".${cacheDir}.d = {
      user = "dms-greeter";
      group = "dms-greeter";
      mode = "0750";
    };

    systemd.services.greetd.preStart =
      let
        allConfigFiles = cfg.configFiles ++ configFilesFromHome;
      in
      ''
        cd ${cacheDir}
        ${lib.concatStringsSep "\n" (
          lib.map (f: ''
            if [ -f "${f}" ]; then
                cp "${f}" .
            fi
          '') allConfigFiles
        )}

        if [ -f session.json ]; then
            copy_wallpaper() {
                local path=$(${jq} -r ".$1 // empty" session.json)
                if [ -f "$path" ]; then
                    cp "$path" "$2"
                    ${jq} ".$1 = \"${cacheDir}/$2\"" session.json > session.tmp
                    mv session.tmp session.json
                fi
            }

            copy_monitor_wallpapers() {
                ${jq} -r ".$1 // {} | to_entries[] | .key + \":\" + .value" session.json 2>/dev/null | while IFS=: read monitor path; do
                    local dest="$2-$(echo "$monitor" | tr -c '[:alnum:]' '-')"
                    if [ -f "$path" ]; then
                        cp "$path" "$dest"
                        ${jq} --arg m "$monitor" --arg p "${cacheDir}/$dest" ".$1[\$m] = \$p" session.json > session.tmp
                        mv session.tmp session.json
                    fi
                done
            }

            copy_wallpaper "wallpaperPath" "wallpaper"
            copy_wallpaper "wallpaperPathLight" "wallpaper-light"
            copy_wallpaper "wallpaperPathDark" "wallpaper-dark"
            copy_monitor_wallpapers "monitorWallpapers" "wallpaper-monitor"
            copy_monitor_wallpapers "monitorWallpapersLight" "wallpaper-monitor-light"
            copy_monitor_wallpapers "monitorWallpapersDark" "wallpaper-monitor-dark"
        fi

        if [ -f settings.json ]; then
            theme_file="$(${jq} -r '.customThemeFile // empty' settings.json)"
            if [ -f "$theme_file" ] && [ -r "$theme_file" ]; then
                cp "$theme_file" custom-theme.json
                mv settings.json settings.orig.json
                ${jq} '.customThemeFile = "${cacheDir}/custom-theme.json"' settings.orig.json > settings.json
            fi
        fi

        mv dms-colors.json colors.json || :
        chown dms-greeter:dms-greeter * || :
      '';

    services.displayManager.dms-greeter.configFiles = lib.mkIf (
      cfg.configHome != null
    ) configFilesFromHome;

    users.groups.dms-greeter = { };
    users.users.dms-greeter = {
      description = "DankMaterialShell greeter user";
      isSystemUser = true;
      home = cacheDir;
      homeMode = "0750";
      createHome = true;
      group = "dms-greeter";
      extraGroups = [ "video" ];
    };

    security.pam.services.dms-greeter = { };

    hardware.graphics.enable = mkDefault true;
    services.libinput.enable = mkDefault true;
  };

  meta.teams = [ lib.teams.danklinux ];
}