about summary refs log tree commit diff
path: root/nixos/modules/services/display-managers/default.nix
blob: de3feb500f33b7f1c39a9878678b48e9bde4ff14 (plain) (blame)
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
{ config, lib, pkgs, ... }:

let
  cfg = config.services.displayManager;

  installedSessions = pkgs.runCommand "desktops"
    { # trivial derivation
      preferLocalBuild = true;
      allowSubstitutes = false;
    }
    ''
      mkdir -p "$out/share/"{xsessions,wayland-sessions}

      ${lib.concatMapStrings (pkg: ''
        for n in ${lib.concatStringsSep " " pkg.providedSessions}; do
          if ! test -f ${pkg}/share/wayland-sessions/$n.desktop -o \
                    -f ${pkg}/share/xsessions/$n.desktop; then
            echo "Couldn't find provided session name, $n.desktop, in session package ${pkg.name}:"
            echo "  ${pkg}"
            return 1
          fi
        done

        if test -d ${pkg}/share/xsessions; then
          ${pkgs.buildPackages.xorg.lndir}/bin/lndir ${pkg}/share/xsessions $out/share/xsessions
        fi
        if test -d ${pkg}/share/wayland-sessions; then
          ${pkgs.buildPackages.xorg.lndir}/bin/lndir ${pkg}/share/wayland-sessions $out/share/wayland-sessions
        fi
      '') cfg.sessionPackages}
    '';

  dmDefault = config.services.xserver.desktopManager.default;
  # fallback default for cases when only default wm is set
  dmFallbackDefault = if dmDefault != null then dmDefault else "none";
  wmDefault = config.services.xserver.windowManager.default;
  defaultSessionFromLegacyOptions = dmFallbackDefault + lib.optionalString (wmDefault != null && wmDefault != "none") "+${wmDefault}";
in
{
  options = {
    services.displayManager = {
      enable = lib.mkEnableOption "systemd's display-manager service";

      preStart = lib.mkOption {
        type = lib.types.lines;
        default = "";
        example = "rm -f /var/log/my-display-manager.log";
        description = "Script executed before the display manager is started.";
      };

      execCmd = lib.mkOption {
        type = lib.types.str;
        example = lib.literalExpression ''"''${pkgs.lightdm}/bin/lightdm"'';
        description = "Command to start the display manager.";
      };

      environment = lib.mkOption {
        type = with lib.types; attrsOf unspecified;
        default = {};
        description = "Additional environment variables needed by the display manager.";
      };

      hiddenUsers = lib.mkOption {
        type = with lib.types; listOf str;
        default = [ "nobody" ];
        description = ''
          A list of users which will not be shown in the display manager.
        '';
      };

      logToFile = lib.mkOption {
        type = lib.types.bool;
        default = false;
        description = ''
          Whether the display manager redirects the output of the
          session script to {file}`~/.xsession-errors`.
        '';
      };

      logToJournal = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = ''
          Whether the display manager redirects the output of the
          session script to the systemd journal.
        '';
      };

      # Configuration for automatic login. Common for all DM.
      autoLogin = lib.mkOption {
        type = lib.types.submodule ({ config, options, ... }: {
          options = {
            enable = lib.mkOption {
              type = lib.types.bool;
              default = config.user != null;
              defaultText = lib.literalExpression "config.${options.user} != null";
              description = ''
                Automatically log in as {option}`autoLogin.user`.
              '';
            };

            user = lib.mkOption {
              type = with lib.types; nullOr str;
              default = null;
              description = ''
                User to be used for the automatic login.
              '';
            };
          };
        });

        default = {};
        description = ''
          Auto login configuration attrset.
        '';
      };

      defaultSession = lib.mkOption {
        type = lib.types.nullOr lib.types.str // {
          description = "session name";
          check = d:
            lib.assertMsg (d != null -> (lib.types.str.check d && lib.elem d config.services.displayManager.sessionData.sessionNames)) ''
                Default graphical session, '${d}', not found.
                Valid names for 'services.displayManager.defaultSession' are:
                  ${lib.concatStringsSep "\n  " cfg.displayManager.sessionData.sessionNames}
              '';
        };
        default =
          if dmDefault != null || wmDefault != null then
            defaultSessionFromLegacyOptions
          else
            null;
        defaultText = lib.literalMD ''
          Taken from display manager settings or window manager settings, if either is set.
        '';
        example = "gnome";
        description = ''
          Graphical session to pre-select in the session chooser (only effective for GDM, LightDM and SDDM).

          On GDM, LightDM and SDDM, it will also be used as a session for auto-login.
        '';
      };

      sessionData = lib.mkOption {
        description = "Data exported for display managers’ convenience";
        internal = true;
        default = {};
      };

      sessionPackages = lib.mkOption {
        type = lib.types.listOf (lib.types.package // {
          description = "package with provided sessions";
          check = p: lib.assertMsg
            (lib.types.package.check p && p ? providedSessions
            && p.providedSessions != [] && lib.all lib.isString p.providedSessions)
            ''
              Package, '${p.name}', did not specify any session names, as strings, in
              'passthru.providedSessions'. This is required when used as a session package.

              The session names can be looked up in:
                ${p}/share/xsessions
                ${p}/share/wayland-sessions
           '';
        });
        default = [];
        description = ''
          A list of packages containing x11 or wayland session files to be passed to the display manager.
        '';
      };
    };
  };

  imports = [
    (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "autoLogin" ] [ "services" "displayManager" "autoLogin" ])
    (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "defaultSession" ] [ "services" "displayManager" "defaultSession" ])
    (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "hiddenUsers" ] [ "services" "displayManager" "hiddenUsers" ])
    (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "job" "environment" ] [ "services" "displayManager" "environment" ])
    (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "job" "execCmd" ] [ "services" "displayManager" "execCmd" ])
    (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "job" "logToFile" ] [ "services" "displayManager" "logToFile" ])
    (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "job" "logToJournal" ] [ "services" "displayManager" "logToJournal" ])
    (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "job" "preStart" ] [ "services" "displayManager" "preStart" ])
    (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "sessionData" ] [ "services" "displayManager" "sessionData" ])
    (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "sessionPackages" ] [ "services" "displayManager" "sessionPackages" ])
  ];

  config = lib.mkIf cfg.enable {
    assertions = [
      { assertion = cfg.autoLogin.enable -> cfg.autoLogin.user != null;
        message = ''
          services.displayManager.autoLogin.enable requires services.displayManager.autoLogin.user to be set
        '';
      }
    ];

    warnings =
      lib.mkIf (dmDefault != null || wmDefault != null) [
        ''
          The following options are deprecated:
            ${lib.concatStringsSep "\n  " (map ({c, t}: t) (lib.filter ({c, t}: c != null) [
            { c = dmDefault; t = "- services.xserver.desktopManager.default"; }
            { c = wmDefault; t = "- services.xserver.windowManager.default"; }
            ]))}
          Please use
            services.displayManager.defaultSession = "${defaultSessionFromLegacyOptions}";
          instead.
        ''
      ];

    # Make xsessions and wayland sessions available in XDG_DATA_DIRS
    # as some programs have behavior that depends on them being present
    environment.sessionVariables.XDG_DATA_DIRS = lib.mkIf (cfg.sessionPackages != [ ]) [
      "${cfg.sessionData.desktops}/share"
    ];

    services.displayManager.sessionData = {
      desktops = installedSessions;
      sessionNames = lib.concatMap (p: p.providedSessions) config.services.displayManager.sessionPackages;
      # We do not want to force users to set defaultSession when they have only single DE.
      autologinSession =
        if cfg.defaultSession != null then
          cfg.defaultSession
        else if cfg.sessionData.sessionNames != [] then
          lib.head cfg.sessionData.sessionNames
        else
          null;
    };

    # so that the service won't be enabled when only startx is used
    systemd.services.display-manager.enable =
      let dmConf = config.services.xserver.displayManager;
          noDmUsed = !(dmConf.gdm.enable
                    || cfg.sddm.enable
                    || dmConf.xpra.enable
                    || dmConf.lightdm.enable);
      in lib.mkIf noDmUsed (lib.mkDefault false);

    systemd.services.display-manager = {
      description = "Display Manager";
      after = [ "acpid.service" "systemd-logind.service" "systemd-user-sessions.service" ];
      restartIfChanged = false;

      environment = lib.optionalAttrs config.hardware.opengl.setLdLibraryPath {
        LD_LIBRARY_PATH = lib.makeLibraryPath [ pkgs.addOpenGLRunpath.driverLink ];
      } // cfg.environment;

      preStart = cfg.preStart;
      script = lib.mkIf (config.systemd.services.display-manager.enable == true) cfg.execCmd;

      # Stop restarting if the display manager stops (crashes) 2 times
      # in one minute. Starting X typically takes 3-4s.
      startLimitIntervalSec = 30;
      startLimitBurst = 3;
      serviceConfig = {
        Restart = "always";
        RestartSec = "200ms";
        SyslogIdentifier = "display-manager";
      };
    };
  };
}