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>
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 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
56 user-processes-service
57 session-environment-service
58 session-environment-service-type
60 console-keymap-service
62 console-font-service-type
67 udev-configuration-rules
77 mingetty-configuration
78 mingetty-configuration?
83 %nscd-default-configuration
100 %default-authorized-guix-keys
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
117 guix-publish-configuration
118 guix-publish-configuration?
120 guix-publish-service-type
127 urandom-seed-service-type
136 kmscon-configuration?
139 pam-limits-service-type
146 ;;; Base system services---i.e., services that 99% of the users will want to
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)
160 (string-append "LABEL=" (file-system-device file-system)))
164 (uuid->string (file-system-device file-system))))
166 (file-system-device file-system)))
168 (file-system-mount-point file-system) "\t"
169 (file-system-type file-system) "\t"
170 (or (file-system-options file-system) "defaults") "\t"
172 ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
173 ;; don't have anything sensible to put in there.
176 (define (file-systems->fstab file-systems)
177 "Return a @file{/etc} entry for an @file{fstab} describing
179 `(("fstab" ,(plain-file "fstab"
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
189 (define fstab-service-type
190 ;; The /etc/fstab service.
191 (service-type (name 'fstab)
193 (list (service-extension etc-service-type
194 file-systems->fstab)))
195 (compose concatenate)
198 (define %root-file-system-shepherd-service
200 (documentation "Take care of the root file system.")
201 (provision '(root-file-system))
204 ;; Return #f if successfully stopped.
207 (call-with-blocked-asyncs
209 (let ((null (%make-void-port "w")))
210 ;; Close 'shepherd.log'.
211 (display "closing log\n")
212 ((@ (shepherd comm) stop-logging))
214 ;; Redirect the default output ports..
215 (set-current-output-port null)
216 (set-current-error-port null)
218 ;; Close /dev/console.
219 (for-each close-fdes '(0 1 2))
221 ;; At this point, there are no open files left, so the
222 ;; root file system can be re-mounted read-only.
224 (logior MS_REMOUNT MS_RDONLY)
230 (define root-file-system-service-type
231 (shepherd-service-type 'root-file-system
232 (const %root-file-system-shepherd-service)))
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.)
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))
242 (define (file-system->shepherd-service-name file-system)
243 "Return the symbol that denotes the service mounting and unmounting
245 (symbol-append 'file-system-
246 (string->symbol (file-system-mount-point file-system))))
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))))
253 (define dependency->shepherd-service-name
255 ((? mapped-device? md)
256 (mapped-device->shepherd-service-name md))
258 (file-system->shepherd-service-name fs))))
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))
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
285 (let (($PATH (getenv "PATH")))
286 ;; Make sure fsck.ext2 & co. can be found.
292 "/run/current-system/profile/sbin:"
296 `(#$device #$title #$target #$type #$flags
300 (setenv "PATH" $PATH)))
303 ;; Normally there are no processes left at this point, so
304 ;; TARGET can be safely unmounted.
306 ;; Make sure PID 1 doesn't keep TARGET busy.
312 ;; We need an additional module.
313 (modules `(((gnu build file-systems)
314 #:select (mount-file-system))
315 ,@%default-modules)))))))
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)))
322 (provision '(file-systems))
323 (requirement (cons* 'root-file-system 'user-file-systems
324 (map file-system->shepherd-service-name
326 (documentation "Target for all the initially-mounted file systems")
328 (stop #~(const #f))))
330 (cons sink (map file-system-shepherd-service file-systems))))
332 (define file-system-service-type
333 (service-type (name 'file-systems)
335 (list (service-extension shepherd-root-service-type
336 file-system-shepherd-services)
337 (service-extension fstab-service-type
339 (compose concatenate)
342 (define user-unmount-service-type
343 (shepherd-service-type
345 (lambda (known-mount-points)
347 (documentation "Unmount manually-mounted file systems.")
348 (provision '(user-file-systems))
351 (define (known? mount-point)
353 (cons* "/proc" "/sys" '#$known-mount-points)))
355 ;; Make sure we don't keep the user's mount points busy.
358 (for-each (lambda (mount-point)
359 (format #t "unmounting '~a'...~%" mount-point)
362 (umount mount-point))
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)))
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))
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")
380 (define user-processes-service-type
381 (shepherd-service-type
383 (lambda (grace-delay)
385 (documentation "When stopped, terminate all user processes.")
386 (provision '(user-processes))
387 (requirement '(file-systems))
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)
401 ;; List of PIDs that must not be killed.
402 (if (file-exists? #$%do-not-kill-file)
404 (call-with-input-file #$%do-not-kill-file
405 (compose string-tokenize
406 (@ (ice-9 rdelim) read-string))))
410 (car (gettimeofday)))
413 ;; Really sleep N seconds.
414 ;; Work around <http://bugs.gnu.org/19581>.
416 (let loop ((elapsed 0))
418 (sleep (- n elapsed))
419 (loop (- (now) start)))))
421 (define lset= (@ (srfi srfi-1) lset=))
423 (display "sending all processes the TERM signal\n")
425 (if (null? omitted-pids)
427 ;; Easy: terminate all of them.
429 (sleep* #$grace-delay)
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
436 (kill-except omitted-pids SIGTERM)
437 (sleep* #$grace-delay)
438 (kill-except omitted-pids SIGKILL)
439 (delete-file #$%do-not-kill-file)))
442 (let ((pids (processes)))
443 (unless (lset= = pids (cons 1 omitted-pids))
444 (format #t "waiting for process termination\
445 (processes left: ~s)~%"
450 (display "all processes have been terminated\n")
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.
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.
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))
469 ;;; Preserve entropy to seed /dev/urandom on boot.
472 (define %random-seed-file
473 "/var/lib/random-seed")
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))
482 ;; On boot, write random seed into /dev/urandom.
483 (when (file-exists? #$%random-seed-file)
484 (call-with-input-file #$%random-seed-file
486 (call-with-output-file "/dev/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"
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
499 (put-bytevector seed buf)))
500 (umask previous-umask))))
503 ;; During shutdown, write from /dev/urandom into random seed.
504 (let ((buf (make-bytevector 512)))
505 (call-with-input-file "/dev/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
512 (put-bytevector seed buf)))
513 (umask previous-umask))
515 (modules `((rnrs bytevectors)
517 ,@%default-modules)))))
519 (define urandom-seed-service-type
520 (service-type (name 'urandom-seed)
522 (list (service-extension shepherd-root-service-type
523 urandom-seed-shepherd-service)))))
525 (define (urandom-seed-service)
526 (service urandom-seed-service-type #f))
530 ;;; Add hardware random number generator to entropy pool.
533 (define-record-type* <rngd-configuration>
534 rngd-configuration make-rngd-configuration
536 (rng-tools rngd-configuration-rng-tools) ;package
537 (device rngd-configuration-device)) ;string
539 (define rngd-service-type
540 (shepherd-service-type
543 (define rng-tools (rngd-configuration-rng-tools config))
544 (define device (rngd-configuration-device config))
547 (list (file-append rng-tools "/sbin/rngd")
551 (documentation "Add TRNG to entropy pool.")
552 (requirement '(udev))
554 (start #~(make-forkexec-constructor #$@rngd-command))
555 (stop #~(make-kill-destructor))))))
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
565 (rng-tools rng-tools)
570 ;;; System-wide environment variables.
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
578 (list key "=" value "\n")))
581 (define session-environment-service-type
583 (name 'session-environment)
585 (list (service-extension
588 (list `("environment"
589 ,(environment-variables->environment-file vars)))))))
590 (compose concatenate)
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.
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))
606 (define host-name-service-type
607 (shepherd-service-type
611 (documentation "Initialize the machine's host name.")
612 (provision '(host-name))
614 (sethostname #$name)))
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))
621 (define (unicode-start tty)
622 "Return a gexp to start Unicode support on @var{tty}."
624 ;; We have to run 'unicode_start' in a pipe so that when it invokes the
625 ;; 'tty' command, that command returns TTY.
627 (let ((pid (primitive-fork)))
631 (dup2 (open-fdes #$tty O_RDONLY) 0)
633 (dup2 (open-fdes #$tty O_WRONLY) 1)
634 (execl #$(file-append kbd "/bin/unicode_start")
637 (zero? (cdr (waitpid pid))))))))
639 (define console-keymap-service-type
640 (shepherd-service-type
644 (documentation (string-append "Load console keymap (loadkeys)."))
645 (provision '(console-keymap))
647 (zero? (system* #$(file-append kbd "/bin/loadkeys")
651 (define (console-keymap-service . files)
652 "Return a service to load console keymaps from @var{files}."
653 (service console-keymap-service-type files))
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.
661 (define (console-font-shepherd-services tty+font)
662 "Return a list of Shepherd services for each pair in TTY+FONT."
665 (let ((device (string-append "/dev/" tty)))
667 (documentation "Load a Unicode console font.")
668 (provision (list (symbol-append 'console-font-
669 (string->symbol tty))))
671 ;; Start after mingetty has been started on TTY, otherwise the settings
673 (requirement (list (symbol-append 'term-
674 (string->symbol tty))))
677 (and #$(unicode-start device)
679 (system* #$(file-append kbd "/bin/setfont")
680 "-C" #$device #$font)))))
685 (define console-font-service-type
686 (service-type (name 'console-fonts)
688 (list (service-extension shepherd-root-service-type
689 console-font-shepherd-services)))
690 (compose concatenate)
693 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
694 "This procedure is deprecated in favor of @code{console-font-service-type}.
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))))
701 (define %default-motd
702 (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
704 (define-record-type* <login-configuration>
705 login-configuration make-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
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)
721 (login-configuration-motd config))))
723 (define login-service-type
724 (service-type (name 'login)
725 (extensions (list (service-extension pam-root-service-type
726 login-pam-service)))))
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))
733 (define-record-type* <mingetty-configuration>
734 mingetty-configuration make-mingetty-configuration
735 mingetty-configuration?
736 (mingetty mingetty-configuration-mingetty ;<package>
738 (tty mingetty-configuration-tty) ;string
739 (auto-login mingetty-auto-login ;string | #f
741 (login-program mingetty-login-program ;gexp
743 (login-pause? mingetty-login-pause? ;Boolean
746 (define mingetty-shepherd-service
748 (($ <mingetty-configuration> mingetty tty auto-login login-program
752 (documentation "Run mingetty on an tty.")
753 (provision (list (symbol-append 'term- (string->symbol tty))))
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))
760 (start #~(make-forkexec-constructor
761 (list #$(file-append mingetty "/sbin/mingetty")
764 #~("--autologin" #$auto-login)
767 #~("--loginprog" #$login-program)
772 (stop #~(make-kill-destructor)))))))
774 (define mingetty-service-type
775 (service-type (name 'mingetty)
776 (extensions (list (service-extension shepherd-root-service-type
777 mingetty-shepherd-service)))))
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))
784 (define-record-type* <nscd-configuration> nscd-configuration
785 make-nscd-configuration
787 (log-file nscd-configuration-log-file ;string
788 (default "/var/log/nscd.log"))
789 (debug-level nscd-debug-level ;integer
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>
796 (glibc nscd-configuration-glibc ;<package>
797 (default (canonical-package glibc))))
799 (define-record-type* <nscd-cache> nscd-cache make-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
808 (check-files? nscd-cache-check-files? ;Boolean
810 (persistent? nscd-cache-persistent? ;Boolean
812 (shared? nscd-cache-shared? ;Boolean
814 (max-database-size nscd-cache-max-database-size ;integer
815 (default (* 32 (expt 2 20))))
816 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
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)
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)
830 (nscd-cache (database 'services)
832 ;; Services are unlikely to change, so we can be even more
834 (positive-time-to-live (* 3600 24))
835 (negative-time-to-live 3600)
836 (check-files? #t) ;check /etc/services changes
839 (define %nscd-default-configuration
840 ;; Default nscd configuration.
841 (nscd-configuration))
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
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"
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")))))
871 (($ <nscd-configuration> log-file debug-level caches)
872 (plain-file "nscd.conf"
874 # Configuration of libc's name service cache daemon (nscd).\n\n"
876 (string-append "logfile\t" log-file)
880 (string-append "debug-level\t"
881 (number->string debug-level))
885 (map cache->config caches)))))))
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).")
894 (requirement '(user-processes))
895 (start #~(make-forkexec-constructor
896 (list #$(file-append (nscd-configuration-glibc config)
898 "-f" #$nscd.conf "--foreground")
900 ;; Wait for the PID file. However, the PID file is
901 ;; written before nscd is actually listening on its
903 #:pid-file "/var/run/nscd/nscd.pid"
905 #:environment-variables
906 (list (string-append "LD_LIBRARY_PATH="
909 (string-append dir "/lib"))
910 (list #$@name-services))
912 (stop #~(make-kill-destructor))))))
914 (define nscd-activation
915 ;; Actions to take before starting nscd.
917 (use-modules (guix build utils))
918 (mkdir-p "/var/run/nscd")
919 (mkdir-p "/var/db/nscd"))) ;for the persistent cache
921 (define nscd-service-type
922 (service-type (name 'nscd)
924 (list (service-extension activation-service-type
925 (const nscd-activation))
926 (service-extension shepherd-root-service-type
927 nscd-shepherd-service)))
929 ;; This can be extended by providing additional name services
931 (compose concatenate)
932 (extend (lambda (config name-services)
935 (name-services (append
936 (nscd-configuration-name-services config)
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))
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)))
954 (define syslog-service-type
955 (shepherd-service-type
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))))))
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
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
981 # Same, in a different place.
982 *.info;mail.none;authpriv.none /dev/tty12
984 # The authpriv file has restricted access.
985 authpriv.* /var/log/secure
987 # Log all the mail messages in one place.
988 mail.* /var/log/maillog
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.
995 @xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
996 information on the configuration file syntax."
997 (service syslog-service-type config))
1000 (define pam-limits-service-type
1001 (let ((security-limits
1002 ;; Create /etc/security containing the provided "limits.conf" file.
1003 (lambda (limits-file)
1009 (stat #$limits-file)
1010 (symlink #$limits-file
1011 (string-append #$output "/limits.conf"))))))))
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"))
1022 (session (cons pam-limits
1023 (pam-service-session pam))))
1028 (list (service-extension etc-service-type security-limits)
1029 (service-extension pam-root-service-type
1030 (lambda _ (list pam-extension))))))))
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)
1045 (define* (guix-build-accounts count #:key
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)
1054 (name (format #f "guixbuilder~2,'0d" n))
1056 (uid (+ first-uid n -1))
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"))
1064 (comment (format #f "Guix Build User ~2d" n))
1065 (home-directory "/var/empty")
1066 (shell (file-append shadow "/sbin/nologin"))))
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)))
1078 (port (open-file key "r0b")))
1079 (format #t "registering public key '~a'...~%" key)
1080 (close-port (current-input-port))
1082 (execl #$(file-append guix "/bin/guix")
1083 "guix" "archive" "--authorize")
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))))))))
1091 (define %default-authorized-guix-keys
1092 ;; List of authorized substitute keys.
1093 (list (file-append guix "/share/guix/hydra.gnu.org.pub")))
1095 (define-record-type* <guix-configuration>
1096 guix-configuration make-guix-configuration
1098 (guix guix-configuration-guix ;<package>
1100 (build-group guix-configuration-build-group ;string
1101 (default "guixbuild"))
1102 (build-accounts guix-configuration-build-accounts ;integer
1104 (authorize-key? guix-configuration-authorize-key? ;Boolean
1106 (authorized-keys guix-configuration-authorized-keys ;list of gexps
1107 (default %default-authorized-guix-keys))
1108 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
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
1114 (log-file guix-configuration-log-file ;string
1115 (default "/var/log/guix-daemon.log"))
1116 (lsof guix-configuration-lsof ;<package>
1119 (define %default-guix-configuration
1120 (guix-configuration))
1122 (define (guix-shepherd-service config)
1123 "Return a <shepherd-service> for the Guix daemon service with CONFIG."
1125 (($ <guix-configuration> guix build-group build-accounts
1127 use-substitutes? substitute-urls extra-options
1129 (list (shepherd-service
1130 (documentation "Run the Guix daemon.")
1131 (provision '(guix-daemon))
1132 (requirement '(user-processes))
1134 #~(make-forkexec-constructor
1135 (list #$(file-append guix "/bin/guix-daemon")
1136 "--build-users-group" #$build-group
1137 #$@(if use-substitutes?
1139 '("--no-substitutes"))
1140 "--substitute-urls" #$(string-join substitute-urls)
1143 ;; Add 'lsof' (for the GC) to the daemon's $PATH.
1144 #:environment-variables
1145 (list (string-append "PATH=" #$lsof "/bin"))
1147 #:log-file #$log-file))
1148 (stop #~(make-kill-destructor)))))))
1150 (define (guix-accounts config)
1151 "Return the user accounts and user groups for CONFIG."
1153 (($ <guix-configuration> _ build-group build-accounts)
1158 ;; Use a fixed GID so that we can create the store with the right
1161 (guix-build-accounts build-accounts
1162 #:group build-group)))))
1164 (define (guix-activation config)
1165 "Return the activation gexp for 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.
1172 ;; Optionally authorize hydra.gnu.org's key.
1175 #$@(map (cut hydra-key-authorization <> guix) keys))
1178 (define guix-service-type
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))))))
1188 (define* (guix-service #:optional (config %default-guix-configuration))
1189 "Return a service that runs the Guix build daemon according to
1191 (service guix-service-type config))
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
1199 (port guix-publish-configuration-port ;number
1201 (host guix-publish-configuration-host ;string
1202 (default "localhost")))
1204 (define guix-publish-shepherd-service
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)))))))
1217 (define %guix-publish-accounts
1218 (list (user-group (name "guix-publish") (system? #t))
1220 (name "guix-publish")
1221 (group "guix-publish")
1223 (comment "guix publish user")
1224 (home-directory "/var/empty")
1225 (shell (file-append shadow "/sbin/nologin")))))
1227 (define guix-publish-service-type
1228 (service-type (name 'guix-publish)
1230 (list (service-extension shepherd-root-service-type
1231 guix-publish-shepherd-service)
1232 (service-extension account-service-type
1233 (const %guix-publish-accounts))))))
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}).
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))))
1250 (define-record-type* <udev-configuration>
1251 udev-configuration make-udev-configuration
1253 (udev udev-configuration-udev ;<package>
1255 (rules udev-configuration-rules ;list of <package>
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}."
1262 (with-imported-modules '((guix build union)
1265 (use-modules (guix build union)
1270 (define %standard-locations
1271 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
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)))
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)))))
1283 (computed-file "udev-rules" build))
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))
1290 (use-modules (guix build utils))
1293 (string-append #$output "/lib/udev/rules.d"))
1296 (call-with-output-file
1297 (string-append rules.d "/" #$file-name)
1299 (display #$contents port)))))))
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.
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"))
1311 (define udev-shepherd-service
1312 ;; Return a <shepherd-service> for UDEV with RULES.
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
1320 "udev_rules=\"~a/lib/udev/rules.d\"\n"
1326 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
1328 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
1329 (requirement '(root-file-system))
1331 (documentation "Populate the /dev directory, dynamically.")
1334 (@ (srfi srfi-1) find))
1337 ;; Choose the right 'udevd'.
1339 (map (lambda (suffix)
1340 (string-append #$udev suffix))
1341 '("/libexec/udev/udevd" ;udev
1342 "/sbin/udevd")))) ;eudev
1344 (define (wait-for-udevd)
1345 ;; Wait until someone's listening on udevd's control
1347 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
1349 (catch 'system-error
1351 (connect sock PF_UNIX "/run/udev/control")
1354 (format #t "waiting for udevd...~%")
1358 ;; Allow udev to find the modules.
1359 (setenv "LINUX_MODULE_DIRECTORY"
1360 "/run/booted-system/kernel/lib/modules")
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"))
1367 (let ((pid (primitive-fork)))
1370 (exec-command (list udevd)))
1372 ;; Wait until udevd is up and running. This
1373 ;; appears to be needed so that the events
1374 ;; triggered below are actually handled.
1377 ;; Trigger device node creation.
1378 (system* #$(file-append udev "/bin/udevadm")
1379 "trigger" "--action=add")
1381 ;; Wait for things to settle down.
1382 (system* #$(file-append udev "/bin/udevadm")
1385 (stop #~(make-kill-destructor))
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.
1392 (define udev-service-type
1393 (service-type (name 'udev)
1395 (list (service-extension shepherd-root-service-type
1396 udev-shepherd-service)))
1398 (compose concatenate) ;concatenate the list of rules
1399 (extend (lambda (config rules)
1401 (($ <udev-configuration> udev initial-rules)
1404 (rules (append initial-rules rules)))))))))
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))))
1412 (define swap-service-type
1413 (shepherd-service-type
1417 (if (string-prefix? "/dev/mapper/" device)
1418 (list (symbol-append 'device-mapping-
1419 (string->symbol (basename device))))
1423 (provision (list (symbol-append 'swap- (string->symbol device))))
1424 (requirement `(udev ,@requirement))
1425 (documentation "Enable the given swap device.")
1427 (restart-on-EINTR (swapon #$device))
1430 (restart-on-EINTR (swapoff #$device))
1434 (define (swap-service device)
1435 "Return a service that uses @var{device} as a swap device."
1436 (service swap-service-type device))
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
1443 (define gpm-shepherd-service
1445 (($ <gpm-configuration> gpm options)
1446 (list (shepherd-service
1447 (requirement '(udev))
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")
1456 ;; Wait for the PID file to appear; declare failure if
1457 ;; it doesn't show up.
1459 (or (file-exists? "/var/run/gpm.pid")
1467 ;; Return #f if successfully stopped.
1468 (not (zero? (system* #$(file-append gpm "/sbin/gpm")
1471 (define gpm-service-type
1472 (service-type (name 'gpm)
1474 (list (service-extension shepherd-root-service-type
1475 gpm-shepherd-service)))))
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.
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))))
1490 (define-record-type* <kmscon-configuration>
1491 kmscon-configuration make-kmscon-configuration
1492 kmscon-configuration?
1493 (kmscon kmscon-configuration-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
1500 (hardware-acceleration? kmscon-configuration-hardware-acceleration?
1501 (default #f))) ; #t causes failure
1503 (define kmscon-service-type
1504 (shepherd-service-type
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)))
1513 (define kmscon-command
1515 #$(file-append kmscon "/bin/kmscon") "--login"
1516 "--vt" #$virtual-terminal
1517 #$@(if hardware-acceleration? '("--hwaccel") '())
1518 "--" #$login-program #$@login-arguments))
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)))))))
1528 (define %base-services
1529 ;; Convenience variable holding the basic services.
1530 (list (login-service)
1532 (service console-font-service-type
1534 (cons tty %default-console-font))
1535 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
1537 (mingetty-service (mingetty-configuration
1539 (mingetty-service (mingetty-configuration
1541 (mingetty-service (mingetty-configuration
1543 (mingetty-service (mingetty-configuration
1545 (mingetty-service (mingetty-configuration
1547 (mingetty-service (mingetty-configuration
1550 (service static-networking-service-type
1551 (list (static-networking (interface "lo")
1553 (provision '(loopback)))))
1555 (urandom-seed-service)
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))
1564 (service special-files-service-type
1565 `(("/bin/sh" ,(file-append (canonical-package bash)
1568 ;;; base.scm ends here