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