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>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (gnu services xorg)
24 #:use-module (gnu artwork)
25 #:use-module (gnu services)
26 #:use-module (gnu services shepherd)
27 #:use-module (gnu system pam)
28 #:use-module (gnu system keyboard)
29 #:use-module (gnu services dbus)
30 #:use-module ((gnu packages base) #:select (canonical-package))
31 #:use-module (gnu packages guile)
32 #:use-module (gnu packages xorg)
33 #:use-module (gnu packages fonts)
34 #:use-module (gnu packages gl)
35 #:use-module (gnu packages glib)
36 #:use-module (gnu packages display-managers)
37 #:use-module (gnu packages gnustep)
38 #:use-module (gnu packages gnome)
39 #:use-module (gnu packages admin)
40 #:use-module (gnu packages bash)
41 #:use-module (gnu system shadow)
42 #:use-module (guix gexp)
43 #:use-module (guix store)
44 #:use-module (guix packages)
45 #:use-module (guix derivations)
46 #:use-module (guix records)
47 #:use-module (guix deprecation)
48 #:use-module (srfi srfi-1)
49 #:use-module (srfi srfi-9)
50 #:use-module (srfi srfi-26)
51 #:use-module (ice-9 match)
52 #:export (xorg-configuration
54 xorg-configuration-modules
55 xorg-configuration-fonts
56 xorg-configuration-drivers
57 xorg-configuration-resolutions
58 xorg-configuration-extra-config
59 xorg-configuration-server
60 xorg-configuration-server-arguments
69 %default-slim-theme-name
73 slim-configuration-slim
74 slim-configuration-allow-empty-passwords?
75 slim-configuration-auto-login?
76 slim-configuration-default-user
77 slim-configuration-theme
78 slim-configuration-theme-name
79 slim-configuration-xauth
80 slim-configuration-shepherd
81 slim-configuration-auto-login-session
82 slim-configuration-startx
89 screen-locker-service-type
98 ;;; Services that relate to the X Window System.
102 (define %default-xorg-modules
103 ;; Default list of modules loaded by the server. When multiple drivers
104 ;; match, the first one in the list is loaded.
105 (list xf86-video-vesa
116 ;; Libinput is the new thing and is recommended over evdev/synaptics:
117 ;; <http://who-t.blogspot.fr/2015/01/xf86-input-libinput-compatibility-with.html>.
123 xf86-input-synaptics))
125 (define %default-xorg-fonts
126 ;; Default list of fonts available to the X server.
127 (list (file-append font-alias "/share/fonts/X11/75dpi")
128 (file-append font-alias "/share/fonts/X11/100dpi")
129 (file-append font-alias "/share/fonts/X11/misc")
130 (file-append font-alias "/share/fonts/X11/cyrillic")
131 (file-append font-misc-misc ;default fonts for xterm
132 "/share/fonts/X11/misc")
133 (file-append font-adobe75dpi "/share/fonts/X11/75dpi")))
135 (define %default-xorg-server-arguments
136 ;; Default command-line arguments for X.
137 '("-nolisten" "tcp"))
139 ;; Configuration of an Xorg server.
140 (define-record-type* <xorg-configuration>
141 xorg-configuration make-xorg-configuration
143 (modules xorg-configuration-modules ;list of packages
144 (default %default-xorg-modules))
145 (fonts xorg-configuration-fonts ;list of packges
146 (default %default-xorg-fonts))
147 (drivers xorg-configuration-drivers ;list of strings
149 (resolutions xorg-configuration-resolutions ;list of tuples
151 (keyboard-layout xorg-configuration-keyboard-layout ;#f | <keyboard-layout>
153 (extra-config xorg-configuration-extra-config ;list of strings
155 (server xorg-configuration-server ;package
156 (default xorg-server))
157 (server-arguments xorg-configuration-server-arguments ;list of strings
158 (default %default-xorg-server-arguments)))
160 (define (xorg-configuration->file config)
161 "Compute an Xorg configuration file corresponding to CONFIG, an
162 <xorg-configuration> record."
164 ;; 'xorg-server' provides 'fbdevhw.so' etc.
165 (append (xorg-configuration-modules config)
170 (use-modules (ice-9 match)
174 (call-with-output-file #$output
177 '#$(xorg-configuration-drivers config))
179 (define (device-section driver)
182 Identifier \"device-" driver "\"
183 Driver \"" driver "\"
186 (define (screen-section driver resolutions)
189 Identifier \"screen-" driver "\"
190 Device \"device-" driver "\"
191 SubSection \"Display\"
193 (string-join (map (match-lambda
195 (string-append "\"" (number->string x)
196 "x" (number->string y) "\"")))
201 (define (input-class-section layout variant model options)
203 Section \"InputClass\"
204 Identifier \"evdev keyboard catchall\"
205 MatchIsKeyboard \"on\"
206 Option \"XkbLayout\" " (object->string layout)
208 (string-append " Option \"XkbVariant\" \""
212 (string-append " Option \"XkbModel\" \""
219 (string-append " Option \"XkbOptions\" \""
220 (string-join options ",") "\""))) "
222 MatchDevicePath \"/dev/input/event*\"
226 (define (expand modules)
227 ;; Append to MODULES the relevant /lib/xorg/modules
229 (append-map (lambda (module)
230 (filter-map (lambda (directory)
231 (let ((full (string-append module
233 (and (file-exists? full)
235 '("/lib/xorg/modules/drivers"
236 "/lib/xorg/modules/input"
237 "/lib/xorg/modules/multimedia"
238 "/lib/xorg/modules/extensions")))
241 (display "Section \"Files\"\n" port)
242 (for-each (lambda (font)
243 (format port " FontPath \"~a\"~%" font))
244 '#$(xorg-configuration-fonts config))
245 (for-each (lambda (module)
247 " ModulePath \"~a\"~%"
249 (append (expand '#$all-modules)
251 ;; For fbdevhw.so and so on.
252 (list #$(file-append xorg-server
253 "/lib/xorg/modules"))))
254 (display "EndSection\n" port)
256 Section \"ServerFlags\"
257 Option \"AllowMouseOpenFail\" \"on\"
260 (display (string-join (map device-section drivers) "\n")
263 (display (string-join
264 (map (cut screen-section <>
265 '#$(xorg-configuration-resolutions config))
271 (let ((layout #$(and=> (xorg-configuration-keyboard-layout config)
272 keyboard-layout-name))
273 (variant #$(and=> (xorg-configuration-keyboard-layout config)
274 keyboard-layout-variant))
275 (model #$(and=> (xorg-configuration-keyboard-layout config)
276 keyboard-layout-model))
277 (options '#$(and=> (xorg-configuration-keyboard-layout config)
278 keyboard-layout-options)))
280 (display (input-class-section layout variant model options)
284 (for-each (lambda (config)
285 (display config port))
286 '#$(xorg-configuration-extra-config config))))))
288 (computed-file "xserver.conf" build))
290 (define (xorg-configuration-directory modules)
291 "Return a directory that contains the @code{.conf} files for X.org that
292 includes the @code{share/X11/xorg.conf.d} directories of each package listed
294 (with-imported-modules '((guix build utils))
295 (computed-file "xorg.conf.d"
297 (use-modules (guix build utils)
301 (append-map (lambda (module)
302 (find-files (string-append
304 "/share/X11/xorg.conf.d")
309 (for-each (lambda (file)
311 (string-append #$output "/"
316 (define* (xorg-wrapper #:optional (config (xorg-configuration)))
317 "Return a derivation that builds a script to start the X server with the
318 given @var{config}. The resulting script should be used in place of
321 ;; Write a small wrapper around the X server.
323 (setenv "XORG_DRI_DRIVER_PATH" (string-append #$mesa "/lib/dri"))
324 (setenv "XKB_BINDIR" (string-append #$xkbcomp "/bin"))
326 (let ((X (string-append #$(xorg-configuration-server config) "/bin/X")))
328 "-xkbdir" (string-append #$xkeyboard-config "/share/X11/xkb")
329 "-config" #$(xorg-configuration->file config)
330 "-configdir" #$(xorg-configuration-directory
331 (xorg-configuration-modules config))
332 (cdr (command-line))))))
334 (program-file "X-wrapper" exp))
336 (define* (xorg-start-command #:optional (config (xorg-configuration)))
337 "Return a @code{startx} script in which the modules, fonts, etc. specified
338 in @var{config}, are available. The result should be used in place of
341 (xorg-wrapper config))
344 ;; Write a small wrapper around the X server.
345 #~(apply execl #$X #$X ;; Second #$X is for argv[0].
346 "-logverbose" "-verbose" "-terminate"
347 #$@(xorg-configuration-server-arguments config)
348 (cdr (command-line))))
350 (program-file "startx" exp))
352 (define* (xinitrc #:key fallback-session)
353 "Return a system-wide xinitrc script that starts the specified X session,
354 which should be passed to this script as the first argument. If not, the
355 @var{fallback-session} will be used or, if @var{fallback-session} is false, a
356 desktop session from the system or user profile will be used."
359 (use-modules (ice-9 match)
366 (define (close-all-fdes)
367 ;; Close all the open file descriptors except 0 to 2.
369 (when (< fd 4096) ;FIXME: use sysconf + _SC_OPEN_MAX
370 (false-if-exception (close-fdes fd))
373 (define (exec-from-login-shell command . args)
374 ;; Run COMMAND from a login shell so that it gets to see the same
375 ;; environment variables that one gets when logging in on a tty, for
377 (let* ((pw (getpw (getuid)))
378 (shell (passwd:shell pw)))
379 ;; Close any open file descriptors. This is all the more
380 ;; important that SLiM itself exec's us directly without closing
381 ;; its own file descriptors!
384 ;; The '--login' option is supported at least by Bash and zsh.
385 (execl shell shell "--login" "-c"
386 (string-join (cons command args)))))
388 (define system-profile
389 "/run/current-system/profile")
392 (and=> (getpw (getuid))
394 (string-append (passwd:dir pw) "/.guix-profile"))))
396 (define (xsession-command desktop-file)
397 ;; Read from DESKTOP-FILE its X session command and return it as a
400 (make-regexp "^[[:blank:]]*Exec=(.*)$"))
402 (call-with-input-file desktop-file
405 (match (read-line port)
407 ((= (cut regexp-exec exec-regexp <>) result)
409 (string-tokenize (match:substring result 1))
412 (define (find-session profile)
413 ;; Return an X session command from PROFILE or #f if none was found.
414 (let ((directory (string-append profile "/share/xsessions")))
415 (match (scandir directory
416 (cut string-suffix? ".desktop" <>))
420 (any xsession-command
421 (map (cut string-append directory "/" <>)
424 (let* ((home (getenv "HOME"))
425 (xsession-file (string-append home "/.xsession"))
426 (session (match (command-line)
428 #$(if fallback-session
429 #~(list #$fallback-session)
433 (if (file-exists? xsession-file)
434 ;; Run ~/.xsession when it exists.
435 (apply exec-from-login-shell xsession-file
437 ;; Otherwise, start the specified session or a fallback.
438 (apply exec-from-login-shell
440 (find-session user-profile)
441 (find-session system-profile)))))))
443 (program-file "xinitrc" builder))
447 ;;; SLiM log-in manager.
450 (define %default-slim-theme
451 ;; Theme based on work by Felipe López.
452 (file-append %artwork-repository "/slim"))
454 (define %default-slim-theme-name
455 ;; This must be the name of the sub-directory in %DEFAULT-SLIM-THEME that
456 ;; contains the actual theme files.
459 (define-record-type* <slim-configuration>
460 slim-configuration make-slim-configuration
462 (slim slim-configuration-slim
464 (allow-empty-passwords? slim-configuration-allow-empty-passwords?
466 (auto-login? slim-configuration-auto-login?
468 (default-user slim-configuration-default-user
470 (theme slim-configuration-theme
471 (default %default-slim-theme))
472 (theme-name slim-configuration-theme-name
473 (default %default-slim-theme-name))
474 (xauth slim-configuration-xauth
476 (shepherd slim-configuration-shepherd
478 (auto-login-session slim-configuration-auto-login-session
480 (xorg-configuration slim-configuration-xorg
481 (default (xorg-configuration)))
482 (sessreg slim-configuration-sessreg
485 (define (slim-pam-service config)
486 "Return a PAM service for @command{slim}."
487 (list (unix-pam-service
489 #:allow-empty-passwords?
490 (slim-configuration-allow-empty-passwords? config))))
492 (define (slim-shepherd-service config)
494 (let ((xinitrc (xinitrc #:fallback-session
495 (slim-configuration-auto-login-session config)))
496 (slim (slim-configuration-slim config))
497 (xauth (slim-configuration-xauth config))
498 (startx (xorg-start-command (slim-configuration-xorg config)))
499 (shepherd (slim-configuration-shepherd config))
500 (theme-name (slim-configuration-theme-name config))
501 (sessreg (slim-configuration-sessreg config)))
502 (mixed-text-file "slim.cfg" "
503 default_path /run/current-system/profile/bin
504 default_xserver " startx "
505 xserver_arguments :0 vt7
506 xauth_path " xauth "/bin/xauth
507 authfile /var/run/slim.auth
509 # The login command. '%session' is replaced by the chosen session name, one
510 # of the names specified in the 'sessions' setting: 'wmaker', 'xfce', etc.
511 login_cmd exec " xinitrc " %session
512 sessiondir /run/current-system/profile/share/xsessions
513 session_msg session (F1 to change):
514 sessionstart_cmd " sessreg "/bin/sessreg -a -l $DISPLAY %user
515 sessionstop_cmd " sessreg "/bin/sessreg -d -l $DISPLAY %user
517 halt_cmd " shepherd "/sbin/halt
518 reboot_cmd " shepherd "/sbin/reboot\n"
519 (if (slim-configuration-auto-login? config)
520 (string-append "auto_login yes\ndefault_user "
521 (slim-configuration-default-user config) "\n")
524 (string-append "current_theme " theme-name "\n")
528 (slim-configuration-theme config))
530 (list (shepherd-service
531 (documentation "Xorg display server")
532 (provision '(xorg-server))
533 (requirement '(user-processes host-name udev))
536 ;; A stale lock file can prevent SLiM from starting, so remove it to
537 ;; be on the safe side.
538 (false-if-exception (delete-file "/var/run/slim.lock"))
541 (list (string-append #$slim "/bin/slim") "-nodaemon")
542 #:environment-variables
543 (list (string-append "SLIM_CFGFILE=" #$slim.cfg)
545 (list #~(string-append "SLIM_THEMESDIR=" #$theme))
547 (stop #~(make-kill-destructor))
550 (define slim-service-type
551 (service-type (name 'slim)
553 (list (service-extension shepherd-root-service-type
554 slim-shepherd-service)
555 (service-extension pam-root-service-type
558 ;; Unconditionally add xterm to the system profile, to
559 ;; avoid bad surprises.
560 (service-extension profile-service-type
561 (const (list xterm)))))
562 (default-value (slim-configuration))))
564 (define-deprecated (slim-service #:key (slim slim)
565 (allow-empty-passwords? #t) auto-login?
567 (theme %default-slim-theme)
568 (theme-name %default-slim-theme-name)
569 (xauth xauth) (shepherd shepherd)
570 (auto-login-session #f)
571 (startx (xorg-start-command)))
573 "Return a service that spawns the SLiM graphical login manager, which in
574 turn starts the X display server with @var{startx}, a command as returned by
575 @code{xorg-start-command}.
579 SLiM automatically looks for session types described by the @file{.desktop}
580 files in @file{/run/current-system/profile/share/xsessions} and allows users
581 to choose a session from the log-in screen using @kbd{F1}. Packages such as
582 @var{xfce}, @var{sawfish}, and @var{ratpoison} provide @file{.desktop} files;
583 adding them to the system-wide set of packages automatically makes them
584 available at the log-in screen.
586 In addition, @file{~/.xsession} files are honored. When available,
587 @file{~/.xsession} must be an executable that starts a window manager
588 and/or other X clients.
590 When @var{allow-empty-passwords?} is true, allow logins with an empty
591 password. When @var{auto-login?} is true, log in automatically as
592 @var{default-user} with @var{auto-login-session}.
594 If @var{theme} is @code{#f}, the use the default log-in theme; otherwise
595 @var{theme} must be a gexp denoting the name of a directory containing the
596 theme to use. In that case, @var{theme-name} specifies the name of the
598 (service slim-service-type
601 (allow-empty-passwords? allow-empty-passwords?)
602 (auto-login? auto-login?) (default-user default-user)
603 (theme theme) (theme-name theme-name)
604 (xauth xauth) (shepherd shepherd)
605 (auto-login-session auto-login-session))))
609 ;;; Screen lockers & co.
612 (define-record-type <screen-locker>
613 (screen-locker name program empty?)
615 (name screen-locker-name) ;string
616 (program screen-locker-program) ;gexp
617 (empty? screen-locker-allows-empty-passwords?)) ;Boolean
619 (define screen-locker-pam-services
621 (($ <screen-locker> name _ empty?)
622 (list (unix-pam-service name
623 #:allow-empty-passwords? empty?)))))
625 (define screen-locker-setuid-programs
626 (compose list screen-locker-program))
628 (define screen-locker-service-type
629 (service-type (name 'screen-locker)
631 (list (service-extension pam-root-service-type
632 screen-locker-pam-services)
633 (service-extension setuid-program-service-type
634 screen-locker-setuid-programs)))))
636 (define* (screen-locker-service package
638 (program (package-name package))
639 #:key allow-empty-passwords?)
640 "Add @var{package}, a package for a screen locker or screen saver whose
641 command is @var{program}, to the set of setuid programs and add a PAM entry
645 (screen-locker-service xlockmore \"xlock\")
648 makes the good ol' XlockMore usable."
649 (service screen-locker-service-type
650 (screen-locker program
651 (file-append package "/bin/" program)
652 allow-empty-passwords?)))
654 (define %gdm-accounts
655 (list (user-group (name "gdm") (system? #t))
660 (comment "GNOME Display Manager user")
661 (home-directory "/var/lib/gdm")
662 (shell (file-append shadow "/sbin/nologin")))))
664 (define dbus-daemon-wrapper
665 (program-file "gdm-dbus-wrapper"
667 (setenv "XDG_CONFIG_DIRS"
668 "/run/current-system/profile/etc/xdg")
669 (setenv "XDG_DATA_DIRS"
670 "/run/current-system/profile/share")
671 (apply execl (string-append #$dbus "/bin/dbus-daemon")
672 (program-arguments)))))
674 (define-record-type* <gdm-configuration>
675 gdm-configuration make-gdm-configuration
677 (gdm gdm-configuration-gdm (default gdm))
678 (allow-empty-passwords? gdm-configuration-allow-empty-passwords? (default #t))
679 (auto-login? gdm-configuration-auto-login? (default #f))
680 (dbus-daemon gdm-configuration-dbus-daemon (default dbus-daemon-wrapper))
681 (default-user gdm-configuration-default-user (default #f))
682 (gnome-shell-assets gdm-configuration-gnome-shell-assets
683 (default (list adwaita-icon-theme font-cantarell)))
684 (xorg-configuration gdm-configuration-xorg
685 (default (xorg-configuration)))
686 (x-session gdm-configuration-x-session
687 (default (xinitrc))))
689 (define (gdm-configuration-file config)
690 (mixed-text-file "gdm-custom.conf"
694 (if (gdm-configuration-auto-login? config)
696 "AutomaticLoginEnable=true\n"
698 (or (gdm-configuration-default-user config)
699 (error "missing default user for auto-login"))
702 "AutomaticLoginEnable=false\n"
703 "#AutomaticLogin=\n"))
704 "#TimedLoginEnable=false\n"
706 "#TimedLoginDelay=0\n"
707 "#InitialSetupEnable=true\n"
708 ;; Enable me once X is working.
709 "WaylandEnable=false\n"
715 "#DisallowTCP=true\n"
716 "#AllowRemoteAutoLogin=false\n"))
718 (define (gdm-pam-service config)
719 "Return a PAM service for @command{gdm}."
722 (inherit (unix-pam-service "gdm-autologin"))
723 (auth (list (pam-entry
724 (control "[success=ok default=1]")
725 (module (file-append (gdm-configuration-gdm config)
726 "/lib/security/pam_gdm.so")))
728 (control "sufficient")
729 (module "pam_permit.so")))))
731 (inherit (unix-pam-service "gdm-launch-environment"))
732 (auth (list (pam-entry
734 (module "pam_permit.so")))))
735 (unix-pam-service "gdm-password"
736 #:allow-empty-passwords?
737 (gdm-configuration-allow-empty-passwords? config))))
739 (define (gdm-shepherd-service config)
740 (list (shepherd-service
741 (documentation "Xorg display server (GDM)")
742 (provision '(xorg-server))
743 (requirement '(dbus-system user-processes host-name udev))
746 (list #$(file-append (gdm-configuration-gdm config)
748 #:environment-variables
751 #$(gdm-configuration-file config))
754 #$(gdm-configuration-dbus-daemon config))
758 (gdm-configuration-xorg config)))
761 #$(gdm-configuration-x-session config))
764 ((lambda (ls) (string-join ls ":"))
766 (string-append path "/share"))
767 ;; XXX: Remove gnome-shell below when GDM
768 ;; can depend on GNOME Shell directly.
770 '#$(gdm-configuration-gnome-shell-assets
772 (stop #~(make-kill-destructor))
775 (define gdm-service-type
776 (service-type (name 'gdm)
778 (list (service-extension shepherd-root-service-type
779 gdm-shepherd-service)
780 (service-extension account-service-type
781 (const %gdm-accounts))
782 (service-extension pam-root-service-type
784 (service-extension profile-service-type
785 gdm-configuration-gnome-shell-assets)
786 (service-extension dbus-root-service-type
788 gdm-configuration-gdm))))
789 (default-value (gdm-configuration))
791 "Run the GNOME Desktop Manager (GDM), a program that allows
792 you to log in in a graphical session, whether or not you use GNOME.")))
794 (define-deprecated (gdm-service #:key (gdm gdm)
795 (allow-empty-passwords? #t)
796 (x-server (xorg-wrapper)))
798 "Return a service that spawns the GDM graphical login manager, which in turn
799 starts the X display server with @var{X}, a command as returned by
804 GDM automatically looks for session types described by the @file{.desktop}
805 files in @file{/run/current-system/profile/share/xsessions} and allows users
806 to choose a session from the log-in screen using @kbd{F1}. Packages such as
807 @var{xfce}, @var{sawfish}, and @var{ratpoison} provide @file{.desktop} files;
808 adding them to the system-wide set of packages automatically makes them
809 available at the log-in screen.
811 In addition, @file{~/.xsession} files are honored. When available,
812 @file{~/.xsession} must be an executable that starts a window manager
813 and/or other X clients.
815 When @var{allow-empty-passwords?} is true, allow logins with an empty
817 (service gdm-service-type
820 (allow-empty-passwords? allow-empty-passwords?))))
822 ;;; xorg.scm ends here