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