install: Register the hydra.gnu.org key on the installation image.
[jackhill/guix/guix.git] / gnu / services / base.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
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 base)
20 #:use-module (gnu services)
21 #:use-module (gnu system shadow) ; 'user-account', etc.
22 #:use-module (gnu system linux) ; 'pam-service', etc.
23 #:use-module (gnu packages admin)
24 #:use-module ((gnu packages base)
25 #:select (glibc-final))
26 #:use-module (gnu packages package-management)
27 #:use-module (guix gexp)
28 #:use-module (guix monads)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-26)
31 #:use-module (ice-9 format)
32 #:export (root-file-system-service
33 file-system-service
34 user-processes-service
35 host-name-service
36 mingetty-service
37 nscd-service
38 syslog-service
39 guix-service
40 %base-services))
41
42 ;;; Commentary:
43 ;;;
44 ;;; Base system services---i.e., services that 99% of the users will want to
45 ;;; use.
46 ;;;
47 ;;; Code:
48
49 (define (root-file-system-service)
50 "Return a service whose sole purpose is to re-mount read-only the root file
51 system upon shutdown (aka. cleanly \"umounting\" root.)
52
53 This service must be the root of the service dependency graph so that its
54 'stop' action is invoked when dmd is the only process left."
55 (with-monad %store-monad
56 (return
57 (service
58 (documentation "Take care of the root file system.")
59 (provision '(root-file-system))
60 (start #~(const #t))
61 (stop #~(lambda _
62 ;; Return #f if successfully stopped.
63 (sync)
64
65 (call-with-blocked-asyncs
66 (lambda ()
67 (let ((null (%make-void-port "w")))
68 ;; Close 'dmd.log'.
69 (display "closing log\n")
70 ;; XXX: Ideally we'd use 'stop-logging', but that one
71 ;; doesn't actually close the port as of dmd 0.1.
72 (close-port (@@ (dmd comm) log-output-port))
73 (set! (@@ (dmd comm) log-output-port) null)
74
75 ;; Redirect the default output ports..
76 (set-current-output-port null)
77 (set-current-error-port null)
78
79 ;; Close /dev/console.
80 (for-each close-fdes '(0 1 2))
81
82 ;; At this point, there are no open files left, so the
83 ;; root file system can be re-mounted read-only.
84 (mount #f "/" #f
85 (logior MS_REMOUNT MS_RDONLY)
86 #:update-mtab? #f)
87
88 #f)))))
89 (respawn? #f)))))
90
91 (define* (file-system-service device target type
92 #:key (check? #t) options (title 'any))
93 "Return a service that mounts DEVICE on TARGET as a file system TYPE with
94 OPTIONS. TITLE is a symbol specifying what kind of name DEVICE is: 'label for
95 a partition label, 'device for a device file name, or 'any. When CHECK? is
96 true, check the file system before mounting it."
97 (with-monad %store-monad
98 (return
99 (service
100 (provision (list (symbol-append 'file-system- (string->symbol target))))
101 (requirement '(root-file-system))
102 (documentation "Check, mount, and unmount the given file system.")
103 (start #~(lambda args
104 (let ((device (canonicalize-device-spec #$device '#$title)))
105 #$(if check?
106 #~(check-file-system device #$type)
107 #~#t)
108 (mount device #$target #$type 0 #$options))
109 #t))
110 (stop #~(lambda args
111 ;; Normally there are no processes left at this point, so
112 ;; TARGET can be safely unmounted.
113 (umount #$target)
114 #f))))))
115
116 (define %do-not-kill-file
117 ;; Name of the file listing PIDs of processes that must survive when halting
118 ;; the system. Typical example is user-space file systems.
119 "/etc/dmd/do-not-kill")
120
121 (define* (user-processes-service requirements #:key (grace-delay 2))
122 "Return the service that is responsible for terminating all the processes so
123 that the root file system can be re-mounted read-only, just before
124 rebooting/halting. Processes still running GRACE-DELAY seconds after SIGTERM
125 has been sent are terminated with SIGKILL.
126
127 The returned service will depend on 'root-file-system' and on all the services
128 listed in REQUIREMENTS.
129
130 All the services that spawn processes must depend on this one so that they are
131 stopped before 'kill' is called."
132 (with-monad %store-monad
133 (return (service
134 (documentation "When stopped, terminate all user processes.")
135 (provision '(user-processes))
136 (requirement (cons 'root-file-system requirements))
137 (start #~(const #t))
138 (stop #~(lambda _
139 (define (kill-except omit signal)
140 ;; Kill all the processes with SIGNAL except those
141 ;; listed in OMIT and the current process.
142 (let ((omit (cons (getpid) omit)))
143 (for-each (lambda (pid)
144 (unless (memv pid omit)
145 (false-if-exception
146 (kill pid signal))))
147 (processes))))
148
149 (define omitted-pids
150 ;; List of PIDs that must not be killed.
151 (if (file-exists? #$%do-not-kill-file)
152 (map string->number
153 (call-with-input-file #$%do-not-kill-file
154 (compose string-tokenize
155 (@ (ice-9 rdelim) read-string))))
156 '()))
157
158 ;; When this happens, all the processes have been
159 ;; killed, including 'deco', so DMD-OUTPUT-PORT and
160 ;; thus CURRENT-OUTPUT-PORT are dangling.
161 (call-with-output-file "/dev/console"
162 (lambda (port)
163 (display "sending all processes the TERM signal\n"
164 port)))
165
166 (if (null? omitted-pids)
167 (begin
168 ;; Easy: terminate all of them.
169 (kill -1 SIGTERM)
170 (sleep #$grace-delay)
171 (kill -1 SIGKILL))
172 (begin
173 ;; Kill them all except OMITTED-PIDS. XXX: We
174 ;; would like to (kill -1 SIGSTOP) to get a fixed
175 ;; list of processes, like 'killall5' does, but
176 ;; that seems unreliable.
177 (kill-except omitted-pids SIGTERM)
178 (sleep #$grace-delay)
179 (kill-except omitted-pids SIGKILL)
180 (delete-file #$%do-not-kill-file)))
181
182 (display "all processes have been terminated\n")
183 #f))
184 (respawn? #f)))))
185
186 (define (host-name-service name)
187 "Return a service that sets the host name to NAME."
188 (with-monad %store-monad
189 (return (service
190 (documentation "Initialize the machine's host name.")
191 (provision '(host-name))
192 (start #~(lambda _
193 (sethostname #$name)))
194 (respawn? #f)))))
195
196 (define* (mingetty-service tty
197 #:key
198 (motd (text-file "motd" "Welcome.\n"))
199 auto-login
200 login-program
201 login-pause?
202 (allow-empty-passwords? #t))
203 "Return a service to run mingetty on @var{tty}.
204
205 When @var{allow-empty-passwords?} is true, allow empty log-in password. When
206 @var{auto-login} is true, it must be a user name under which to log-in
207 automatically. @var{login-pause?} can be set to @code{#t} in conjunction with
208 @var{auto-login}, in which case the user will have to press a key before the
209 login shell is launched.
210
211 When true, @var{login-program} is a gexp or a monadic gexp denoting the name
212 of the log-in program (the default is the @code{login} program from the Shadow
213 tool suite.)
214
215 @var{motd} is a monadic value containing a text file to use as
216 the \"message of the day\"."
217 (mlet %store-monad ((motd motd)
218 (login-program (cond ((gexp? login-program)
219 (return login-program))
220 ((not login-program)
221 (return #f))
222 (else
223 login-program))))
224 (return
225 (service
226 (documentation (string-append "Run mingetty on " tty "."))
227 (provision (list (symbol-append 'term- (string->symbol tty))))
228
229 ;; Since the login prompt shows the host name, wait for the 'host-name'
230 ;; service to be done.
231 (requirement '(user-processes host-name))
232
233 (start #~(make-forkexec-constructor
234 (string-append #$mingetty "/sbin/mingetty")
235 "--noclear" #$tty
236 #$@(if auto-login
237 #~("--autologin" #$auto-login)
238 #~())
239 #$@(if login-program
240 #~("--loginprog" #$login-program)
241 #~())
242 #$@(if login-pause?
243 #~("--loginpause")
244 #~())))
245 (stop #~(make-kill-destructor))
246
247 (pam-services
248 ;; Let 'login' be known to PAM. All the mingetty services will have
249 ;; that PAM service, but that's fine because they're all identical and
250 ;; duplicates are removed.
251 (list (unix-pam-service "login"
252 #:allow-empty-passwords? allow-empty-passwords?
253 #:motd motd)))))))
254
255 (define* (nscd-service #:key (glibc glibc-final))
256 "Return a service that runs libc's name service cache daemon (nscd)."
257 (with-monad %store-monad
258 (return (service
259 (documentation "Run libc's name service cache daemon (nscd).")
260 (provision '(nscd))
261 (requirement '(user-processes))
262
263 (activate #~(begin
264 (use-modules (guix build utils))
265 (mkdir-p "/var/run/nscd")))
266
267 (start
268 #~(make-forkexec-constructor (string-append #$glibc "/sbin/nscd")
269 "-f" "/dev/null"
270 "--foreground"))
271 (stop #~(make-kill-destructor))
272
273 (respawn? #f)))))
274
275 (define (syslog-service)
276 "Return a service that runs 'syslogd' with reasonable default settings."
277
278 ;; Snippet adapted from the GNU inetutils manual.
279 (define contents "
280 # Log all error messages, authentication messages of
281 # level notice or higher and anything of level err or
282 # higher to the console.
283 # Don't log private authentication messages!
284 *.err;auth.notice;authpriv.none /dev/console
285
286 # Log anything (except mail) of level info or higher.
287 # Don't log private authentication messages!
288 *.info;mail.none;authpriv.none /var/log/messages
289
290 # Same, in a different place.
291 *.info;mail.none;authpriv.none /dev/tty12
292
293 # The authpriv file has restricted access.
294 authpriv.* /var/log/secure
295
296 # Log all the mail messages in one place.
297 mail.* /var/log/maillog
298 ")
299
300 (mlet %store-monad
301 ((syslog.conf (text-file "syslog.conf" contents)))
302 (return
303 (service
304 (documentation "Run the syslog daemon (syslogd).")
305 (provision '(syslogd))
306 (requirement '(user-processes))
307 (start
308 #~(make-forkexec-constructor (string-append #$inetutils
309 "/libexec/syslogd")
310 "--no-detach"
311 "--rcfile" #$syslog.conf))
312 (stop #~(make-kill-destructor))))))
313
314 (define* (guix-build-accounts count #:key
315 (group "guixbuild")
316 (first-uid 30001)
317 (shadow shadow))
318 "Return a list of COUNT user accounts for Guix build users, with UIDs
319 starting at FIRST-UID, and under GID."
320 (with-monad %store-monad
321 (return (unfold (cut > <> count)
322 (lambda (n)
323 (user-account
324 (name (format #f "guixbuilder~2,'0d" n))
325 (uid (+ first-uid n -1))
326 (group group)
327 (comment (format #f "Guix Build User ~2d" n))
328 (home-directory "/var/empty")
329 (shell #~(string-append #$shadow "/sbin/nologin"))))
330 1+
331 1))))
332
333 (define (hydra-key-authorization guix)
334 "Return a gexp with code to register the hydra.gnu.org public key with
335 GUIX."
336 #~(unless (file-exists? "/etc/guix/acl")
337 (let ((pid (primitive-fork)))
338 (case pid
339 ((0)
340 (let* ((key (string-append #$guix
341 "/share/guix/hydra.gnu.org.pub"))
342 (port (open-file key "r0b")))
343 (format #t "registering public key '~a'...~%" key)
344 (close-port (current-input-port))
345 ;; (close-fdes 0)
346 (dup port 0)
347 (execl (string-append #$guix "/bin/guix")
348 "guix" "archive" "--authorize")
349 (exit 1)))
350 (else
351 (let ((status (cdr (waitpid pid))))
352 (unless (zero? status)
353 (format (current-error-port) "warning: \
354 failed to register hydra.gnu.org public key: ~a~%" status))))))))
355
356 (define* (guix-service #:key (guix guix) (builder-group "guixbuild")
357 (build-accounts 10) authorize-hydra-key?)
358 "Return a service that runs the build daemon from GUIX, and has
359 BUILD-ACCOUNTS user accounts available under BUILD-USER-GID.
360
361 When AUTHORIZE-HYDRA-KEY? is true, the hydra.gnu.org public key provided by
362 GUIX is authorized upon activation, meaning that substitutes from
363 hydra.gnu.org are used by default."
364 (mlet %store-monad ((accounts (guix-build-accounts build-accounts
365 #:group builder-group)))
366 (return (service
367 (provision '(guix-daemon))
368 (requirement '(user-processes))
369 (start
370 #~(make-forkexec-constructor (string-append #$guix
371 "/bin/guix-daemon")
372 "--build-users-group"
373 #$builder-group))
374 (stop #~(make-kill-destructor))
375 (user-accounts accounts)
376 (user-groups (list (user-group
377 (name builder-group)
378 (members (map user-account-name
379 user-accounts)))))
380 (activate (and authorize-hydra-key?
381 (hydra-key-authorization guix)))))))
382
383 (define %base-services
384 ;; Convenience variable holding the basic services.
385 (let ((motd (text-file "motd" "
386 This is the GNU operating system, welcome!\n\n")))
387 (list (mingetty-service "tty1" #:motd motd)
388 (mingetty-service "tty2" #:motd motd)
389 (mingetty-service "tty3" #:motd motd)
390 (mingetty-service "tty4" #:motd motd)
391 (mingetty-service "tty5" #:motd motd)
392 (mingetty-service "tty6" #:motd motd)
393 (syslog-service)
394 (guix-service)
395 (nscd-service))))
396
397 ;;; base.scm ends here