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>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 (define-module (gnu services xorg)
23 #:use-module (gnu artwork)
24 #:use-module (gnu services)
25 #:use-module (gnu services shepherd)
26 #:use-module (gnu system pam)
27 #:use-module (gnu services dbus)
28 #:use-module ((gnu packages base) #:select (canonical-package))
29 #:use-module (gnu packages guile)
30 #:use-module (gnu packages xorg)
31 #:use-module (gnu packages fonts)
32 #:use-module (gnu packages gl)
33 #:use-module (gnu packages glib)
34 #:use-module (gnu packages display-managers)
35 #:use-module (gnu packages gnustep)
36 #:use-module (gnu packages gnome)
37 #:use-module (gnu packages admin)
38 #:use-module (gnu packages bash)
39 #:use-module (gnu system shadow)
40 #:use-module (guix gexp)
41 #:use-module (guix store)
42 #:use-module (guix packages)
43 #:use-module (guix derivations)
44 #:use-module (guix records)
45 #:use-module (guix deprecation)
46 #:use-module (srfi srfi-1)
47 #:use-module (srfi srfi-9)
48 #:use-module (srfi srfi-26)
49 #:use-module (ice-9 match)
50 #:export (xorg-configuration-file
58 %default-slim-theme-name
62 slim-configuration-slim
63 slim-configuration-allow-empty-passwords?
64 slim-configuration-auto-login?
65 slim-configuration-default-user
66 slim-configuration-theme
67 slim-configuration-theme-name
68 slim-configuration-xauth
69 slim-configuration-shepherd
70 slim-configuration-auto-login-session
71 slim-configuration-startx
78 screen-locker-service-type
87 ;;; Services that relate to the X Window System.
91 (define %default-xorg-modules
92 ;; Default list of modules loaded by the server. When multiple drivers
93 ;; match, the first one in the list is loaded.
105 ;; Libinput is the new thing and is recommended over evdev/synaptics:
106 ;; <http://who-t.blogspot.fr/2015/01/xf86-input-libinput-compatibility-with.html>.
112 xf86-input-synaptics))
114 (define %default-xorg-fonts
115 ;; Default list of fonts available to the X server.
116 (list (file-append font-alias "/share/fonts/X11/75dpi")
117 (file-append font-alias "/share/fonts/X11/100dpi")
118 (file-append font-alias "/share/fonts/X11/misc")
119 (file-append font-alias "/share/fonts/X11/cyrillic")
120 (file-append font-misc-misc ;default fonts for xterm
121 "/share/fonts/X11/misc")
122 (file-append font-adobe75dpi "/share/fonts/X11/75dpi")))
124 (define* (xorg-configuration-file #:key
125 (modules %default-xorg-modules)
126 (fonts %default-xorg-fonts)
127 (drivers '()) (resolutions '())
129 "Return a configuration file for the Xorg server containing search paths for
130 all the common drivers.
132 @var{modules} must be a list of @dfn{module packages} loaded by the Xorg
133 server---e.g., @code{xf86-video-vesa}, @code{xf86-input-keyboard}, and so on.
134 @var{fonts} must be a list of font directories to add to the server's
137 @var{drivers} must be either the empty list, in which case Xorg chooses a
138 graphics driver automatically, or a list of driver names that will be tried in
139 this order---e.g., @code{(\"modesetting\" \"vesa\")}.
141 Likewise, when @var{resolutions} is the empty list, Xorg chooses an
142 appropriate screen resolution; otherwise, it must be a list of
143 resolutions---e.g., @code{((1024 768) (640 480))}.
145 Last, @var{extra-config} is a list of strings or objects appended to the
146 configuration file. It is used to pass extra text to be
147 added verbatim to the configuration file."
149 ;; 'xorg-server' provides 'fbdevhw.so' etc.
150 (append modules (list xorg-server)))
154 (use-modules (ice-9 match)
158 (call-with-output-file #$output
163 (define (device-section driver)
166 Identifier \"device-" driver "\"
167 Driver \"" driver "\"
170 (define (screen-section driver resolutions)
173 Identifier \"screen-" driver "\"
174 Device \"device-" driver "\"
175 SubSection \"Display\"
177 (string-join (map (match-lambda
179 (string-append "\"" (number->string x)
180 "x" (number->string y) "\"")))
185 (define (expand modules)
186 ;; Append to MODULES the relevant /lib/xorg/modules
188 (append-map (lambda (module)
189 (filter-map (lambda (directory)
190 (let ((full (string-append module
192 (and (file-exists? full)
194 '("/lib/xorg/modules/drivers"
195 "/lib/xorg/modules/input"
196 "/lib/xorg/modules/multimedia"
197 "/lib/xorg/modules/extensions")))
200 (display "Section \"Files\"\n" port)
201 (for-each (lambda (font)
202 (format port " FontPath \"~a\"~%" font))
204 (for-each (lambda (module)
206 " ModulePath \"~a\"~%"
208 (append (expand '#$all-modules)
210 ;; For fbdevhw.so and so on.
211 (list #$(file-append xorg-server
212 "/lib/xorg/modules"))))
213 (display "EndSection\n" port)
215 Section \"ServerFlags\"
216 Option \"AllowMouseOpenFail\" \"on\"
219 (display (string-join (map device-section drivers) "\n")
222 (display (string-join
223 (map (cut screen-section <> '#$resolutions)
229 (for-each (lambda (config)
230 (display config port))
233 (computed-file "xserver.conf" build))
236 (define (xorg-configuration-directory modules)
237 "Return a directory that contains the @code{.conf} files for X.org that
238 includes the @code{share/X11/xorg.conf.d} directories of each package listed
240 (with-imported-modules '((guix build utils))
241 (computed-file "xorg.conf.d"
243 (use-modules (guix build utils)
247 (append-map (lambda (module)
248 (find-files (string-append
250 "/share/X11/xorg.conf.d")
255 (for-each (lambda (file)
257 (string-append #$output "/"
262 (define* (xorg-wrapper #:key
263 (guile (canonical-package guile-2.0))
264 (modules %default-xorg-modules)
265 (configuration-file (xorg-configuration-file
267 (xorg-server xorg-server))
268 "Return a derivation that builds a @var{guile} script to start the X server
269 from @var{xorg-server}. @var{configuration-file} is the server configuration
270 file or a derivation that builds it; when omitted, the result of
271 @code{xorg-configuration-file} is used. The resulting script should be used
272 in place of @code{/usr/bin/X}."
274 ;; Write a small wrapper around the X server.
276 (setenv "XORG_DRI_DRIVER_PATH" (string-append #$mesa "/lib/dri"))
277 (setenv "XKB_BINDIR" (string-append #$xkbcomp "/bin"))
279 (let ((X (string-append #$xorg-server "/bin/X")))
281 "-xkbdir" (string-append #$xkeyboard-config "/share/X11/xkb")
282 "-config" #$configuration-file
283 "-configdir" #$(xorg-configuration-directory modules)
284 (cdr (command-line))))))
286 (program-file "X-wrapper" exp))
288 (define* (xorg-start-command #:key
289 (guile (canonical-package guile-2.0))
290 (modules %default-xorg-modules)
291 (fonts %default-xorg-fonts)
293 (xorg-configuration-file #:modules modules
295 (xorg-server xorg-server))
296 "Return a @code{startx} script in which @var{modules}, a list of X module
297 packages, and @var{fonts}, a list of X font directories, are available. See
298 @code{xorg-wrapper} for more details on the arguments. The result should be
299 used in place of @code{startx}."
301 (xorg-wrapper #:guile guile
302 #:configuration-file configuration-file
304 #:xorg-server xorg-server))
306 ;; Write a small wrapper around the X server.
307 #~(apply execl #$X #$X ;; Second #$X is for argv[0].
308 "-logverbose" "-verbose" "-nolisten" "tcp" "-terminate"
309 (cdr (command-line))))
311 (program-file "startx" exp))
313 (define* (xinitrc #:key
314 (guile (canonical-package guile-2.0))
316 "Return a system-wide xinitrc script that starts the specified X session,
317 which should be passed to this script as the first argument. If not, the
318 @var{fallback-session} will be used or, if @var{fallback-session} is false, a
319 desktop session from the system or user profile will be used."
322 (use-modules (ice-9 match)
329 (define (close-all-fdes)
330 ;; Close all the open file descriptors except 0 to 2.
332 (when (< fd 4096) ;FIXME: use sysconf + _SC_OPEN_MAX
333 (false-if-exception (close-fdes fd))
336 (define (exec-from-login-shell command . args)
337 ;; Run COMMAND from a login shell so that it gets to see the same
338 ;; environment variables that one gets when logging in on a tty, for
340 (let* ((pw (getpw (getuid)))
341 (shell (passwd:shell pw)))
342 ;; Close any open file descriptors. This is all the more
343 ;; important that SLiM itself exec's us directly without closing
344 ;; its own file descriptors!
347 ;; The '--login' option is supported at least by Bash and zsh.
348 (execl shell shell "--login" "-c"
349 (string-join (cons command args)))))
351 (define system-profile
352 "/run/current-system/profile")
355 (and=> (getpw (getuid))
357 (string-append (passwd:dir pw) "/.guix-profile"))))
359 (define (xsession-command desktop-file)
360 ;; Read from DESKTOP-FILE its X session command and return it as a
363 (make-regexp "^[[:blank:]]*Exec=(.*)$"))
365 (call-with-input-file desktop-file
368 (match (read-line port)
370 ((= (cut regexp-exec exec-regexp <>) result)
372 (string-tokenize (match:substring result 1))
375 (define (find-session profile)
376 ;; Return an X session command from PROFILE or #f if none was found.
377 (let ((directory (string-append profile "/share/xsessions")))
378 (match (scandir directory
379 (cut string-suffix? ".desktop" <>))
383 (any xsession-command
384 (map (cut string-append directory "/" <>)
387 (let* ((home (getenv "HOME"))
388 (xsession-file (string-append home "/.xsession"))
389 (session (match (command-line)
391 #$(if fallback-session
392 #~(list #$fallback-session)
396 (if (file-exists? xsession-file)
397 ;; Run ~/.xsession when it exists.
398 (apply exec-from-login-shell xsession-file
400 ;; Otherwise, start the specified session or a fallback.
401 (apply exec-from-login-shell
403 (find-session user-profile)
404 (find-session system-profile)))))))
406 (program-file "xinitrc" builder))
410 ;;; SLiM log-in manager.
413 (define %default-slim-theme
414 ;; Theme based on work by Felipe López.
415 (file-append %artwork-repository "/slim"))
417 (define %default-slim-theme-name
418 ;; This must be the name of the sub-directory in %DEFAULT-SLIM-THEME that
419 ;; contains the actual theme files.
422 (define-record-type* <slim-configuration>
423 slim-configuration make-slim-configuration
425 (slim slim-configuration-slim
427 (allow-empty-passwords? slim-configuration-allow-empty-passwords?
429 (auto-login? slim-configuration-auto-login?
431 (default-user slim-configuration-default-user
433 (theme slim-configuration-theme
434 (default %default-slim-theme))
435 (theme-name slim-configuration-theme-name
436 (default %default-slim-theme-name))
437 (xauth slim-configuration-xauth
439 (shepherd slim-configuration-shepherd
441 (auto-login-session slim-configuration-auto-login-session
443 (startx slim-configuration-startx
444 (default (xorg-start-command)))
445 (sessreg slim-configuration-sessreg
448 (define (slim-pam-service config)
449 "Return a PAM service for @command{slim}."
450 (list (unix-pam-service
452 #:allow-empty-passwords?
453 (slim-configuration-allow-empty-passwords? config))))
455 (define (slim-shepherd-service config)
457 (let ((xinitrc (xinitrc #:fallback-session
458 (slim-configuration-auto-login-session config)))
459 (slim (slim-configuration-slim config))
460 (xauth (slim-configuration-xauth config))
461 (startx (slim-configuration-startx config))
462 (shepherd (slim-configuration-shepherd config))
463 (theme-name (slim-configuration-theme-name config))
464 (sessreg (slim-configuration-sessreg config)))
465 (mixed-text-file "slim.cfg" "
466 default_path /run/current-system/profile/bin
467 default_xserver " startx "
468 xserver_arguments :0 vt7
469 xauth_path " xauth "/bin/xauth
470 authfile /var/run/slim.auth
472 # The login command. '%session' is replaced by the chosen session name, one
473 # of the names specified in the 'sessions' setting: 'wmaker', 'xfce', etc.
474 login_cmd exec " xinitrc " %session
475 sessiondir /run/current-system/profile/share/xsessions
476 session_msg session (F1 to change):
477 sessionstart_cmd " sessreg "/bin/sessreg -a -l $DISPLAY %user
478 sessionstop_cmd " sessreg "/bin/sessreg -d -l $DISPLAY %user
480 halt_cmd " shepherd "/sbin/halt
481 reboot_cmd " shepherd "/sbin/reboot\n"
482 (if (slim-configuration-auto-login? config)
483 (string-append "auto_login yes\ndefault_user "
484 (slim-configuration-default-user config) "\n")
487 (string-append "current_theme " theme-name "\n")
491 (slim-configuration-theme config))
493 (list (shepherd-service
494 (documentation "Xorg display server")
495 (provision '(xorg-server))
496 (requirement '(user-processes host-name udev))
499 ;; A stale lock file can prevent SLiM from starting, so remove it to
500 ;; be on the safe side.
501 (false-if-exception (delete-file "/var/run/slim.lock"))
504 (list (string-append #$slim "/bin/slim") "-nodaemon")
505 #:environment-variables
506 (list (string-append "SLIM_CFGFILE=" #$slim.cfg)
508 (list #~(string-append "SLIM_THEMESDIR=" #$theme))
510 (stop #~(make-kill-destructor))
513 (define slim-service-type
514 (service-type (name 'slim)
516 (list (service-extension shepherd-root-service-type
517 slim-shepherd-service)
518 (service-extension pam-root-service-type
521 ;; Unconditionally add xterm to the system profile, to
522 ;; avoid bad surprises.
523 (service-extension profile-service-type
524 (const (list xterm)))))
525 (default-value (slim-configuration))))
527 (define-deprecated (slim-service #:key (slim slim)
528 (allow-empty-passwords? #t) auto-login?
530 (theme %default-slim-theme)
531 (theme-name %default-slim-theme-name)
532 (xauth xauth) (shepherd shepherd)
533 (auto-login-session #f)
534 (startx (xorg-start-command)))
536 "Return a service that spawns the SLiM graphical login manager, which in
537 turn starts the X display server with @var{startx}, a command as returned by
538 @code{xorg-start-command}.
542 SLiM automatically looks for session types described by the @file{.desktop}
543 files in @file{/run/current-system/profile/share/xsessions} and allows users
544 to choose a session from the log-in screen using @kbd{F1}. Packages such as
545 @var{xfce}, @var{sawfish}, and @var{ratpoison} provide @file{.desktop} files;
546 adding them to the system-wide set of packages automatically makes them
547 available at the log-in screen.
549 In addition, @file{~/.xsession} files are honored. When available,
550 @file{~/.xsession} must be an executable that starts a window manager
551 and/or other X clients.
553 When @var{allow-empty-passwords?} is true, allow logins with an empty
554 password. When @var{auto-login?} is true, log in automatically as
555 @var{default-user} with @var{auto-login-session}.
557 If @var{theme} is @code{#f}, the use the default log-in theme; otherwise
558 @var{theme} must be a gexp denoting the name of a directory containing the
559 theme to use. In that case, @var{theme-name} specifies the name of the
561 (service slim-service-type
564 (allow-empty-passwords? allow-empty-passwords?)
565 (auto-login? auto-login?) (default-user default-user)
566 (theme theme) (theme-name theme-name)
567 (xauth xauth) (shepherd shepherd)
568 (auto-login-session auto-login-session)
573 ;;; Screen lockers & co.
576 (define-record-type <screen-locker>
577 (screen-locker name program empty?)
579 (name screen-locker-name) ;string
580 (program screen-locker-program) ;gexp
581 (empty? screen-locker-allows-empty-passwords?)) ;Boolean
583 (define screen-locker-pam-services
585 (($ <screen-locker> name _ empty?)
586 (list (unix-pam-service name
587 #:allow-empty-passwords? empty?)))))
589 (define screen-locker-setuid-programs
590 (compose list screen-locker-program))
592 (define screen-locker-service-type
593 (service-type (name 'screen-locker)
595 (list (service-extension pam-root-service-type
596 screen-locker-pam-services)
597 (service-extension setuid-program-service-type
598 screen-locker-setuid-programs)))))
600 (define* (screen-locker-service package
602 (program (package-name package))
603 #:key allow-empty-passwords?)
604 "Add @var{package}, a package for a screen locker or screen saver whose
605 command is @var{program}, to the set of setuid programs and add a PAM entry
609 (screen-locker-service xlockmore \"xlock\")
612 makes the good ol' XlockMore usable."
613 (service screen-locker-service-type
614 (screen-locker program
615 (file-append package "/bin/" program)
616 allow-empty-passwords?)))
618 (define %gdm-accounts
619 (list (user-group (name "gdm") (system? #t))
624 (comment "GNOME Display Manager user")
625 (home-directory "/var/lib/gdm")
626 (shell (file-append shadow "/sbin/nologin")))))
628 (define dbus-daemon-wrapper
629 (program-file "gdm-dbus-wrapper"
631 (setenv "XDG_CONFIG_DIRS"
632 "/run/current-system/profile/etc/xdg")
633 (setenv "XDG_DATA_DIRS"
634 "/run/current-system/profile/share")
635 (apply execl (string-append #$dbus "/bin/dbus-daemon")
636 (program-arguments)))))
638 (define-record-type* <gdm-configuration>
639 gdm-configuration make-gdm-configuration
641 (gdm gdm-configuration-gdm (default gdm))
642 (allow-empty-passwords? gdm-configuration-allow-empty-passwords? (default #t))
643 (auto-login? gdm-configuration-auto-login? (default #f))
644 (dbus-daemon gdm-configuration-dbus-daemon (default dbus-daemon-wrapper))
645 (default-user gdm-configuration-default-user (default #f))
646 (gnome-shell-assets gdm-configuration-gnome-shell-assets
647 (default (list adwaita-icon-theme font-cantarell)))
648 (x-server gdm-configuration-x-server
649 (default (xorg-wrapper)))
650 (x-session gdm-configuration-x-session
651 (default (xinitrc))))
653 (define (gdm-configuration-file config)
654 (mixed-text-file "gdm-custom.conf"
658 (if (gdm-configuration-auto-login? config)
660 "AutomaticLoginEnable=true\n"
662 (or (gdm-configuration-default-user config)
663 (error "missing default user for auto-login"))
666 "AutomaticLoginEnable=false\n"
667 "#AutomaticLogin=\n"))
668 "#TimedLoginEnable=false\n"
670 "#TimedLoginDelay=0\n"
671 "#InitialSetupEnable=true\n"
672 ;; Enable me once X is working.
673 "WaylandEnable=false\n"
679 "#DisallowTCP=true\n"
680 "#AllowRemoteAutoLogin=false\n"))
682 (define (gdm-pam-service config)
683 "Return a PAM service for @command{gdm}."
686 (inherit (unix-pam-service "gdm-autologin"))
687 (auth (list (pam-entry
688 (control "[success=ok default=1]")
689 (module (file-append (gdm-configuration-gdm config)
690 "/lib/security/pam_gdm.so")))
692 (control "sufficient")
693 (module "pam_permit.so")))))
695 (inherit (unix-pam-service "gdm-launch-environment"))
696 (auth (list (pam-entry
698 (module "pam_permit.so")))))
699 (unix-pam-service "gdm-password"
700 #:allow-empty-passwords?
701 (gdm-configuration-allow-empty-passwords? config))))
703 (define (gdm-shepherd-service config)
704 (list (shepherd-service
705 (documentation "Xorg display server (GDM)")
706 (provision '(xorg-server))
707 (requirement '(dbus-system user-processes host-name udev))
710 (list #$(file-append (gdm-configuration-gdm config)
712 #:environment-variables
715 #$(gdm-configuration-file config))
718 #$(gdm-configuration-dbus-daemon config))
721 #$(gdm-configuration-x-server config))
724 #$(gdm-configuration-x-session config))
727 ((lambda (ls) (string-join ls ":"))
729 (string-append path "/share"))
730 ;; XXX: Remove gnome-shell below when GDM
731 ;; can depend on GNOME Shell directly.
733 '#$(gdm-configuration-gnome-shell-assets
735 (stop #~(make-kill-destructor))
738 (define gdm-service-type
739 (service-type (name 'gdm)
741 (list (service-extension shepherd-root-service-type
742 gdm-shepherd-service)
743 (service-extension account-service-type
744 (const %gdm-accounts))
745 (service-extension pam-root-service-type
747 (service-extension profile-service-type
748 gdm-configuration-gnome-shell-assets)
749 (service-extension dbus-root-service-type
751 gdm-configuration-gdm))))
752 (default-value (gdm-configuration))
754 "Run the GNOME Desktop Manager (GDM), a program that allows
755 you to log in in a graphical session, whether or not you use GNOME.")))
757 ;; This service isn't working yet; it gets as far as starting to run the
758 ;; greeter from gnome-shell but doesn't get any further. It is here because
759 ;; it doesn't hurt anyone and perhaps it inspires someone to fix it :)
760 (define-deprecated (gdm-service #:key (gdm gdm)
761 (allow-empty-passwords? #t)
762 (x-server (xorg-wrapper)))
764 "Return a service that spawns the GDM graphical login manager, which in turn
765 starts the X display server with @var{X}, a command as returned by
770 GDM automatically looks for session types described by the @file{.desktop}
771 files in @file{/run/current-system/profile/share/xsessions} and allows users
772 to choose a session from the log-in screen using @kbd{F1}. Packages such as
773 @var{xfce}, @var{sawfish}, and @var{ratpoison} provide @file{.desktop} files;
774 adding them to the system-wide set of packages automatically makes them
775 available at the log-in screen.
777 In addition, @file{~/.xsession} files are honored. When available,
778 @file{~/.xsession} must be an executable that starts a window manager
779 and/or other X clients.
781 When @var{allow-empty-passwords?} is true, allow logins with an empty
783 (service gdm-service-type
786 (allow-empty-passwords? allow-empty-passwords?)
787 (x-server x-server))))
789 ;;; xorg.scm ends here