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