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