Merge branch 'master' into core-updates
[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 system linux) ; 'pam-service'
24 #:use-module ((gnu packages base) #:select (canonical-package))
25 #:use-module (gnu packages guile)
26 #:use-module (gnu packages xorg)
27 #:use-module (gnu packages gl)
28 #:use-module (gnu packages slim)
29 #:use-module (gnu packages gnustep)
30 #:use-module (gnu packages admin)
31 #:use-module (gnu packages bash)
32 #:use-module (guix gexp)
33 #:use-module (guix store)
34 #:use-module (guix monads)
35 #:use-module (guix derivations)
36 #:use-module (guix records)
37 #:use-module (srfi srfi-1)
38 #:use-module (srfi srfi-26)
39 #:use-module (ice-9 match)
40 #:export (xorg-configuration-file
41 xorg-start-command
42 %default-slim-theme
43 %default-slim-theme-name
44 slim-service))
45
46 ;;; Commentary:
47 ;;;
48 ;;; Services that relate to the X Window System.
49 ;;;
50 ;;; Code:
51
52 (define* (xorg-configuration-file #:key (drivers '()) (resolutions '())
53 (extra-config '()))
54 "Return a configuration file for the Xorg server containing search paths for
55 all the common drivers.
56
57 @var{drivers} must be either the empty list, in which case Xorg chooses a
58 graphics driver automatically, or a list of driver names that will be tried in
59 this order---e.g., @code{(\"modesetting\" \"vesa\")}.
60
61 Likewise, when @var{resolutions} is the empty list, Xorg chooses an
62 appropriate screen resolution; otherwise, it must be a list of
63 resolutions---e.g., @code{((1024 768) (640 480))}.
64
65 Last, @var{extra-config} is a list of strings or objects appended to the
66 @code{text-file*} argument list. It is used to pass extra text to be added
67 verbatim to the configuration file."
68 (define (device-section driver)
69 (string-append "
70 Section \"Device\"
71 Identifier \"device-" driver "\"
72 Driver \"" driver "\"
73 EndSection"))
74
75 (define (screen-section driver resolutions)
76 (string-append "
77 Section \"Screen\"
78 Identifier \"screen-" driver "\"
79 Device \"device-" driver "\"
80 SubSection \"Display\"
81 Modes "
82 (string-join (map (match-lambda
83 ((x y)
84 (string-append "\"" (number->string x)
85 "x" (number->string y) "\"")))
86 resolutions)) "
87 EndSubSection
88 EndSection"))
89
90 (apply text-file* "xserver.conf" "
91 Section \"Files\"
92 FontPath \"" font-adobe75dpi "/share/fonts/X11/75dpi\"
93 ModulePath \"" xf86-video-vesa "/lib/xorg/modules/drivers\"
94 ModulePath \"" xf86-video-fbdev "/lib/xorg/modules/drivers\"
95 ModulePath \"" xf86-video-modesetting "/lib/xorg/modules/drivers\"
96 ModulePath \"" xf86-video-cirrus "/lib/xorg/modules/drivers\"
97 ModulePath \"" xf86-video-intel "/lib/xorg/modules/drivers\"
98 ModulePath \"" xf86-video-mach64 "/lib/xorg/modules/drivers\"
99 ModulePath \"" xf86-video-nouveau "/lib/xorg/modules/drivers\"
100 ModulePath \"" xf86-video-nv "/lib/xorg/modules/drivers\"
101 ModulePath \"" xf86-video-sis "/lib/xorg/modules/drivers\"
102
103 # Libinput is the new thing and is recommended over evdev/synaptics
104 # by those who know:
105 # <http://who-t.blogspot.fr/2015/01/xf86-input-libinput-compatibility-with.html>.
106 ModulePath \"" xf86-input-libinput "/lib/xorg/modules/input\"
107
108 ModulePath \"" xf86-input-evdev "/lib/xorg/modules/input\"
109 ModulePath \"" xf86-input-keyboard "/lib/xorg/modules/input\"
110 ModulePath \"" xf86-input-mouse "/lib/xorg/modules/input\"
111 ModulePath \"" xf86-input-synaptics "/lib/xorg/modules/input\"
112 ModulePath \"" xorg-server "/lib/xorg/modules\"
113 ModulePath \"" xorg-server "/lib/xorg/modules/extensions\"
114 ModulePath \"" xorg-server "/lib/xorg/modules/multimedia\"
115 EndSection
116
117 Section \"ServerFlags\"
118 Option \"AllowMouseOpenFail\" \"on\"
119 EndSection
120 "
121 (string-join (map device-section drivers) "\n") "\n"
122 (string-join (map (cut screen-section <> resolutions)
123 drivers)
124 "\n")
125
126 "\n"
127 extra-config))
128
129 (define* (xorg-start-command #:key
130 (guile (canonical-package guile-2.0))
131 configuration-file
132 (xorg-server xorg-server))
133 "Return a derivation that builds a @var{guile} script to start the X server
134 from @var{xorg-server}. @var{configuration-file} is the server configuration
135 file or a derivation that builds it; when omitted, the result of
136 @code{xorg-configuration-file} is used.
137
138 Usually the X server is started by a login manager."
139 (mlet %store-monad ((config (if configuration-file
140 (return configuration-file)
141 (xorg-configuration-file))))
142 (define script
143 ;; Write a small wrapper around the X server.
144 #~(begin
145 (setenv "XORG_DRI_DRIVER_PATH" (string-append #$mesa "/lib/dri"))
146 (setenv "XKB_BINDIR" (string-append #$xkbcomp "/bin"))
147
148 (apply execl (string-append #$xorg-server "/bin/X")
149 (string-append #$xorg-server "/bin/X") ;argv[0]
150 "-logverbose" "-verbose"
151 "-xkbdir" (string-append #$xkeyboard-config "/share/X11/xkb")
152 "-config" #$config
153 "-nolisten" "tcp" "-terminate"
154
155 ;; Note: SLiM and other display managers add the
156 ;; '-auth' flag by themselves.
157 (cdr (command-line)))))
158
159 (gexp->script "start-xorg" script)))
160
161 (define* (xinitrc #:key
162 (guile (canonical-package guile-2.0))
163 fallback-session)
164 "Return a system-wide xinitrc script that starts the specified X session,
165 which should be passed to this script as the first argument. If not, the
166 @var{fallback-session} will be used."
167 (define builder
168 #~(begin
169 (use-modules (ice-9 match))
170
171 (define (close-all-fdes)
172 ;; Close all the open file descriptors except 0 to 2.
173 (let loop ((fd 3))
174 (when (< fd 4096) ;FIXME: use sysconf + _SC_OPEN_MAX
175 (false-if-exception (close-fdes fd))
176 (loop (+ 1 fd)))))
177
178 (define (exec-from-login-shell command . args)
179 ;; Run COMMAND from a login shell so that it gets to see the same
180 ;; environment variables that one gets when logging in on a tty, for
181 ;; instance.
182 (let* ((pw (getpw (getuid)))
183 (shell (passwd:shell pw)))
184 ;; Close any open file descriptors. This is all the more
185 ;; important that SLiM itself exec's us directly without closing
186 ;; its own file descriptors!
187 (close-all-fdes)
188
189 ;; The '--login' option is supported at least by Bash and zsh.
190 (execl shell shell "--login" "-c"
191 (string-join (cons command args)))))
192
193 (let* ((home (getenv "HOME"))
194 (xsession-file (string-append home "/.xsession"))
195 (session (match (command-line)
196 ((_ x) x)
197 (_ #$fallback-session))))
198 (if (file-exists? xsession-file)
199 ;; Run ~/.xsession when it exists.
200 (exec-from-login-shell xsession-file session)
201 ;; Otherwise, start the specified session.
202 (exec-from-login-shell session)))))
203 (gexp->script "xinitrc" builder))
204
205 \f
206 ;;;
207 ;;; SLiM log-in manager.
208 ;;;
209
210 (define %default-slim-theme
211 ;; Theme based on work by Felipe López.
212 #~(string-append #$%artwork-repository "/slim"))
213
214 (define %default-slim-theme-name
215 ;; This must be the name of the sub-directory in %DEFAULT-SLIM-THEME that
216 ;; contains the actual theme files.
217 "0.x")
218
219 (define* (slim-service #:key (slim slim)
220 (allow-empty-passwords? #t) auto-login?
221 (default-user "")
222 (theme %default-slim-theme)
223 (theme-name %default-slim-theme-name)
224 (xauth xauth) (dmd dmd) (bash bash)
225 (auto-login-session #~(string-append #$windowmaker
226 "/bin/wmaker"))
227 startx)
228 "Return a service that spawns the SLiM graphical login manager, which in
229 turn starts the X display server with @var{startx}, a command as returned by
230 @code{xorg-start-command}.
231
232 @cindex X session
233
234 SLiM automatically looks for session types described by the @file{.desktop}
235 files in @file{/run/current-system/profile/share/xsessions} and allows users
236 to choose a session from the log-in screen using @kbd{F1}. Packages such as
237 @var{xfce}, @var{sawfish}, and @var{ratpoison} provide @file{.desktop} files;
238 adding them to the system-wide set of packages automatically makes them
239 available at the log-in screen.
240
241 In addition, @file{~/.xsession} files are honored. When available,
242 @file{~/.xsession} must be an executable that starts a window manager
243 and/or other X clients.
244
245 When @var{allow-empty-passwords?} is true, allow logins with an empty
246 password. When @var{auto-login?} is true, log in automatically as
247 @var{default-user} with @var{auto-login-session}.
248
249 If @var{theme} is @code{#f}, the use the default log-in theme; otherwise
250 @var{theme} must be a gexp denoting the name of a directory containing the
251 theme to use. In that case, @var{theme-name} specifies the name of the
252 theme."
253
254 (define (slim.cfg)
255 (mlet %store-monad ((startx (if startx
256 (return startx)
257 (xorg-start-command)))
258 (xinitrc (xinitrc #:fallback-session
259 auto-login-session)))
260 (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
275 "
276 (if auto-login?
277 (string-append "auto_login yes\ndefault_user " default-user "\n")
278 "")
279 (if theme-name
280 (string-append "current_theme " theme-name "\n")
281 ""))))
282
283 (mlet %store-monad ((slim.cfg (slim.cfg)))
284 (return
285 (service
286 (documentation "Xorg display server")
287 (provision '(xorg-server))
288 (requirement '(user-processes host-name udev))
289 (start
290 #~(lambda ()
291 ;; A stale lock file can prevent SLiM from starting, so remove it
292 ;; to be on the safe side.
293 (false-if-exception (delete-file "/var/run/slim.lock"))
294
295 (fork+exec-command
296 (list (string-append #$slim "/bin/slim") "-nodaemon")
297 #:environment-variables
298 (list (string-append "SLIM_CFGFILE=" #$slim.cfg)
299 #$@(if theme
300 (list #~(string-append "SLIM_THEMESDIR=" #$theme))
301 #~())))))
302 (stop #~(make-kill-destructor))
303 (respawn? #t)
304 (pam-services
305 ;; Tell PAM about 'slim'.
306 (list (unix-pam-service
307 "slim"
308 #:allow-empty-passwords? allow-empty-passwords?)))))))
309
310 ;;; xorg.scm ends here