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