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