gnu: calibre: Update to 2.21.0.
[jackhill/guix/guix.git] / gnu / services / xorg.scm
CommitLineData
db4fdc04 1;;; GNU Guix --- Functional package management for GNU
e87f0591 2;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
db4fdc04
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (gnu services xorg)
84dfb458 20 #:use-module (gnu artwork)
db4fdc04
LC
21 #:use-module (gnu services)
22 #:use-module (gnu system linux) ; 'pam-service'
bdb36958
LC
23 #:use-module ((gnu packages base) #:select (canonical-package))
24 #:use-module (gnu packages guile)
db4fdc04
LC
25 #:use-module (gnu packages xorg)
26 #:use-module (gnu packages gl)
27 #:use-module (gnu packages slim)
28 #:use-module (gnu packages ratpoison)
9e4eddb4 29 #:use-module (gnu packages gnustep)
5ce93d9a 30 #:use-module (gnu packages sawfish)
db4fdc04
LC
31 #:use-module (gnu packages admin)
32 #:use-module (gnu packages bash)
b5f4e686 33 #:use-module (guix gexp)
e87f0591 34 #:use-module (guix store)
db4fdc04
LC
35 #:use-module (guix monads)
36 #:use-module (guix derivations)
ffc3a02b 37 #:use-module (guix records)
d2e59637
LC
38 #:use-module (srfi srfi-1)
39 #:use-module (srfi srfi-26)
40 #:use-module (ice-9 match)
db4fdc04 41 #:export (xorg-start-command
24d56899 42 %default-xsessions
ffc3a02b
LC
43 %ratpoison-session-type
44 %windowmaker-session-type
5ce93d9a 45 %sawfish-session-type
ffc3a02b
LC
46
47 session-type?
48 session-type-name
49
0ecc3bf3
LC
50 %default-slim-theme
51 %default-slim-theme-name
db4fdc04
LC
52 slim-service))
53
54;;; Commentary:
55;;;
56;;; Services that relate to the X Window System.
57;;;
58;;; Code:
59
60(define* (xorg-start-command #:key
bdb36958 61 (guile (canonical-package guile-2.0))
f703413e 62 (xorg-server xorg-server)
d2e59637 63 (drivers '()) (resolutions '()))
f703413e
LC
64 "Return a derivation that builds a @var{guile} script to start the X server
65from @var{xorg-server}. Usually the X server is started by a login manager.
66
67@var{drivers} must be either the empty list, in which case Xorg chooses a
68graphics driver automatically, or a list of driver names that will be tried in
d2e59637
LC
69this order---e.g., @code{(\"modesetting\" \"vesa\")}.
70
71Likewise, when @var{resolutions} is the empty list, Xorg chooses an
72appropriate screen resolution; otherwise, it must be a list of
73resolutions---e.g., @code{((1024 768) (640 480))}."
f703413e
LC
74
75 (define (device-section driver)
76 (string-append "
77Section \"Device\"
78 Identifier \"device-" driver "\"
79 Driver \"" driver "\"
80EndSection"))
db4fdc04 81
d2e59637
LC
82 (define (screen-section driver resolutions)
83 (string-append "
84Section \"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
95EndSection"))
96
db4fdc04
LC
97 (define (xserver.conf)
98 (text-file* "xserver.conf" "
99Section \"Files\"
378377eb 100 FontPath \"" font-adobe75dpi "/share/fonts/X11/75dpi\"
db4fdc04 101 ModulePath \"" xf86-video-vesa "/lib/xorg/modules/drivers\"
0adb027b 102 ModulePath \"" xf86-video-fbdev "/lib/xorg/modules/drivers\"
3fc4eb21 103 ModulePath \"" xf86-video-modesetting "/lib/xorg/modules/drivers\"
0adb027b
LC
104 ModulePath \"" xf86-video-cirrus "/lib/xorg/modules/drivers\"
105 ModulePath \"" xf86-video-intel "/lib/xorg/modules/drivers\"
106 ModulePath \"" xf86-video-mach64 "/lib/xorg/modules/drivers\"
ca63770a 107 ModulePath \"" xf86-video-nouveau "/lib/xorg/modules/drivers\"
0adb027b 108 ModulePath \"" xf86-video-nv "/lib/xorg/modules/drivers\"
2c37a34c 109 ModulePath \"" xf86-video-sis "/lib/xorg/modules/drivers\"
073cd609 110 ModulePath \"" xf86-input-evdev "/lib/xorg/modules/input\"
db4fdc04 111 ModulePath \"" xf86-input-keyboard "/lib/xorg/modules/input\"
0adb027b
LC
112 ModulePath \"" xf86-input-mouse "/lib/xorg/modules/input\"
113 ModulePath \"" xf86-input-synaptics "/lib/xorg/modules/input\"
db4fdc04
LC
114 ModulePath \"" xorg-server "/lib/xorg/modules\"
115 ModulePath \"" xorg-server "/lib/xorg/modules/extensions\"
116 ModulePath \"" xorg-server "/lib/xorg/modules/multimedia\"
117EndSection
118
119Section \"ServerFlags\"
e30442b5 120 Option \"AllowMouseOpenFail\" \"on\"
db4fdc04 121EndSection
f703413e 122"
d2e59637
LC
123 (string-join (map device-section drivers) "\n")
124 (string-join (map (cut screen-section <> resolutions)
125 drivers)
126 "\n")))
db4fdc04 127
8779d342
LC
128 (mlet %store-monad ((config (xserver.conf)))
129 (define script
db4fdc04 130 ;; Write a small wrapper around the X server.
8779d342
LC
131 #~(begin
132 (setenv "XORG_DRI_DRIVER_PATH" (string-append #$mesa "/lib/dri"))
133 (setenv "XKB_BINDIR" (string-append #$xkbcomp "/bin"))
134
135 (apply execl (string-append #$xorg-server "/bin/X")
91cc5aff 136 (string-append #$xorg-server "/bin/X") ;argv[0]
594340bc 137 "-logverbose" "-verbose"
8779d342
LC
138 "-xkbdir" (string-append #$xkeyboard-config "/share/X11/xkb")
139 "-config" #$config
140 "-nolisten" "tcp" "-terminate"
141
142 ;; Note: SLiM and other display managers add the
143 ;; '-auth' flag by themselves.
144 (cdr (command-line)))))
145
146 (gexp->script "start-xorg" script)))
db4fdc04 147
9e4eddb4 148(define* (xinitrc #:key
bdb36958 149 (guile (canonical-package guile-2.0))
24d56899
SB
150 fallback-session)
151 "Return a system-wide xinitrc script that starts the specified X session,
152which should be passed to this script as the first argument. If not, the
153@var{fallback-session} will be used."
8779d342
LC
154 (define builder
155 #~(begin
156 (use-modules (ice-9 match))
157
16c33bfb
LC
158 (define (close-all-fdes)
159 ;; Close all the open file descriptors except 0 to 2.
160 (let loop ((fd 3))
161 (when (< fd 4096) ;FIXME: use sysconf + _SC_OPEN_MAX
162 (false-if-exception (close-fdes fd))
163 (loop (+ 1 fd)))))
164
b2bd7c25
LC
165 (define (exec-from-login-shell command . args)
166 ;; Run COMMAND from a login shell so that it gets to see the same
167 ;; environment variables that one gets when logging in on a tty, for
168 ;; instance.
169 (let* ((pw (getpw (getuid)))
170 (shell (passwd:shell pw))
171 (st (stat command #f)))
172 (when (and st (not (zero? (logand (stat:mode st) #o100))))
16c33bfb
LC
173 ;; Close any open file descriptors. This is all the more
174 ;; important that SLiM itself exec's us directly without closing
175 ;; its own file descriptors!
176 (close-all-fdes)
177
b2bd7c25
LC
178 ;; The '--login' option is supported at least by Bash and zsh.
179 (execl shell shell "--login" "-c"
180 (string-join (cons command args))))))
181
24d56899
SB
182 (let ((home (getenv "HOME"))
183 (session (match (command-line)
184 ((_ x) x)
185 (_ #$fallback-session))))
186 ;; First, try to run ~/.xsession.
187 (exec-from-login-shell (string-append home "/.xsession"))
188 ;; Then try to start the specified session.
189 (exec-from-login-shell session))))
8779d342 190 (gexp->script "xinitrc" builder))
9e4eddb4 191
0ecc3bf3
LC
192\f
193;;;
194;;; SLiM log-in manager.
195;;;
196
ffc3a02b
LC
197(define-record-type* <session-type> session-type make-session-type
198 session-type?
199 (name session-type-name) ;string
200 (executable session-type-executable)) ;string-valued gexp
201
202(define %windowmaker-session-type
203 (session-type
204 (name "WindowMaker")
205 (executable #~(string-append #$windowmaker "/bin/wmaker"))))
206
207(define %ratpoison-session-type
208 (session-type
209 (name "Ratpoison")
210 (executable #~(string-append #$ratpoison "/bin/ratpoison"))))
211
5ce93d9a
SB
212(define %sawfish-session-type
213 (session-type
214 (name "Sawfish")
215 (executable #~(string-append #$sawfish "/bin/sawfish"))))
216
24d56899 217(define %default-xsessions
ffc3a02b
LC
218 ;; Default session types available to the log-in manager.
219 (list %windowmaker-session-type %ratpoison-session-type))
24d56899
SB
220
221(define (xsessions-directory sessions)
a21b23d3
LC
222 "Return a directory containing SESSIONS, a list of <session-type> objects.
223The alphabetical order of the files in that directory match the order of the
224elements in SESSIONS."
ffc3a02b
LC
225 (define builder
226 #~(begin
a21b23d3
LC
227 (use-modules (srfi srfi-1)
228 (ice-9 format))
229
ffc3a02b
LC
230 (mkdir #$output)
231 (chdir #$output)
a21b23d3
LC
232 (fold (lambda (name executable number)
233 ;; Create file names such that the order of the items in
234 ;; SESSION is respected. SLiM gets them in lexicographic
235 ;; order and uses the first one as the default session.
236 (let ((file (format #f "~2,'0d-~a.desktop"
237 number (string-downcase name))))
238 (call-with-output-file file
239 (lambda (port)
240 (format port "[Desktop Entry]
ffc3a02b
LC
241Name=~a
242Exec=~a
243Type=Application~%"
a21b23d3
LC
244 name executable)))
245 (+ 1 number)))
246 1
247 '#$(map session-type-name sessions)
248 (list #$@(map session-type-executable sessions)))))
ffc3a02b
LC
249
250 (gexp->derivation "xsessions-dir" builder))
24d56899 251
0ecc3bf3
LC
252(define %default-slim-theme
253 ;; Theme based on work by Felipe López.
254 #~(string-append #$%artwork-repository "/slim"))
255
256(define %default-slim-theme-name
257 ;; This must be the name of the sub-directory in %DEFAULT-SLIM-THEME that
258 ;; contains the actual theme files.
259 "0.8")
260
db4fdc04
LC
261(define* (slim-service #:key (slim slim)
262 (allow-empty-passwords? #t) auto-login?
263 (default-user "")
0ecc3bf3
LC
264 (theme %default-slim-theme)
265 (theme-name %default-slim-theme-name)
db4fdc04 266 (xauth xauth) (dmd dmd) (bash bash)
24d56899
SB
267 (sessions %default-xsessions)
268 (auto-login-session #~(string-append #$windowmaker
269 "/bin/wmaker"))
db4fdc04
LC
270 startx)
271 "Return a service that spawns the SLiM graphical login manager, which in
51da7ca0
LC
272turn starts the X display server with @var{startx}, a command as returned by
273@code{xorg-start-command}.
db4fdc04 274
51da7ca0
LC
275When @var{allow-empty-passwords?} is true, allow logins with an empty
276password. When @var{auto-login?} is true, log in automatically as
24d56899 277@var{default-user} with @var{auto-login-session}.
0ecc3bf3
LC
278
279If @var{theme} is @code{#f}, the use the default log-in theme; otherwise
280@var{theme} must be a gexp denoting the name of a directory containing the
281theme to use. In that case, @var{theme-name} specifies the name of the
ffc3a02b
LC
282theme.
283
284Last, @var{session} is a list of @code{<session-type>} objects denoting the
a21b23d3
LC
285available session types that can be chosen from the log-in screen. The first
286one is chosen by default."
0ecc3bf3 287
db4fdc04 288 (define (slim.cfg)
9e4eddb4 289 (mlet %store-monad ((startx (or startx (xorg-start-command)))
24d56899
SB
290 (xinitrc (xinitrc #:fallback-session
291 auto-login-session))
292 (sessiondir (xsessions-directory sessions)))
db4fdc04 293 (text-file* "slim.cfg" "
b4140694 294default_path /run/current-system/profile/bin
db4fdc04
LC
295default_xserver " startx "
296xserver_arguments :0 vt7
297xauth_path " xauth "/bin/xauth
298authfile /var/run/slim.auth
299
300# The login command. '%session' is replaced by the chosen session name, one
301# of the names specified in the 'sessions' setting: 'wmaker', 'xfce', etc.
057d6ce5 302login_cmd exec " xinitrc " %session
24d56899 303sessiondir " sessiondir "
e1509174 304session_msg session (F1 to change):
db4fdc04
LC
305
306halt_cmd " dmd "/sbin/halt
307reboot_cmd " dmd "/sbin/reboot
0ecc3bf3
LC
308"
309(if auto-login?
310 (string-append "auto_login yes\ndefault_user " default-user "\n")
311 "")
312(if theme-name
313 (string-append "current_theme " theme-name "\n")
314 ""))))
db4fdc04 315
b5f4e686 316 (mlet %store-monad ((slim.cfg (slim.cfg)))
db4fdc04
LC
317 (return
318 (service
319 (documentation "Xorg display server")
320 (provision '(xorg-server))
8dbab712 321 (requirement '(user-processes host-name udev))
db4fdc04 322 (start
47b73c34
LC
323 #~(lambda ()
324 ;; A stale lock file can prevent SLiM from starting, so remove it
325 ;; to be on the safe side.
326 (false-if-exception (delete-file "/var/run/slim.lock"))
327
328 (fork+exec-command
329 (list (string-append #$slim "/bin/slim") "-nodaemon")
330 #:environment-variables
0ecc3bf3
LC
331 (list (string-append "SLIM_CFGFILE=" #$slim.cfg)
332 #$@(if theme
333 (list #~(string-append "SLIM_THEMESDIR=" #$theme))
334 #~())))))
b5f4e686 335 (stop #~(make-kill-destructor))
db4fdc04
LC
336 (respawn? #t)
337 (pam-services
338 ;; Tell PAM about 'slim'.
339 (list (unix-pam-service
340 "slim"
341 #:allow-empty-passwords? allow-empty-passwords?)))))))
342
343;;; xorg.scm ends here