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