at 23.11-pre 19 kB view raw
1# This module declares the options to define a *display manager*, the 2# program responsible for handling X logins (such as LightDM, GDM, or SDDM). 3# The display manager allows the user to select a *session 4# type*. When the user logs in, the display manager starts the 5# *session script* ("xsession" below) to launch the selected session 6# type. The session type defines two things: the *desktop manager* 7# (e.g., KDE, Gnome or a plain xterm), and optionally the *window 8# manager* (e.g. kwin or twm). 9 10{ config, lib, options, pkgs, ... }: 11 12with lib; 13 14let 15 16 cfg = config.services.xserver; 17 opt = options.services.xserver; 18 xorg = pkgs.xorg; 19 20 fontconfig = config.fonts.fontconfig; 21 xresourcesXft = pkgs.writeText "Xresources-Xft" '' 22 Xft.antialias: ${if fontconfig.antialias then "1" else "0"} 23 Xft.rgba: ${fontconfig.subpixel.rgba} 24 Xft.lcdfilter: lcd${fontconfig.subpixel.lcdfilter} 25 Xft.hinting: ${if fontconfig.hinting.enable then "1" else "0"} 26 Xft.autohint: ${if fontconfig.hinting.autohint then "1" else "0"} 27 Xft.hintstyle: ${fontconfig.hinting.style} 28 ''; 29 30 # file provided by services.xserver.displayManager.sessionData.wrapper 31 xsessionWrapper = pkgs.writeScript "xsession-wrapper" 32 '' 33 #! ${pkgs.bash}/bin/bash 34 35 # Shared environment setup for graphical sessions. 36 37 . /etc/profile 38 if test -f ~/.profile; then 39 source ~/.profile 40 fi 41 42 cd "$HOME" 43 44 # Allow the user to execute commands at the beginning of the X session. 45 if test -f ~/.xprofile; then 46 source ~/.xprofile 47 fi 48 49 ${optionalString cfg.displayManager.job.logToJournal '' 50 if [ -z "$_DID_SYSTEMD_CAT" ]; then 51 export _DID_SYSTEMD_CAT=1 52 exec ${config.systemd.package}/bin/systemd-cat -t xsession "$0" "$@" 53 fi 54 ''} 55 56 ${optionalString cfg.displayManager.job.logToFile '' 57 exec &> >(tee ~/.xsession-errors) 58 ''} 59 60 # Load X defaults. This should probably be safe on wayland too. 61 ${xorg.xrdb}/bin/xrdb -merge ${xresourcesXft} 62 if test -e ~/.Xresources; then 63 ${xorg.xrdb}/bin/xrdb -merge ~/.Xresources 64 elif test -e ~/.Xdefaults; then 65 ${xorg.xrdb}/bin/xrdb -merge ~/.Xdefaults 66 fi 67 68 # Import environment variables into the systemd user environment. 69 ${optionalString (cfg.displayManager.importedVariables != []) ( 70 "/run/current-system/systemd/bin/systemctl --user import-environment " 71 + toString (unique cfg.displayManager.importedVariables) 72 )} 73 74 # Speed up application start by 50-150ms according to 75 # http://kdemonkey.blogspot.nl/2008/04/magic-trick.html 76 compose_cache="''${XCOMPOSECACHE:-$HOME/.compose-cache}" 77 mkdir -p "$compose_cache" 78 # To avoid accidentally deleting a wrongly set up XCOMPOSECACHE directory, 79 # defensively try to delete cache *files* only, following the file format specified in 80 # https://gitlab.freedesktop.org/xorg/lib/libx11/-/blob/master/modules/im/ximcp/imLcIm.c#L353-358 81 # sprintf (*res, "%s/%c%d_%03x_%08x_%08x", dir, _XimGetMyEndian(), XIM_CACHE_VERSION, (unsigned int)sizeof (DefTree), hash, hash2); 82 ${pkgs.findutils}/bin/find "$compose_cache" -maxdepth 1 -regextype posix-extended -regex '.*/[Bl][0-9]+_[0-9a-f]{3}_[0-9a-f]{8}_[0-9a-f]{8}' -delete 83 unset compose_cache 84 85 # Work around KDE errors when a user first logs in and 86 # .local/share doesn't exist yet. 87 mkdir -p "''${XDG_DATA_HOME:-$HOME/.local/share}" 88 89 unset _DID_SYSTEMD_CAT 90 91 ${cfg.displayManager.sessionCommands} 92 93 # Start systemd user services for graphical sessions 94 /run/current-system/systemd/bin/systemctl --user start graphical-session.target 95 96 # Allow the user to setup a custom session type. 97 if test -x ~/.xsession; then 98 eval exec ~/.xsession "$@" 99 fi 100 101 if test "$1"; then 102 # Run the supplied session command. Remove any double quotes with eval. 103 eval exec "$@" 104 else 105 # TODO: Do we need this? Should not the session always exist? 106 echo "error: unknown session $1" 1>&2 107 exit 1 108 fi 109 ''; 110 111 installedSessions = pkgs.runCommand "desktops" 112 { # trivial derivation 113 preferLocalBuild = true; 114 allowSubstitutes = false; 115 } 116 '' 117 mkdir -p "$out/share/"{xsessions,wayland-sessions} 118 119 ${concatMapStrings (pkg: '' 120 for n in ${concatStringsSep " " pkg.providedSessions}; do 121 if ! test -f ${pkg}/share/wayland-sessions/$n.desktop -o \ 122 -f ${pkg}/share/xsessions/$n.desktop; then 123 echo "Couldn't find provided session name, $n.desktop, in session package ${pkg.name}:" 124 echo " ${pkg}" 125 return 1 126 fi 127 done 128 129 if test -d ${pkg}/share/xsessions; then 130 ${pkgs.buildPackages.xorg.lndir}/bin/lndir ${pkg}/share/xsessions $out/share/xsessions 131 fi 132 if test -d ${pkg}/share/wayland-sessions; then 133 ${pkgs.buildPackages.xorg.lndir}/bin/lndir ${pkg}/share/wayland-sessions $out/share/wayland-sessions 134 fi 135 '') cfg.displayManager.sessionPackages} 136 ''; 137 138 dmDefault = cfg.desktopManager.default; 139 # fallback default for cases when only default wm is set 140 dmFallbackDefault = if dmDefault != null then dmDefault else "none"; 141 wmDefault = cfg.windowManager.default; 142 143 defaultSessionFromLegacyOptions = dmFallbackDefault + optionalString (wmDefault != null && wmDefault != "none") "+${wmDefault}"; 144 145in 146 147{ 148 options = { 149 150 services.xserver.displayManager = { 151 152 xauthBin = mkOption { 153 internal = true; 154 default = "${xorg.xauth}/bin/xauth"; 155 defaultText = literalExpression ''"''${pkgs.xorg.xauth}/bin/xauth"''; 156 description = lib.mdDoc "Path to the {command}`xauth` program used by display managers."; 157 }; 158 159 xserverBin = mkOption { 160 type = types.path; 161 description = lib.mdDoc "Path to the X server used by display managers."; 162 }; 163 164 xserverArgs = mkOption { 165 type = types.listOf types.str; 166 default = []; 167 example = [ "-ac" "-logverbose" "-verbose" "-nolisten tcp" ]; 168 description = lib.mdDoc "List of arguments for the X server."; 169 }; 170 171 setupCommands = mkOption { 172 type = types.lines; 173 default = ""; 174 description = lib.mdDoc '' 175 Shell commands executed just after the X server has started. 176 177 This option is only effective for display managers for which this feature 178 is supported; currently these are LightDM, GDM and SDDM. 179 ''; 180 }; 181 182 sessionCommands = mkOption { 183 type = types.lines; 184 default = ""; 185 example = 186 '' 187 xmessage "Hello World!" & 188 ''; 189 description = lib.mdDoc '' 190 Shell commands executed just before the window or desktop manager is 191 started. These commands are not currently sourced for Wayland sessions. 192 ''; 193 }; 194 195 hiddenUsers = mkOption { 196 type = types.listOf types.str; 197 default = [ "nobody" ]; 198 description = lib.mdDoc '' 199 A list of users which will not be shown in the display manager. 200 ''; 201 }; 202 203 sessionPackages = mkOption { 204 type = with types; listOf (package // { 205 description = "package with provided sessions"; 206 check = p: assertMsg 207 (package.check p && p ? providedSessions 208 && p.providedSessions != [] && all isString p.providedSessions) 209 '' 210 Package, '${p.name}', did not specify any session names, as strings, in 211 'passthru.providedSessions'. This is required when used as a session package. 212 213 The session names can be looked up in: 214 ${p}/share/xsessions 215 ${p}/share/wayland-sessions 216 ''; 217 }); 218 default = []; 219 description = lib.mdDoc '' 220 A list of packages containing x11 or wayland session files to be passed to the display manager. 221 ''; 222 }; 223 224 session = mkOption { 225 default = []; 226 type = types.listOf types.attrs; 227 example = literalExpression 228 '' 229 [ { manage = "desktop"; 230 name = "xterm"; 231 start = ''' 232 ''${pkgs.xterm}/bin/xterm -ls & 233 waitPID=$! 234 '''; 235 } 236 ] 237 ''; 238 description = lib.mdDoc '' 239 List of sessions supported with the command used to start each 240 session. Each session script can set the 241 {var}`waitPID` shell variable to make this script 242 wait until the end of the user session. Each script is used 243 to define either a window manager or a desktop manager. These 244 can be differentiated by setting the attribute 245 {var}`manage` either to `"window"` 246 or `"desktop"`. 247 248 The list of desktop manager and window manager should appear 249 inside the display manager with the desktop manager name 250 followed by the window manager name. 251 ''; 252 }; 253 254 sessionData = mkOption { 255 description = lib.mdDoc "Data exported for display managers convenience"; 256 internal = true; 257 default = {}; 258 apply = val: { 259 wrapper = xsessionWrapper; 260 desktops = installedSessions; 261 sessionNames = concatMap (p: p.providedSessions) cfg.displayManager.sessionPackages; 262 # We do not want to force users to set defaultSession when they have only single DE. 263 autologinSession = 264 if cfg.displayManager.defaultSession != null then 265 cfg.displayManager.defaultSession 266 else if cfg.displayManager.sessionData.sessionNames != [] then 267 head cfg.displayManager.sessionData.sessionNames 268 else 269 null; 270 }; 271 }; 272 273 defaultSession = mkOption { 274 type = with types; nullOr str // { 275 description = "session name"; 276 check = d: 277 assertMsg (d != null -> (str.check d && elem d cfg.displayManager.sessionData.sessionNames)) '' 278 Default graphical session, '${d}', not found. 279 Valid names for 'services.xserver.displayManager.defaultSession' are: 280 ${concatStringsSep "\n " cfg.displayManager.sessionData.sessionNames} 281 ''; 282 }; 283 default = 284 if dmDefault != null || wmDefault != null then 285 defaultSessionFromLegacyOptions 286 else 287 null; 288 defaultText = literalMD '' 289 Taken from display manager settings or window manager settings, if either is set. 290 ''; 291 example = "gnome"; 292 description = lib.mdDoc '' 293 Graphical session to pre-select in the session chooser (only effective for GDM, LightDM and SDDM). 294 295 On GDM, LightDM and SDDM, it will also be used as a session for auto-login. 296 ''; 297 }; 298 299 importedVariables = mkOption { 300 type = types.listOf (types.strMatching "[a-zA-Z_][a-zA-Z0-9_]*"); 301 visible = false; 302 description = lib.mdDoc '' 303 Environment variables to import into the systemd user environment. 304 ''; 305 }; 306 307 job = { 308 309 preStart = mkOption { 310 type = types.lines; 311 default = ""; 312 example = "rm -f /var/log/my-display-manager.log"; 313 description = lib.mdDoc "Script executed before the display manager is started."; 314 }; 315 316 execCmd = mkOption { 317 type = types.str; 318 example = literalExpression ''"''${pkgs.lightdm}/bin/lightdm"''; 319 description = lib.mdDoc "Command to start the display manager."; 320 }; 321 322 environment = mkOption { 323 type = types.attrsOf types.unspecified; 324 default = {}; 325 description = lib.mdDoc "Additional environment variables needed by the display manager."; 326 }; 327 328 logToFile = mkOption { 329 type = types.bool; 330 default = false; 331 description = lib.mdDoc '' 332 Whether the display manager redirects the output of the 333 session script to {file}`~/.xsession-errors`. 334 ''; 335 }; 336 337 logToJournal = mkOption { 338 type = types.bool; 339 default = true; 340 description = lib.mdDoc '' 341 Whether the display manager redirects the output of the 342 session script to the systemd journal. 343 ''; 344 }; 345 346 }; 347 348 # Configuration for automatic login. Common for all DM. 349 autoLogin = mkOption { 350 type = types.submodule ({ config, options, ... }: { 351 options = { 352 enable = mkOption { 353 type = types.bool; 354 default = config.user != null; 355 defaultText = literalExpression "config.${options.user} != null"; 356 description = lib.mdDoc '' 357 Automatically log in as {option}`autoLogin.user`. 358 ''; 359 }; 360 361 user = mkOption { 362 type = types.nullOr types.str; 363 default = null; 364 description = lib.mdDoc '' 365 User to be used for the automatic login. 366 ''; 367 }; 368 }; 369 }); 370 371 default = {}; 372 description = lib.mdDoc '' 373 Auto login configuration attrset. 374 ''; 375 }; 376 377 }; 378 379 }; 380 381 config = { 382 assertions = [ 383 { assertion = cfg.displayManager.autoLogin.enable -> cfg.displayManager.autoLogin.user != null; 384 message = '' 385 services.xserver.displayManager.autoLogin.enable requires services.xserver.displayManager.autoLogin.user to be set 386 ''; 387 } 388 { 389 assertion = cfg.desktopManager.default != null || cfg.windowManager.default != null -> cfg.displayManager.defaultSession == defaultSessionFromLegacyOptions; 390 message = "You cannot use both services.xserver.displayManager.defaultSession option and legacy options (services.xserver.desktopManager.default and services.xserver.windowManager.default)."; 391 } 392 ]; 393 394 warnings = 395 mkIf (dmDefault != null || wmDefault != null) [ 396 '' 397 The following options are deprecated: 398 ${concatStringsSep "\n " (map ({c, t}: t) (filter ({c, t}: c != null) [ 399 { c = dmDefault; t = "- services.xserver.desktopManager.default"; } 400 { c = wmDefault; t = "- services.xserver.windowManager.default"; } 401 ]))} 402 Please use 403 services.xserver.displayManager.defaultSession = "${defaultSessionFromLegacyOptions}"; 404 instead. 405 '' 406 ]; 407 408 services.xserver.displayManager.xserverBin = "${xorg.xorgserver.out}/bin/X"; 409 410 services.xserver.displayManager.importedVariables = [ 411 # This is required by user units using the session bus. 412 "DBUS_SESSION_BUS_ADDRESS" 413 # These are needed by the ssh-agent unit. 414 "DISPLAY" 415 "XAUTHORITY" 416 # This is required to specify session within user units (e.g. loginctl lock-session). 417 "XDG_SESSION_ID" 418 ]; 419 420 systemd.user.targets.graphical-session = { 421 unitConfig = { 422 RefuseManualStart = false; 423 StopWhenUnneeded = false; 424 }; 425 }; 426 427 # Create desktop files and scripts for starting sessions for WMs/DMs 428 # that do not have upstream session files (those defined using services.{display,desktop,window}Manager.session options). 429 services.xserver.displayManager.sessionPackages = 430 let 431 dms = filter (s: s.manage == "desktop") cfg.displayManager.session; 432 wms = filter (s: s.manage == "window") cfg.displayManager.session; 433 434 # Script responsible for starting the window manager and the desktop manager. 435 xsession = dm: wm: pkgs.writeScript "xsession" '' 436 #! ${pkgs.bash}/bin/bash 437 438 # Legacy session script used to construct .desktop files from 439 # `services.xserver.displayManager.session` entries. Called from 440 # `sessionWrapper`. 441 442 # Start the window manager. 443 ${wm.start} 444 445 # Start the desktop manager. 446 ${dm.start} 447 448 ${optionalString cfg.updateDbusEnvironment '' 449 ${lib.getBin pkgs.dbus}/bin/dbus-update-activation-environment --systemd --all 450 ''} 451 452 test -n "$waitPID" && wait "$waitPID" 453 454 /run/current-system/systemd/bin/systemctl --user stop graphical-session.target 455 456 exit 0 457 ''; 458 in 459 # We will generate every possible pair of WM and DM. 460 concatLists ( 461 builtins.map 462 ({dm, wm}: let 463 sessionName = "${dm.name}${optionalString (wm.name != "none") ("+" + wm.name)}"; 464 script = xsession dm wm; 465 desktopNames = if dm ? desktopNames 466 then concatStringsSep ";" dm.desktopNames 467 else sessionName; 468 in 469 optional (dm.name != "none" || wm.name != "none") 470 (pkgs.writeTextFile { 471 name = "${sessionName}-xsession"; 472 destination = "/share/xsessions/${sessionName}.desktop"; 473 # Desktop Entry Specification: 474 # - https://standards.freedesktop.org/desktop-entry-spec/latest/ 475 # - https://standards.freedesktop.org/desktop-entry-spec/latest/ar01s06.html 476 text = '' 477 [Desktop Entry] 478 Version=1.0 479 Type=XSession 480 TryExec=${script} 481 Exec=${script} 482 Name=${sessionName} 483 DesktopNames=${desktopNames} 484 ''; 485 } // { 486 providedSessions = [ sessionName ]; 487 }) 488 ) 489 (cartesianProductOfSets { dm = dms; wm = wms; }) 490 ); 491 492 # Make xsessions and wayland sessions available in XDG_DATA_DIRS 493 # as some programs have behavior that depends on them being present 494 environment.sessionVariables.XDG_DATA_DIRS = [ 495 "${cfg.displayManager.sessionData.desktops}/share" 496 ]; 497 }; 498 499 imports = [ 500 (mkRemovedOptionModule [ "services" "xserver" "displayManager" "desktopManagerHandlesLidAndPower" ] 501 "The option is no longer necessary because all display managers have already delegated lid management to systemd.") 502 (mkRenamedOptionModule [ "services" "xserver" "displayManager" "job" "logsXsession" ] [ "services" "xserver" "displayManager" "job" "logToFile" ]) 503 (mkRenamedOptionModule [ "services" "xserver" "displayManager" "logToJournal" ] [ "services" "xserver" "displayManager" "job" "logToJournal" ]) 504 (mkRenamedOptionModule [ "services" "xserver" "displayManager" "extraSessionFilesPackages" ] [ "services" "xserver" "displayManager" "sessionPackages" ]) 505 ]; 506 507}