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