services: Add 'gpm-service'.
[jackhill/guix/guix.git] / gnu / services / base.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Alex Kost <alezost@gmail.com>
4 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gnu services base)
22 #:use-module (guix store)
23 #:use-module (gnu services)
24 #:use-module (gnu services dmd)
25 #:use-module (gnu services networking)
26 #:use-module (gnu system pam)
27 #:use-module (gnu system shadow) ; 'user-account', etc.
28 #:use-module (gnu system file-systems) ; 'file-system', etc.
29 #:use-module (gnu packages admin)
30 #:use-module ((gnu packages linux)
31 #:select (eudev kbd e2fsprogs lvm2 fuse alsa-utils crda gpm))
32 #:use-module ((gnu packages base)
33 #:select (canonical-package glibc))
34 #:use-module (gnu packages package-management)
35 #:use-module (gnu packages lsh)
36 #:use-module (gnu packages lsof)
37 #:use-module ((gnu build file-systems)
38 #:select (mount-flags->bit-mask))
39 #:use-module (guix gexp)
40 #:use-module (guix records)
41 #:use-module (srfi srfi-1)
42 #:use-module (srfi srfi-26)
43 #:use-module (ice-9 match)
44 #:use-module (ice-9 format)
45 #:export (root-file-system-service
46 file-system-service
47 user-unmount-service
48 device-mapping-service
49 swap-service
50 user-processes-service
51 host-name-service
52 console-keymap-service
53 console-font-service
54 udev-service-type
55 udev-service
56
57 mingetty-configuration
58 mingetty-configuration?
59 mingetty-service
60 mingetty-service-type
61
62 %nscd-default-caches
63 %nscd-default-configuration
64
65 nscd-configuration
66 nscd-configuration?
67
68 nscd-cache
69 nscd-cache?
70
71 nscd-service-type
72 nscd-service
73 syslog-service
74
75 guix-configuration
76 guix-configuration?
77 guix-service
78 guix-service-type
79 guix-publish-configuration
80 guix-publish-configuration?
81 guix-publish-service
82 guix-publish-service-type
83 gpm-service-type
84 gpm-service
85
86 %base-services))
87
88 ;;; Commentary:
89 ;;;
90 ;;; Base system services---i.e., services that 99% of the users will want to
91 ;;; use.
92 ;;;
93 ;;; Code:
94
95 \f
96 ;;;
97 ;;; File systems.
98 ;;;
99
100 (define %root-file-system-dmd-service
101 (dmd-service
102 (documentation "Take care of the root file system.")
103 (provision '(root-file-system))
104 (start #~(const #t))
105 (stop #~(lambda _
106 ;; Return #f if successfully stopped.
107 (sync)
108
109 (call-with-blocked-asyncs
110 (lambda ()
111 (let ((null (%make-void-port "w")))
112 ;; Close 'dmd.log'.
113 (display "closing log\n")
114 ;; XXX: Ideally we'd use 'stop-logging', but that one
115 ;; doesn't actually close the port as of dmd 0.1.
116 (close-port (@@ (dmd comm) log-output-port))
117 (set! (@@ (dmd comm) log-output-port) null)
118
119 ;; Redirect the default output ports..
120 (set-current-output-port null)
121 (set-current-error-port null)
122
123 ;; Close /dev/console.
124 (for-each close-fdes '(0 1 2))
125
126 ;; At this point, there are no open files left, so the
127 ;; root file system can be re-mounted read-only.
128 (mount #f "/" #f
129 (logior MS_REMOUNT MS_RDONLY)
130 #:update-mtab? #f)
131
132 #f)))))
133 (respawn? #f)))
134
135 (define root-file-system-service-type
136 (dmd-service-type 'root-file-system
137 (const %root-file-system-dmd-service)))
138
139 (define (root-file-system-service)
140 "Return a service whose sole purpose is to re-mount read-only the root file
141 system upon shutdown (aka. cleanly \"umounting\" root.)
142
143 This service must be the root of the service dependency graph so that its
144 'stop' action is invoked when dmd is the only process left."
145 (service root-file-system-service-type #f))
146
147 (define (file-system->dmd-service-name file-system)
148 "Return the symbol that denotes the service mounting and unmounting
149 FILE-SYSTEM."
150 (symbol-append 'file-system-
151 (string->symbol (file-system-mount-point file-system))))
152
153 (define (mapped-device->dmd-service-name md)
154 "Return the symbol that denotes the dmd service of MD, a <mapped-device>."
155 (symbol-append 'device-mapping-
156 (string->symbol (mapped-device-target md))))
157
158 (define dependency->dmd-service-name
159 (match-lambda
160 ((? mapped-device? md)
161 (mapped-device->dmd-service-name md))
162 ((? file-system? fs)
163 (file-system->dmd-service-name fs))))
164
165 (define file-system-service-type
166 ;; TODO(?): Make this an extensible service that takes <file-system> objects
167 ;; and returns a list of <dmd-service>.
168 (dmd-service-type
169 'file-system
170 (lambda (file-system)
171 (let ((target (file-system-mount-point file-system))
172 (device (file-system-device file-system))
173 (type (file-system-type file-system))
174 (title (file-system-title file-system))
175 (check? (file-system-check? file-system))
176 (create? (file-system-create-mount-point? file-system))
177 (dependencies (file-system-dependencies file-system)))
178 (dmd-service
179 (provision (list (file-system->dmd-service-name file-system)))
180 (requirement `(root-file-system
181 ,@(map dependency->dmd-service-name dependencies)))
182 (documentation "Check, mount, and unmount the given file system.")
183 (start #~(lambda args
184 ;; FIXME: Use or factorize with 'mount-file-system'.
185 (let ((device (canonicalize-device-spec #$device '#$title))
186 (flags #$(mount-flags->bit-mask
187 (file-system-flags file-system))))
188 #$(if create?
189 #~(mkdir-p #$target)
190 #~#t)
191 #$(if check?
192 #~(begin
193 ;; Make sure fsck.ext2 & co. can be found.
194 (setenv "PATH"
195 (string-append
196 #$e2fsprogs "/sbin:"
197 "/run/current-system/profile/sbin:"
198 (getenv "PATH")))
199 (check-file-system device #$type))
200 #~#t)
201
202 (mount device #$target #$type flags
203 #$(file-system-options file-system))
204
205 ;; For read-only bind mounts, an extra remount is needed,
206 ;; as per <http://lwn.net/Articles/281157/>, which still
207 ;; applies to Linux 4.0.
208 (when (and (= MS_BIND (logand flags MS_BIND))
209 (= MS_RDONLY (logand flags MS_RDONLY)))
210 (mount device #$target #$type
211 (logior MS_BIND MS_REMOUNT MS_RDONLY))))
212 #t))
213 (stop #~(lambda args
214 ;; Normally there are no processes left at this point, so
215 ;; TARGET can be safely unmounted.
216
217 ;; Make sure PID 1 doesn't keep TARGET busy.
218 (chdir "/")
219
220 (umount #$target)
221 #f)))))))
222
223 (define* (file-system-service file-system)
224 "Return a service that mounts @var{file-system}, a @code{<file-system>}
225 object."
226 (service file-system-service-type file-system))
227
228 (define user-unmount-service-type
229 (dmd-service-type
230 'user-file-systems
231 (lambda (known-mount-points)
232 (dmd-service
233 (documentation "Unmount manually-mounted file systems.")
234 (provision '(user-file-systems))
235 (start #~(const #t))
236 (stop #~(lambda args
237 (define (known? mount-point)
238 (member mount-point
239 (cons* "/proc" "/sys" '#$known-mount-points)))
240
241 ;; Make sure we don't keep the user's mount points busy.
242 (chdir "/")
243
244 (for-each (lambda (mount-point)
245 (format #t "unmounting '~a'...~%" mount-point)
246 (catch 'system-error
247 (lambda ()
248 (umount mount-point))
249 (lambda args
250 (let ((errno (system-error-errno args)))
251 (format #t "failed to unmount '~a': ~a~%"
252 mount-point (strerror errno))))))
253 (filter (negate known?) (mount-points)))
254 #f))))))
255
256 (define (user-unmount-service known-mount-points)
257 "Return a service whose sole purpose is to unmount file systems not listed
258 in KNOWN-MOUNT-POINTS when it is stopped."
259 (service user-unmount-service-type known-mount-points))
260
261 (define %do-not-kill-file
262 ;; Name of the file listing PIDs of processes that must survive when halting
263 ;; the system. Typical example is user-space file systems.
264 "/etc/dmd/do-not-kill")
265
266 (define user-processes-service-type
267 (dmd-service-type
268 'user-processes
269 (match-lambda
270 ((requirements grace-delay)
271 (dmd-service
272 (documentation "When stopped, terminate all user processes.")
273 (provision '(user-processes))
274 (requirement (cons* 'root-file-system 'user-file-systems
275 (map file-system->dmd-service-name
276 requirements)))
277 (start #~(const #t))
278 (stop #~(lambda _
279 (define (kill-except omit signal)
280 ;; Kill all the processes with SIGNAL except those listed
281 ;; in OMIT and the current process.
282 (let ((omit (cons (getpid) omit)))
283 (for-each (lambda (pid)
284 (unless (memv pid omit)
285 (false-if-exception
286 (kill pid signal))))
287 (processes))))
288
289 (define omitted-pids
290 ;; List of PIDs that must not be killed.
291 (if (file-exists? #$%do-not-kill-file)
292 (map string->number
293 (call-with-input-file #$%do-not-kill-file
294 (compose string-tokenize
295 (@ (ice-9 rdelim) read-string))))
296 '()))
297
298 (define (now)
299 (car (gettimeofday)))
300
301 (define (sleep* n)
302 ;; Really sleep N seconds.
303 ;; Work around <http://bugs.gnu.org/19581>.
304 (define start (now))
305 (let loop ((elapsed 0))
306 (when (> n elapsed)
307 (sleep (- n elapsed))
308 (loop (- (now) start)))))
309
310 (define lset= (@ (srfi srfi-1) lset=))
311
312 (display "sending all processes the TERM signal\n")
313
314 (if (null? omitted-pids)
315 (begin
316 ;; Easy: terminate all of them.
317 (kill -1 SIGTERM)
318 (sleep* #$grace-delay)
319 (kill -1 SIGKILL))
320 (begin
321 ;; Kill them all except OMITTED-PIDS. XXX: We would
322 ;; like to (kill -1 SIGSTOP) to get a fixed list of
323 ;; processes, like 'killall5' does, but that seems
324 ;; unreliable.
325 (kill-except omitted-pids SIGTERM)
326 (sleep* #$grace-delay)
327 (kill-except omitted-pids SIGKILL)
328 (delete-file #$%do-not-kill-file)))
329
330 (let wait ()
331 (let ((pids (processes)))
332 (unless (lset= = pids (cons 1 omitted-pids))
333 (format #t "waiting for process termination\
334 (processes left: ~s)~%"
335 pids)
336 (sleep* 2)
337 (wait))))
338
339 (display "all processes have been terminated\n")
340 #f))
341 (respawn? #f))))))
342
343 (define* (user-processes-service file-systems #:key (grace-delay 4))
344 "Return the service that is responsible for terminating all the processes so
345 that the root file system can be re-mounted read-only, just before
346 rebooting/halting. Processes still running GRACE-DELAY seconds after SIGTERM
347 has been sent are terminated with SIGKILL.
348
349 The returned service will depend on 'root-file-system' and on all the dmd
350 services corresponding to FILE-SYSTEMS.
351
352 All the services that spawn processes must depend on this one so that they are
353 stopped before 'kill' is called."
354 (service user-processes-service-type
355 (list file-systems grace-delay)))
356
357 \f
358 ;;;
359 ;;; Console & co.
360 ;;;
361
362 (define host-name-service-type
363 (dmd-service-type
364 'host-name
365 (lambda (name)
366 (dmd-service
367 (documentation "Initialize the machine's host name.")
368 (provision '(host-name))
369 (start #~(lambda _
370 (sethostname #$name)))
371 (respawn? #f)))))
372
373 (define (host-name-service name)
374 "Return a service that sets the host name to @var{name}."
375 (service host-name-service-type name))
376
377 (define (unicode-start tty)
378 "Return a gexp to start Unicode support on @var{tty}."
379
380 ;; We have to run 'unicode_start' in a pipe so that when it invokes the
381 ;; 'tty' command, that command returns TTY.
382 #~(begin
383 (let ((pid (primitive-fork)))
384 (case pid
385 ((0)
386 (close-fdes 0)
387 (dup2 (open-fdes #$tty O_RDONLY) 0)
388 (close-fdes 1)
389 (dup2 (open-fdes #$tty O_WRONLY) 1)
390 (execl (string-append #$kbd "/bin/unicode_start")
391 "unicode_start"))
392 (else
393 (zero? (cdr (waitpid pid))))))))
394
395 (define console-keymap-service-type
396 (dmd-service-type
397 'console-keymap
398 (lambda (file)
399 (dmd-service
400 (documentation (string-append "Load console keymap (loadkeys)."))
401 (provision '(console-keymap))
402 (start #~(lambda _
403 (zero? (system* (string-append #$kbd "/bin/loadkeys")
404 #$file))))
405 (respawn? #f)))))
406
407 (define (console-keymap-service file)
408 "Return a service to load console keymap from @var{file}."
409 (service console-keymap-service-type file))
410
411 (define console-font-service-type
412 (dmd-service-type
413 'console-font
414 (match-lambda
415 ((tty font)
416 (let ((device (string-append "/dev/" tty)))
417 (dmd-service
418 (documentation "Load a Unicode console font.")
419 (provision (list (symbol-append 'console-font-
420 (string->symbol tty))))
421
422 ;; Start after mingetty has been started on TTY, otherwise the settings
423 ;; are ignored.
424 (requirement (list (symbol-append 'term-
425 (string->symbol tty))))
426
427 (start #~(lambda _
428 (and #$(unicode-start device)
429 (zero?
430 (system* (string-append #$kbd "/bin/setfont")
431 "-C" #$device #$font)))))
432 (stop #~(const #t))
433 (respawn? #f)))))))
434
435 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
436 "Return a service that sets up Unicode support in @var{tty} and loads
437 @var{font} for that tty (fonts are per virtual console in Linux.)"
438 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
439 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
440 ;; codepoints notably found in the UTF-8 manual.
441 (service console-font-service-type (list tty font)))
442
443 (define-record-type* <mingetty-configuration>
444 mingetty-configuration make-mingetty-configuration
445 mingetty-configuration?
446 (mingetty mingetty-configuration-mingetty ;<package>
447 (default mingetty))
448 (tty mingetty-configuration-tty) ;string
449 (motd mingetty-configuration-motd ;file-like
450 (default (plain-file "motd" "Welcome.\n")))
451 (auto-login mingetty-auto-login ;string | #f
452 (default #f))
453 (login-program mingetty-login-program ;gexp
454 (default #f))
455 (login-pause? mingetty-login-pause? ;Boolean
456 (default #f))
457
458 ;; Allow empty passwords by default so that first-time users can log in when
459 ;; the 'root' account has just been created.
460 (allow-empty-passwords? mingetty-configuration-allow-empty-passwords?
461 (default #t))) ;Boolean
462
463 (define (mingetty-pam-service conf)
464 "Return the list of PAM service needed for CONF."
465 ;; Let 'login' be known to PAM. All the mingetty services will have that
466 ;; PAM service, but that's fine because they're all identical and duplicates
467 ;; are removed.
468 (list (unix-pam-service "login"
469 #:allow-empty-passwords?
470 (mingetty-configuration-allow-empty-passwords? conf)
471 #:motd
472 (mingetty-configuration-motd conf))))
473
474 (define mingetty-dmd-service
475 (match-lambda
476 (($ <mingetty-configuration> mingetty tty motd auto-login login-program
477 login-pause? allow-empty-passwords?)
478 (list
479 (dmd-service
480 (documentation "Run mingetty on an tty.")
481 (provision (list (symbol-append 'term- (string->symbol tty))))
482
483 ;; Since the login prompt shows the host name, wait for the 'host-name'
484 ;; service to be done. Also wait for udev essentially so that the tty
485 ;; text is not lost in the middle of kernel messages (XXX).
486 (requirement '(user-processes host-name udev))
487
488 (start #~(make-forkexec-constructor
489 (list (string-append #$mingetty "/sbin/mingetty")
490 "--noclear" #$tty
491 #$@(if auto-login
492 #~("--autologin" #$auto-login)
493 #~())
494 #$@(if login-program
495 #~("--loginprog" #$login-program)
496 #~())
497 #$@(if login-pause?
498 #~("--loginpause")
499 #~()))))
500 (stop #~(make-kill-destructor)))))))
501
502 (define mingetty-service-type
503 (service-type (name 'mingetty)
504 (extensions (list (service-extension dmd-root-service-type
505 mingetty-dmd-service)
506 (service-extension pam-root-service-type
507 mingetty-pam-service)))))
508
509 (define* (mingetty-service config)
510 "Return a service to run mingetty according to @var{config}, which specifies
511 the tty to run, among other things."
512 (service mingetty-service-type config))
513
514 (define-record-type* <nscd-configuration> nscd-configuration
515 make-nscd-configuration
516 nscd-configuration?
517 (log-file nscd-configuration-log-file ;string
518 (default "/var/log/nscd.log"))
519 (debug-level nscd-debug-level ;integer
520 (default 0))
521 ;; TODO: See nscd.conf in glibc for other options to add.
522 (caches nscd-configuration-caches ;list of <nscd-cache>
523 (default %nscd-default-caches))
524 (name-services nscd-configuration-name-services ;list of <packages>
525 (default '()))
526 (glibc nscd-configuration-glibc ;<package>
527 (default (canonical-package glibc))))
528
529 (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
530 nscd-cache?
531 (database nscd-cache-database) ;symbol
532 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
533 (negative-time-to-live nscd-cache-negative-time-to-live
534 (default 20)) ;integer
535 (suggested-size nscd-cache-suggested-size ;integer ("default module
536 ;of hash table")
537 (default 211))
538 (check-files? nscd-cache-check-files? ;Boolean
539 (default #t))
540 (persistent? nscd-cache-persistent? ;Boolean
541 (default #t))
542 (shared? nscd-cache-shared? ;Boolean
543 (default #t))
544 (max-database-size nscd-cache-max-database-size ;integer
545 (default (* 32 (expt 2 20))))
546 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
547 (default #t)))
548
549 (define %nscd-default-caches
550 ;; Caches that we want to enable by default. Note that when providing an
551 ;; empty nscd.conf, all caches are disabled.
552 (list (nscd-cache (database 'hosts)
553
554 ;; Aggressively cache the host name cache to improve
555 ;; privacy and resilience.
556 (positive-time-to-live (* 3600 12))
557 (negative-time-to-live 20)
558 (persistent? #t))
559
560 (nscd-cache (database 'services)
561
562 ;; Services are unlikely to change, so we can be even more
563 ;; aggressive.
564 (positive-time-to-live (* 3600 24))
565 (negative-time-to-live 3600)
566 (check-files? #t) ;check /etc/services changes
567 (persistent? #t))))
568
569 (define %nscd-default-configuration
570 ;; Default nscd configuration.
571 (nscd-configuration))
572
573 (define (nscd.conf-file config)
574 "Return the @file{nscd.conf} configuration file for @var{config}, an
575 @code{<nscd-configuration>} object."
576 (define cache->config
577 (match-lambda
578 (($ <nscd-cache> (= symbol->string database)
579 positive-ttl negative-ttl size check-files?
580 persistent? shared? max-size propagate?)
581 (string-append "\nenable-cache\t" database "\tyes\n"
582
583 "positive-time-to-live\t" database "\t"
584 (number->string positive-ttl) "\n"
585 "negative-time-to-live\t" database "\t"
586 (number->string negative-ttl) "\n"
587 "suggested-size\t" database "\t"
588 (number->string size) "\n"
589 "check-files\t" database "\t"
590 (if check-files? "yes\n" "no\n")
591 "persistent\t" database "\t"
592 (if persistent? "yes\n" "no\n")
593 "shared\t" database "\t"
594 (if shared? "yes\n" "no\n")
595 "max-db-size\t" database "\t"
596 (number->string max-size) "\n"
597 "auto-propagate\t" database "\t"
598 (if propagate? "yes\n" "no\n")))))
599
600 (match config
601 (($ <nscd-configuration> log-file debug-level caches)
602 (plain-file "nscd.conf"
603 (string-append "\
604 # Configuration of libc's name service cache daemon (nscd).\n\n"
605 (if log-file
606 (string-append "logfile\t" log-file)
607 "")
608 "\n"
609 (if debug-level
610 (string-append "debug-level\t"
611 (number->string debug-level))
612 "")
613 "\n"
614 (string-concatenate
615 (map cache->config caches)))))))
616
617 (define (nscd-dmd-service config)
618 "Return a dmd service for CONFIG, an <nscd-configuration> object."
619 (let ((nscd.conf (nscd.conf-file config))
620 (name-services (nscd-configuration-name-services config)))
621 (list (dmd-service
622 (documentation "Run libc's name service cache daemon (nscd).")
623 (provision '(nscd))
624 (requirement '(user-processes))
625 (start #~(make-forkexec-constructor
626 (list (string-append #$(nscd-configuration-glibc config)
627 "/sbin/nscd")
628 "-f" #$nscd.conf "--foreground")
629
630 #:environment-variables
631 (list (string-append "LD_LIBRARY_PATH="
632 (string-join
633 (map (lambda (dir)
634 (string-append dir "/lib"))
635 (list #$@name-services))
636 ":")))))
637 (stop #~(make-kill-destructor))
638
639 (respawn? #f)))))
640
641 (define nscd-activation
642 ;; Actions to take before starting nscd.
643 #~(begin
644 (use-modules (guix build utils))
645 (mkdir-p "/var/run/nscd")
646 (mkdir-p "/var/db/nscd"))) ;for the persistent cache
647
648 (define nscd-service-type
649 (service-type (name 'nscd)
650 (extensions
651 (list (service-extension activation-service-type
652 (const nscd-activation))
653 (service-extension dmd-root-service-type
654 nscd-dmd-service)))
655
656 ;; This can be extended by providing additional name services
657 ;; such as nss-mdns.
658 (compose concatenate)
659 (extend (lambda (config name-services)
660 (nscd-configuration
661 (inherit config)
662 (name-services (append
663 (nscd-configuration-name-services config)
664 name-services)))))))
665
666 (define* (nscd-service #:optional (config %nscd-default-configuration))
667 "Return a service that runs libc's name service cache daemon (nscd) with the
668 given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
669 Service Switch}, for an example."
670 (service nscd-service-type config))
671
672 (define syslog-service-type
673 (dmd-service-type
674 'syslog
675 (lambda (config-file)
676 (dmd-service
677 (documentation "Run the syslog daemon (syslogd).")
678 (provision '(syslogd))
679 (requirement '(user-processes))
680 (start #~(make-forkexec-constructor
681 (list (string-append #$inetutils "/libexec/syslogd")
682 "--no-detach" "--rcfile" #$config-file)))
683 (stop #~(make-kill-destructor))))))
684
685 ;; Snippet adapted from the GNU inetutils manual.
686 (define %default-syslog.conf
687 (plain-file "syslog.conf" "
688 # Log all error messages, authentication messages of
689 # level notice or higher and anything of level err or
690 # higher to the console.
691 # Don't log private authentication messages!
692 *.alert;auth.notice;authpriv.none /dev/console
693
694 # Log anything (except mail) of level info or higher.
695 # Don't log private authentication messages!
696 *.info;mail.none;authpriv.none /var/log/messages
697
698 # Same, in a different place.
699 *.info;mail.none;authpriv.none /dev/tty12
700
701 # The authpriv file has restricted access.
702 authpriv.* /var/log/secure
703
704 # Log all the mail messages in one place.
705 mail.* /var/log/maillog
706 "))
707
708 (define* (syslog-service #:key (config-file %default-syslog.conf))
709 "Return a service that runs @code{syslogd}.
710 If configuration file name @var{config-file} is not specified, use some
711 reasonable default settings."
712 (service syslog-service-type config-file))
713
714 \f
715 ;;;
716 ;;; Guix services.
717 ;;;
718
719 (define* (guix-build-accounts count #:key
720 (group "guixbuild")
721 (first-uid 30001)
722 (shadow shadow))
723 "Return a list of COUNT user accounts for Guix build users, with UIDs
724 starting at FIRST-UID, and under GID."
725 (unfold (cut > <> count)
726 (lambda (n)
727 (user-account
728 (name (format #f "guixbuilder~2,'0d" n))
729 (system? #t)
730 (uid (+ first-uid n -1))
731 (group group)
732
733 ;; guix-daemon expects GROUP to be listed as a
734 ;; supplementary group too:
735 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
736 (supplementary-groups (list group "kvm"))
737
738 (comment (format #f "Guix Build User ~2d" n))
739 (home-directory "/var/empty")
740 (shell #~(string-append #$shadow "/sbin/nologin"))))
741 1+
742 1))
743
744 (define (hydra-key-authorization guix)
745 "Return a gexp with code to register the hydra.gnu.org public key with
746 GUIX."
747 #~(unless (file-exists? "/etc/guix/acl")
748 (let ((pid (primitive-fork)))
749 (case pid
750 ((0)
751 (let* ((key (string-append #$guix
752 "/share/guix/hydra.gnu.org.pub"))
753 (port (open-file key "r0b")))
754 (format #t "registering public key '~a'...~%" key)
755 (close-port (current-input-port))
756 (dup port 0)
757 (execl (string-append #$guix "/bin/guix")
758 "guix" "archive" "--authorize")
759 (exit 1)))
760 (else
761 (let ((status (cdr (waitpid pid))))
762 (unless (zero? status)
763 (format (current-error-port) "warning: \
764 failed to register hydra.gnu.org public key: ~a~%" status))))))))
765
766 (define-record-type* <guix-configuration>
767 guix-configuration make-guix-configuration
768 guix-configuration?
769 (guix guix-configuration-guix ;<package>
770 (default guix))
771 (build-group guix-configuration-build-group ;string
772 (default "guixbuild"))
773 (build-accounts guix-configuration-build-accounts ;integer
774 (default 10))
775 (authorize-key? guix-configuration-authorize-key? ;Boolean
776 (default #t))
777 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
778 (default #t))
779 (substitute-urls guix-configuration-substitute-urls ;list of strings
780 (default %default-substitute-urls))
781 (extra-options guix-configuration-extra-options ;list of strings
782 (default '()))
783 (lsof guix-configuration-lsof ;<package>
784 (default lsof))
785 (lsh guix-configuration-lsh ;<package>
786 (default lsh)))
787
788 (define %default-guix-configuration
789 (guix-configuration))
790
791 (define (guix-dmd-service config)
792 "Return a <dmd-service> for the Guix daemon service with CONFIG."
793 (match config
794 (($ <guix-configuration> guix build-group build-accounts authorize-key?
795 use-substitutes? substitute-urls extra-options
796 lsof lsh)
797 (list (dmd-service
798 (documentation "Run the Guix daemon.")
799 (provision '(guix-daemon))
800 (requirement '(user-processes))
801 (start
802 #~(make-forkexec-constructor
803 (list (string-append #$guix "/bin/guix-daemon")
804 "--build-users-group" #$build-group
805 #$@(if use-substitutes?
806 '()
807 '("--no-substitutes"))
808 "--substitute-urls" #$(string-join substitute-urls)
809 #$@extra-options)
810
811 ;; Add 'lsof' (for the GC) and 'lsh' (for offloading) to the
812 ;; daemon's $PATH.
813 #:environment-variables
814 (list (string-append "PATH=" #$lsof "/bin:" #$lsh "/bin"))))
815 (stop #~(make-kill-destructor)))))))
816
817 (define (guix-accounts config)
818 "Return the user accounts and user groups for CONFIG."
819 (match config
820 (($ <guix-configuration> _ build-group build-accounts)
821 (cons (user-group
822 (name build-group)
823 (system? #t)
824
825 ;; Use a fixed GID so that we can create the store with the right
826 ;; owner.
827 (id 30000))
828 (guix-build-accounts build-accounts
829 #:group build-group)))))
830
831 (define (guix-activation config)
832 "Return the activation gexp for CONFIG."
833 (match config
834 (($ <guix-configuration> guix build-group build-accounts authorize-key?)
835 ;; Assume that the store has BUILD-GROUP as its group. We could
836 ;; otherwise call 'chown' here, but the problem is that on a COW unionfs,
837 ;; chown leads to an entire copy of the tree, which is a bad idea.
838
839 ;; Optionally authorize hydra.gnu.org's key.
840 (and authorize-key?
841 (hydra-key-authorization guix)))))
842
843 (define guix-service-type
844 (service-type
845 (name 'guix)
846 (extensions
847 (list (service-extension dmd-root-service-type guix-dmd-service)
848 (service-extension account-service-type guix-accounts)
849 (service-extension activation-service-type guix-activation)))))
850
851 (define* (guix-service #:optional (config %default-guix-configuration))
852 "Return a service that runs the Guix build daemon according to
853 @var{config}."
854 (service guix-service-type config))
855
856
857 (define-record-type* <guix-publish-configuration>
858 guix-publish-configuration make-guix-publish-configuration
859 guix-publish-configuration?
860 (guix guix-publish-configuration-guix ;package
861 (default guix))
862 (port guix-publish-configuration-port ;number
863 (default 80))
864 (host guix-publish-configuration-host ;string
865 (default "localhost")))
866
867 (define guix-publish-dmd-service
868 (match-lambda
869 (($ <guix-publish-configuration> guix port host)
870 (list (dmd-service
871 (provision '(guix-publish))
872 (requirement '(guix-daemon))
873 (start #~(make-forkexec-constructor
874 (list (string-append #$guix "/bin/guix")
875 "publish" "-u" "guix-publish"
876 "-p" #$(number->string port)
877 (string-append "--listen=" #$host))))
878 (stop #~(make-kill-destructor)))))))
879
880 (define %guix-publish-accounts
881 (list (user-group (name "guix-publish") (system? #t))
882 (user-account
883 (name "guix-publish")
884 (group "guix-publish")
885 (system? #t)
886 (comment "guix publish user")
887 (home-directory "/var/empty")
888 (shell #~(string-append #$shadow "/sbin/nologin")))))
889
890 (define guix-publish-service-type
891 (service-type (name 'guix-publish)
892 (extensions
893 (list (service-extension dmd-root-service-type
894 guix-publish-dmd-service)
895 (service-extension account-service-type
896 (const %guix-publish-accounts))))))
897
898 (define* (guix-publish-service #:key (guix guix) (port 80) (host "localhost"))
899 "Return a service that runs @command{guix publish} listening on @var{host}
900 and @var{port} (@pxref{Invoking guix publish}).
901
902 This assumes that @file{/etc/guix} already contains a signing key pair as
903 created by @command{guix archive --generate-key} (@pxref{Invoking guix
904 archive}). If that is not the case, the service will fail to start."
905 (service guix-publish-service-type
906 (guix-publish-configuration (guix guix) (port port) (host host))))
907
908 \f
909 ;;;
910 ;;; Udev.
911 ;;;
912
913 (define-record-type* <udev-configuration>
914 udev-configuration make-udev-configuration
915 udev-configuration?
916 (udev udev-configuration-udev ;<package>
917 (default udev))
918 (rules udev-configuration-rules ;list of <package>
919 (default '())))
920
921 (define (udev-rules-union packages)
922 "Return the union of the @code{lib/udev/rules.d} directories found in each
923 item of @var{packages}."
924 (define build
925 #~(begin
926 (use-modules (guix build union)
927 (guix build utils)
928 (srfi srfi-1)
929 (srfi srfi-26))
930
931 (define %standard-locations
932 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
933
934 (define (rules-sub-directory directory)
935 ;; Return the sub-directory of DIRECTORY containing udev rules, or
936 ;; #f if none was found.
937 (find directory-exists?
938 (map (cut string-append directory <>) %standard-locations)))
939
940 (mkdir-p (string-append #$output "/lib/udev"))
941 (union-build (string-append #$output "/lib/udev/rules.d")
942 (filter-map rules-sub-directory '#$packages))))
943
944 (computed-file "udev-rules" build
945 #:modules '((guix build union)
946 (guix build utils))))
947
948 (define* (kvm-udev-rule)
949 "Return a directory with a udev rule that changes the group of
950 @file{/dev/kvm} to \"kvm\" and makes it #o660."
951 ;; Apparently QEMU-KVM used to ship this rule, but now we have to add it by
952 ;; ourselves.
953 (computed-file "kvm-udev-rules"
954 #~(begin
955 (use-modules (guix build utils))
956
957 (define rules.d
958 (string-append #$output "/lib/udev/rules.d"))
959
960 (mkdir-p rules.d)
961 (call-with-output-file
962 (string-append rules.d "/90-kvm.rules")
963 (lambda (port)
964 ;; Build users are part of the "kvm" group, so we
965 ;; can fearlessly make /dev/kvm 660 (see
966 ;; <http://bugs.gnu.org/18994>, for background.)
967 (display "\
968 KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n" port))))
969 #:modules '((guix build utils))))
970
971 (define udev-dmd-service
972 ;; Return a <dmd-service> for UDEV with RULES.
973 (match-lambda
974 (($ <udev-configuration> udev rules)
975 (let* ((rules (udev-rules-union (cons* udev (kvm-udev-rule) rules)))
976 (udev.conf (computed-file "udev.conf"
977 #~(call-with-output-file #$output
978 (lambda (port)
979 (format port
980 "udev_rules=\"~a/lib/udev/rules.d\"\n"
981 #$rules))))))
982 (list
983 (dmd-service
984 (provision '(udev))
985
986 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
987 ;; be added: see
988 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
989 (requirement '(root-file-system))
990
991 (documentation "Populate the /dev directory, dynamically.")
992 (start #~(lambda ()
993 (define find
994 (@ (srfi srfi-1) find))
995
996 (define udevd
997 ;; Choose the right 'udevd'.
998 (find file-exists?
999 (map (lambda (suffix)
1000 (string-append #$udev suffix))
1001 '("/libexec/udev/udevd" ;udev
1002 "/sbin/udevd")))) ;eudev
1003
1004 (define (wait-for-udevd)
1005 ;; Wait until someone's listening on udevd's control
1006 ;; socket.
1007 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
1008 (let try ()
1009 (catch 'system-error
1010 (lambda ()
1011 (connect sock PF_UNIX "/run/udev/control")
1012 (close-port sock))
1013 (lambda args
1014 (format #t "waiting for udevd...~%")
1015 (usleep 500000)
1016 (try))))))
1017
1018 ;; Allow udev to find the modules.
1019 (setenv "LINUX_MODULE_DIRECTORY"
1020 "/run/booted-system/kernel/lib/modules")
1021
1022 ;; The first one is for udev, the second one for eudev.
1023 (setenv "UDEV_CONFIG_FILE" #$udev.conf)
1024 (setenv "EUDEV_RULES_DIRECTORY"
1025 (string-append #$rules "/lib/udev/rules.d"))
1026
1027 (let ((pid (primitive-fork)))
1028 (case pid
1029 ((0)
1030 (exec-command (list udevd)))
1031 (else
1032 ;; Wait until udevd is up and running. This
1033 ;; appears to be needed so that the events
1034 ;; triggered below are actually handled.
1035 (wait-for-udevd)
1036
1037 ;; Trigger device node creation.
1038 (system* (string-append #$udev "/bin/udevadm")
1039 "trigger" "--action=add")
1040
1041 ;; Wait for things to settle down.
1042 (system* (string-append #$udev "/bin/udevadm")
1043 "settle")
1044 pid)))))
1045 (stop #~(make-kill-destructor))
1046
1047 ;; When halting the system, 'udev' is actually killed by
1048 ;; 'user-processes', i.e., before its own 'stop' method was called.
1049 ;; Thus, make sure it is not respawned.
1050 (respawn? #f)))))))
1051
1052 (define udev-service-type
1053 (service-type (name 'udev)
1054 (extensions
1055 (list (service-extension dmd-root-service-type
1056 udev-dmd-service)))
1057
1058 (compose concatenate) ;concatenate the list of rules
1059 (extend (lambda (config rules)
1060 (match config
1061 (($ <udev-configuration> udev initial-rules)
1062 (udev-configuration
1063 (udev udev)
1064 (rules (append initial-rules rules)))))))))
1065
1066 (define* (udev-service #:key (udev eudev) (rules '()))
1067 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
1068 extra rules from the packages listed in @var{rules}."
1069 (service udev-service-type
1070 (udev-configuration (udev udev) (rules rules))))
1071
1072 (define device-mapping-service-type
1073 (dmd-service-type
1074 'device-mapping
1075 (match-lambda
1076 ((target open close)
1077 (dmd-service
1078 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
1079 (requirement '(udev))
1080 (documentation "Map a device node using Linux's device mapper.")
1081 (start #~(lambda () #$open))
1082 (stop #~(lambda _ (not #$close)))
1083 (respawn? #f))))))
1084
1085 (define (device-mapping-service target open close)
1086 "Return a service that maps device @var{target}, a string such as
1087 @code{\"home\"} (meaning @code{/dev/mapper/home}). Evaluate @var{open}, a
1088 gexp, to open it, and evaluate @var{close} to close it."
1089 (service device-mapping-service-type
1090 (list target open close)))
1091
1092 (define swap-service-type
1093 (dmd-service-type
1094 'swap
1095 (lambda (device)
1096 (define requirement
1097 (if (string-prefix? "/dev/mapper/" device)
1098 (list (symbol-append 'device-mapping-
1099 (string->symbol (basename device))))
1100 '()))
1101
1102 (dmd-service
1103 (provision (list (symbol-append 'swap- (string->symbol device))))
1104 (requirement `(udev ,@requirement))
1105 (documentation "Enable the given swap device.")
1106 (start #~(lambda ()
1107 (restart-on-EINTR (swapon #$device))
1108 #t))
1109 (stop #~(lambda _
1110 (restart-on-EINTR (swapoff #$device))
1111 #f))
1112 (respawn? #f)))))
1113
1114 (define (swap-service device)
1115 "Return a service that uses @var{device} as a swap device."
1116 (service swap-service-type device))
1117
1118
1119 (define-record-type* <gpm-configuration>
1120 gpm-configuration make-gpm-configuration gpm-configuration?
1121 (gpm gpm-configuration-gpm) ;package
1122 (options gpm-configuration-options)) ;list of strings
1123
1124 (define gpm-dmd-service
1125 (match-lambda
1126 (($ <gpm-configuration> dmd options)
1127 (list (dmd-service
1128 (requirement '(udev))
1129 (provision '(gpm))
1130 (start #~(lambda ()
1131 ;; 'gpm' runs in the background and sets a PID file.
1132 ;; Note that it requires running as "root".
1133 (false-if-exception (delete-file "/var/run/gpm.pid"))
1134 (fork+exec-command (list (string-append #$gpm "/sbin/gpm")
1135 #$@options))
1136
1137 ;; Wait for the PID file to appear; declare failure if
1138 ;; it doesn't show up.
1139 (let loop ((i 3))
1140 (or (file-exists? "/var/run/gpm.pid")
1141 (if (zero? i)
1142 #f
1143 (begin
1144 (sleep 1)
1145 (loop (1- i))))))))
1146
1147 (stop #~(lambda (_)
1148 ;; Return #f if successfully stopped.
1149 (not (zero? (system* (string-append #$gpm "/sbin/gpm")
1150 "-k"))))))))))
1151
1152 (define gpm-service-type
1153 (service-type (name 'gpm)
1154 (extensions
1155 (list (service-extension dmd-root-service-type
1156 gpm-dmd-service)))))
1157
1158 (define* (gpm-service #:key (gpm gpm)
1159 (options '("-m" "/dev/input/mice" "-t" "ps2")))
1160 "Run @var{gpm}, the general-purpose mouse daemon, with the given
1161 command-line @var{options}. GPM allows users to use the mouse in the console,
1162 notably to select, copy, and paste text. The default value of @var{options}
1163 uses the @code{ps2} protocol, which works for both USB and PS/2 mice.
1164
1165 This service is not part of @var{%base-services}."
1166 ;; To test in QEMU, use "-usbdevice mouse" and then, in the monitor, use
1167 ;; "info mice" and "mouse_set X" to use the right mouse.
1168 (service gpm-service-type
1169 (gpm-configuration (gpm gpm) (options options))))
1170
1171 \f
1172 (define %base-services
1173 ;; Convenience variable holding the basic services.
1174 (let ((motd (plain-file "motd" "
1175 This is the GNU operating system, welcome!\n\n")))
1176 (list (console-font-service "tty1")
1177 (console-font-service "tty2")
1178 (console-font-service "tty3")
1179 (console-font-service "tty4")
1180 (console-font-service "tty5")
1181 (console-font-service "tty6")
1182
1183 (mingetty-service (mingetty-configuration
1184 (tty "tty1") (motd motd)))
1185 (mingetty-service (mingetty-configuration
1186 (tty "tty2") (motd motd)))
1187 (mingetty-service (mingetty-configuration
1188 (tty "tty3") (motd motd)))
1189 (mingetty-service (mingetty-configuration
1190 (tty "tty4") (motd motd)))
1191 (mingetty-service (mingetty-configuration
1192 (tty "tty5") (motd motd)))
1193 (mingetty-service (mingetty-configuration
1194 (tty "tty6") (motd motd)))
1195
1196 (static-networking-service "lo" "127.0.0.1"
1197 #:provision '(loopback))
1198 (syslog-service)
1199 (guix-service)
1200 (nscd-service)
1201
1202 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
1203 ;; used, so enable them by default. The FUSE and ALSA rules are
1204 ;; less critical, but handy.
1205 (udev-service #:rules (list lvm2 fuse alsa-utils crda)))))
1206
1207 ;;; base.scm ends here