services: guix-build-coordinator: Fix passing parallel-hooks.
[jackhill/guix/guix.git] / gnu / services / xorg.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Andy Wingo <wingo@igalia.com>
3 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
4 ;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
5 ;;; Copyright © 2018, 2019 Timothy Sample <samplet@ngyro.com>
6 ;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
7 ;;; Copyright © 2019 Tim Gesthuizen <tim.gesthuizen@yahoo.de>
8 ;;; Copyright © 2020 shtwzrd <shtwzrd@protonmail.com>
9 ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
10 ;;; Copyright © 2020 Alex Griffin <a@ajgrf.com>
11 ;;; Copyright © 2021 Brice Waegeneire <brice@waegenei.re>
12 ;;; Copyright © 2021 Oleg Pykhalov <go.wigust@gmail.com>
13 ;;; Copyright © 2021 Josselin Poiret <josselin.poiret@protonmail.ch>
14 ;;; Copyright © 2022 Chris Marusich <cmmarusich@gmail.com>
15 ;;;
16 ;;; This file is part of GNU Guix.
17 ;;;
18 ;;; GNU Guix is free software; you can redistribute it and/or modify it
19 ;;; under the terms of the GNU General Public License as published by
20 ;;; the Free Software Foundation; either version 3 of the License, or (at
21 ;;; your option) any later version.
22 ;;;
23 ;;; GNU Guix is distributed in the hope that it will be useful, but
24 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;;; GNU General Public License for more details.
27 ;;;
28 ;;; You should have received a copy of the GNU General Public License
29 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
30
31 (define-module (gnu services xorg)
32 #:autoload (gnu services sddm) (sddm-service-type)
33 #:use-module (gnu artwork)
34 #:use-module (gnu services)
35 #:use-module (gnu services shepherd)
36 #:use-module (gnu system pam)
37 #:use-module (gnu system setuid)
38 #:use-module (gnu system keyboard)
39 #:use-module (gnu services base)
40 #:use-module (gnu services dbus)
41 #:use-module (gnu packages base)
42 #:use-module (gnu packages guile)
43 #:use-module (gnu packages xorg)
44 #:use-module (gnu packages fonts)
45 #:use-module (gnu packages gl)
46 #:use-module (gnu packages glib)
47 #:use-module (gnu packages display-managers)
48 #:use-module (gnu packages freedesktop)
49 #:use-module (gnu packages gnustep)
50 #:use-module (gnu packages gnome)
51 #:use-module (gnu packages admin)
52 #:use-module (gnu packages bash)
53 #:use-module (gnu system shadow)
54 #:use-module (guix build-system glib-or-gtk)
55 #:use-module (guix build-system trivial)
56 #:use-module (guix gexp)
57 #:use-module (guix store)
58 #:use-module (guix packages)
59 #:use-module (guix derivations)
60 #:use-module (guix records)
61 #:use-module (guix deprecation)
62 #:use-module (guix utils)
63 #:use-module (srfi srfi-1)
64 #:use-module (srfi srfi-9)
65 #:use-module (srfi srfi-26)
66 #:use-module (ice-9 match)
67 #:export (xorg-configuration
68 xorg-configuration?
69 xorg-configuration-modules
70 xorg-configuration-fonts
71 xorg-configuration-drivers
72 xorg-configuration-resolutions
73 xorg-configuration-extra-config
74 xorg-configuration-server
75 xorg-configuration-server-arguments
76
77 %default-xorg-modules
78 %default-xorg-fonts
79 %default-xorg-server-arguments
80
81 xorg-wrapper
82 xorg-start-command
83 xinitrc
84 xorg-server-service-type
85
86 %default-slim-theme
87 %default-slim-theme-name
88
89 slim-configuration
90 slim-configuration?
91 slim-configuration-slim
92 slim-configuration-allow-empty-passwords?
93 slim-configuration-auto-login?
94 slim-configuration-default-user
95 slim-configuration-theme
96 slim-configuration-theme-name
97 slim-configuration-xauth
98 slim-configuration-shepherd
99 slim-configuration-auto-login-session
100 slim-configuration-xorg
101 slim-configuration-display
102 slim-configuration-vt
103 slim-configuration-sessreg
104
105 slim-service-type
106
107 screen-locker
108 screen-locker?
109 screen-locker-service-type
110 screen-locker-service
111
112 localed-configuration
113 localed-configuration?
114 localed-service-type
115
116 gdm-configuration
117 gdm-service-type
118
119 handle-xorg-configuration
120 set-xorg-configuration))
121
122 ;;; Commentary:
123 ;;;
124 ;;; Services that relate to the X Window System.
125 ;;;
126 ;;; Code:
127
128 (define %default-xorg-modules
129 ;; Default list of modules loaded by the server. When multiple drivers
130 ;; match, the first one in the list is loaded.
131 (list xf86-video-vesa
132 xf86-video-fbdev
133 xf86-video-amdgpu
134 xf86-video-ati
135 xf86-video-cirrus
136 xf86-video-intel
137 xf86-video-mach64
138 xf86-video-nouveau
139 xf86-video-nv
140 xf86-video-sis
141
142 ;; Libinput is the new thing and is recommended over evdev/synaptics:
143 ;; <http://who-t.blogspot.fr/2015/01/xf86-input-libinput-compatibility-with.html>.
144 xf86-input-libinput
145
146 xf86-input-evdev
147 xf86-input-keyboard
148 xf86-input-mouse))
149
150 (define %default-xorg-fonts
151 ;; Default list of fonts available to the X server.
152 (list (file-append font-alias "/share/fonts/X11/75dpi")
153 (file-append font-alias "/share/fonts/X11/100dpi")
154 (file-append font-alias "/share/fonts/X11/misc")
155 (file-append font-alias "/share/fonts/X11/cyrillic")
156 (file-append font-misc-misc ;default fonts for xterm
157 "/share/fonts/X11/misc")
158 (file-append font-adobe75dpi "/share/fonts/X11/75dpi")))
159
160 (define %default-xorg-server-arguments
161 ;; Default command-line arguments for X.
162 '("-nolisten" "tcp"))
163
164 ;; Configuration of an Xorg server.
165 (define-record-type* <xorg-configuration>
166 xorg-configuration make-xorg-configuration
167 xorg-configuration?
168 (modules xorg-configuration-modules ;list of file-like
169 (thunked)
170 ; filter out modules not supported on current system
171 (default (filter
172 (lambda (p)
173 (member (%current-system)
174 (package-supported-systems p)))
175 %default-xorg-modules)))
176 (fonts xorg-configuration-fonts ;list of packges
177 (default %default-xorg-fonts))
178 (drivers xorg-configuration-drivers ;list of strings
179 (default '()))
180 (resolutions xorg-configuration-resolutions ;list of tuples
181 (default '()))
182 (keyboard-layout xorg-configuration-keyboard-layout ;#f | <keyboard-layout>
183 (default #f))
184 (extra-config xorg-configuration-extra-config ;list of strings
185 (default '()))
186 (server xorg-configuration-server ;file-like
187 (default xorg-server))
188 (server-arguments xorg-configuration-server-arguments ;list of strings
189 (default %default-xorg-server-arguments)))
190
191 (define (xorg-configuration->file config)
192 "Compute an Xorg configuration file corresponding to CONFIG, an
193 <xorg-configuration> record."
194 (let ((xorg-server (xorg-configuration-server config)))
195 (define all-modules
196 ;; 'xorg-server' provides 'fbdevhw.so' etc.
197 (append (xorg-configuration-modules config)
198 (list xorg-server)))
199
200 (define build
201 #~(begin
202 (use-modules (ice-9 match)
203 (srfi srfi-1)
204 (srfi srfi-26))
205
206 (call-with-output-file #$output
207 (lambda (port)
208 (define drivers
209 '#$(xorg-configuration-drivers config))
210
211 (define (device-section driver)
212 (string-append "
213 Section \"Device\"
214 Identifier \"device-" driver "\"
215 Driver \"" driver "\"
216 EndSection"))
217
218 (define (screen-section driver resolutions)
219 (string-append "
220 Section \"Screen\"
221 Identifier \"screen-" driver "\"
222 Device \"device-" driver "\"
223 SubSection \"Display\"
224 Modes "
225 (string-join (map (match-lambda
226 ((x y)
227 (string-append "\"" (number->string x)
228 "x" (number->string y) "\"")))
229 resolutions)) "
230 EndSubSection
231 EndSection"))
232
233 (define (input-class-section layout variant model options)
234 (string-append "
235 Section \"InputClass\"
236 Identifier \"evdev keyboard catchall\"
237 MatchIsKeyboard \"on\"
238 Option \"XkbLayout\" " (object->string layout)
239 (if variant
240 (string-append " Option \"XkbVariant\" \""
241 variant "\"")
242 "")
243 (if model
244 (string-append " Option \"XkbModel\" \""
245 model "\"")
246 "")
247 (match options
248 (()
249 "")
250 (_
251 (string-append " Option \"XkbOptions\" \""
252 (string-join options ",") "\""))) "
253
254 MatchDevicePath \"/dev/input/event*\"
255 Driver \"evdev\"
256 EndSection\n"))
257
258 (define (expand modules)
259 ;; Append to MODULES the relevant /lib/xorg/modules
260 ;; sub-directories.
261 (append-map (lambda (module)
262 (filter-map (lambda (directory)
263 (let ((full (string-append module
264 directory)))
265 (and (file-exists? full)
266 full)))
267 '("/lib/xorg/modules/drivers"
268 "/lib/xorg/modules/input"
269 "/lib/xorg/modules/multimedia"
270 "/lib/xorg/modules/extensions")))
271 modules))
272
273 (display "Section \"Files\"\n" port)
274 (for-each (lambda (font)
275 (format port " FontPath \"~a\"~%" font))
276 '#$(xorg-configuration-fonts config))
277 (for-each (lambda (module)
278 (format port
279 " ModulePath \"~a\"~%"
280 module))
281 (append (expand '#$all-modules)
282
283 ;; For fbdevhw.so and so on.
284 (list #$(file-append xorg-server
285 "/lib/xorg/modules"))))
286 (display "EndSection\n" port)
287 (display "
288 Section \"ServerFlags\"
289 Option \"AllowMouseOpenFail\" \"on\"
290 EndSection\n" port)
291
292 (display (string-join (map device-section drivers) "\n")
293 port)
294 (newline port)
295 (display (string-join
296 (map (cut screen-section <>
297 '#$(xorg-configuration-resolutions config))
298 drivers)
299 "\n")
300 port)
301 (newline port)
302
303 (let ((layout #$(and=> (xorg-configuration-keyboard-layout config)
304 keyboard-layout-name))
305 (variant #$(and=> (xorg-configuration-keyboard-layout config)
306 keyboard-layout-variant))
307 (model #$(and=> (xorg-configuration-keyboard-layout config)
308 keyboard-layout-model))
309 (options '#$(and=> (xorg-configuration-keyboard-layout config)
310 keyboard-layout-options)))
311 (when layout
312 (display (input-class-section layout variant model options)
313 port)
314 (newline port)))
315
316 (for-each (lambda (config)
317 (display config port))
318 '#$(xorg-configuration-extra-config config))))))
319
320 (computed-file "xserver.conf" build)))
321
322 (define (xorg-configuration-directory modules)
323 "Return a directory that contains the @code{.conf} files for X.org that
324 includes the @code{share/X11/xorg.conf.d} directories of each package listed
325 in @var{modules}."
326 (with-imported-modules '((guix build utils))
327 (computed-file "xorg.conf.d"
328 #~(begin
329 (use-modules (guix build utils)
330 (srfi srfi-1))
331
332 (define files
333 (append-map (lambda (module)
334 (find-files (string-append
335 module
336 "/share/X11/xorg.conf.d")
337 "\\.conf$"))
338 (list #$@modules)))
339
340 (mkdir #$output)
341 (for-each (lambda (file)
342 (symlink file
343 (string-append #$output "/"
344 (basename file))))
345 files)
346 #t))))
347
348 (define* (xorg-wrapper #:optional (config (xorg-configuration)))
349 "Return a derivation that builds a script to start the X server with the
350 given @var{config}. The resulting script should be used in place of
351 @code{/usr/bin/X}."
352 (define exp
353 ;; Write a small wrapper around the X server.
354 #~(begin
355 (setenv "XORG_DRI_DRIVER_PATH" (string-append #$mesa "/lib/dri"))
356 (setenv "XKB_BINDIR" (string-append #$xkbcomp "/bin"))
357
358 (let ((X (string-append #$(xorg-configuration-server config) "/bin/X")))
359 (apply execl X X
360 "-xkbdir" (string-append #$xkeyboard-config "/share/X11/xkb")
361 "-config" #$(xorg-configuration->file config)
362 "-configdir" #$(xorg-configuration-directory
363 (xorg-configuration-modules config))
364 (cdr (command-line))))))
365
366 (program-file "X-wrapper" exp))
367
368 (define* (xorg-start-command #:optional (config (xorg-configuration)))
369 "Return a @code{startx} script in which the modules, fonts, etc. specified
370 in @var{config}, are available. The result should be used in place of
371 @code{startx}."
372 (define X
373 (xorg-wrapper config))
374
375 (define exp
376 ;; Write a small wrapper around the X server.
377 #~(apply execl #$X #$X ;; Second #$X is for argv[0].
378 "-logverbose" "-verbose" "-terminate"
379 #$@(xorg-configuration-server-arguments config)
380 (cdr (command-line))))
381
382 (program-file "startx" exp))
383
384 (define* (xinitrc #:key fallback-session)
385 "Return a system-wide xinitrc script that starts the specified X session,
386 which should be passed to this script as the first argument. If not, the
387 @var{fallback-session} will be used or, if @var{fallback-session} is false, a
388 desktop session from the system or user profile will be used."
389 (define builder
390 #~(begin
391 (use-modules (ice-9 match)
392 (ice-9 regex)
393 (ice-9 ftw)
394 (ice-9 rdelim)
395 (srfi srfi-1)
396 (srfi srfi-26))
397
398 (define (close-all-fdes)
399 ;; Close all the open file descriptors except 0 to 2.
400 (let loop ((fd 3))
401 (when (< fd 4096) ;FIXME: use sysconf + _SC_OPEN_MAX
402 (false-if-exception (close-fdes fd))
403 (loop (+ 1 fd)))))
404
405 (define (exec-from-login-shell command . args)
406 ;; Run COMMAND from a login shell so that it gets to see the same
407 ;; environment variables that one gets when logging in on a tty, for
408 ;; instance.
409 (let* ((pw (getpw (getuid)))
410 (shell (passwd:shell pw)))
411 ;; Close any open file descriptors. This is all the more
412 ;; important that SLiM itself exec's us directly without closing
413 ;; its own file descriptors!
414 (close-all-fdes)
415
416 ;; The '--login' option is supported at least by Bash and zsh.
417 (execl shell shell "--login" "-c"
418 (string-join (cons command args)))))
419
420 (define system-profile
421 "/run/current-system/profile")
422
423 (define user-profile
424 (and=> (getpw (getuid))
425 (lambda (pw)
426 (string-append (passwd:dir pw) "/.guix-profile"))))
427
428 (define (xsession-command desktop-file)
429 ;; Read from DESKTOP-FILE its X session command and return it as a
430 ;; list.
431 (define exec-regexp
432 (make-regexp "^[[:blank:]]*Exec=(.*)$"))
433
434 (call-with-input-file desktop-file
435 (lambda (port)
436 (let loop ()
437 (match (read-line port)
438 ((? eof-object?) #f)
439 ((= (cut regexp-exec exec-regexp <>) result)
440 (if result
441 (string-tokenize (match:substring result 1))
442 (loop))))))))
443
444 (define (find-session profile)
445 ;; Return an X session command from PROFILE or #f if none was found.
446 (let ((directory (string-append profile "/share/xsessions")))
447 (match (scandir directory
448 (cut string-suffix? ".desktop" <>))
449 ((or () #f)
450 #f)
451 ((sessions ...)
452 (any xsession-command
453 (map (cut string-append directory "/" <>)
454 sessions))))))
455
456 (let* ((home (getenv "HOME"))
457 (xsession-file (string-append home "/.xsession"))
458 (session (match (command-line)
459 ((_)
460 #$(if fallback-session
461 #~(list #$fallback-session)
462 #f))
463 ((_ x ..1)
464 x))))
465 (if (file-exists? xsession-file)
466 ;; Run ~/.xsession when it exists.
467 (apply exec-from-login-shell xsession-file
468 (or session '()))
469 ;; Otherwise, start the specified session or a fallback.
470 (apply exec-from-login-shell
471 (or session
472 (find-session user-profile)
473 (find-session system-profile)))))))
474
475 (program-file "xinitrc" builder))
476
477 (define-syntax handle-xorg-configuration
478 (syntax-rules ()
479 "Generate the `compose' and `extend' entries of a login manager
480 `service-type' to handle specifying the `xorg-configuration' through
481 a `service-extension', as used by `set-xorg-configuration'."
482 ((_ configuration-record service-type-definition)
483 (service-type
484 (inherit service-type-definition)
485 (compose (lambda (extensions)
486 (match extensions
487 (() #f)
488 ((config . _) config))))
489 (extend (lambda (config xorg-configuration)
490 (if xorg-configuration
491 (configuration-record
492 (inherit config)
493 (xorg-configuration xorg-configuration))
494 config)))))))
495
496 (define (xorg-server-profile-service config)
497 ;; XXX: profile-service-type only accepts <package> objects.
498 (list
499 (package
500 (name "xorg-wrapper")
501 (version (package-version xorg-server))
502 (source (xorg-wrapper config))
503 (build-system trivial-build-system)
504 (arguments
505 '(#:modules ((guix build utils))
506 #:builder
507 (begin
508 (use-modules (guix build utils))
509 (let* ((source (assoc-ref %build-inputs "source"))
510 (out (assoc-ref %outputs "out"))
511 (bin (string-append out "/bin")))
512 (mkdir-p bin)
513 (symlink source (string-append bin "/X"))
514 (symlink source (string-append bin "/Xorg"))
515 #t))))
516 (home-page (package-home-page xorg-server))
517 (synopsis (package-synopsis xorg-server))
518 (description (package-description xorg-server))
519 (license (package-license xorg-server)))))
520
521 (define xorg-server-service-type
522 (service-type
523 (name 'xorg-server)
524 (extensions
525 (list (service-extension profile-service-type
526 xorg-server-profile-service)))
527 (default-value (xorg-configuration))
528 (description "Add @command{X} to the system profile, to be used with
529 @command{sx} or @command{xinit}.")))
530
531 \f
532 ;;;
533 ;;; SLiM log-in manager.
534 ;;;
535
536 (define %default-slim-theme
537 ;; Theme based on work by Felipe López.
538 (file-append %artwork-repository "/slim"))
539
540 (define %default-slim-theme-name
541 ;; This must be the name of the sub-directory in %DEFAULT-SLIM-THEME that
542 ;; contains the actual theme files.
543 "1.x")
544
545 (define-record-type* <slim-configuration>
546 slim-configuration make-slim-configuration
547 slim-configuration?
548 (slim slim-configuration-slim
549 (default slim))
550 (allow-empty-passwords? slim-configuration-allow-empty-passwords?
551 (default #t))
552 (gnupg? slim-configuration-gnupg?
553 (default #f))
554 (auto-login? slim-configuration-auto-login?
555 (default #f))
556 (default-user slim-configuration-default-user
557 (default ""))
558 (theme slim-configuration-theme
559 (default %default-slim-theme))
560 (theme-name slim-configuration-theme-name
561 (default %default-slim-theme-name))
562 (xauth slim-configuration-xauth
563 (default xauth))
564 (shepherd slim-configuration-shepherd
565 (default shepherd))
566 (auto-login-session slim-configuration-auto-login-session
567 (default #f))
568 (xorg-configuration slim-configuration-xorg
569 (default (xorg-configuration)))
570 (display slim-configuration-display
571 (default ":0"))
572 (vt slim-configuration-vt
573 (default "vt7"))
574 (sessreg slim-configuration-sessreg
575 (default sessreg)))
576
577 (define (slim-pam-service config)
578 "Return a PAM service for @command{slim}."
579 (list (unix-pam-service
580 "slim"
581 #:login-uid? #t
582 #:allow-empty-passwords?
583 (slim-configuration-allow-empty-passwords? config)
584 #:gnupg?
585 (slim-configuration-gnupg? config))))
586
587 (define (slim-shepherd-service config)
588 (let* ((xinitrc (xinitrc #:fallback-session
589 (slim-configuration-auto-login-session config)))
590 (xauth (slim-configuration-xauth config))
591 (startx (xorg-start-command (slim-configuration-xorg config)))
592 (display (slim-configuration-display config))
593 (vt (slim-configuration-vt config))
594 (shepherd (slim-configuration-shepherd config))
595 (theme-name (slim-configuration-theme-name config))
596 (sessreg (slim-configuration-sessreg config))
597 (lockfile (string-append "/var/run/slim-" vt ".lock")))
598 (define slim.cfg
599 (mixed-text-file "slim.cfg" "
600 default_path /run/current-system/profile/bin
601 default_xserver " startx "
602 display_name " display "
603 xserver_arguments " vt "
604 xauth_path " xauth "/bin/xauth
605 authfile /var/run/slim-" vt ".auth
606 lockfile " lockfile "
607 logfile /var/log/slim-" vt ".log
608
609 # The login command. '%session' is replaced by the chosen session name, one
610 # of the names specified in the 'sessions' setting: 'wmaker', 'xfce', etc.
611 login_cmd exec " xinitrc " %session
612 sessiondir /run/current-system/profile/share/xsessions
613 session_msg session (F1 to change):
614 sessionstart_cmd " sessreg "/bin/sessreg -a -l $DISPLAY %user
615 sessionstop_cmd " sessreg "/bin/sessreg -d -l $DISPLAY %user
616
617 halt_cmd " shepherd "/sbin/halt
618 reboot_cmd " shepherd "/sbin/reboot\n"
619 (if (slim-configuration-auto-login? config)
620 (string-append "auto_login yes\ndefault_user "
621 (slim-configuration-default-user config) "\n")
622 "")
623 (if theme-name
624 (string-append "current_theme " theme-name "\n")
625 "")))
626
627 (define theme
628 (slim-configuration-theme config))
629
630 (list (shepherd-service
631 (documentation "Xorg display server")
632 (provision (append
633 ;; For compatibility, also provide 'xorg-server'.
634 (if (string=? vt "vt7")
635 '(xorg-server)
636 '())
637
638 (list (symbol-append 'xorg-server-
639 (string->symbol vt)))))
640 (requirement '(user-processes host-name udev))
641 (start
642 #~(lambda ()
643 ;; A stale lock file can prevent SLiM from starting, so remove it to
644 ;; be on the safe side.
645 (false-if-exception (delete-file lockfile))
646
647 (fork+exec-command
648 (list (string-append #$(slim-configuration-slim config)
649 "/bin/slim")
650 "-nodaemon")
651 #:environment-variables
652 (list (string-append "SLIM_CFGFILE=" #$slim.cfg)
653 #$@(if theme
654 (list #~(string-append "SLIM_THEMESDIR=" #$theme))
655 #~())))))
656 (stop #~(make-kill-destructor))
657 (respawn? #t)))))
658
659 (define slim-service-type
660 (handle-xorg-configuration slim-configuration
661 (service-type (name 'slim)
662 (extensions
663 (list (service-extension shepherd-root-service-type
664 slim-shepherd-service)
665 (service-extension pam-root-service-type
666 slim-pam-service)
667
668 ;; Unconditionally add xterm to the system profile, to
669 ;; avoid bad surprises.
670 (service-extension profile-service-type
671 (const (list xterm)))))
672
673 (default-value (slim-configuration))
674 (description
675 "Run the SLiM graphical login manager for X11."))))
676
677 \f
678 ;;;
679 ;;; Screen lockers & co.
680 ;;;
681
682 (define-record-type <screen-locker>
683 (screen-locker name program empty?)
684 screen-locker?
685 (name screen-locker-name) ;string
686 (program screen-locker-program) ;gexp
687 (empty? screen-locker-allows-empty-passwords?)) ;Boolean
688
689 (define screen-locker-pam-services
690 (match-lambda
691 (($ <screen-locker> name _ empty?)
692 (list (unix-pam-service name
693 #:allow-empty-passwords? empty?)))))
694
695 (define screen-locker-setuid-programs
696 (compose list file-like->setuid-program screen-locker-program))
697
698 (define screen-locker-service-type
699 (service-type (name 'screen-locker)
700 (extensions
701 (list (service-extension pam-root-service-type
702 screen-locker-pam-services)
703 (service-extension setuid-program-service-type
704 screen-locker-setuid-programs)))
705 (description
706 "Allow the given program to be used as a screen locker for
707 the graphical server by making it setuid-root, so it can authenticate users,
708 and by creating a PAM service for it.")))
709
710 (define* (screen-locker-service package
711 #:optional
712 (program (package-name package))
713 #:key allow-empty-passwords?)
714 "Add @var{package}, a package for a screen locker or screen saver whose
715 command is @var{program}, to the set of setuid programs and add a PAM entry
716 for it. For example:
717
718 @lisp
719 (screen-locker-service xlockmore \"xlock\")
720 @end lisp
721
722 makes the good ol' XlockMore usable."
723 (service screen-locker-service-type
724 (screen-locker program
725 (file-append package "/bin/" program)
726 allow-empty-passwords?)))
727
728 \f
729 ;;;
730 ;;; Locale service.
731 ;;;
732
733 (define-record-type* <localed-configuration>
734 localed-configuration make-localed-configuration
735 localed-configuration?
736 (localed localed-configuration-localed
737 (default localed))
738 (keyboard-layout localed-configuration-keyboard-layout
739 (default #f)))
740
741 (define (localed-dbus-service config)
742 "Return the 'localed' D-Bus service for @var{config}, a
743 @code{<localed-configuration>} record."
744 (define keyboard-layout
745 (localed-configuration-keyboard-layout config))
746
747 ;; The primary purpose of 'localed' is to tell GDM what the "current" Xorg
748 ;; keyboard layout is. If 'localed' is missing, or if it's unable to
749 ;; determine the current XKB layout, then GDM forcefully installs its
750 ;; default XKB config (US English). Here we communicate the configured
751 ;; layout through environment variables.
752
753 (if keyboard-layout
754 (let* ((layout (keyboard-layout-name keyboard-layout))
755 (variant (keyboard-layout-variant keyboard-layout))
756 (model (keyboard-layout-model keyboard-layout))
757 (options (keyboard-layout-options keyboard-layout)))
758 (list (wrapped-dbus-service
759 (localed-configuration-localed config)
760 "libexec/localed/localed"
761 `(("GUIX_XKB_LAYOUT" ,layout)
762 ,@(if variant
763 `(("GUIX_XKB_VARIANT" ,variant))
764 '())
765 ,@(if model
766 `(("GUIX_XKB_MODEL" ,model))
767 '())
768 ,@(if (null? options)
769 '()
770 `(("GUIX_XKB_OPTIONS"
771 ,(string-join options ","))))))))
772 '()))
773
774 (define localed-service-type
775 (let ((package (lambda (config)
776 ;; Don't bother if the user didn't specify any keyboard
777 ;; layout.
778 (if (localed-configuration-keyboard-layout config)
779 (list (localed-configuration-localed config))
780 '()))))
781 (service-type (name 'localed)
782 (extensions
783 (list (service-extension dbus-root-service-type
784 localed-dbus-service)
785 (service-extension udev-service-type package)
786 (service-extension polkit-service-type package)
787
788 ;; Add 'localectl' to the profile.
789 (service-extension profile-service-type package)))
790
791 ;; This service can be extended, typically by the X login
792 ;; manager, to communicate the chosen Xorg keyboard layout.
793 (compose (lambda (extensions)
794 (find keyboard-layout? extensions)))
795 (extend (lambda (config keyboard-layout)
796 (localed-configuration
797 (inherit config)
798 (keyboard-layout keyboard-layout))))
799 (description
800 "Run the locale daemon, @command{localed}, which can be used
801 to control the system locale and keyboard mapping from user programs such as
802 the GNOME desktop environment.")
803 (default-value (localed-configuration)))))
804
805 \f
806 ;;;
807 ;;; GNOME Desktop Manager.
808 ;;;
809
810 (define %gdm-accounts
811 (list (user-group (name "gdm") (system? #t))
812 (user-account
813 (name "gdm")
814 (group "gdm")
815 (supplementary-groups '("video"))
816 (system? #t)
817 (comment "GNOME Display Manager user")
818 (home-directory "/var/lib/gdm")
819 (shell (file-append shadow "/sbin/nologin")))))
820
821 (define %gdm-activation
822 ;; Ensure /var/lib/gdm is owned by the "gdm" user. This is normally the
823 ;; case but could be wrong if the "gdm" user was created, then removed, and
824 ;; then recreated under a different UID/GID: <https://bugs.gnu.org/37423>.
825 (with-imported-modules '((guix build utils))
826 #~(begin
827 (use-modules (guix build utils))
828
829 (let* ((gdm (getpwnam "gdm"))
830 (uid (passwd:uid gdm))
831 (gid (passwd:gid gdm))
832 (st (stat "/var/lib/gdm" #f)))
833 ;; Recurse into /var/lib/gdm only if it has wrong ownership.
834 (when (and st
835 (or (not (= uid (stat:uid st)))
836 (not (= gid (stat:gid st)))))
837 (for-each (lambda (file)
838 (chown file uid gid))
839 (find-files "/var/lib/gdm"
840 #:directories? #t)))))))
841
842 (define dbus-daemon-wrapper
843 (program-file
844 "gdm-dbus-wrapper"
845 #~(begin
846 (use-modules (srfi srfi-26))
847
848 (define system-profile
849 "/run/current-system/profile")
850
851 (define user-profile
852 (and=> (getpw (getuid))
853 (lambda (pw)
854 (string-append (passwd:dir pw) "/.guix-profile"))))
855
856 ;; If we are able to find the user's profile, we can add it to
857 ;; the search paths set below. We need to do this so that D-Bus
858 ;; can start services installed by the user. This allows
859 ;; applications that require session D-Bus services (e.g,
860 ;; 'evolution') to work even if those services are only available
861 ;; in the user's profile. See <https://bugs.gnu.org/35267>.
862 (define profiles
863 (if user-profile
864 (list user-profile system-profile)
865 (list system-profile)))
866
867 (setenv "XDG_CONFIG_DIRS"
868 (string-join (map (cut string-append <> "/etc/xdg") profiles)
869 ":"))
870 (setenv "XDG_DATA_DIRS"
871 (string-join (map (cut string-append <> "/share") profiles)
872 ":"))
873 (apply execl (string-append #$dbus "/bin/dbus-daemon")
874 (program-arguments)))))
875
876 ;; Wrapper script for Wayland sessions, similar to Xsession.
877 ;;
878 ;; See `xinitrc`. By default, it launches the specified session through a
879 ;; login shell. With the default Guix configuration, this should source
880 ;; /etc/profile, setting up the Guix profile environment variables. However,
881 ;; gdm launches its own graphical session through the same method, so we need
882 ;; to ignore this case, since `gdm` doesn't have a login shell.
883 (define gdm-wayland-session-wrapper
884 (program-file
885 "gdm-wayland-session-wrapper"
886 #~((let* ((user (getpw (getuid)))
887 (name (passwd:name user))
888 (shell (passwd:shell user))
889 (args (cdr (command-line))))
890 (if (string=? name "gdm")
891 (apply execl (cons (car args) args))
892 (execl shell shell "--login" "-c" (string-join args)))))))
893
894 (define-record-type* <gdm-configuration>
895 gdm-configuration make-gdm-configuration
896 gdm-configuration?
897 (gdm gdm-configuration-gdm (default gdm))
898 (allow-empty-passwords? gdm-configuration-allow-empty-passwords? (default #t))
899 (auto-login? gdm-configuration-auto-login? (default #f))
900 (dbus-daemon gdm-configuration-dbus-daemon (default dbus-daemon-wrapper))
901 (debug? gdm-configuration-debug? (default #f))
902 (default-user gdm-configuration-default-user (default #f))
903 (gnome-shell-assets gdm-configuration-gnome-shell-assets
904 (default (list adwaita-icon-theme font-abattis-cantarell)))
905 (xorg-configuration gdm-configuration-xorg
906 (default (xorg-configuration)))
907 (x-session gdm-configuration-x-session
908 (default (xinitrc)))
909 (wayland? gdm-configuration-wayland? (default #f))
910 (wayland-session gdm-configuration-wayland-session
911 (default gdm-wayland-session-wrapper)))
912
913 (define (gdm-configuration-file config)
914 (mixed-text-file "gdm-custom.conf"
915 "[daemon]\n"
916 "#User=gdm\n"
917 "#Group=gdm\n"
918 (if (gdm-configuration-auto-login? config)
919 (string-append
920 "AutomaticLoginEnable=true\n"
921 "AutomaticLogin="
922 (or (gdm-configuration-default-user config)
923 (error "missing default user for auto-login"))
924 "\n")
925 (string-append
926 "AutomaticLoginEnable=false\n"
927 "#AutomaticLogin=\n"))
928 "#TimedLoginEnable=false\n"
929 "#TimedLogin=\n"
930 "#TimedLoginDelay=0\n"
931 ;; Disable initial system setup inside GDM.
932 ;; Whatever settings are set there should already be
933 ;; taken care of through `guix system'.
934 ;; See also
935 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=39281>.
936 "InitialSetupEnable=false\n"
937 "WaylandEnable=" (if (gdm-configuration-wayland? config)
938 "true"
939 "false") "\n"
940 "\n"
941 "[debug]\n"
942 "Enable=" (if (gdm-configuration-debug? config)
943 "true"
944 "false") "\n"
945 "\n"
946 "[security]\n"
947 "#DisallowTCP=true\n"
948 "#AllowRemoteAutoLogin=false\n"))
949
950 (define (gdm-pam-service config)
951 "Return a PAM service for @command{gdm}."
952 (list
953 (pam-service
954 (inherit (unix-pam-service "gdm-autologin"
955 #:login-uid? #t))
956 (auth (list (pam-entry
957 (control "optional")
958 (module (file-append (gdm-configuration-gdm config)
959 "/lib/security/pam_gdm.so")))
960 (pam-entry
961 (control "sufficient")
962 (module "pam_permit.so")))))
963 (pam-service
964 (inherit (unix-pam-service "gdm-launch-environment"))
965 (auth (list (pam-entry
966 (control "required")
967 (module "pam_permit.so")))))
968 (unix-pam-service "gdm-password"
969 #:login-uid? #t
970 #:allow-empty-passwords?
971 (gdm-configuration-allow-empty-passwords? config))))
972
973 (define (gdm-shepherd-service config)
974 (list (shepherd-service
975 (documentation "Xorg display server (GDM)")
976 (provision '(xorg-server))
977 (requirement '(dbus-system user-processes host-name udev elogind))
978 (start #~(lambda ()
979 (fork+exec-command
980 (list #$(file-append (gdm-configuration-gdm config)
981 "/bin/gdm"))
982 #:environment-variables
983 (list (string-append
984 "GDM_CUSTOM_CONF="
985 #$(gdm-configuration-file config))
986 (string-append
987 "GDM_DBUS_DAEMON="
988 #$(gdm-configuration-dbus-daemon config))
989 (string-append
990 "GDM_X_SERVER="
991 #$(xorg-wrapper
992 (gdm-configuration-xorg config)))
993 (string-append
994 "GDM_X_SESSION="
995 #$(gdm-configuration-x-session config))
996 (string-append
997 "XDG_DATA_DIRS="
998 ((lambda (ls) (string-join ls ":"))
999 (map (lambda (path)
1000 (string-append path "/share"))
1001 ;; XXX: Remove gnome-shell below when GDM
1002 ;; can depend on GNOME Shell directly.
1003 (cons #$gnome-shell
1004 '#$(gdm-configuration-gnome-shell-assets
1005 config)))))
1006 ;; Add XCURSOR_PATH so that mutter can find its
1007 ;; cursors. gdm doesn't login so doesn't source
1008 ;; the corresponding line in /etc/profile.
1009 "XCURSOR_PATH=/run/current-system/profile/share/icons"
1010 (string-append
1011 "GDK_PIXBUF_MODULE_FILE="
1012 #$gnome-shell "/" #$%gdk-pixbuf-loaders-cache-file)
1013 (string-append
1014 "GDM_WAYLAND_SESSION="
1015 #$(gdm-configuration-wayland-session config))))))
1016 (stop #~(make-kill-destructor))
1017 (respawn? #t))))
1018
1019 (define gdm-service-type
1020 (handle-xorg-configuration gdm-configuration
1021 (service-type (name 'gdm)
1022 (extensions
1023 (list (service-extension shepherd-root-service-type
1024 gdm-shepherd-service)
1025 (service-extension activation-service-type
1026 (const %gdm-activation))
1027 (service-extension account-service-type
1028 (const %gdm-accounts))
1029 (service-extension pam-root-service-type
1030 gdm-pam-service)
1031 (service-extension profile-service-type
1032 gdm-configuration-gnome-shell-assets)
1033 (service-extension dbus-root-service-type
1034 (compose list
1035 gdm-configuration-gdm))
1036 (service-extension localed-service-type
1037 (compose
1038 xorg-configuration-keyboard-layout
1039 gdm-configuration-xorg))))
1040 (default-value (gdm-configuration))
1041 (description
1042 "Run the GNOME Desktop Manager (GDM), a program that allows
1043 you to log in in a graphical session, whether or not you use GNOME."))))
1044
1045 ;; Since GDM depends on Rust (gdm -> gnome-shell -> gjs -> mozjs -> rust)
1046 ;; and Rust is currently unavailable on non-x86_64 platforms, default to
1047 ;; SDDM there (FIXME).
1048 (define* (set-xorg-configuration config
1049 #:optional
1050 (login-manager-service-type
1051 (if (target-x86-64?)
1052 gdm-service-type
1053 sddm-service-type)))
1054 "Tell the log-in manager (of type @var{login-manager-service-type}) to use
1055 @var{config}, an <xorg-configuration> record."
1056 (simple-service 'set-xorg-configuration
1057 login-manager-service-type
1058 config))
1059
1060 ;;; xorg.scm ends here