1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016 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>
10 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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 package-management)
40 #:use-module (gnu packages lsof)
41 #:use-module (gnu packages terminals)
42 #:use-module ((gnu build file-systems)
43 #:select (mount-flags->bit-mask))
44 #:use-module (guix gexp)
45 #:use-module (guix records)
46 #:use-module (srfi srfi-1)
47 #:use-module (srfi srfi-26)
48 #:use-module (ice-9 match)
49 #:use-module (ice-9 format)
50 #:export (fstab-service-type
51 root-file-system-service
52 file-system-service-type
55 user-processes-service
56 session-environment-service
57 session-environment-service-type
59 console-keymap-service
61 console-font-service-type
66 udev-configuration-rules
76 mingetty-configuration
77 mingetty-configuration?
82 %nscd-default-configuration
99 %default-authorized-guix-keys
104 guix-publish-configuration
105 guix-publish-configuration?
107 guix-publish-service-type
114 urandom-seed-service-type
123 kmscon-configuration?
126 pam-limits-service-type
133 ;;; Base system services---i.e., services that 99% of the users will want to
143 (define (file-system->fstab-entry file-system)
144 "Return a @file{/etc/fstab} entry for @var{file-system}."
145 (string-append (case (file-system-title file-system)
147 (string-append "LABEL=" (file-system-device file-system)))
151 (uuid->string (file-system-device file-system))))
153 (file-system-device file-system)))
155 (file-system-mount-point file-system) "\t"
156 (file-system-type file-system) "\t"
157 (or (file-system-options file-system) "defaults") "\t"
159 ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
160 ;; don't have anything sensible to put in there.
163 (define (file-systems->fstab file-systems)
164 "Return a @file{/etc} entry for an @file{fstab} describing
166 `(("fstab" ,(plain-file "fstab"
169 # This file was generated from your GuixSD configuration. Any changes
170 # will be lost upon reboot or reconfiguration.\n\n"
171 (string-join (map file-system->fstab-entry
176 (define fstab-service-type
177 ;; The /etc/fstab service.
178 (service-type (name 'fstab)
180 (list (service-extension etc-service-type
181 file-systems->fstab)))
182 (compose concatenate)
185 (define %root-file-system-shepherd-service
187 (documentation "Take care of the root file system.")
188 (provision '(root-file-system))
191 ;; Return #f if successfully stopped.
194 (call-with-blocked-asyncs
196 (let ((null (%make-void-port "w")))
197 ;; Close 'shepherd.log'.
198 (display "closing log\n")
199 ((@ (shepherd comm) stop-logging))
201 ;; Redirect the default output ports..
202 (set-current-output-port null)
203 (set-current-error-port null)
205 ;; Close /dev/console.
206 (for-each close-fdes '(0 1 2))
208 ;; At this point, there are no open files left, so the
209 ;; root file system can be re-mounted read-only.
211 (logior MS_REMOUNT MS_RDONLY)
217 (define root-file-system-service-type
218 (shepherd-service-type 'root-file-system
219 (const %root-file-system-shepherd-service)))
221 (define (root-file-system-service)
222 "Return a service whose sole purpose is to re-mount read-only the root file
223 system upon shutdown (aka. cleanly \"umounting\" root.)
225 This service must be the root of the service dependency graph so that its
226 'stop' action is invoked when shepherd is the only process left."
227 (service root-file-system-service-type #f))
229 (define (file-system->shepherd-service-name file-system)
230 "Return the symbol that denotes the service mounting and unmounting
232 (symbol-append 'file-system-
233 (string->symbol (file-system-mount-point file-system))))
235 (define (mapped-device->shepherd-service-name md)
236 "Return the symbol that denotes the shepherd service of MD, a <mapped-device>."
237 (symbol-append 'device-mapping-
238 (string->symbol (mapped-device-target md))))
240 (define dependency->shepherd-service-name
242 ((? mapped-device? md)
243 (mapped-device->shepherd-service-name md))
245 (file-system->shepherd-service-name fs))))
247 (define (file-system-shepherd-service file-system)
248 "Return the shepherd service for @var{file-system}, or @code{#f} if
249 @var{file-system} is not auto-mounted upon boot."
250 (let ((target (file-system-mount-point file-system))
251 (device (file-system-device file-system))
252 (type (file-system-type file-system))
253 (title (file-system-title file-system))
254 (flags (file-system-flags file-system))
255 (options (file-system-options file-system))
256 (check? (file-system-check? file-system))
257 (create? (file-system-create-mount-point? file-system))
258 (dependencies (file-system-dependencies file-system)))
259 (and (file-system-mount? file-system)
260 (with-imported-modules '((gnu build file-systems)
261 (guix build bournish))
263 (provision (list (file-system->shepherd-service-name file-system)))
264 (requirement `(root-file-system
265 ,@(map dependency->shepherd-service-name dependencies)))
266 (documentation "Check, mount, and unmount the given file system.")
267 (start #~(lambda args
272 (let (($PATH (getenv "PATH")))
273 ;; Make sure fsck.ext2 & co. can be found.
279 "/run/current-system/profile/sbin:"
283 `(#$device #$title #$target #$type #$flags
287 (setenv "PATH" $PATH)))
290 ;; Normally there are no processes left at this point, so
291 ;; TARGET can be safely unmounted.
293 ;; Make sure PID 1 doesn't keep TARGET busy.
299 ;; We need an additional module.
300 (modules `(((gnu build file-systems)
301 #:select (mount-file-system))
302 ,@%default-modules)))))))
304 (define file-system-service-type
305 (service-type (name 'file-systems)
307 (list (service-extension shepherd-root-service-type
308 (lambda (file-systems)
309 (filter-map file-system-shepherd-service
311 (service-extension fstab-service-type
313 (compose concatenate)
316 (define user-unmount-service-type
317 (shepherd-service-type
319 (lambda (known-mount-points)
321 (documentation "Unmount manually-mounted file systems.")
322 (provision '(user-file-systems))
325 (define (known? mount-point)
327 (cons* "/proc" "/sys" '#$known-mount-points)))
329 ;; Make sure we don't keep the user's mount points busy.
332 (for-each (lambda (mount-point)
333 (format #t "unmounting '~a'...~%" mount-point)
336 (umount mount-point))
338 (let ((errno (system-error-errno args)))
339 (format #t "failed to unmount '~a': ~a~%"
340 mount-point (strerror errno))))))
341 (filter (negate known?) (mount-points)))
344 (define (user-unmount-service known-mount-points)
345 "Return a service whose sole purpose is to unmount file systems not listed
346 in KNOWN-MOUNT-POINTS when it is stopped."
347 (service user-unmount-service-type known-mount-points))
349 (define %do-not-kill-file
350 ;; Name of the file listing PIDs of processes that must survive when halting
351 ;; the system. Typical example is user-space file systems.
352 "/etc/shepherd/do-not-kill")
354 (define user-processes-service-type
355 (shepherd-service-type
358 ((requirements grace-delay)
360 (documentation "When stopped, terminate all user processes.")
361 (provision '(user-processes))
362 (requirement (cons* 'root-file-system 'user-file-systems
363 (map file-system->shepherd-service-name
367 (define (kill-except omit signal)
368 ;; Kill all the processes with SIGNAL except those listed
369 ;; in OMIT and the current process.
370 (let ((omit (cons (getpid) omit)))
371 (for-each (lambda (pid)
372 (unless (memv pid omit)
378 ;; List of PIDs that must not be killed.
379 (if (file-exists? #$%do-not-kill-file)
381 (call-with-input-file #$%do-not-kill-file
382 (compose string-tokenize
383 (@ (ice-9 rdelim) read-string))))
387 (car (gettimeofday)))
390 ;; Really sleep N seconds.
391 ;; Work around <http://bugs.gnu.org/19581>.
393 (let loop ((elapsed 0))
395 (sleep (- n elapsed))
396 (loop (- (now) start)))))
398 (define lset= (@ (srfi srfi-1) lset=))
400 (display "sending all processes the TERM signal\n")
402 (if (null? omitted-pids)
404 ;; Easy: terminate all of them.
406 (sleep* #$grace-delay)
409 ;; Kill them all except OMITTED-PIDS. XXX: We would
410 ;; like to (kill -1 SIGSTOP) to get a fixed list of
411 ;; processes, like 'killall5' does, but that seems
413 (kill-except omitted-pids SIGTERM)
414 (sleep* #$grace-delay)
415 (kill-except omitted-pids SIGKILL)
416 (delete-file #$%do-not-kill-file)))
419 (let ((pids (processes)))
420 (unless (lset= = pids (cons 1 omitted-pids))
421 (format #t "waiting for process termination\
422 (processes left: ~s)~%"
427 (display "all processes have been terminated\n")
431 (define* (user-processes-service file-systems #:key (grace-delay 4))
432 "Return the service that is responsible for terminating all the processes so
433 that the root file system can be re-mounted read-only, just before
434 rebooting/halting. Processes still running GRACE-DELAY seconds after SIGTERM
435 has been sent are terminated with SIGKILL.
437 The returned service will depend on 'root-file-system' and on all the shepherd
438 services corresponding to FILE-SYSTEMS.
440 All the services that spawn processes must depend on this one so that they are
441 stopped before 'kill' is called."
442 (service user-processes-service-type
443 (list (filter file-system-mount? file-systems) grace-delay)))
447 ;;; Preserve entropy to seed /dev/urandom on boot.
450 (define %random-seed-file
451 "/var/lib/random-seed")
453 (define (urandom-seed-shepherd-service _)
454 "Return a shepherd service for the /dev/urandom seed."
455 (list (shepherd-service
456 (documentation "Preserve entropy across reboots for /dev/urandom.")
457 (provision '(urandom-seed))
458 (requirement '(user-processes))
460 ;; On boot, write random seed into /dev/urandom.
461 (when (file-exists? #$%random-seed-file)
462 (call-with-input-file #$%random-seed-file
464 (call-with-output-file "/dev/urandom"
466 (dump-port seed urandom))))))
467 ;; Immediately refresh the seed in case the system doesn't
468 ;; shut down cleanly.
469 (call-with-input-file "/dev/urandom"
471 (let ((previous-umask (umask #o077))
472 (buf (make-bytevector 512)))
473 (mkdir-p (dirname #$%random-seed-file))
474 (get-bytevector-n! urandom buf 0 512)
475 (call-with-output-file #$%random-seed-file
477 (put-bytevector seed buf)))
478 (umask previous-umask))))
481 ;; During shutdown, write from /dev/urandom into random seed.
482 (let ((buf (make-bytevector 512)))
483 (call-with-input-file "/dev/urandom"
485 (let ((previous-umask (umask #o077)))
486 (get-bytevector-n! urandom buf 0 512)
487 (mkdir-p (dirname #$%random-seed-file))
488 (call-with-output-file #$%random-seed-file
490 (put-bytevector seed buf)))
491 (umask previous-umask))
493 (modules `((rnrs bytevectors)
495 ,@%default-modules)))))
497 (define urandom-seed-service-type
498 (service-type (name 'urandom-seed)
500 (list (service-extension shepherd-root-service-type
501 urandom-seed-shepherd-service)))))
503 (define (urandom-seed-service)
504 (service urandom-seed-service-type #f))
508 ;;; Add hardware random number generator to entropy pool.
511 (define-record-type* <rngd-configuration>
512 rngd-configuration make-rngd-configuration
514 (rng-tools rngd-configuration-rng-tools) ;package
515 (device rngd-configuration-device)) ;string
517 (define rngd-service-type
518 (shepherd-service-type
521 (define rng-tools (rngd-configuration-rng-tools config))
522 (define device (rngd-configuration-device config))
525 (list (file-append rng-tools "/sbin/rngd")
529 (documentation "Add TRNG to entropy pool.")
530 (requirement '(udev))
532 (start #~(make-forkexec-constructor #$@rngd-command))
533 (stop #~(make-kill-destructor))))))
535 (define* (rngd-service #:key
536 (rng-tools rng-tools)
537 (device "/dev/hwrng"))
538 "Return a service that runs the @command{rngd} program from @var{rng-tools}
539 to add @var{device} to the kernel's entropy pool. The service will fail if
540 @var{device} does not exist."
541 (service rngd-service-type
543 (rng-tools rng-tools)
548 ;;; System-wide environment variables.
551 (define (environment-variables->environment-file vars)
552 "Return a file for pam_env(8) that contains environment variables VARS."
553 (apply mixed-text-file "environment"
554 (append-map (match-lambda
556 (list key "=" value "\n")))
559 (define session-environment-service-type
561 (name 'session-environment)
563 (list (service-extension
566 (list `("environment"
567 ,(environment-variables->environment-file vars)))))))
568 (compose concatenate)
571 (define (session-environment-service vars)
572 "Return a service that builds the @file{/etc/environment}, which can be read
573 by PAM-aware applications to set environment variables for sessions.
575 VARS should be an association list in which both the keys and the values are
576 strings or string-valued gexps."
577 (service session-environment-service-type vars))
584 (define host-name-service-type
585 (shepherd-service-type
589 (documentation "Initialize the machine's host name.")
590 (provision '(host-name))
592 (sethostname #$name)))
595 (define (host-name-service name)
596 "Return a service that sets the host name to @var{name}."
597 (service host-name-service-type name))
599 (define (unicode-start tty)
600 "Return a gexp to start Unicode support on @var{tty}."
602 ;; We have to run 'unicode_start' in a pipe so that when it invokes the
603 ;; 'tty' command, that command returns TTY.
605 (let ((pid (primitive-fork)))
609 (dup2 (open-fdes #$tty O_RDONLY) 0)
611 (dup2 (open-fdes #$tty O_WRONLY) 1)
612 (execl #$(file-append kbd "/bin/unicode_start")
615 (zero? (cdr (waitpid pid))))))))
617 (define console-keymap-service-type
618 (shepherd-service-type
622 (documentation (string-append "Load console keymap (loadkeys)."))
623 (provision '(console-keymap))
625 (zero? (system* #$(file-append kbd "/bin/loadkeys")
629 (define (console-keymap-service . files)
630 "Return a service to load console keymaps from @var{files}."
631 (service console-keymap-service-type files))
633 (define %default-console-font
634 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
635 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
636 ;; codepoints notably found in the UTF-8 manual.
639 (define (console-font-shepherd-services tty+font)
640 "Return a list of Shepherd services for each pair in TTY+FONT."
643 (let ((device (string-append "/dev/" tty)))
645 (documentation "Load a Unicode console font.")
646 (provision (list (symbol-append 'console-font-
647 (string->symbol tty))))
649 ;; Start after mingetty has been started on TTY, otherwise the settings
651 (requirement (list (symbol-append 'term-
652 (string->symbol tty))))
655 (and #$(unicode-start device)
657 (system* #$(file-append kbd "/bin/setfont")
658 "-C" #$device #$font)))))
663 (define console-font-service-type
664 (service-type (name 'console-fonts)
666 (list (service-extension shepherd-root-service-type
667 console-font-shepherd-services)))
668 (compose concatenate)
671 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
672 "This procedure is deprecated in favor of @code{console-font-service-type}.
674 Return a service that sets up Unicode support in @var{tty} and loads
675 @var{font} for that tty (fonts are per virtual console in Linux.)"
676 (simple-service (symbol-append 'console-font- (string->symbol tty))
677 console-font-service-type `((,tty . ,font))))
679 (define %default-motd
680 (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
682 (define-record-type* <login-configuration>
683 login-configuration make-login-configuration
685 (motd login-configuration-motd ;file-like
686 (default %default-motd))
687 ;; Allow empty passwords by default so that first-time users can log in when
688 ;; the 'root' account has just been created.
689 (allow-empty-passwords? login-configuration-allow-empty-passwords?
690 (default #t))) ;Boolean
692 (define (login-pam-service config)
693 "Return the list of PAM service needed for CONF."
694 ;; Let 'login' be known to PAM.
695 (list (unix-pam-service "login"
696 #:allow-empty-passwords?
697 (login-configuration-allow-empty-passwords? config)
699 (login-configuration-motd config))))
701 (define login-service-type
702 (service-type (name 'login)
703 (extensions (list (service-extension pam-root-service-type
704 login-pam-service)))))
706 (define* (login-service #:optional (config (login-configuration)))
707 "Return a service configure login according to @var{config}, which specifies
708 the message of the day, among other things."
709 (service login-service-type config))
711 (define-record-type* <mingetty-configuration>
712 mingetty-configuration make-mingetty-configuration
713 mingetty-configuration?
714 (mingetty mingetty-configuration-mingetty ;<package>
716 (tty mingetty-configuration-tty) ;string
717 (auto-login mingetty-auto-login ;string | #f
719 (login-program mingetty-login-program ;gexp
721 (login-pause? mingetty-login-pause? ;Boolean
724 (define mingetty-shepherd-service
726 (($ <mingetty-configuration> mingetty tty auto-login login-program
730 (documentation "Run mingetty on an tty.")
731 (provision (list (symbol-append 'term- (string->symbol tty))))
733 ;; Since the login prompt shows the host name, wait for the 'host-name'
734 ;; service to be done. Also wait for udev essentially so that the tty
735 ;; text is not lost in the middle of kernel messages (XXX).
736 (requirement '(user-processes host-name udev))
738 (start #~(make-forkexec-constructor
739 (list #$(file-append mingetty "/sbin/mingetty")
742 #~("--autologin" #$auto-login)
745 #~("--loginprog" #$login-program)
750 (stop #~(make-kill-destructor)))))))
752 (define mingetty-service-type
753 (service-type (name 'mingetty)
754 (extensions (list (service-extension shepherd-root-service-type
755 mingetty-shepherd-service)))))
757 (define* (mingetty-service config)
758 "Return a service to run mingetty according to @var{config}, which specifies
759 the tty to run, among other things."
760 (service mingetty-service-type config))
762 (define-record-type* <nscd-configuration> nscd-configuration
763 make-nscd-configuration
765 (log-file nscd-configuration-log-file ;string
766 (default "/var/log/nscd.log"))
767 (debug-level nscd-debug-level ;integer
769 ;; TODO: See nscd.conf in glibc for other options to add.
770 (caches nscd-configuration-caches ;list of <nscd-cache>
771 (default %nscd-default-caches))
772 (name-services nscd-configuration-name-services ;list of <packages>
774 (glibc nscd-configuration-glibc ;<package>
775 (default (canonical-package glibc))))
777 (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
779 (database nscd-cache-database) ;symbol
780 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
781 (negative-time-to-live nscd-cache-negative-time-to-live
782 (default 20)) ;integer
783 (suggested-size nscd-cache-suggested-size ;integer ("default module
786 (check-files? nscd-cache-check-files? ;Boolean
788 (persistent? nscd-cache-persistent? ;Boolean
790 (shared? nscd-cache-shared? ;Boolean
792 (max-database-size nscd-cache-max-database-size ;integer
793 (default (* 32 (expt 2 20))))
794 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
797 (define %nscd-default-caches
798 ;; Caches that we want to enable by default. Note that when providing an
799 ;; empty nscd.conf, all caches are disabled.
800 (list (nscd-cache (database 'hosts)
802 ;; Aggressively cache the host name cache to improve
803 ;; privacy and resilience.
804 (positive-time-to-live (* 3600 12))
805 (negative-time-to-live 20)
808 (nscd-cache (database 'services)
810 ;; Services are unlikely to change, so we can be even more
812 (positive-time-to-live (* 3600 24))
813 (negative-time-to-live 3600)
814 (check-files? #t) ;check /etc/services changes
817 (define %nscd-default-configuration
818 ;; Default nscd configuration.
819 (nscd-configuration))
821 (define (nscd.conf-file config)
822 "Return the @file{nscd.conf} configuration file for @var{config}, an
823 @code{<nscd-configuration>} object."
824 (define cache->config
826 (($ <nscd-cache> (= symbol->string database)
827 positive-ttl negative-ttl size check-files?
828 persistent? shared? max-size propagate?)
829 (string-append "\nenable-cache\t" database "\tyes\n"
831 "positive-time-to-live\t" database "\t"
832 (number->string positive-ttl) "\n"
833 "negative-time-to-live\t" database "\t"
834 (number->string negative-ttl) "\n"
835 "suggested-size\t" database "\t"
836 (number->string size) "\n"
837 "check-files\t" database "\t"
838 (if check-files? "yes\n" "no\n")
839 "persistent\t" database "\t"
840 (if persistent? "yes\n" "no\n")
841 "shared\t" database "\t"
842 (if shared? "yes\n" "no\n")
843 "max-db-size\t" database "\t"
844 (number->string max-size) "\n"
845 "auto-propagate\t" database "\t"
846 (if propagate? "yes\n" "no\n")))))
849 (($ <nscd-configuration> log-file debug-level caches)
850 (plain-file "nscd.conf"
852 # Configuration of libc's name service cache daemon (nscd).\n\n"
854 (string-append "logfile\t" log-file)
858 (string-append "debug-level\t"
859 (number->string debug-level))
863 (map cache->config caches)))))))
865 (define (nscd-shepherd-service config)
866 "Return a shepherd service for CONFIG, an <nscd-configuration> object."
867 (let ((nscd.conf (nscd.conf-file config))
868 (name-services (nscd-configuration-name-services config)))
869 (list (shepherd-service
870 (documentation "Run libc's name service cache daemon (nscd).")
872 (requirement '(user-processes))
873 (start #~(make-forkexec-constructor
874 (list #$(file-append (nscd-configuration-glibc config)
876 "-f" #$nscd.conf "--foreground")
878 ;; Wait for the PID file. However, the PID file is
879 ;; written before nscd is actually listening on its
881 #:pid-file "/var/run/nscd/nscd.pid"
883 #:environment-variables
884 (list (string-append "LD_LIBRARY_PATH="
887 (string-append dir "/lib"))
888 (list #$@name-services))
890 (stop #~(make-kill-destructor))))))
892 (define nscd-activation
893 ;; Actions to take before starting nscd.
895 (use-modules (guix build utils))
896 (mkdir-p "/var/run/nscd")
897 (mkdir-p "/var/db/nscd"))) ;for the persistent cache
899 (define nscd-service-type
900 (service-type (name 'nscd)
902 (list (service-extension activation-service-type
903 (const nscd-activation))
904 (service-extension shepherd-root-service-type
905 nscd-shepherd-service)))
907 ;; This can be extended by providing additional name services
909 (compose concatenate)
910 (extend (lambda (config name-services)
913 (name-services (append
914 (nscd-configuration-name-services config)
917 (define* (nscd-service #:optional (config %nscd-default-configuration))
918 "Return a service that runs libc's name service cache daemon (nscd) with the
919 given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
920 Service Switch}, for an example."
921 (service nscd-service-type config))
924 (define-record-type* <syslog-configuration>
925 syslog-configuration make-syslog-configuration
926 syslog-configuration?
927 (syslogd syslog-configuration-syslogd
928 (default (file-append inetutils "/libexec/syslogd")))
929 (config-file syslog-configuration-config-file
930 (default %default-syslog.conf)))
932 (define syslog-service-type
933 (shepherd-service-type
937 (documentation "Run the syslog daemon (syslogd).")
938 (provision '(syslogd))
939 (requirement '(user-processes))
940 (start #~(make-forkexec-constructor
941 (list #$(syslog-configuration-syslogd config)
942 "--rcfile" #$(syslog-configuration-config-file config))
943 #:pid-file "/var/run/syslog.pid"))
944 (stop #~(make-kill-destructor))))))
946 ;; Snippet adapted from the GNU inetutils manual.
947 (define %default-syslog.conf
948 (plain-file "syslog.conf" "
949 # Log all error messages, authentication messages of
950 # level notice or higher and anything of level err or
951 # higher to the console.
952 # Don't log private authentication messages!
953 *.alert;auth.notice;authpriv.none /dev/console
955 # Log anything (except mail) of level info or higher.
956 # Don't log private authentication messages!
957 *.info;mail.none;authpriv.none /var/log/messages
959 # Same, in a different place.
960 *.info;mail.none;authpriv.none /dev/tty12
962 # The authpriv file has restricted access.
963 authpriv.* /var/log/secure
965 # Log all the mail messages in one place.
966 mail.* /var/log/maillog
969 (define* (syslog-service #:optional (config (syslog-configuration)))
970 "Return a service that runs @command{syslogd} and takes
971 @var{<syslog-configuration>} as a parameter.
973 @xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
974 information on the configuration file syntax."
975 (service syslog-service-type config))
978 (define pam-limits-service-type
979 (let ((security-limits
980 ;; Create /etc/security containing the provided "limits.conf" file.
981 (lambda (limits-file)
988 (symlink #$limits-file
989 (string-append #$output "/limits.conf"))))))))
992 (let ((pam-limits (pam-entry
994 (module "pam_limits.so")
995 (arguments '("conf=/etc/security/limits.conf")))))
996 (if (member (pam-service-name pam)
997 '("login" "su" "slim"))
1000 (session (cons pam-limits
1001 (pam-service-session pam))))
1006 (list (service-extension etc-service-type security-limits)
1007 (service-extension pam-root-service-type
1008 (lambda _ (list pam-extension))))))))
1010 (define* (pam-limits-service #:optional (limits '()))
1011 "Return a service that makes selected programs respect the list of
1012 pam-limits-entry specified in LIMITS via pam_limits.so."
1013 (service pam-limits-service-type
1014 (plain-file "limits.conf"
1015 (string-join (map pam-limits-entry->string limits)
1023 (define* (guix-build-accounts count #:key
1027 "Return a list of COUNT user accounts for Guix build users, with UIDs
1028 starting at FIRST-UID, and under GID."
1029 (unfold (cut > <> count)
1032 (name (format #f "guixbuilder~2,'0d" n))
1034 (uid (+ first-uid n -1))
1037 ;; guix-daemon expects GROUP to be listed as a
1038 ;; supplementary group too:
1039 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
1040 (supplementary-groups (list group "kvm"))
1042 (comment (format #f "Guix Build User ~2d" n))
1043 (home-directory "/var/empty")
1044 (shell (file-append shadow "/sbin/nologin"))))
1048 (define (hydra-key-authorization key guix)
1049 "Return a gexp with code to register KEY, a file containing a 'guix archive'
1050 public key, with GUIX."
1051 #~(unless (file-exists? "/etc/guix/acl")
1052 (let ((pid (primitive-fork)))
1056 (port (open-file key "r0b")))
1057 (format #t "registering public key '~a'...~%" key)
1058 (close-port (current-input-port))
1060 (execl #$(file-append guix "/bin/guix")
1061 "guix" "archive" "--authorize")
1064 (let ((status (cdr (waitpid pid))))
1065 (unless (zero? status)
1066 (format (current-error-port) "warning: \
1067 failed to register hydra.gnu.org public key: ~a~%" status))))))))
1069 (define %default-authorized-guix-keys
1070 ;; List of authorized substitute keys.
1071 (list (file-append guix "/share/guix/hydra.gnu.org.pub")))
1073 (define-record-type* <guix-configuration>
1074 guix-configuration make-guix-configuration
1076 (guix guix-configuration-guix ;<package>
1078 (build-group guix-configuration-build-group ;string
1079 (default "guixbuild"))
1080 (build-accounts guix-configuration-build-accounts ;integer
1082 (authorize-key? guix-configuration-authorize-key? ;Boolean
1084 (authorized-keys guix-configuration-authorized-keys ;list of gexps
1085 (default %default-authorized-guix-keys))
1086 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
1088 (substitute-urls guix-configuration-substitute-urls ;list of strings
1089 (default %default-substitute-urls))
1090 (extra-options guix-configuration-extra-options ;list of strings
1092 (lsof guix-configuration-lsof ;<package>
1095 (define %default-guix-configuration
1096 (guix-configuration))
1098 (define (guix-shepherd-service config)
1099 "Return a <shepherd-service> for the Guix daemon service with CONFIG."
1101 (($ <guix-configuration> guix build-group build-accounts
1103 use-substitutes? substitute-urls extra-options
1105 (list (shepherd-service
1106 (documentation "Run the Guix daemon.")
1107 (provision '(guix-daemon))
1108 (requirement '(user-processes))
1110 #~(make-forkexec-constructor
1111 (list #$(file-append guix "/bin/guix-daemon")
1112 "--build-users-group" #$build-group
1113 #$@(if use-substitutes?
1115 '("--no-substitutes"))
1116 "--substitute-urls" #$(string-join substitute-urls)
1119 ;; Add 'lsof' (for the GC) to the daemon's $PATH.
1120 #:environment-variables
1121 (list (string-append "PATH=" #$lsof "/bin"))))
1122 (stop #~(make-kill-destructor)))))))
1124 (define (guix-accounts config)
1125 "Return the user accounts and user groups for CONFIG."
1127 (($ <guix-configuration> _ build-group build-accounts)
1132 ;; Use a fixed GID so that we can create the store with the right
1135 (guix-build-accounts build-accounts
1136 #:group build-group)))))
1138 (define (guix-activation config)
1139 "Return the activation gexp for CONFIG."
1141 (($ <guix-configuration> guix build-group build-accounts authorize-key? keys)
1142 ;; Assume that the store has BUILD-GROUP as its group. We could
1143 ;; otherwise call 'chown' here, but the problem is that on a COW unionfs,
1144 ;; chown leads to an entire copy of the tree, which is a bad idea.
1146 ;; Optionally authorize hydra.gnu.org's key.
1149 #$@(map (cut hydra-key-authorization <> guix) keys))
1152 (define guix-service-type
1156 (list (service-extension shepherd-root-service-type guix-shepherd-service)
1157 (service-extension account-service-type guix-accounts)
1158 (service-extension activation-service-type guix-activation)
1159 (service-extension profile-service-type
1160 (compose list guix-configuration-guix))))))
1162 (define* (guix-service #:optional (config %default-guix-configuration))
1163 "Return a service that runs the Guix build daemon according to
1165 (service guix-service-type config))
1168 (define-record-type* <guix-publish-configuration>
1169 guix-publish-configuration make-guix-publish-configuration
1170 guix-publish-configuration?
1171 (guix guix-publish-configuration-guix ;package
1173 (port guix-publish-configuration-port ;number
1175 (host guix-publish-configuration-host ;string
1176 (default "localhost")))
1178 (define guix-publish-shepherd-service
1180 (($ <guix-publish-configuration> guix port host)
1181 (list (shepherd-service
1182 (provision '(guix-publish))
1183 (requirement '(guix-daemon))
1184 (start #~(make-forkexec-constructor
1185 (list #$(file-append guix "/bin/guix")
1186 "publish" "-u" "guix-publish"
1187 "-p" #$(number->string port)
1188 (string-append "--listen=" #$host))))
1189 (stop #~(make-kill-destructor)))))))
1191 (define %guix-publish-accounts
1192 (list (user-group (name "guix-publish") (system? #t))
1194 (name "guix-publish")
1195 (group "guix-publish")
1197 (comment "guix publish user")
1198 (home-directory "/var/empty")
1199 (shell (file-append shadow "/sbin/nologin")))))
1201 (define guix-publish-service-type
1202 (service-type (name 'guix-publish)
1204 (list (service-extension shepherd-root-service-type
1205 guix-publish-shepherd-service)
1206 (service-extension account-service-type
1207 (const %guix-publish-accounts))))))
1209 (define* (guix-publish-service #:key (guix guix) (port 80) (host "localhost"))
1210 "Return a service that runs @command{guix publish} listening on @var{host}
1211 and @var{port} (@pxref{Invoking guix publish}).
1213 This assumes that @file{/etc/guix} already contains a signing key pair as
1214 created by @command{guix archive --generate-key} (@pxref{Invoking guix
1215 archive}). If that is not the case, the service will fail to start."
1216 (service guix-publish-service-type
1217 (guix-publish-configuration (guix guix) (port port) (host host))))
1224 (define-record-type* <udev-configuration>
1225 udev-configuration make-udev-configuration
1227 (udev udev-configuration-udev ;<package>
1229 (rules udev-configuration-rules ;list of <package>
1232 (define (udev-rules-union packages)
1233 "Return the union of the @code{lib/udev/rules.d} directories found in each
1234 item of @var{packages}."
1236 (with-imported-modules '((guix build union)
1239 (use-modules (guix build union)
1244 (define %standard-locations
1245 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
1247 (define (rules-sub-directory directory)
1248 ;; Return the sub-directory of DIRECTORY containing udev rules, or
1249 ;; #f if none was found.
1250 (find directory-exists?
1251 (map (cut string-append directory <>) %standard-locations)))
1253 (mkdir-p (string-append #$output "/lib/udev"))
1254 (union-build (string-append #$output "/lib/udev/rules.d")
1255 (filter-map rules-sub-directory '#$packages)))))
1257 (computed-file "udev-rules" build))
1259 (define (udev-rule file-name contents)
1260 "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
1261 (computed-file file-name
1262 (with-imported-modules '((guix build utils))
1264 (use-modules (guix build utils))
1267 (string-append #$output "/lib/udev/rules.d"))
1270 (call-with-output-file
1271 (string-append rules.d "/" #$file-name)
1273 (display #$contents port)))))))
1275 (define kvm-udev-rule
1276 ;; Return a directory with a udev rule that changes the group of /dev/kvm to
1277 ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
1278 ;; but now we have to add it by ourselves.
1280 ;; Build users are part of the "kvm" group, so we can fearlessly make
1281 ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
1282 (udev-rule "90-kvm.rules"
1283 "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
1285 (define udev-shepherd-service
1286 ;; Return a <shepherd-service> for UDEV with RULES.
1288 (($ <udev-configuration> udev rules)
1289 (let* ((rules (udev-rules-union (cons* udev kvm-udev-rule rules)))
1290 (udev.conf (computed-file "udev.conf"
1291 #~(call-with-output-file #$output
1294 "udev_rules=\"~a/lib/udev/rules.d\"\n"
1300 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
1302 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
1303 (requirement '(root-file-system))
1305 (documentation "Populate the /dev directory, dynamically.")
1308 (@ (srfi srfi-1) find))
1311 ;; Choose the right 'udevd'.
1313 (map (lambda (suffix)
1314 (string-append #$udev suffix))
1315 '("/libexec/udev/udevd" ;udev
1316 "/sbin/udevd")))) ;eudev
1318 (define (wait-for-udevd)
1319 ;; Wait until someone's listening on udevd's control
1321 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
1323 (catch 'system-error
1325 (connect sock PF_UNIX "/run/udev/control")
1328 (format #t "waiting for udevd...~%")
1332 ;; Allow udev to find the modules.
1333 (setenv "LINUX_MODULE_DIRECTORY"
1334 "/run/booted-system/kernel/lib/modules")
1336 ;; The first one is for udev, the second one for eudev.
1337 (setenv "UDEV_CONFIG_FILE" #$udev.conf)
1338 (setenv "EUDEV_RULES_DIRECTORY"
1339 #$(file-append rules "/lib/udev/rules.d"))
1341 (let ((pid (primitive-fork)))
1344 (exec-command (list udevd)))
1346 ;; Wait until udevd is up and running. This
1347 ;; appears to be needed so that the events
1348 ;; triggered below are actually handled.
1351 ;; Trigger device node creation.
1352 (system* #$(file-append udev "/bin/udevadm")
1353 "trigger" "--action=add")
1355 ;; Wait for things to settle down.
1356 (system* #$(file-append udev "/bin/udevadm")
1359 (stop #~(make-kill-destructor))
1361 ;; When halting the system, 'udev' is actually killed by
1362 ;; 'user-processes', i.e., before its own 'stop' method was called.
1363 ;; Thus, make sure it is not respawned.
1366 (define udev-service-type
1367 (service-type (name 'udev)
1369 (list (service-extension shepherd-root-service-type
1370 udev-shepherd-service)))
1372 (compose concatenate) ;concatenate the list of rules
1373 (extend (lambda (config rules)
1375 (($ <udev-configuration> udev initial-rules)
1378 (rules (append initial-rules rules)))))))))
1380 (define* (udev-service #:key (udev eudev) (rules '()))
1381 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
1382 extra rules from the packages listed in @var{rules}."
1383 (service udev-service-type
1384 (udev-configuration (udev udev) (rules rules))))
1386 (define swap-service-type
1387 (shepherd-service-type
1391 (if (string-prefix? "/dev/mapper/" device)
1392 (list (symbol-append 'device-mapping-
1393 (string->symbol (basename device))))
1397 (provision (list (symbol-append 'swap- (string->symbol device))))
1398 (requirement `(udev ,@requirement))
1399 (documentation "Enable the given swap device.")
1401 (restart-on-EINTR (swapon #$device))
1404 (restart-on-EINTR (swapoff #$device))
1408 (define (swap-service device)
1409 "Return a service that uses @var{device} as a swap device."
1410 (service swap-service-type device))
1412 (define-record-type* <gpm-configuration>
1413 gpm-configuration make-gpm-configuration gpm-configuration?
1414 (gpm gpm-configuration-gpm) ;package
1415 (options gpm-configuration-options)) ;list of strings
1417 (define gpm-shepherd-service
1419 (($ <gpm-configuration> gpm options)
1420 (list (shepherd-service
1421 (requirement '(udev))
1424 ;; 'gpm' runs in the background and sets a PID file.
1425 ;; Note that it requires running as "root".
1426 (false-if-exception (delete-file "/var/run/gpm.pid"))
1427 (fork+exec-command (list #$(file-append gpm "/sbin/gpm")
1430 ;; Wait for the PID file to appear; declare failure if
1431 ;; it doesn't show up.
1433 (or (file-exists? "/var/run/gpm.pid")
1441 ;; Return #f if successfully stopped.
1442 (not (zero? (system* #$(file-append gpm "/sbin/gpm")
1445 (define gpm-service-type
1446 (service-type (name 'gpm)
1448 (list (service-extension shepherd-root-service-type
1449 gpm-shepherd-service)))))
1451 (define* (gpm-service #:key (gpm gpm)
1452 (options '("-m" "/dev/input/mice" "-t" "ps2")))
1453 "Run @var{gpm}, the general-purpose mouse daemon, with the given
1454 command-line @var{options}. GPM allows users to use the mouse in the console,
1455 notably to select, copy, and paste text. The default value of @var{options}
1456 uses the @code{ps2} protocol, which works for both USB and PS/2 mice.
1458 This service is not part of @var{%base-services}."
1459 ;; To test in QEMU, use "-usbdevice mouse" and then, in the monitor, use
1460 ;; "info mice" and "mouse_set X" to use the right mouse.
1461 (service gpm-service-type
1462 (gpm-configuration (gpm gpm) (options options))))
1464 (define-record-type* <kmscon-configuration>
1465 kmscon-configuration make-kmscon-configuration
1466 kmscon-configuration?
1467 (kmscon kmscon-configuration-kmscon
1469 (virtual-terminal kmscon-configuration-virtual-terminal)
1470 (login-program kmscon-configuration-login-program
1471 (default (file-append shadow "/bin/login")))
1472 (login-arguments kmscon-configuration-login-arguments
1474 (hardware-acceleration? kmscon-configuration-hardware-acceleration?
1475 (default #f))) ; #t causes failure
1477 (define kmscon-service-type
1478 (shepherd-service-type
1481 (let ((kmscon (kmscon-configuration-kmscon config))
1482 (virtual-terminal (kmscon-configuration-virtual-terminal config))
1483 (login-program (kmscon-configuration-login-program config))
1484 (login-arguments (kmscon-configuration-login-arguments config))
1485 (hardware-acceleration? (kmscon-configuration-hardware-acceleration? config)))
1487 (define kmscon-command
1489 #$(file-append kmscon "/bin/kmscon") "--login"
1490 "--vt" #$virtual-terminal
1491 #$@(if hardware-acceleration? '("--hwaccel") '())
1492 "--" #$login-program #$@login-arguments))
1495 (documentation "kmscon virtual terminal")
1496 (requirement '(user-processes udev dbus-system))
1497 (provision (list (symbol-append 'term- (string->symbol virtual-terminal))))
1498 (start #~(make-forkexec-constructor #$kmscon-command))
1499 (stop #~(make-kill-destructor)))))))
1502 (define %base-services
1503 ;; Convenience variable holding the basic services.
1504 (list (login-service)
1506 (service console-font-service-type
1508 (cons tty %default-console-font))
1509 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
1511 (mingetty-service (mingetty-configuration
1513 (mingetty-service (mingetty-configuration
1515 (mingetty-service (mingetty-configuration
1517 (mingetty-service (mingetty-configuration
1519 (mingetty-service (mingetty-configuration
1521 (mingetty-service (mingetty-configuration
1524 (static-networking-service "lo" "127.0.0.1"
1525 #:provision '(loopback))
1527 (urandom-seed-service)
1531 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
1532 ;; used, so enable them by default. The FUSE and ALSA rules are
1533 ;; less critical, but handy.
1534 (udev-service #:rules (list lvm2 fuse alsa-utils crda))))
1536 ;;; base.scm ends here