gnu: services: Log debug messages to /var/log/debug.
[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>
c510cbb4 3;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
4bd43bbe 4;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
db4fdc04
LC
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21(define-module (gnu services xorg)
84dfb458 22 #:use-module (gnu artwork)
db4fdc04 23 #:use-module (gnu services)
0190c1c0 24 #:use-module (gnu services shepherd)
6e828634 25 #:use-module (gnu system pam)
bdb36958
LC
26 #:use-module ((gnu packages base) #:select (canonical-package))
27 #:use-module (gnu packages guile)
db4fdc04
LC
28 #:use-module (gnu packages xorg)
29 #:use-module (gnu packages gl)
5fd66a37 30 #:use-module (gnu packages display-managers)
9e4eddb4 31 #:use-module (gnu packages gnustep)
db4fdc04
LC
32 #:use-module (gnu packages admin)
33 #:use-module (gnu packages bash)
b5f4e686 34 #:use-module (guix gexp)
e87f0591 35 #:use-module (guix store)
6726282b 36 #:use-module (guix packages)
db4fdc04 37 #:use-module (guix derivations)
ffc3a02b 38 #:use-module (guix records)
d2e59637 39 #:use-module (srfi srfi-1)
6726282b 40 #:use-module (srfi srfi-9)
d2e59637
LC
41 #:use-module (srfi srfi-26)
42 #:use-module (ice-9 match)
d1cdd7ba 43 #:export (xorg-configuration-file
79fd74fa 44 %default-xorg-modules
92753a8b 45 xorg-wrapper
d1cdd7ba 46 xorg-start-command
f2901d82
DC
47 xinitrc
48
0ecc3bf3
LC
49 %default-slim-theme
50 %default-slim-theme-name
4b7513e0
DT
51 slim-configuration
52 slim-service-type
6726282b
LC
53 slim-service
54
24e96431
55 screen-locker
56 screen-locker?
6726282b
LC
57 screen-locker-service-type
58 screen-locker-service))
db4fdc04
LC
59
60;;; Commentary:
61;;;
62;;; Services that relate to the X Window System.
63;;;
64;;; Code:
65
12422c9d
LC
66(define* (xorg-configuration-file #:key (drivers '()) (resolutions '())
67 (extra-config '()))
d1cdd7ba
LC
68 "Return a configuration file for the Xorg server containing search paths for
69all the common drivers.
f703413e
LC
70
71@var{drivers} must be either the empty list, in which case Xorg chooses a
72graphics driver automatically, or a list of driver names that will be tried in
d2e59637
LC
73this order---e.g., @code{(\"modesetting\" \"vesa\")}.
74
75Likewise, when @var{resolutions} is the empty list, Xorg chooses an
76appropriate screen resolution; otherwise, it must be a list of
12422c9d
LC
77resolutions---e.g., @code{((1024 768) (640 480))}.
78
79Last, @var{extra-config} is a list of strings or objects appended to the
be1c2c54
LC
80@code{mixed-text-file} argument list. It is used to pass extra text to be
81added verbatim to the configuration file."
f703413e
LC
82 (define (device-section driver)
83 (string-append "
84Section \"Device\"
85 Identifier \"device-" driver "\"
86 Driver \"" driver "\"
87EndSection"))
db4fdc04 88
d2e59637
LC
89 (define (screen-section driver resolutions)
90 (string-append "
91Section \"Screen\"
92 Identifier \"screen-" driver "\"
93 Device \"device-" driver "\"
94 SubSection \"Display\"
95 Modes "
96 (string-join (map (match-lambda
d1cdd7ba
LC
97 ((x y)
98 (string-append "\"" (number->string x)
99 "x" (number->string y) "\"")))
d2e59637
LC
100 resolutions)) "
101 EndSubSection
102EndSection"))
103
be1c2c54 104 (apply mixed-text-file "xserver.conf" "
db4fdc04 105Section \"Files\"
d1ccd094
EB
106 FontPath \"" font-alias "/share/fonts/X11/75dpi\"
107 FontPath \"" font-alias "/share/fonts/X11/100dpi\"
108 FontPath \"" font-alias "/share/fonts/X11/misc\"
109 FontPath \"" font-alias "/share/fonts/X11/cyrillic\"
378377eb 110 FontPath \"" font-adobe75dpi "/share/fonts/X11/75dpi\"
db4fdc04 111 ModulePath \"" xf86-video-vesa "/lib/xorg/modules/drivers\"
0adb027b 112 ModulePath \"" xf86-video-fbdev "/lib/xorg/modules/drivers\"
3126fd88 113 ModulePath \"" xf86-video-ati "/lib/xorg/modules/drivers\"
0adb027b
LC
114 ModulePath \"" xf86-video-cirrus "/lib/xorg/modules/drivers\"
115 ModulePath \"" xf86-video-intel "/lib/xorg/modules/drivers\"
116 ModulePath \"" xf86-video-mach64 "/lib/xorg/modules/drivers\"
ca63770a 117 ModulePath \"" xf86-video-nouveau "/lib/xorg/modules/drivers\"
0adb027b 118 ModulePath \"" xf86-video-nv "/lib/xorg/modules/drivers\"
2c37a34c 119 ModulePath \"" xf86-video-sis "/lib/xorg/modules/drivers\"
c2ee19e6
LC
120
121 # Libinput is the new thing and is recommended over evdev/synaptics
122 # by those who know:
123 # <http://who-t.blogspot.fr/2015/01/xf86-input-libinput-compatibility-with.html>.
124 ModulePath \"" xf86-input-libinput "/lib/xorg/modules/input\"
125
073cd609 126 ModulePath \"" xf86-input-evdev "/lib/xorg/modules/input\"
db4fdc04 127 ModulePath \"" xf86-input-keyboard "/lib/xorg/modules/input\"
0adb027b
LC
128 ModulePath \"" xf86-input-mouse "/lib/xorg/modules/input\"
129 ModulePath \"" xf86-input-synaptics "/lib/xorg/modules/input\"
db4fdc04
LC
130 ModulePath \"" xorg-server "/lib/xorg/modules\"
131 ModulePath \"" xorg-server "/lib/xorg/modules/extensions\"
132 ModulePath \"" xorg-server "/lib/xorg/modules/multimedia\"
133EndSection
134
135Section \"ServerFlags\"
e30442b5 136 Option \"AllowMouseOpenFail\" \"on\"
db4fdc04 137EndSection
f703413e 138"
12422c9d 139 (string-join (map device-section drivers) "\n") "\n"
d2e59637
LC
140 (string-join (map (cut screen-section <> resolutions)
141 drivers)
12422c9d
LC
142 "\n")
143
144 "\n"
145 extra-config))
db4fdc04 146
79fd74fa
AW
147(define %default-xorg-modules
148 (list xf86-video-vesa
149 xf86-video-fbdev
3126fd88 150 xf86-video-ati
79fd74fa
AW
151 xf86-video-cirrus
152 xf86-video-intel
153 xf86-video-mach64
154 xf86-video-nouveau
155 xf86-video-nv
156 xf86-video-sis
157 xf86-input-libinput
158 xf86-input-evdev
159 xf86-input-keyboard
160 xf86-input-mouse
161 xf86-input-synaptics))
162
163(define (xorg-configuration-directory modules)
164 "Return a directory that contains the @code{.conf} files for X.org that
165includes the @code{share/X11/xorg.conf.d} directories of each package listed
166in @var{modules}."
4ee96a79
LC
167 (with-imported-modules '((guix build utils))
168 (computed-file "xorg.conf.d"
169 #~(begin
170 (use-modules (guix build utils)
171 (srfi srfi-1))
172
173 (define files
174 (append-map (lambda (module)
175 (find-files (string-append
176 module
177 "/share/X11/xorg.conf.d")
178 "\\.conf$"))
179 (list #$@modules)))
180
181 (mkdir #$output)
182 (for-each (lambda (file)
183 (symlink file
184 (string-append #$output "/"
185 (basename file))))
186 files)
187 #t))))
79fd74fa 188
92753a8b
AW
189(define* (xorg-wrapper #:key
190 (guile (canonical-package guile-2.0))
191 (configuration-file (xorg-configuration-file))
192 (modules %default-xorg-modules)
193 (xorg-server xorg-server))
d1cdd7ba
LC
194 "Return a derivation that builds a @var{guile} script to start the X server
195from @var{xorg-server}. @var{configuration-file} is the server configuration
196file or a derivation that builds it; when omitted, the result of
92753a8b
AW
197@code{xorg-configuration-file} is used. The resulting script should be used
198in place of @code{/usr/bin/X}."
be1c2c54
LC
199 (define exp
200 ;; Write a small wrapper around the X server.
201 #~(begin
202 (setenv "XORG_DRI_DRIVER_PATH" (string-append #$mesa "/lib/dri"))
203 (setenv "XKB_BINDIR" (string-append #$xkbcomp "/bin"))
204
92753a8b
AW
205 (let ((X (string-append #$xorg-server "/bin/X")))
206 (apply execl X X
207 "-xkbdir" (string-append #$xkeyboard-config "/share/X11/xkb")
208 "-config" #$configuration-file
209 "-configdir" #$(xorg-configuration-directory modules)
210 (cdr (command-line))))))
211
212 (program-file "X-wrapper" exp))
be1c2c54 213
92753a8b
AW
214(define* (xorg-start-command #:key
215 (guile (canonical-package guile-2.0))
216 (configuration-file (xorg-configuration-file))
217 (modules %default-xorg-modules)
218 (xorg-server xorg-server))
219 "Return a derivation that builds a @code{startx} script in which a number of
220X modules are available. See @code{xorg-wrapper} for more details on the
221arguments. The result should be used in place of @code{startx}."
222 (define X
223 (xorg-wrapper #:guile guile
224 #:configuration-file configuration-file
225 #:modules modules
226 #:xorg-server xorg-server))
227 (define exp
228 ;; Write a small wrapper around the X server.
229 #~(apply execl #$X #$X ;; Second #$X is for argv[0].
230 "-logverbose" "-verbose" "-nolisten" "tcp" "-terminate"
231 (cdr (command-line))))
be1c2c54 232
92753a8b 233 (program-file "startx" exp))
db4fdc04 234
9e4eddb4 235(define* (xinitrc #:key
bdb36958 236 (guile (canonical-package guile-2.0))
24d56899
SB
237 fallback-session)
238 "Return a system-wide xinitrc script that starts the specified X session,
239which should be passed to this script as the first argument. If not, the
240@var{fallback-session} will be used."
8779d342
LC
241 (define builder
242 #~(begin
243 (use-modules (ice-9 match))
244
16c33bfb
LC
245 (define (close-all-fdes)
246 ;; Close all the open file descriptors except 0 to 2.
247 (let loop ((fd 3))
248 (when (< fd 4096) ;FIXME: use sysconf + _SC_OPEN_MAX
249 (false-if-exception (close-fdes fd))
250 (loop (+ 1 fd)))))
251
b2bd7c25
LC
252 (define (exec-from-login-shell command . args)
253 ;; Run COMMAND from a login shell so that it gets to see the same
254 ;; environment variables that one gets when logging in on a tty, for
255 ;; instance.
256 (let* ((pw (getpw (getuid)))
e0b85670
SB
257 (shell (passwd:shell pw)))
258 ;; Close any open file descriptors. This is all the more
259 ;; important that SLiM itself exec's us directly without closing
260 ;; its own file descriptors!
261 (close-all-fdes)
262
263 ;; The '--login' option is supported at least by Bash and zsh.
264 (execl shell shell "--login" "-c"
265 (string-join (cons command args)))))
266
267 (let* ((home (getenv "HOME"))
268 (xsession-file (string-append home "/.xsession"))
269 (session (match (command-line)
c510cbb4
LC
270 ((_) (list #$fallback-session))
271 ((_ x ..1) x))))
e0b85670
SB
272 (if (file-exists? xsession-file)
273 ;; Run ~/.xsession when it exists.
f2ab9250 274 (apply exec-from-login-shell xsession-file session)
e0b85670 275 ;; Otherwise, start the specified session.
c510cbb4
LC
276 (apply exec-from-login-shell session)))))
277
be1c2c54 278 (program-file "xinitrc" builder))
9e4eddb4 279
0ecc3bf3
LC
280\f
281;;;
282;;; SLiM log-in manager.
283;;;
284
0ecc3bf3
LC
285(define %default-slim-theme
286 ;; Theme based on work by Felipe López.
9e41130b 287 (file-append %artwork-repository "/slim"))
0ecc3bf3
LC
288
289(define %default-slim-theme-name
290 ;; This must be the name of the sub-directory in %DEFAULT-SLIM-THEME that
291 ;; contains the actual theme files.
cf2abac8 292 "0.x")
0ecc3bf3 293
0adfe95a
LC
294(define-record-type* <slim-configuration>
295 slim-configuration make-slim-configuration
296 slim-configuration?
297 (slim slim-configuration-slim
298 (default slim))
299 (allow-empty-passwords? slim-configuration-allow-empty-passwords?)
300 (auto-login? slim-configuration-auto-login?)
301 (default-user slim-configuration-default-user)
302 (theme slim-configuration-theme)
303 (theme-name slim-configuration-theme-name)
304 (xauth slim-configuration-xauth
305 (default xauth))
26b94866
AK
306 (shepherd slim-configuration-shepherd
307 (default shepherd))
0adfe95a
LC
308 (bash slim-configuration-bash
309 (default bash))
310 (auto-login-session slim-configuration-auto-login-session)
311 (startx slim-configuration-startx))
312
313(define (slim-pam-service config)
314 "Return a PAM service for @command{slim}."
315 (list (unix-pam-service
316 "slim"
317 #:allow-empty-passwords?
318 (slim-configuration-allow-empty-passwords? config))))
319
d4053c71 320(define (slim-shepherd-service config)
0adfe95a
LC
321 (define slim.cfg
322 (let ((xinitrc (xinitrc #:fallback-session
323 (slim-configuration-auto-login-session config)))
324 (slim (slim-configuration-slim config))
325 (xauth (slim-configuration-xauth config))
326 (startx (slim-configuration-startx config))
26b94866 327 (shepherd (slim-configuration-shepherd config))
0adfe95a
LC
328 (theme-name (slim-configuration-theme-name config)))
329 (mixed-text-file "slim.cfg" "
330default_path /run/current-system/profile/bin
331default_xserver " startx "
332xserver_arguments :0 vt7
333xauth_path " xauth "/bin/xauth
334authfile /var/run/slim.auth
335
336# The login command. '%session' is replaced by the chosen session name, one
337# of the names specified in the 'sessions' setting: 'wmaker', 'xfce', etc.
338login_cmd exec " xinitrc " %session
339sessiondir /run/current-system/profile/share/xsessions
340session_msg session (F1 to change):
341
26b94866
AK
342halt_cmd " shepherd "/sbin/halt
343reboot_cmd " shepherd "/sbin/reboot\n"
0adfe95a
LC
344(if (slim-configuration-auto-login? config)
345 (string-append "auto_login yes\ndefault_user "
346 (slim-configuration-default-user config) "\n")
347 "")
348(if theme-name
349 (string-append "current_theme " theme-name "\n")
350 ""))))
351
352 (define theme
353 (slim-configuration-theme config))
354
d4053c71 355 (list (shepherd-service
0adfe95a
LC
356 (documentation "Xorg display server")
357 (provision '(xorg-server))
358 (requirement '(user-processes host-name udev))
359 (start
360 #~(lambda ()
361 ;; A stale lock file can prevent SLiM from starting, so remove it to
362 ;; be on the safe side.
363 (false-if-exception (delete-file "/var/run/slim.lock"))
364
365 (fork+exec-command
366 (list (string-append #$slim "/bin/slim") "-nodaemon")
367 #:environment-variables
368 (list (string-append "SLIM_CFGFILE=" #$slim.cfg)
369 #$@(if theme
370 (list #~(string-append "SLIM_THEMESDIR=" #$theme))
371 #~())))))
372 (stop #~(make-kill-destructor))
373 (respawn? #t))))
374
375(define slim-service-type
376 (service-type (name 'slim)
377 (extensions
d4053c71
AK
378 (list (service-extension shepherd-root-service-type
379 slim-shepherd-service)
0adfe95a 380 (service-extension pam-root-service-type
e9b82124
LC
381 slim-pam-service)
382
383 ;; Unconditionally add xterm to the system profile, to
384 ;; avoid bad surprises.
385 (service-extension profile-service-type
386 (const (list xterm)))))))
0adfe95a 387
db4fdc04
LC
388(define* (slim-service #:key (slim slim)
389 (allow-empty-passwords? #t) auto-login?
390 (default-user "")
0ecc3bf3
LC
391 (theme %default-slim-theme)
392 (theme-name %default-slim-theme-name)
26b94866 393 (xauth xauth) (shepherd shepherd) (bash bash)
9e41130b
LC
394 (auto-login-session (file-append windowmaker
395 "/bin/wmaker"))
be1c2c54 396 (startx (xorg-start-command)))
db4fdc04 397 "Return a service that spawns the SLiM graphical login manager, which in
51da7ca0
LC
398turn starts the X display server with @var{startx}, a command as returned by
399@code{xorg-start-command}.
db4fdc04 400
04e4e6ab
LC
401@cindex X session
402
403SLiM automatically looks for session types described by the @file{.desktop}
404files in @file{/run/current-system/profile/share/xsessions} and allows users
405to choose a session from the log-in screen using @kbd{F1}. Packages such as
406@var{xfce}, @var{sawfish}, and @var{ratpoison} provide @file{.desktop} files;
407adding them to the system-wide set of packages automatically makes them
408available at the log-in screen.
409
410In addition, @file{~/.xsession} files are honored. When available,
411@file{~/.xsession} must be an executable that starts a window manager
412and/or other X clients.
413
51da7ca0
LC
414When @var{allow-empty-passwords?} is true, allow logins with an empty
415password. When @var{auto-login?} is true, log in automatically as
24d56899 416@var{default-user} with @var{auto-login-session}.
0ecc3bf3
LC
417
418If @var{theme} is @code{#f}, the use the default log-in theme; otherwise
419@var{theme} must be a gexp denoting the name of a directory containing the
420theme to use. In that case, @var{theme-name} specifies the name of the
4bd43bbe 421theme."
0adfe95a
LC
422 (service slim-service-type
423 (slim-configuration
424 (slim slim)
425 (allow-empty-passwords? allow-empty-passwords?)
426 (auto-login? auto-login?) (default-user default-user)
427 (theme theme) (theme-name theme-name)
26b94866 428 (xauth xauth) (shepherd shepherd) (bash bash)
0adfe95a
LC
429 (auto-login-session auto-login-session)
430 (startx startx))))
db4fdc04 431
6726282b
LC
432\f
433;;;
434;;; Screen lockers & co.
435;;;
436
437(define-record-type <screen-locker>
438 (screen-locker name program empty?)
439 screen-locker?
440 (name screen-locker-name) ;string
441 (program screen-locker-program) ;gexp
442 (empty? screen-locker-allows-empty-passwords?)) ;Boolean
443
444(define screen-locker-pam-services
445 (match-lambda
446 (($ <screen-locker> name _ empty?)
447 (list (unix-pam-service name
448 #:allow-empty-passwords? empty?)))))
449
450(define screen-locker-setuid-programs
451 (compose list screen-locker-program))
452
453(define screen-locker-service-type
454 (service-type (name 'screen-locker)
455 (extensions
456 (list (service-extension pam-root-service-type
457 screen-locker-pam-services)
458 (service-extension setuid-program-service-type
459 screen-locker-setuid-programs)))))
460
461(define* (screen-locker-service package
462 #:optional
463 (program (package-name package))
464 #:key allow-empty-passwords?)
465 "Add @var{package}, a package for a screen-locker or screen-saver whose
466command is @var{program}, to the set of setuid programs and add a PAM entry
467for it. For example:
468
469@lisp
9e41130b 470 (screen-locker-service xlockmore \"xlock\")
6726282b
LC
471@end lisp
472
473makes the good ol' XlockMore usable."
474 (service screen-locker-service-type
475 (screen-locker program
9e41130b 476 (file-append package "/bin/" program)
6726282b
LC
477 allow-empty-passwords?)))
478
db4fdc04 479;;; xorg.scm ends here