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