1 { config, lib, pkgs, ... }:
4 cfg = config.services.displayManager;
6 installedSessions = pkgs.runCommand "desktops"
8 preferLocalBuild = true;
9 allowSubstitutes = false;
12 mkdir -p "$out/share/"{xsessions,wayland-sessions}
14 ${lib.concatMapStrings (pkg: ''
15 for n in ${lib.concatStringsSep " " pkg.providedSessions}; do
16 if ! test -f ${pkg}/share/wayland-sessions/$n.desktop -o \
17 -f ${pkg}/share/xsessions/$n.desktop; then
18 echo "Couldn't find provided session name, $n.desktop, in session package ${pkg.name}:"
24 if test -d ${pkg}/share/xsessions; then
25 ${pkgs.buildPackages.xorg.lndir}/bin/lndir ${pkg}/share/xsessions $out/share/xsessions
27 if test -d ${pkg}/share/wayland-sessions; then
28 ${pkgs.buildPackages.xorg.lndir}/bin/lndir ${pkg}/share/wayland-sessions $out/share/wayland-sessions
30 '') cfg.sessionPackages}
35 services.displayManager = {
36 enable = lib.mkEnableOption "systemd's display-manager service";
38 preStart = lib.mkOption {
39 type = lib.types.lines;
41 example = "rm -f /var/log/my-display-manager.log";
42 description = "Script executed before the display manager is started.";
45 execCmd = lib.mkOption {
47 example = lib.literalExpression ''"''${pkgs.lightdm}/bin/lightdm"'';
48 description = "Command to start the display manager.";
51 environment = lib.mkOption {
52 type = with lib.types; attrsOf unspecified;
54 description = "Additional environment variables needed by the display manager.";
57 hiddenUsers = lib.mkOption {
58 type = with lib.types; listOf str;
59 default = [ "nobody" ];
61 A list of users which will not be shown in the display manager.
65 logToFile = lib.mkOption {
66 type = lib.types.bool;
69 Whether the display manager redirects the output of the
70 session script to {file}`~/.xsession-errors`.
74 logToJournal = lib.mkOption {
75 type = lib.types.bool;
78 Whether the display manager redirects the output of the
79 session script to the systemd journal.
83 # Configuration for automatic login. Common for all DM.
84 autoLogin = lib.mkOption {
85 type = lib.types.submodule ({ config, options, ... }: {
87 enable = lib.mkOption {
88 type = lib.types.bool;
89 default = config.user != null;
90 defaultText = lib.literalExpression "config.${options.user} != null";
92 Automatically log in as {option}`autoLogin.user`.
97 type = with lib.types; nullOr str;
100 User to be used for the automatic login.
108 Auto login configuration attrset.
112 defaultSession = lib.mkOption {
113 type = lib.types.nullOr lib.types.str // {
114 description = "session name";
116 lib.assertMsg (d != null -> (lib.types.str.check d && lib.elem d cfg.sessionData.sessionNames)) ''
117 Default graphical session, '${d}', not found.
118 Valid names for 'services.displayManager.defaultSession' are:
119 ${lib.concatStringsSep "\n " cfg.sessionData.sessionNames}
125 Graphical session to pre-select in the session chooser (only effective for GDM, LightDM and SDDM).
127 On GDM, LightDM and SDDM, it will also be used as a session for auto-login.
129 Set this option to empty string to get an error with a list of currently available sessions.
133 sessionData = lib.mkOption {
134 description = "Data exported for display managers’ convenience";
139 sessionPackages = lib.mkOption {
140 type = lib.types.listOf (lib.types.package // {
141 description = "package with provided sessions";
142 check = p: lib.assertMsg
143 (lib.types.package.check p && p ? providedSessions
144 && p.providedSessions != [] && lib.all lib.isString p.providedSessions)
146 Package, '${p.name}', did not specify any session names, as strings, in
147 'passthru.providedSessions'. This is required when used as a session package.
149 The session names can be looked up in:
151 ${p}/share/wayland-sessions
156 A list of packages containing x11 or wayland session files to be passed to the display manager.
163 (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "autoLogin" ] [ "services" "displayManager" "autoLogin" ])
164 (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "defaultSession" ] [ "services" "displayManager" "defaultSession" ])
165 (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "hiddenUsers" ] [ "services" "displayManager" "hiddenUsers" ])
166 (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "job" "environment" ] [ "services" "displayManager" "environment" ])
167 (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "job" "execCmd" ] [ "services" "displayManager" "execCmd" ])
168 (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "job" "logToFile" ] [ "services" "displayManager" "logToFile" ])
169 (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "job" "logToJournal" ] [ "services" "displayManager" "logToJournal" ])
170 (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "job" "preStart" ] [ "services" "displayManager" "preStart" ])
171 (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "sessionData" ] [ "services" "displayManager" "sessionData" ])
172 (lib.mkRenamedOptionModule [ "services" "xserver" "displayManager" "sessionPackages" ] [ "services" "displayManager" "sessionPackages" ])
175 config = lib.mkIf cfg.enable {
177 { assertion = cfg.autoLogin.enable -> cfg.autoLogin.user != null;
179 services.displayManager.autoLogin.enable requires services.displayManager.autoLogin.user to be set
184 # Make xsessions and wayland sessions available in XDG_DATA_DIRS
185 # as some programs have behavior that depends on them being present
186 environment.sessionVariables.XDG_DATA_DIRS = lib.mkIf (cfg.sessionPackages != [ ]) [
187 "${cfg.sessionData.desktops}/share"
190 services.displayManager.sessionData = {
191 desktops = installedSessions;
192 sessionNames = lib.concatMap (p: p.providedSessions) cfg.sessionPackages;
193 # We do not want to force users to set defaultSession when they have only single DE.
195 if cfg.defaultSession != null then
197 else if cfg.sessionData.sessionNames != [] then
198 lib.head cfg.sessionData.sessionNames
203 # so that the service won't be enabled when only startx is used
204 systemd.services.display-manager.enable =
205 let dmConf = config.services.xserver.displayManager;
206 noDmUsed = !(dmConf.gdm.enable
208 || dmConf.xpra.enable
209 || dmConf.lightdm.enable
211 in lib.mkIf noDmUsed (lib.mkDefault false);
213 systemd.services.display-manager = {
214 description = "Display Manager";
215 after = [ "acpid.service" "systemd-logind.service" "systemd-user-sessions.service" ];
216 restartIfChanged = false;
218 environment = cfg.environment;
220 preStart = cfg.preStart;
221 script = lib.mkIf (config.systemd.services.display-manager.enable == true) cfg.execCmd;
223 # Stop restarting if the display manager stops (crashes) 2 times
224 # in one minute. Starting X typically takes 3-4s.
225 startLimitIntervalSec = 30;
229 RestartSec = "200ms";
230 SyslogIdentifier = "display-manager";