Merge branch 'staging' into core-updates
[jackhill/guix/guix.git] / gnu / services / base.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
4 ;;; Copyright © 2015, 2016 Mark H Weaver <mhw@netris.org>
5 ;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
6 ;;; Copyright © 2016 Leo Famulari <leo@famulari.name>
7 ;;; Copyright © 2016 David Craven <david@craven.ch>
8 ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
9 ;;;
10 ;;; This file is part of GNU Guix.
11 ;;;
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
16 ;;;
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24
25 (define-module (gnu services base)
26 #:use-module (guix store)
27 #:use-module (gnu services)
28 #:use-module (gnu services shepherd)
29 #:use-module (gnu services networking)
30 #:use-module (gnu system pam)
31 #:use-module (gnu system shadow) ; 'user-account', etc.
32 #:use-module (gnu system file-systems) ; 'file-system', etc.
33 #:use-module (gnu system mapped-devices)
34 #:use-module (gnu packages admin)
35 #:use-module ((gnu packages linux)
36 #:select (alsa-utils crda eudev e2fsprogs fuse gpm kbd lvm2 rng-tools))
37 #:use-module ((gnu packages base)
38 #:select (canonical-package glibc))
39 #:use-module (gnu packages bash)
40 #:use-module (gnu packages package-management)
41 #:use-module (gnu packages lsof)
42 #:use-module (gnu packages terminals)
43 #:use-module ((gnu build file-systems)
44 #:select (mount-flags->bit-mask))
45 #:use-module (guix gexp)
46 #:use-module (guix records)
47 #:use-module (srfi srfi-1)
48 #:use-module (srfi srfi-26)
49 #:use-module (ice-9 match)
50 #:use-module (ice-9 format)
51 #:export (fstab-service-type
52 root-file-system-service
53 file-system-service-type
54 user-unmount-service
55 swap-service
56 user-processes-service
57 session-environment-service
58 session-environment-service-type
59 host-name-service
60 console-keymap-service
61 %default-console-font
62 console-font-service-type
63 console-font-service
64
65 udev-configuration
66 udev-configuration?
67 udev-configuration-rules
68 udev-service-type
69 udev-service
70 udev-rule
71
72 login-configuration
73 login-configuration?
74 login-service-type
75 login-service
76
77 mingetty-configuration
78 mingetty-configuration?
79 mingetty-service
80 mingetty-service-type
81
82 %nscd-default-caches
83 %nscd-default-configuration
84
85 nscd-configuration
86 nscd-configuration?
87
88 nscd-cache
89 nscd-cache?
90
91 nscd-service-type
92 nscd-service
93
94 syslog-configuration
95 syslog-configuration?
96 syslog-service
97 syslog-service-type
98 %default-syslog.conf
99
100 %default-authorized-guix-keys
101 guix-configuration
102 guix-configuration?
103
104 guix-configuration-guix
105 guix-configuration-build-group
106 guix-configuration-build-accounts
107 guix-configuration-authorize-key?
108 guix-configuration-authorized-keys
109 guix-configuration-use-substitutes?
110 guix-configuration-substitute-urls
111 guix-configuration-extra-options
112 guix-configuration-log-file
113 guix-configuration-lsof
114
115 guix-service
116 guix-service-type
117 guix-publish-configuration
118 guix-publish-configuration?
119 guix-publish-service
120 guix-publish-service-type
121
122 gpm-configuration
123 gpm-configuration?
124 gpm-service-type
125 gpm-service
126
127 urandom-seed-service-type
128 urandom-seed-service
129
130 rngd-configuration
131 rngd-configuration?
132 rngd-service-type
133 rngd-service
134
135 kmscon-configuration
136 kmscon-configuration?
137 kmscon-service-type
138
139 pam-limits-service-type
140 pam-limits-service
141
142 %base-services))
143
144 ;;; Commentary:
145 ;;;
146 ;;; Base system services---i.e., services that 99% of the users will want to
147 ;;; use.
148 ;;;
149 ;;; Code:
150
151 \f
152 ;;;
153 ;;; File systems.
154 ;;;
155
156 (define (file-system->fstab-entry file-system)
157 "Return a @file{/etc/fstab} entry for @var{file-system}."
158 (string-append (case (file-system-title file-system)
159 ((label)
160 (string-append "LABEL=" (file-system-device file-system)))
161 ((uuid)
162 (string-append
163 "UUID="
164 (uuid->string (file-system-device file-system))))
165 (else
166 (file-system-device file-system)))
167 "\t"
168 (file-system-mount-point file-system) "\t"
169 (file-system-type file-system) "\t"
170 (or (file-system-options file-system) "defaults") "\t"
171
172 ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
173 ;; don't have anything sensible to put in there.
174 ))
175
176 (define (file-systems->fstab file-systems)
177 "Return a @file{/etc} entry for an @file{fstab} describing
178 @var{file-systems}."
179 `(("fstab" ,(plain-file "fstab"
180 (string-append
181 "\
182 # This file was generated from your GuixSD configuration. Any changes
183 # will be lost upon reboot or reconfiguration.\n\n"
184 (string-join (map file-system->fstab-entry
185 file-systems)
186 "\n")
187 "\n")))))
188
189 (define fstab-service-type
190 ;; The /etc/fstab service.
191 (service-type (name 'fstab)
192 (extensions
193 (list (service-extension etc-service-type
194 file-systems->fstab)))
195 (compose concatenate)
196 (extend append)))
197
198 (define %root-file-system-shepherd-service
199 (shepherd-service
200 (documentation "Take care of the root file system.")
201 (provision '(root-file-system))
202 (start #~(const #t))
203 (stop #~(lambda _
204 ;; Return #f if successfully stopped.
205 (sync)
206
207 (call-with-blocked-asyncs
208 (lambda ()
209 (let ((null (%make-void-port "w")))
210 ;; Close 'shepherd.log'.
211 (display "closing log\n")
212 ((@ (shepherd comm) stop-logging))
213
214 ;; Redirect the default output ports..
215 (set-current-output-port null)
216 (set-current-error-port null)
217
218 ;; Close /dev/console.
219 (for-each close-fdes '(0 1 2))
220
221 ;; At this point, there are no open files left, so the
222 ;; root file system can be re-mounted read-only.
223 (mount #f "/" #f
224 (logior MS_REMOUNT MS_RDONLY)
225 #:update-mtab? #f)
226
227 #f)))))
228 (respawn? #f)))
229
230 (define root-file-system-service-type
231 (shepherd-service-type 'root-file-system
232 (const %root-file-system-shepherd-service)))
233
234 (define (root-file-system-service)
235 "Return a service whose sole purpose is to re-mount read-only the root file
236 system upon shutdown (aka. cleanly \"umounting\" root.)
237
238 This service must be the root of the service dependency graph so that its
239 'stop' action is invoked when shepherd is the only process left."
240 (service root-file-system-service-type #f))
241
242 (define (file-system->shepherd-service-name file-system)
243 "Return the symbol that denotes the service mounting and unmounting
244 FILE-SYSTEM."
245 (symbol-append 'file-system-
246 (string->symbol (file-system-mount-point file-system))))
247
248 (define (mapped-device->shepherd-service-name md)
249 "Return the symbol that denotes the shepherd service of MD, a <mapped-device>."
250 (symbol-append 'device-mapping-
251 (string->symbol (mapped-device-target md))))
252
253 (define dependency->shepherd-service-name
254 (match-lambda
255 ((? mapped-device? md)
256 (mapped-device->shepherd-service-name md))
257 ((? file-system? fs)
258 (file-system->shepherd-service-name fs))))
259
260 (define (file-system-shepherd-service file-system)
261 "Return the shepherd service for @var{file-system}, or @code{#f} if
262 @var{file-system} is not auto-mounted upon boot."
263 (let ((target (file-system-mount-point file-system))
264 (device (file-system-device file-system))
265 (type (file-system-type file-system))
266 (title (file-system-title file-system))
267 (flags (file-system-flags file-system))
268 (options (file-system-options file-system))
269 (check? (file-system-check? file-system))
270 (create? (file-system-create-mount-point? file-system))
271 (dependencies (file-system-dependencies file-system)))
272 (and (file-system-mount? file-system)
273 (with-imported-modules '((gnu build file-systems)
274 (guix build bournish))
275 (shepherd-service
276 (provision (list (file-system->shepherd-service-name file-system)))
277 (requirement `(root-file-system
278 ,@(map dependency->shepherd-service-name dependencies)))
279 (documentation "Check, mount, and unmount the given file system.")
280 (start #~(lambda args
281 #$(if create?
282 #~(mkdir-p #$target)
283 #t)
284
285 (let (($PATH (getenv "PATH")))
286 ;; Make sure fsck.ext2 & co. can be found.
287 (dynamic-wind
288 (lambda ()
289 (setenv "PATH"
290 (string-append
291 #$e2fsprogs "/sbin:"
292 "/run/current-system/profile/sbin:"
293 $PATH)))
294 (lambda ()
295 (mount-file-system
296 `(#$device #$title #$target #$type #$flags
297 #$options #$check?)
298 #:root "/"))
299 (lambda ()
300 (setenv "PATH" $PATH)))
301 #t)))
302 (stop #~(lambda args
303 ;; Normally there are no processes left at this point, so
304 ;; TARGET can be safely unmounted.
305
306 ;; Make sure PID 1 doesn't keep TARGET busy.
307 (chdir "/")
308
309 (umount #$target)
310 #f))
311
312 ;; We need an additional module.
313 (modules `(((gnu build file-systems)
314 #:select (mount-file-system))
315 ,@%default-modules)))))))
316
317 (define (file-system-shepherd-services file-systems)
318 "Return the list of Shepherd services for FILE-SYSTEMS."
319 (let* ((file-systems (filter file-system-mount? file-systems)))
320 (define sink
321 (shepherd-service
322 (provision '(file-systems))
323 (requirement (cons* 'root-file-system 'user-file-systems
324 (map file-system->shepherd-service-name
325 file-systems)))
326 (documentation "Target for all the initially-mounted file systems")
327 (start #~(const #t))
328 (stop #~(const #f))))
329
330 (cons sink (map file-system-shepherd-service file-systems))))
331
332 (define file-system-service-type
333 (service-type (name 'file-systems)
334 (extensions
335 (list (service-extension shepherd-root-service-type
336 file-system-shepherd-services)
337 (service-extension fstab-service-type
338 identity)))
339 (compose concatenate)
340 (extend append)))
341
342 (define user-unmount-service-type
343 (shepherd-service-type
344 'user-file-systems
345 (lambda (known-mount-points)
346 (shepherd-service
347 (documentation "Unmount manually-mounted file systems.")
348 (provision '(user-file-systems))
349 (start #~(const #t))
350 (stop #~(lambda args
351 (define (known? mount-point)
352 (member mount-point
353 (cons* "/proc" "/sys" '#$known-mount-points)))
354
355 ;; Make sure we don't keep the user's mount points busy.
356 (chdir "/")
357
358 (for-each (lambda (mount-point)
359 (format #t "unmounting '~a'...~%" mount-point)
360 (catch 'system-error
361 (lambda ()
362 (umount mount-point))
363 (lambda args
364 (let ((errno (system-error-errno args)))
365 (format #t "failed to unmount '~a': ~a~%"
366 mount-point (strerror errno))))))
367 (filter (negate known?) (mount-points)))
368 #f))))))
369
370 (define (user-unmount-service known-mount-points)
371 "Return a service whose sole purpose is to unmount file systems not listed
372 in KNOWN-MOUNT-POINTS when it is stopped."
373 (service user-unmount-service-type known-mount-points))
374
375 (define %do-not-kill-file
376 ;; Name of the file listing PIDs of processes that must survive when halting
377 ;; the system. Typical example is user-space file systems.
378 "/etc/shepherd/do-not-kill")
379
380 (define user-processes-service-type
381 (shepherd-service-type
382 'user-processes
383 (lambda (grace-delay)
384 (shepherd-service
385 (documentation "When stopped, terminate all user processes.")
386 (provision '(user-processes))
387 (requirement '(file-systems))
388 (start #~(const #t))
389 (stop #~(lambda _
390 (define (kill-except omit signal)
391 ;; Kill all the processes with SIGNAL except those listed
392 ;; in OMIT and the current process.
393 (let ((omit (cons (getpid) omit)))
394 (for-each (lambda (pid)
395 (unless (memv pid omit)
396 (false-if-exception
397 (kill pid signal))))
398 (processes))))
399
400 (define omitted-pids
401 ;; List of PIDs that must not be killed.
402 (if (file-exists? #$%do-not-kill-file)
403 (map string->number
404 (call-with-input-file #$%do-not-kill-file
405 (compose string-tokenize
406 (@ (ice-9 rdelim) read-string))))
407 '()))
408
409 (define (now)
410 (car (gettimeofday)))
411
412 (define (sleep* n)
413 ;; Really sleep N seconds.
414 ;; Work around <http://bugs.gnu.org/19581>.
415 (define start (now))
416 (let loop ((elapsed 0))
417 (when (> n elapsed)
418 (sleep (- n elapsed))
419 (loop (- (now) start)))))
420
421 (define lset= (@ (srfi srfi-1) lset=))
422
423 (display "sending all processes the TERM signal\n")
424
425 (if (null? omitted-pids)
426 (begin
427 ;; Easy: terminate all of them.
428 (kill -1 SIGTERM)
429 (sleep* #$grace-delay)
430 (kill -1 SIGKILL))
431 (begin
432 ;; Kill them all except OMITTED-PIDS. XXX: We would
433 ;; like to (kill -1 SIGSTOP) to get a fixed list of
434 ;; processes, like 'killall5' does, but that seems
435 ;; unreliable.
436 (kill-except omitted-pids SIGTERM)
437 (sleep* #$grace-delay)
438 (kill-except omitted-pids SIGKILL)
439 (delete-file #$%do-not-kill-file)))
440
441 (let wait ()
442 (let ((pids (processes)))
443 (unless (lset= = pids (cons 1 omitted-pids))
444 (format #t "waiting for process termination\
445 (processes left: ~s)~%"
446 pids)
447 (sleep* 2)
448 (wait))))
449
450 (display "all processes have been terminated\n")
451 #f))
452 (respawn? #f)))))
453
454 (define* (user-processes-service #:key (grace-delay 4))
455 "Return the service that is responsible for terminating all the processes so
456 that the root file system can be re-mounted read-only, just before
457 rebooting/halting. Processes still running GRACE-DELAY seconds after SIGTERM
458 has been sent are terminated with SIGKILL.
459
460 The returned service will depend on 'file-systems', meaning that it is
461 considered started after all the auto-mount file systems have been mounted.
462
463 All the services that spawn processes must depend on this one so that they are
464 stopped before 'kill' is called."
465 (service user-processes-service-type grace-delay))
466
467 \f
468 ;;;
469 ;;; Preserve entropy to seed /dev/urandom on boot.
470 ;;;
471
472 (define %random-seed-file
473 "/var/lib/random-seed")
474
475 (define (urandom-seed-shepherd-service _)
476 "Return a shepherd service for the /dev/urandom seed."
477 (list (shepherd-service
478 (documentation "Preserve entropy across reboots for /dev/urandom.")
479 (provision '(urandom-seed))
480 (requirement '(user-processes))
481 (start #~(lambda _
482 ;; On boot, write random seed into /dev/urandom.
483 (when (file-exists? #$%random-seed-file)
484 (call-with-input-file #$%random-seed-file
485 (lambda (seed)
486 (call-with-output-file "/dev/urandom"
487 (lambda (urandom)
488 (dump-port seed urandom))))))
489 ;; Immediately refresh the seed in case the system doesn't
490 ;; shut down cleanly.
491 (call-with-input-file "/dev/urandom"
492 (lambda (urandom)
493 (let ((previous-umask (umask #o077))
494 (buf (make-bytevector 512)))
495 (mkdir-p (dirname #$%random-seed-file))
496 (get-bytevector-n! urandom buf 0 512)
497 (call-with-output-file #$%random-seed-file
498 (lambda (seed)
499 (put-bytevector seed buf)))
500 (umask previous-umask))))
501 #t))
502 (stop #~(lambda _
503 ;; During shutdown, write from /dev/urandom into random seed.
504 (let ((buf (make-bytevector 512)))
505 (call-with-input-file "/dev/urandom"
506 (lambda (urandom)
507 (let ((previous-umask (umask #o077)))
508 (get-bytevector-n! urandom buf 0 512)
509 (mkdir-p (dirname #$%random-seed-file))
510 (call-with-output-file #$%random-seed-file
511 (lambda (seed)
512 (put-bytevector seed buf)))
513 (umask previous-umask))
514 #t)))))
515 (modules `((rnrs bytevectors)
516 (rnrs io ports)
517 ,@%default-modules)))))
518
519 (define urandom-seed-service-type
520 (service-type (name 'urandom-seed)
521 (extensions
522 (list (service-extension shepherd-root-service-type
523 urandom-seed-shepherd-service)))))
524
525 (define (urandom-seed-service)
526 (service urandom-seed-service-type #f))
527
528
529 ;;;
530 ;;; Add hardware random number generator to entropy pool.
531 ;;;
532
533 (define-record-type* <rngd-configuration>
534 rngd-configuration make-rngd-configuration
535 rngd-configuration?
536 (rng-tools rngd-configuration-rng-tools) ;package
537 (device rngd-configuration-device)) ;string
538
539 (define rngd-service-type
540 (shepherd-service-type
541 'rngd
542 (lambda (config)
543 (define rng-tools (rngd-configuration-rng-tools config))
544 (define device (rngd-configuration-device config))
545
546 (define rngd-command
547 (list (file-append rng-tools "/sbin/rngd")
548 "-f" "-r" device))
549
550 (shepherd-service
551 (documentation "Add TRNG to entropy pool.")
552 (requirement '(udev))
553 (provision '(trng))
554 (start #~(make-forkexec-constructor #$@rngd-command))
555 (stop #~(make-kill-destructor))))))
556
557 (define* (rngd-service #:key
558 (rng-tools rng-tools)
559 (device "/dev/hwrng"))
560 "Return a service that runs the @command{rngd} program from @var{rng-tools}
561 to add @var{device} to the kernel's entropy pool. The service will fail if
562 @var{device} does not exist."
563 (service rngd-service-type
564 (rngd-configuration
565 (rng-tools rng-tools)
566 (device device))))
567
568
569 ;;;
570 ;;; System-wide environment variables.
571 ;;;
572
573 (define (environment-variables->environment-file vars)
574 "Return a file for pam_env(8) that contains environment variables VARS."
575 (apply mixed-text-file "environment"
576 (append-map (match-lambda
577 ((key . value)
578 (list key "=" value "\n")))
579 vars)))
580
581 (define session-environment-service-type
582 (service-type
583 (name 'session-environment)
584 (extensions
585 (list (service-extension
586 etc-service-type
587 (lambda (vars)
588 (list `("environment"
589 ,(environment-variables->environment-file vars)))))))
590 (compose concatenate)
591 (extend append)))
592
593 (define (session-environment-service vars)
594 "Return a service that builds the @file{/etc/environment}, which can be read
595 by PAM-aware applications to set environment variables for sessions.
596
597 VARS should be an association list in which both the keys and the values are
598 strings or string-valued gexps."
599 (service session-environment-service-type vars))
600
601 \f
602 ;;;
603 ;;; Console & co.
604 ;;;
605
606 (define host-name-service-type
607 (shepherd-service-type
608 'host-name
609 (lambda (name)
610 (shepherd-service
611 (documentation "Initialize the machine's host name.")
612 (provision '(host-name))
613 (start #~(lambda _
614 (sethostname #$name)))
615 (respawn? #f)))))
616
617 (define (host-name-service name)
618 "Return a service that sets the host name to @var{name}."
619 (service host-name-service-type name))
620
621 (define (unicode-start tty)
622 "Return a gexp to start Unicode support on @var{tty}."
623
624 ;; We have to run 'unicode_start' in a pipe so that when it invokes the
625 ;; 'tty' command, that command returns TTY.
626 #~(begin
627 (let ((pid (primitive-fork)))
628 (case pid
629 ((0)
630 (close-fdes 0)
631 (dup2 (open-fdes #$tty O_RDONLY) 0)
632 (close-fdes 1)
633 (dup2 (open-fdes #$tty O_WRONLY) 1)
634 (execl #$(file-append kbd "/bin/unicode_start")
635 "unicode_start"))
636 (else
637 (zero? (cdr (waitpid pid))))))))
638
639 (define console-keymap-service-type
640 (shepherd-service-type
641 'console-keymap
642 (lambda (files)
643 (shepherd-service
644 (documentation (string-append "Load console keymap (loadkeys)."))
645 (provision '(console-keymap))
646 (start #~(lambda _
647 (zero? (system* #$(file-append kbd "/bin/loadkeys")
648 #$@files))))
649 (respawn? #f)))))
650
651 (define (console-keymap-service . files)
652 "Return a service to load console keymaps from @var{files}."
653 (service console-keymap-service-type files))
654
655 (define %default-console-font
656 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
657 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
658 ;; codepoints notably found in the UTF-8 manual.
659 "LatGrkCyr-8x16")
660
661 (define (console-font-shepherd-services tty+font)
662 "Return a list of Shepherd services for each pair in TTY+FONT."
663 (map (match-lambda
664 ((tty . font)
665 (let ((device (string-append "/dev/" tty)))
666 (shepherd-service
667 (documentation "Load a Unicode console font.")
668 (provision (list (symbol-append 'console-font-
669 (string->symbol tty))))
670
671 ;; Start after mingetty has been started on TTY, otherwise the settings
672 ;; are ignored.
673 (requirement (list (symbol-append 'term-
674 (string->symbol tty))))
675
676 (start #~(lambda _
677 (and #$(unicode-start device)
678 (zero?
679 (system* #$(file-append kbd "/bin/setfont")
680 "-C" #$device #$font)))))
681 (stop #~(const #t))
682 (respawn? #f)))))
683 tty+font))
684
685 (define console-font-service-type
686 (service-type (name 'console-fonts)
687 (extensions
688 (list (service-extension shepherd-root-service-type
689 console-font-shepherd-services)))
690 (compose concatenate)
691 (extend append)))
692
693 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
694 "This procedure is deprecated in favor of @code{console-font-service-type}.
695
696 Return a service that sets up Unicode support in @var{tty} and loads
697 @var{font} for that tty (fonts are per virtual console in Linux.)"
698 (simple-service (symbol-append 'console-font- (string->symbol tty))
699 console-font-service-type `((,tty . ,font))))
700
701 (define %default-motd
702 (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
703
704 (define-record-type* <login-configuration>
705 login-configuration make-login-configuration
706 login-configuration?
707 (motd login-configuration-motd ;file-like
708 (default %default-motd))
709 ;; Allow empty passwords by default so that first-time users can log in when
710 ;; the 'root' account has just been created.
711 (allow-empty-passwords? login-configuration-allow-empty-passwords?
712 (default #t))) ;Boolean
713
714 (define (login-pam-service config)
715 "Return the list of PAM service needed for CONF."
716 ;; Let 'login' be known to PAM.
717 (list (unix-pam-service "login"
718 #:allow-empty-passwords?
719 (login-configuration-allow-empty-passwords? config)
720 #:motd
721 (login-configuration-motd config))))
722
723 (define login-service-type
724 (service-type (name 'login)
725 (extensions (list (service-extension pam-root-service-type
726 login-pam-service)))))
727
728 (define* (login-service #:optional (config (login-configuration)))
729 "Return a service configure login according to @var{config}, which specifies
730 the message of the day, among other things."
731 (service login-service-type config))
732
733 (define-record-type* <mingetty-configuration>
734 mingetty-configuration make-mingetty-configuration
735 mingetty-configuration?
736 (mingetty mingetty-configuration-mingetty ;<package>
737 (default mingetty))
738 (tty mingetty-configuration-tty) ;string
739 (auto-login mingetty-auto-login ;string | #f
740 (default #f))
741 (login-program mingetty-login-program ;gexp
742 (default #f))
743 (login-pause? mingetty-login-pause? ;Boolean
744 (default #f)))
745
746 (define mingetty-shepherd-service
747 (match-lambda
748 (($ <mingetty-configuration> mingetty tty auto-login login-program
749 login-pause?)
750 (list
751 (shepherd-service
752 (documentation "Run mingetty on an tty.")
753 (provision (list (symbol-append 'term- (string->symbol tty))))
754
755 ;; Since the login prompt shows the host name, wait for the 'host-name'
756 ;; service to be done. Also wait for udev essentially so that the tty
757 ;; text is not lost in the middle of kernel messages (XXX).
758 (requirement '(user-processes host-name udev))
759
760 (start #~(make-forkexec-constructor
761 (list #$(file-append mingetty "/sbin/mingetty")
762 "--noclear" #$tty
763 #$@(if auto-login
764 #~("--autologin" #$auto-login)
765 #~())
766 #$@(if login-program
767 #~("--loginprog" #$login-program)
768 #~())
769 #$@(if login-pause?
770 #~("--loginpause")
771 #~()))))
772 (stop #~(make-kill-destructor)))))))
773
774 (define mingetty-service-type
775 (service-type (name 'mingetty)
776 (extensions (list (service-extension shepherd-root-service-type
777 mingetty-shepherd-service)))))
778
779 (define* (mingetty-service config)
780 "Return a service to run mingetty according to @var{config}, which specifies
781 the tty to run, among other things."
782 (service mingetty-service-type config))
783
784 (define-record-type* <nscd-configuration> nscd-configuration
785 make-nscd-configuration
786 nscd-configuration?
787 (log-file nscd-configuration-log-file ;string
788 (default "/var/log/nscd.log"))
789 (debug-level nscd-debug-level ;integer
790 (default 0))
791 ;; TODO: See nscd.conf in glibc for other options to add.
792 (caches nscd-configuration-caches ;list of <nscd-cache>
793 (default %nscd-default-caches))
794 (name-services nscd-configuration-name-services ;list of <packages>
795 (default '()))
796 (glibc nscd-configuration-glibc ;<package>
797 (default (canonical-package glibc))))
798
799 (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
800 nscd-cache?
801 (database nscd-cache-database) ;symbol
802 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
803 (negative-time-to-live nscd-cache-negative-time-to-live
804 (default 20)) ;integer
805 (suggested-size nscd-cache-suggested-size ;integer ("default module
806 ;of hash table")
807 (default 211))
808 (check-files? nscd-cache-check-files? ;Boolean
809 (default #t))
810 (persistent? nscd-cache-persistent? ;Boolean
811 (default #t))
812 (shared? nscd-cache-shared? ;Boolean
813 (default #t))
814 (max-database-size nscd-cache-max-database-size ;integer
815 (default (* 32 (expt 2 20))))
816 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
817 (default #t)))
818
819 (define %nscd-default-caches
820 ;; Caches that we want to enable by default. Note that when providing an
821 ;; empty nscd.conf, all caches are disabled.
822 (list (nscd-cache (database 'hosts)
823
824 ;; Aggressively cache the host name cache to improve
825 ;; privacy and resilience.
826 (positive-time-to-live (* 3600 12))
827 (negative-time-to-live 20)
828 (persistent? #t))
829
830 (nscd-cache (database 'services)
831
832 ;; Services are unlikely to change, so we can be even more
833 ;; aggressive.
834 (positive-time-to-live (* 3600 24))
835 (negative-time-to-live 3600)
836 (check-files? #t) ;check /etc/services changes
837 (persistent? #t))))
838
839 (define %nscd-default-configuration
840 ;; Default nscd configuration.
841 (nscd-configuration))
842
843 (define (nscd.conf-file config)
844 "Return the @file{nscd.conf} configuration file for @var{config}, an
845 @code{<nscd-configuration>} object."
846 (define cache->config
847 (match-lambda
848 (($ <nscd-cache> (= symbol->string database)
849 positive-ttl negative-ttl size check-files?
850 persistent? shared? max-size propagate?)
851 (string-append "\nenable-cache\t" database "\tyes\n"
852
853 "positive-time-to-live\t" database "\t"
854 (number->string positive-ttl) "\n"
855 "negative-time-to-live\t" database "\t"
856 (number->string negative-ttl) "\n"
857 "suggested-size\t" database "\t"
858 (number->string size) "\n"
859 "check-files\t" database "\t"
860 (if check-files? "yes\n" "no\n")
861 "persistent\t" database "\t"
862 (if persistent? "yes\n" "no\n")
863 "shared\t" database "\t"
864 (if shared? "yes\n" "no\n")
865 "max-db-size\t" database "\t"
866 (number->string max-size) "\n"
867 "auto-propagate\t" database "\t"
868 (if propagate? "yes\n" "no\n")))))
869
870 (match config
871 (($ <nscd-configuration> log-file debug-level caches)
872 (plain-file "nscd.conf"
873 (string-append "\
874 # Configuration of libc's name service cache daemon (nscd).\n\n"
875 (if log-file
876 (string-append "logfile\t" log-file)
877 "")
878 "\n"
879 (if debug-level
880 (string-append "debug-level\t"
881 (number->string debug-level))
882 "")
883 "\n"
884 (string-concatenate
885 (map cache->config caches)))))))
886
887 (define (nscd-shepherd-service config)
888 "Return a shepherd service for CONFIG, an <nscd-configuration> object."
889 (let ((nscd.conf (nscd.conf-file config))
890 (name-services (nscd-configuration-name-services config)))
891 (list (shepherd-service
892 (documentation "Run libc's name service cache daemon (nscd).")
893 (provision '(nscd))
894 (requirement '(user-processes))
895 (start #~(make-forkexec-constructor
896 (list #$(file-append (nscd-configuration-glibc config)
897 "/sbin/nscd")
898 "-f" #$nscd.conf "--foreground")
899
900 ;; Wait for the PID file. However, the PID file is
901 ;; written before nscd is actually listening on its
902 ;; socket (XXX).
903 #:pid-file "/var/run/nscd/nscd.pid"
904
905 #:environment-variables
906 (list (string-append "LD_LIBRARY_PATH="
907 (string-join
908 (map (lambda (dir)
909 (string-append dir "/lib"))
910 (list #$@name-services))
911 ":")))))
912 (stop #~(make-kill-destructor))))))
913
914 (define nscd-activation
915 ;; Actions to take before starting nscd.
916 #~(begin
917 (use-modules (guix build utils))
918 (mkdir-p "/var/run/nscd")
919 (mkdir-p "/var/db/nscd"))) ;for the persistent cache
920
921 (define nscd-service-type
922 (service-type (name 'nscd)
923 (extensions
924 (list (service-extension activation-service-type
925 (const nscd-activation))
926 (service-extension shepherd-root-service-type
927 nscd-shepherd-service)))
928
929 ;; This can be extended by providing additional name services
930 ;; such as nss-mdns.
931 (compose concatenate)
932 (extend (lambda (config name-services)
933 (nscd-configuration
934 (inherit config)
935 (name-services (append
936 (nscd-configuration-name-services config)
937 name-services)))))))
938
939 (define* (nscd-service #:optional (config %nscd-default-configuration))
940 "Return a service that runs libc's name service cache daemon (nscd) with the
941 given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
942 Service Switch}, for an example."
943 (service nscd-service-type config))
944
945
946 (define-record-type* <syslog-configuration>
947 syslog-configuration make-syslog-configuration
948 syslog-configuration?
949 (syslogd syslog-configuration-syslogd
950 (default (file-append inetutils "/libexec/syslogd")))
951 (config-file syslog-configuration-config-file
952 (default %default-syslog.conf)))
953
954 (define syslog-service-type
955 (shepherd-service-type
956 'syslog
957 (lambda (config)
958 (shepherd-service
959 (documentation "Run the syslog daemon (syslogd).")
960 (provision '(syslogd))
961 (requirement '(user-processes))
962 (start #~(make-forkexec-constructor
963 (list #$(syslog-configuration-syslogd config)
964 "--rcfile" #$(syslog-configuration-config-file config))
965 #:pid-file "/var/run/syslog.pid"))
966 (stop #~(make-kill-destructor))))))
967
968 ;; Snippet adapted from the GNU inetutils manual.
969 (define %default-syslog.conf
970 (plain-file "syslog.conf" "
971 # Log all error messages, authentication messages of
972 # level notice or higher and anything of level err or
973 # higher to the console.
974 # Don't log private authentication messages!
975 *.alert;auth.notice;authpriv.none /dev/console
976
977 # Log anything (except mail) of level info or higher.
978 # Don't log private authentication messages!
979 *.info;mail.none;authpriv.none /var/log/messages
980
981 # Same, in a different place.
982 *.info;mail.none;authpriv.none /dev/tty12
983
984 # The authpriv file has restricted access.
985 authpriv.* /var/log/secure
986
987 # Log all the mail messages in one place.
988 mail.* /var/log/maillog
989 "))
990
991 (define* (syslog-service #:optional (config (syslog-configuration)))
992 "Return a service that runs @command{syslogd} and takes
993 @var{<syslog-configuration>} as a parameter.
994
995 @xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
996 information on the configuration file syntax."
997 (service syslog-service-type config))
998
999
1000 (define pam-limits-service-type
1001 (let ((security-limits
1002 ;; Create /etc/security containing the provided "limits.conf" file.
1003 (lambda (limits-file)
1004 `(("security"
1005 ,(computed-file
1006 "security"
1007 #~(begin
1008 (mkdir #$output)
1009 (stat #$limits-file)
1010 (symlink #$limits-file
1011 (string-append #$output "/limits.conf"))))))))
1012 (pam-extension
1013 (lambda (pam)
1014 (let ((pam-limits (pam-entry
1015 (control "required")
1016 (module "pam_limits.so")
1017 (arguments '("conf=/etc/security/limits.conf")))))
1018 (if (member (pam-service-name pam)
1019 '("login" "su" "slim"))
1020 (pam-service
1021 (inherit pam)
1022 (session (cons pam-limits
1023 (pam-service-session pam))))
1024 pam)))))
1025 (service-type
1026 (name 'limits)
1027 (extensions
1028 (list (service-extension etc-service-type security-limits)
1029 (service-extension pam-root-service-type
1030 (lambda _ (list pam-extension))))))))
1031
1032 (define* (pam-limits-service #:optional (limits '()))
1033 "Return a service that makes selected programs respect the list of
1034 pam-limits-entry specified in LIMITS via pam_limits.so."
1035 (service pam-limits-service-type
1036 (plain-file "limits.conf"
1037 (string-join (map pam-limits-entry->string limits)
1038 "\n"))))
1039
1040 \f
1041 ;;;
1042 ;;; Guix services.
1043 ;;;
1044
1045 (define* (guix-build-accounts count #:key
1046 (group "guixbuild")
1047 (first-uid 30001)
1048 (shadow shadow))
1049 "Return a list of COUNT user accounts for Guix build users, with UIDs
1050 starting at FIRST-UID, and under GID."
1051 (unfold (cut > <> count)
1052 (lambda (n)
1053 (user-account
1054 (name (format #f "guixbuilder~2,'0d" n))
1055 (system? #t)
1056 (uid (+ first-uid n -1))
1057 (group group)
1058
1059 ;; guix-daemon expects GROUP to be listed as a
1060 ;; supplementary group too:
1061 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
1062 (supplementary-groups (list group "kvm"))
1063
1064 (comment (format #f "Guix Build User ~2d" n))
1065 (home-directory "/var/empty")
1066 (shell (file-append shadow "/sbin/nologin"))))
1067 1+
1068 1))
1069
1070 (define (hydra-key-authorization key guix)
1071 "Return a gexp with code to register KEY, a file containing a 'guix archive'
1072 public key, with GUIX."
1073 #~(unless (file-exists? "/etc/guix/acl")
1074 (let ((pid (primitive-fork)))
1075 (case pid
1076 ((0)
1077 (let* ((key #$key)
1078 (port (open-file key "r0b")))
1079 (format #t "registering public key '~a'...~%" key)
1080 (close-port (current-input-port))
1081 (dup port 0)
1082 (execl #$(file-append guix "/bin/guix")
1083 "guix" "archive" "--authorize")
1084 (exit 1)))
1085 (else
1086 (let ((status (cdr (waitpid pid))))
1087 (unless (zero? status)
1088 (format (current-error-port) "warning: \
1089 failed to register hydra.gnu.org public key: ~a~%" status))))))))
1090
1091 (define %default-authorized-guix-keys
1092 ;; List of authorized substitute keys.
1093 (list (file-append guix "/share/guix/hydra.gnu.org.pub")))
1094
1095 (define-record-type* <guix-configuration>
1096 guix-configuration make-guix-configuration
1097 guix-configuration?
1098 (guix guix-configuration-guix ;<package>
1099 (default guix))
1100 (build-group guix-configuration-build-group ;string
1101 (default "guixbuild"))
1102 (build-accounts guix-configuration-build-accounts ;integer
1103 (default 10))
1104 (authorize-key? guix-configuration-authorize-key? ;Boolean
1105 (default #t))
1106 (authorized-keys guix-configuration-authorized-keys ;list of gexps
1107 (default %default-authorized-guix-keys))
1108 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
1109 (default #t))
1110 (substitute-urls guix-configuration-substitute-urls ;list of strings
1111 (default %default-substitute-urls))
1112 (extra-options guix-configuration-extra-options ;list of strings
1113 (default '()))
1114 (log-file guix-configuration-log-file ;string
1115 (default "/var/log/guix-daemon.log"))
1116 (lsof guix-configuration-lsof ;<package>
1117 (default lsof)))
1118
1119 (define %default-guix-configuration
1120 (guix-configuration))
1121
1122 (define (guix-shepherd-service config)
1123 "Return a <shepherd-service> for the Guix daemon service with CONFIG."
1124 (match config
1125 (($ <guix-configuration> guix build-group build-accounts
1126 authorize-key? keys
1127 use-substitutes? substitute-urls extra-options
1128 log-file lsof)
1129 (list (shepherd-service
1130 (documentation "Run the Guix daemon.")
1131 (provision '(guix-daemon))
1132 (requirement '(user-processes))
1133 (start
1134 #~(make-forkexec-constructor
1135 (list #$(file-append guix "/bin/guix-daemon")
1136 "--build-users-group" #$build-group
1137 #$@(if use-substitutes?
1138 '()
1139 '("--no-substitutes"))
1140 "--substitute-urls" #$(string-join substitute-urls)
1141 #$@extra-options)
1142
1143 ;; Add 'lsof' (for the GC) to the daemon's $PATH.
1144 #:environment-variables
1145 (list (string-append "PATH=" #$lsof "/bin"))
1146
1147 #:log-file #$log-file))
1148 (stop #~(make-kill-destructor)))))))
1149
1150 (define (guix-accounts config)
1151 "Return the user accounts and user groups for CONFIG."
1152 (match config
1153 (($ <guix-configuration> _ build-group build-accounts)
1154 (cons (user-group
1155 (name build-group)
1156 (system? #t)
1157
1158 ;; Use a fixed GID so that we can create the store with the right
1159 ;; owner.
1160 (id 30000))
1161 (guix-build-accounts build-accounts
1162 #:group build-group)))))
1163
1164 (define (guix-activation config)
1165 "Return the activation gexp for CONFIG."
1166 (match config
1167 (($ <guix-configuration> guix build-group build-accounts authorize-key? keys)
1168 ;; Assume that the store has BUILD-GROUP as its group. We could
1169 ;; otherwise call 'chown' here, but the problem is that on a COW unionfs,
1170 ;; chown leads to an entire copy of the tree, which is a bad idea.
1171
1172 ;; Optionally authorize hydra.gnu.org's key.
1173 (if authorize-key?
1174 #~(begin
1175 #$@(map (cut hydra-key-authorization <> guix) keys))
1176 #~#f))))
1177
1178 (define guix-service-type
1179 (service-type
1180 (name 'guix)
1181 (extensions
1182 (list (service-extension shepherd-root-service-type guix-shepherd-service)
1183 (service-extension account-service-type guix-accounts)
1184 (service-extension activation-service-type guix-activation)
1185 (service-extension profile-service-type
1186 (compose list guix-configuration-guix))))))
1187
1188 (define* (guix-service #:optional (config %default-guix-configuration))
1189 "Return a service that runs the Guix build daemon according to
1190 @var{config}."
1191 (service guix-service-type config))
1192
1193
1194 (define-record-type* <guix-publish-configuration>
1195 guix-publish-configuration make-guix-publish-configuration
1196 guix-publish-configuration?
1197 (guix guix-publish-configuration-guix ;package
1198 (default guix))
1199 (port guix-publish-configuration-port ;number
1200 (default 80))
1201 (host guix-publish-configuration-host ;string
1202 (default "localhost")))
1203
1204 (define guix-publish-shepherd-service
1205 (match-lambda
1206 (($ <guix-publish-configuration> guix port host)
1207 (list (shepherd-service
1208 (provision '(guix-publish))
1209 (requirement '(guix-daemon))
1210 (start #~(make-forkexec-constructor
1211 (list #$(file-append guix "/bin/guix")
1212 "publish" "-u" "guix-publish"
1213 "-p" #$(number->string port)
1214 (string-append "--listen=" #$host))))
1215 (stop #~(make-kill-destructor)))))))
1216
1217 (define %guix-publish-accounts
1218 (list (user-group (name "guix-publish") (system? #t))
1219 (user-account
1220 (name "guix-publish")
1221 (group "guix-publish")
1222 (system? #t)
1223 (comment "guix publish user")
1224 (home-directory "/var/empty")
1225 (shell (file-append shadow "/sbin/nologin")))))
1226
1227 (define guix-publish-service-type
1228 (service-type (name 'guix-publish)
1229 (extensions
1230 (list (service-extension shepherd-root-service-type
1231 guix-publish-shepherd-service)
1232 (service-extension account-service-type
1233 (const %guix-publish-accounts))))))
1234
1235 (define* (guix-publish-service #:key (guix guix) (port 80) (host "localhost"))
1236 "Return a service that runs @command{guix publish} listening on @var{host}
1237 and @var{port} (@pxref{Invoking guix publish}).
1238
1239 This assumes that @file{/etc/guix} already contains a signing key pair as
1240 created by @command{guix archive --generate-key} (@pxref{Invoking guix
1241 archive}). If that is not the case, the service will fail to start."
1242 (service guix-publish-service-type
1243 (guix-publish-configuration (guix guix) (port port) (host host))))
1244
1245 \f
1246 ;;;
1247 ;;; Udev.
1248 ;;;
1249
1250 (define-record-type* <udev-configuration>
1251 udev-configuration make-udev-configuration
1252 udev-configuration?
1253 (udev udev-configuration-udev ;<package>
1254 (default udev))
1255 (rules udev-configuration-rules ;list of <package>
1256 (default '())))
1257
1258 (define (udev-rules-union packages)
1259 "Return the union of the @code{lib/udev/rules.d} directories found in each
1260 item of @var{packages}."
1261 (define build
1262 (with-imported-modules '((guix build union)
1263 (guix build utils))
1264 #~(begin
1265 (use-modules (guix build union)
1266 (guix build utils)
1267 (srfi srfi-1)
1268 (srfi srfi-26))
1269
1270 (define %standard-locations
1271 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
1272
1273 (define (rules-sub-directory directory)
1274 ;; Return the sub-directory of DIRECTORY containing udev rules, or
1275 ;; #f if none was found.
1276 (find directory-exists?
1277 (map (cut string-append directory <>) %standard-locations)))
1278
1279 (mkdir-p (string-append #$output "/lib/udev"))
1280 (union-build (string-append #$output "/lib/udev/rules.d")
1281 (filter-map rules-sub-directory '#$packages)))))
1282
1283 (computed-file "udev-rules" build))
1284
1285 (define (udev-rule file-name contents)
1286 "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
1287 (computed-file file-name
1288 (with-imported-modules '((guix build utils))
1289 #~(begin
1290 (use-modules (guix build utils))
1291
1292 (define rules.d
1293 (string-append #$output "/lib/udev/rules.d"))
1294
1295 (mkdir-p rules.d)
1296 (call-with-output-file
1297 (string-append rules.d "/" #$file-name)
1298 (lambda (port)
1299 (display #$contents port)))))))
1300
1301 (define kvm-udev-rule
1302 ;; Return a directory with a udev rule that changes the group of /dev/kvm to
1303 ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
1304 ;; but now we have to add it by ourselves.
1305
1306 ;; Build users are part of the "kvm" group, so we can fearlessly make
1307 ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
1308 (udev-rule "90-kvm.rules"
1309 "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
1310
1311 (define udev-shepherd-service
1312 ;; Return a <shepherd-service> for UDEV with RULES.
1313 (match-lambda
1314 (($ <udev-configuration> udev rules)
1315 (let* ((rules (udev-rules-union (cons* udev kvm-udev-rule rules)))
1316 (udev.conf (computed-file "udev.conf"
1317 #~(call-with-output-file #$output
1318 (lambda (port)
1319 (format port
1320 "udev_rules=\"~a/lib/udev/rules.d\"\n"
1321 #$rules))))))
1322 (list
1323 (shepherd-service
1324 (provision '(udev))
1325
1326 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
1327 ;; be added: see
1328 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
1329 (requirement '(root-file-system))
1330
1331 (documentation "Populate the /dev directory, dynamically.")
1332 (start #~(lambda ()
1333 (define find
1334 (@ (srfi srfi-1) find))
1335
1336 (define udevd
1337 ;; Choose the right 'udevd'.
1338 (find file-exists?
1339 (map (lambda (suffix)
1340 (string-append #$udev suffix))
1341 '("/libexec/udev/udevd" ;udev
1342 "/sbin/udevd")))) ;eudev
1343
1344 (define (wait-for-udevd)
1345 ;; Wait until someone's listening on udevd's control
1346 ;; socket.
1347 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
1348 (let try ()
1349 (catch 'system-error
1350 (lambda ()
1351 (connect sock PF_UNIX "/run/udev/control")
1352 (close-port sock))
1353 (lambda args
1354 (format #t "waiting for udevd...~%")
1355 (usleep 500000)
1356 (try))))))
1357
1358 ;; Allow udev to find the modules.
1359 (setenv "LINUX_MODULE_DIRECTORY"
1360 "/run/booted-system/kernel/lib/modules")
1361
1362 ;; The first one is for udev, the second one for eudev.
1363 (setenv "UDEV_CONFIG_FILE" #$udev.conf)
1364 (setenv "EUDEV_RULES_DIRECTORY"
1365 #$(file-append rules "/lib/udev/rules.d"))
1366
1367 (let ((pid (primitive-fork)))
1368 (case pid
1369 ((0)
1370 (exec-command (list udevd)))
1371 (else
1372 ;; Wait until udevd is up and running. This
1373 ;; appears to be needed so that the events
1374 ;; triggered below are actually handled.
1375 (wait-for-udevd)
1376
1377 ;; Trigger device node creation.
1378 (system* #$(file-append udev "/bin/udevadm")
1379 "trigger" "--action=add")
1380
1381 ;; Wait for things to settle down.
1382 (system* #$(file-append udev "/bin/udevadm")
1383 "settle")
1384 pid)))))
1385 (stop #~(make-kill-destructor))
1386
1387 ;; When halting the system, 'udev' is actually killed by
1388 ;; 'user-processes', i.e., before its own 'stop' method was called.
1389 ;; Thus, make sure it is not respawned.
1390 (respawn? #f)))))))
1391
1392 (define udev-service-type
1393 (service-type (name 'udev)
1394 (extensions
1395 (list (service-extension shepherd-root-service-type
1396 udev-shepherd-service)))
1397
1398 (compose concatenate) ;concatenate the list of rules
1399 (extend (lambda (config rules)
1400 (match config
1401 (($ <udev-configuration> udev initial-rules)
1402 (udev-configuration
1403 (udev udev)
1404 (rules (append initial-rules rules)))))))))
1405
1406 (define* (udev-service #:key (udev eudev) (rules '()))
1407 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
1408 extra rules from the packages listed in @var{rules}."
1409 (service udev-service-type
1410 (udev-configuration (udev udev) (rules rules))))
1411
1412 (define swap-service-type
1413 (shepherd-service-type
1414 'swap
1415 (lambda (device)
1416 (define requirement
1417 (if (string-prefix? "/dev/mapper/" device)
1418 (list (symbol-append 'device-mapping-
1419 (string->symbol (basename device))))
1420 '()))
1421
1422 (shepherd-service
1423 (provision (list (symbol-append 'swap- (string->symbol device))))
1424 (requirement `(udev ,@requirement))
1425 (documentation "Enable the given swap device.")
1426 (start #~(lambda ()
1427 (restart-on-EINTR (swapon #$device))
1428 #t))
1429 (stop #~(lambda _
1430 (restart-on-EINTR (swapoff #$device))
1431 #f))
1432 (respawn? #f)))))
1433
1434 (define (swap-service device)
1435 "Return a service that uses @var{device} as a swap device."
1436 (service swap-service-type device))
1437
1438 (define-record-type* <gpm-configuration>
1439 gpm-configuration make-gpm-configuration gpm-configuration?
1440 (gpm gpm-configuration-gpm) ;package
1441 (options gpm-configuration-options)) ;list of strings
1442
1443 (define gpm-shepherd-service
1444 (match-lambda
1445 (($ <gpm-configuration> gpm options)
1446 (list (shepherd-service
1447 (requirement '(udev))
1448 (provision '(gpm))
1449 (start #~(lambda ()
1450 ;; 'gpm' runs in the background and sets a PID file.
1451 ;; Note that it requires running as "root".
1452 (false-if-exception (delete-file "/var/run/gpm.pid"))
1453 (fork+exec-command (list #$(file-append gpm "/sbin/gpm")
1454 #$@options))
1455
1456 ;; Wait for the PID file to appear; declare failure if
1457 ;; it doesn't show up.
1458 (let loop ((i 3))
1459 (or (file-exists? "/var/run/gpm.pid")
1460 (if (zero? i)
1461 #f
1462 (begin
1463 (sleep 1)
1464 (loop (1- i))))))))
1465
1466 (stop #~(lambda (_)
1467 ;; Return #f if successfully stopped.
1468 (not (zero? (system* #$(file-append gpm "/sbin/gpm")
1469 "-k"))))))))))
1470
1471 (define gpm-service-type
1472 (service-type (name 'gpm)
1473 (extensions
1474 (list (service-extension shepherd-root-service-type
1475 gpm-shepherd-service)))))
1476
1477 (define* (gpm-service #:key (gpm gpm)
1478 (options '("-m" "/dev/input/mice" "-t" "ps2")))
1479 "Run @var{gpm}, the general-purpose mouse daemon, with the given
1480 command-line @var{options}. GPM allows users to use the mouse in the console,
1481 notably to select, copy, and paste text. The default value of @var{options}
1482 uses the @code{ps2} protocol, which works for both USB and PS/2 mice.
1483
1484 This service is not part of @var{%base-services}."
1485 ;; To test in QEMU, use "-usbdevice mouse" and then, in the monitor, use
1486 ;; "info mice" and "mouse_set X" to use the right mouse.
1487 (service gpm-service-type
1488 (gpm-configuration (gpm gpm) (options options))))
1489
1490 (define-record-type* <kmscon-configuration>
1491 kmscon-configuration make-kmscon-configuration
1492 kmscon-configuration?
1493 (kmscon kmscon-configuration-kmscon
1494 (default kmscon))
1495 (virtual-terminal kmscon-configuration-virtual-terminal)
1496 (login-program kmscon-configuration-login-program
1497 (default (file-append shadow "/bin/login")))
1498 (login-arguments kmscon-configuration-login-arguments
1499 (default '("-p")))
1500 (hardware-acceleration? kmscon-configuration-hardware-acceleration?
1501 (default #f))) ; #t causes failure
1502
1503 (define kmscon-service-type
1504 (shepherd-service-type
1505 'kmscon
1506 (lambda (config)
1507 (let ((kmscon (kmscon-configuration-kmscon config))
1508 (virtual-terminal (kmscon-configuration-virtual-terminal config))
1509 (login-program (kmscon-configuration-login-program config))
1510 (login-arguments (kmscon-configuration-login-arguments config))
1511 (hardware-acceleration? (kmscon-configuration-hardware-acceleration? config)))
1512
1513 (define kmscon-command
1514 #~(list
1515 #$(file-append kmscon "/bin/kmscon") "--login"
1516 "--vt" #$virtual-terminal
1517 #$@(if hardware-acceleration? '("--hwaccel") '())
1518 "--" #$login-program #$@login-arguments))
1519
1520 (shepherd-service
1521 (documentation "kmscon virtual terminal")
1522 (requirement '(user-processes udev dbus-system))
1523 (provision (list (symbol-append 'term- (string->symbol virtual-terminal))))
1524 (start #~(make-forkexec-constructor #$kmscon-command))
1525 (stop #~(make-kill-destructor)))))))
1526
1527 \f
1528 (define %base-services
1529 ;; Convenience variable holding the basic services.
1530 (list (login-service)
1531
1532 (service console-font-service-type
1533 (map (lambda (tty)
1534 (cons tty %default-console-font))
1535 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
1536
1537 (mingetty-service (mingetty-configuration
1538 (tty "tty1")))
1539 (mingetty-service (mingetty-configuration
1540 (tty "tty2")))
1541 (mingetty-service (mingetty-configuration
1542 (tty "tty3")))
1543 (mingetty-service (mingetty-configuration
1544 (tty "tty4")))
1545 (mingetty-service (mingetty-configuration
1546 (tty "tty5")))
1547 (mingetty-service (mingetty-configuration
1548 (tty "tty6")))
1549
1550 (service static-networking-service-type
1551 (list (static-networking (interface "lo")
1552 (ip "127.0.0.1")
1553 (provision '(loopback)))))
1554 (syslog-service)
1555 (urandom-seed-service)
1556 (guix-service)
1557 (nscd-service)
1558
1559 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
1560 ;; used, so enable them by default. The FUSE and ALSA rules are
1561 ;; less critical, but handy.
1562 (udev-service #:rules (list lvm2 fuse alsa-utils crda))
1563
1564 (service special-files-service-type
1565 `(("/bin/sh" ,(file-append (canonical-package bash)
1566 "/bin/sh"))))))
1567
1568 ;;; base.scm ends here