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 Mark H Weaver <mhw@netris.org>
5 ;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 (define-module (gnu services base)
23 #:use-module (guix store)
24 #:use-module (gnu services)
25 #:use-module (gnu services shepherd)
26 #:use-module (gnu services networking)
27 #:use-module (gnu system pam)
28 #:use-module (gnu system shadow) ; 'user-account', etc.
29 #:use-module (gnu system file-systems) ; 'file-system', etc.
30 #:use-module (gnu packages admin)
31 #:use-module ((gnu packages linux)
32 #:select (eudev kbd e2fsprogs lvm2 fuse alsa-utils crda gpm))
33 #:use-module ((gnu packages base)
34 #:select (canonical-package glibc))
35 #:use-module (gnu packages package-management)
36 #:use-module (gnu packages lsh)
37 #:use-module (gnu packages lsof)
38 #:use-module ((gnu build file-systems)
39 #:select (mount-flags->bit-mask))
40 #:use-module (guix gexp)
41 #:use-module (guix records)
42 #:use-module (srfi srfi-1)
43 #:use-module (srfi srfi-26)
44 #:use-module (ice-9 match)
45 #:use-module (ice-9 format)
46 #:export (fstab-service-type
47 root-file-system-service
50 device-mapping-service
52 user-processes-service
53 session-environment-service
54 session-environment-service-type
56 console-keymap-service
61 udev-configuration-rules
66 mingetty-configuration
67 mingetty-configuration?
72 %nscd-default-configuration
88 guix-publish-configuration
89 guix-publish-configuration?
91 guix-publish-service-type
99 ;;; Base system services---i.e., services that 99% of the users will want to
109 (define (file-system->fstab-entry file-system)
110 "Return a @file{/etc/fstab} entry for @var{file-system}."
111 (string-append (case (file-system-title file-system)
113 (string-append "LABEL=" (file-system-device file-system)))
117 (uuid->string (file-system-device file-system))))
119 (file-system-device file-system)))
121 (file-system-mount-point file-system) "\t"
122 (file-system-type file-system) "\t"
123 (or (file-system-options file-system) "defaults") "\t"
125 ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
126 ;; don't have anything sensible to put in there.
129 (define (file-systems->fstab file-systems)
130 "Return a @file{/etc} entry for an @file{fstab} describing
132 `(("fstab" ,(plain-file "fstab"
135 # This file was generated from your GuixSD configuration. Any changes
136 # will be lost upon reboot or reconfiguration.\n\n"
137 (string-join (map file-system->fstab-entry
142 (define fstab-service-type
143 ;; The /etc/fstab service.
144 (service-type (name 'fstab)
146 (list (service-extension etc-service-type
147 file-systems->fstab)))
151 (define %root-file-system-shepherd-service
153 (documentation "Take care of the root file system.")
154 (provision '(root-file-system))
157 ;; Return #f if successfully stopped.
160 (call-with-blocked-asyncs
162 (let ((null (%make-void-port "w")))
163 ;; Close 'shepherd.log'.
164 (display "closing log\n")
165 ((@ (shepherd comm) stop-logging))
167 ;; Redirect the default output ports..
168 (set-current-output-port null)
169 (set-current-error-port null)
171 ;; Close /dev/console.
172 (for-each close-fdes '(0 1 2))
174 ;; At this point, there are no open files left, so the
175 ;; root file system can be re-mounted read-only.
177 (logior MS_REMOUNT MS_RDONLY)
183 (define root-file-system-service-type
184 (shepherd-service-type 'root-file-system
185 (const %root-file-system-shepherd-service)))
187 (define (root-file-system-service)
188 "Return a service whose sole purpose is to re-mount read-only the root file
189 system upon shutdown (aka. cleanly \"umounting\" root.)
191 This service must be the root of the service dependency graph so that its
192 'stop' action is invoked when shepherd is the only process left."
193 (service root-file-system-service-type #f))
195 (define (file-system->shepherd-service-name file-system)
196 "Return the symbol that denotes the service mounting and unmounting
198 (symbol-append 'file-system-
199 (string->symbol (file-system-mount-point file-system))))
201 (define (mapped-device->shepherd-service-name md)
202 "Return the symbol that denotes the shepherd service of MD, a <mapped-device>."
203 (symbol-append 'device-mapping-
204 (string->symbol (mapped-device-target md))))
206 (define dependency->shepherd-service-name
208 ((? mapped-device? md)
209 (mapped-device->shepherd-service-name md))
211 (file-system->shepherd-service-name fs))))
213 (define (file-system-shepherd-service file-system)
214 "Return a list containing the shepherd service for @var{file-system}."
215 (let ((target (file-system-mount-point file-system))
216 (device (file-system-device file-system))
217 (type (file-system-type file-system))
218 (title (file-system-title file-system))
219 (check? (file-system-check? file-system))
220 (create? (file-system-create-mount-point? file-system))
221 (dependencies (file-system-dependencies file-system)))
222 (if (file-system-mount? file-system)
225 (provision (list (file-system->shepherd-service-name file-system)))
226 (requirement `(root-file-system
227 ,@(map dependency->shepherd-service-name dependencies)))
228 (documentation "Check, mount, and unmount the given file system.")
229 (start #~(lambda args
230 ;; FIXME: Use or factorize with 'mount-file-system'.
231 (let ((device (canonicalize-device-spec #$device '#$title))
232 (flags #$(mount-flags->bit-mask
233 (file-system-flags file-system))))
239 ;; Make sure fsck.ext2 & co. can be found.
243 "/run/current-system/profile/sbin:"
245 (check-file-system device #$type))
248 (mount device #$target #$type flags
249 #$(file-system-options file-system))
251 ;; For read-only bind mounts, an extra remount is
252 ;; needed, as per <http://lwn.net/Articles/281157/>,
253 ;; which still applies to Linux 4.0.
254 (when (and (= MS_BIND (logand flags MS_BIND))
255 (= MS_RDONLY (logand flags MS_RDONLY)))
256 (mount device #$target #$type
257 (logior MS_BIND MS_REMOUNT MS_RDONLY))))
260 ;; Normally there are no processes left at this point, so
261 ;; TARGET can be safely unmounted.
263 ;; Make sure PID 1 doesn't keep TARGET busy.
269 ;; We need an additional module.
270 (modules `(((gnu build file-systems)
271 #:select (check-file-system canonicalize-device-spec))
273 (imported-modules `((gnu build file-systems)
274 ,@%default-imported-modules))))
277 (define file-system-service-type
278 ;; TODO(?): Make this an extensible service that takes <file-system> objects
279 ;; and returns a list of <shepherd-service>.
280 (service-type (name 'file-system)
282 (list (service-extension shepherd-root-service-type
283 file-system-shepherd-service)
284 (service-extension fstab-service-type
287 (define* (file-system-service file-system)
288 "Return a service that mounts @var{file-system}, a @code{<file-system>}
290 (service file-system-service-type file-system))
292 (define user-unmount-service-type
293 (shepherd-service-type
295 (lambda (known-mount-points)
297 (documentation "Unmount manually-mounted file systems.")
298 (provision '(user-file-systems))
301 (define (known? mount-point)
303 (cons* "/proc" "/sys" '#$known-mount-points)))
305 ;; Make sure we don't keep the user's mount points busy.
308 (for-each (lambda (mount-point)
309 (format #t "unmounting '~a'...~%" mount-point)
312 (umount mount-point))
314 (let ((errno (system-error-errno args)))
315 (format #t "failed to unmount '~a': ~a~%"
316 mount-point (strerror errno))))))
317 (filter (negate known?) (mount-points)))
320 (define (user-unmount-service known-mount-points)
321 "Return a service whose sole purpose is to unmount file systems not listed
322 in KNOWN-MOUNT-POINTS when it is stopped."
323 (service user-unmount-service-type known-mount-points))
325 (define %do-not-kill-file
326 ;; Name of the file listing PIDs of processes that must survive when halting
327 ;; the system. Typical example is user-space file systems.
328 "/etc/shepherd/do-not-kill")
330 (define user-processes-service-type
331 (shepherd-service-type
334 ((requirements grace-delay)
336 (documentation "When stopped, terminate all user processes.")
337 (provision '(user-processes))
338 (requirement (cons* 'root-file-system 'user-file-systems
339 (map file-system->shepherd-service-name
343 (define (kill-except omit signal)
344 ;; Kill all the processes with SIGNAL except those listed
345 ;; in OMIT and the current process.
346 (let ((omit (cons (getpid) omit)))
347 (for-each (lambda (pid)
348 (unless (memv pid omit)
354 ;; List of PIDs that must not be killed.
355 (if (file-exists? #$%do-not-kill-file)
357 (call-with-input-file #$%do-not-kill-file
358 (compose string-tokenize
359 (@ (ice-9 rdelim) read-string))))
363 (car (gettimeofday)))
366 ;; Really sleep N seconds.
367 ;; Work around <http://bugs.gnu.org/19581>.
369 (let loop ((elapsed 0))
371 (sleep (- n elapsed))
372 (loop (- (now) start)))))
374 (define lset= (@ (srfi srfi-1) lset=))
376 (display "sending all processes the TERM signal\n")
378 (if (null? omitted-pids)
380 ;; Easy: terminate all of them.
382 (sleep* #$grace-delay)
385 ;; Kill them all except OMITTED-PIDS. XXX: We would
386 ;; like to (kill -1 SIGSTOP) to get a fixed list of
387 ;; processes, like 'killall5' does, but that seems
389 (kill-except omitted-pids SIGTERM)
390 (sleep* #$grace-delay)
391 (kill-except omitted-pids SIGKILL)
392 (delete-file #$%do-not-kill-file)))
395 (let ((pids (processes)))
396 (unless (lset= = pids (cons 1 omitted-pids))
397 (format #t "waiting for process termination\
398 (processes left: ~s)~%"
403 (display "all processes have been terminated\n")
407 (define* (user-processes-service file-systems #:key (grace-delay 4))
408 "Return the service that is responsible for terminating all the processes so
409 that the root file system can be re-mounted read-only, just before
410 rebooting/halting. Processes still running GRACE-DELAY seconds after SIGTERM
411 has been sent are terminated with SIGKILL.
413 The returned service will depend on 'root-file-system' and on all the shepherd
414 services corresponding to FILE-SYSTEMS.
416 All the services that spawn processes must depend on this one so that they are
417 stopped before 'kill' is called."
418 (service user-processes-service-type
419 (list (filter file-system-mount? file-systems) grace-delay)))
423 ;;; System-wide environment variables.
426 (define (environment-variables->environment-file vars)
427 "Return a file for pam_env(8) that contains environment variables VARS."
428 (apply mixed-text-file "environment"
429 (append-map (match-lambda
431 (list key "=" value "\n")))
434 (define session-environment-service-type
436 (name 'session-environment)
438 (list (service-extension
441 (list `("environment"
442 ,(environment-variables->environment-file vars)))))))
443 (compose concatenate)
446 (define (session-environment-service vars)
447 "Return a service that builds the @file{/etc/environment}, which can be read
448 by PAM-aware applications to set environment variables for sessions.
450 VARS should be an association list in which both the keys and the values are
451 strings or string-valued gexps."
452 (service session-environment-service-type vars))
459 (define host-name-service-type
460 (shepherd-service-type
464 (documentation "Initialize the machine's host name.")
465 (provision '(host-name))
467 (sethostname #$name)))
470 (define (host-name-service name)
471 "Return a service that sets the host name to @var{name}."
472 (service host-name-service-type name))
474 (define (unicode-start tty)
475 "Return a gexp to start Unicode support on @var{tty}."
477 ;; We have to run 'unicode_start' in a pipe so that when it invokes the
478 ;; 'tty' command, that command returns TTY.
480 (let ((pid (primitive-fork)))
484 (dup2 (open-fdes #$tty O_RDONLY) 0)
486 (dup2 (open-fdes #$tty O_WRONLY) 1)
487 (execl (string-append #$kbd "/bin/unicode_start")
490 (zero? (cdr (waitpid pid))))))))
492 (define console-keymap-service-type
493 (shepherd-service-type
497 (documentation (string-append "Load console keymap (loadkeys)."))
498 (provision '(console-keymap))
500 (zero? (system* (string-append #$kbd "/bin/loadkeys")
504 (define (console-keymap-service file)
505 "Return a service to load console keymap from @var{file}."
506 (service console-keymap-service-type file))
508 (define console-font-service-type
509 (shepherd-service-type
513 (let ((device (string-append "/dev/" tty)))
515 (documentation "Load a Unicode console font.")
516 (provision (list (symbol-append 'console-font-
517 (string->symbol tty))))
519 ;; Start after mingetty has been started on TTY, otherwise the settings
521 (requirement (list (symbol-append 'term-
522 (string->symbol tty))))
525 (and #$(unicode-start device)
527 (system* (string-append #$kbd "/bin/setfont")
528 "-C" #$device #$font)))))
532 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
533 "Return a service that sets up Unicode support in @var{tty} and loads
534 @var{font} for that tty (fonts are per virtual console in Linux.)"
535 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
536 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
537 ;; codepoints notably found in the UTF-8 manual.
538 (service console-font-service-type (list tty font)))
540 (define-record-type* <mingetty-configuration>
541 mingetty-configuration make-mingetty-configuration
542 mingetty-configuration?
543 (mingetty mingetty-configuration-mingetty ;<package>
545 (tty mingetty-configuration-tty) ;string
546 (motd mingetty-configuration-motd ;file-like
547 (default (plain-file "motd" "Welcome.\n")))
548 (auto-login mingetty-auto-login ;string | #f
550 (login-program mingetty-login-program ;gexp
552 (login-pause? mingetty-login-pause? ;Boolean
555 ;; Allow empty passwords by default so that first-time users can log in when
556 ;; the 'root' account has just been created.
557 (allow-empty-passwords? mingetty-configuration-allow-empty-passwords?
558 (default #t))) ;Boolean
560 (define (mingetty-pam-service conf)
561 "Return the list of PAM service needed for CONF."
562 ;; Let 'login' be known to PAM. All the mingetty services will have that
563 ;; PAM service, but that's fine because they're all identical and duplicates
565 (list (unix-pam-service "login"
566 #:allow-empty-passwords?
567 (mingetty-configuration-allow-empty-passwords? conf)
569 (mingetty-configuration-motd conf))))
571 (define mingetty-shepherd-service
573 (($ <mingetty-configuration> mingetty tty motd auto-login login-program
574 login-pause? allow-empty-passwords?)
577 (documentation "Run mingetty on an tty.")
578 (provision (list (symbol-append 'term- (string->symbol tty))))
580 ;; Since the login prompt shows the host name, wait for the 'host-name'
581 ;; service to be done. Also wait for udev essentially so that the tty
582 ;; text is not lost in the middle of kernel messages (XXX).
583 (requirement '(user-processes host-name udev))
585 (start #~(make-forkexec-constructor
586 (list (string-append #$mingetty "/sbin/mingetty")
589 #~("--autologin" #$auto-login)
592 #~("--loginprog" #$login-program)
597 (stop #~(make-kill-destructor)))))))
599 (define mingetty-service-type
600 (service-type (name 'mingetty)
601 (extensions (list (service-extension shepherd-root-service-type
602 mingetty-shepherd-service)
603 (service-extension pam-root-service-type
604 mingetty-pam-service)))))
606 (define* (mingetty-service config)
607 "Return a service to run mingetty according to @var{config}, which specifies
608 the tty to run, among other things."
609 (service mingetty-service-type config))
611 (define-record-type* <nscd-configuration> nscd-configuration
612 make-nscd-configuration
614 (log-file nscd-configuration-log-file ;string
615 (default "/var/log/nscd.log"))
616 (debug-level nscd-debug-level ;integer
618 ;; TODO: See nscd.conf in glibc for other options to add.
619 (caches nscd-configuration-caches ;list of <nscd-cache>
620 (default %nscd-default-caches))
621 (name-services nscd-configuration-name-services ;list of <packages>
623 (glibc nscd-configuration-glibc ;<package>
624 (default (canonical-package glibc))))
626 (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
628 (database nscd-cache-database) ;symbol
629 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
630 (negative-time-to-live nscd-cache-negative-time-to-live
631 (default 20)) ;integer
632 (suggested-size nscd-cache-suggested-size ;integer ("default module
635 (check-files? nscd-cache-check-files? ;Boolean
637 (persistent? nscd-cache-persistent? ;Boolean
639 (shared? nscd-cache-shared? ;Boolean
641 (max-database-size nscd-cache-max-database-size ;integer
642 (default (* 32 (expt 2 20))))
643 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
646 (define %nscd-default-caches
647 ;; Caches that we want to enable by default. Note that when providing an
648 ;; empty nscd.conf, all caches are disabled.
649 (list (nscd-cache (database 'hosts)
651 ;; Aggressively cache the host name cache to improve
652 ;; privacy and resilience.
653 (positive-time-to-live (* 3600 12))
654 (negative-time-to-live 20)
657 (nscd-cache (database 'services)
659 ;; Services are unlikely to change, so we can be even more
661 (positive-time-to-live (* 3600 24))
662 (negative-time-to-live 3600)
663 (check-files? #t) ;check /etc/services changes
666 (define %nscd-default-configuration
667 ;; Default nscd configuration.
668 (nscd-configuration))
670 (define (nscd.conf-file config)
671 "Return the @file{nscd.conf} configuration file for @var{config}, an
672 @code{<nscd-configuration>} object."
673 (define cache->config
675 (($ <nscd-cache> (= symbol->string database)
676 positive-ttl negative-ttl size check-files?
677 persistent? shared? max-size propagate?)
678 (string-append "\nenable-cache\t" database "\tyes\n"
680 "positive-time-to-live\t" database "\t"
681 (number->string positive-ttl) "\n"
682 "negative-time-to-live\t" database "\t"
683 (number->string negative-ttl) "\n"
684 "suggested-size\t" database "\t"
685 (number->string size) "\n"
686 "check-files\t" database "\t"
687 (if check-files? "yes\n" "no\n")
688 "persistent\t" database "\t"
689 (if persistent? "yes\n" "no\n")
690 "shared\t" database "\t"
691 (if shared? "yes\n" "no\n")
692 "max-db-size\t" database "\t"
693 (number->string max-size) "\n"
694 "auto-propagate\t" database "\t"
695 (if propagate? "yes\n" "no\n")))))
698 (($ <nscd-configuration> log-file debug-level caches)
699 (plain-file "nscd.conf"
701 # Configuration of libc's name service cache daemon (nscd).\n\n"
703 (string-append "logfile\t" log-file)
707 (string-append "debug-level\t"
708 (number->string debug-level))
712 (map cache->config caches)))))))
714 (define (nscd-shepherd-service config)
715 "Return a shepherd service for CONFIG, an <nscd-configuration> object."
716 (let ((nscd.conf (nscd.conf-file config))
717 (name-services (nscd-configuration-name-services config)))
718 (list (shepherd-service
719 (documentation "Run libc's name service cache daemon (nscd).")
721 (requirement '(user-processes))
722 (start #~(make-forkexec-constructor
723 (list (string-append #$(nscd-configuration-glibc config)
725 "-f" #$nscd.conf "--foreground")
727 #:environment-variables
728 (list (string-append "LD_LIBRARY_PATH="
731 (string-append dir "/lib"))
732 (list #$@name-services))
734 (stop #~(make-kill-destructor))
738 (define nscd-activation
739 ;; Actions to take before starting nscd.
741 (use-modules (guix build utils))
742 (mkdir-p "/var/run/nscd")
743 (mkdir-p "/var/db/nscd"))) ;for the persistent cache
745 (define nscd-service-type
746 (service-type (name 'nscd)
748 (list (service-extension activation-service-type
749 (const nscd-activation))
750 (service-extension shepherd-root-service-type
751 nscd-shepherd-service)))
753 ;; This can be extended by providing additional name services
755 (compose concatenate)
756 (extend (lambda (config name-services)
759 (name-services (append
760 (nscd-configuration-name-services config)
763 (define* (nscd-service #:optional (config %nscd-default-configuration))
764 "Return a service that runs libc's name service cache daemon (nscd) with the
765 given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
766 Service Switch}, for an example."
767 (service nscd-service-type config))
769 (define syslog-service-type
770 (shepherd-service-type
772 (lambda (config-file)
774 (documentation "Run the syslog daemon (syslogd).")
775 (provision '(syslogd))
776 (requirement '(user-processes))
777 (start #~(make-forkexec-constructor
778 (list (string-append #$inetutils "/libexec/syslogd")
779 "--no-detach" "--rcfile" #$config-file)))
780 (stop #~(make-kill-destructor))))))
782 ;; Snippet adapted from the GNU inetutils manual.
783 (define %default-syslog.conf
784 (plain-file "syslog.conf" "
785 # Log all error messages, authentication messages of
786 # level notice or higher and anything of level err or
787 # higher to the console.
788 # Don't log private authentication messages!
789 *.alert;auth.notice;authpriv.none /dev/console
791 # Log anything (except mail) of level info or higher.
792 # Don't log private authentication messages!
793 *.info;mail.none;authpriv.none /var/log/messages
795 # Same, in a different place.
796 *.info;mail.none;authpriv.none /dev/tty12
798 # The authpriv file has restricted access.
799 authpriv.* /var/log/secure
801 # Log all the mail messages in one place.
802 mail.* /var/log/maillog
805 (define* (syslog-service #:key (config-file %default-syslog.conf))
806 "Return a service that runs @code{syslogd}.
807 If configuration file name @var{config-file} is not specified, use some
808 reasonable default settings."
809 (service syslog-service-type config-file))
816 (define* (guix-build-accounts count #:key
820 "Return a list of COUNT user accounts for Guix build users, with UIDs
821 starting at FIRST-UID, and under GID."
822 (unfold (cut > <> count)
825 (name (format #f "guixbuilder~2,'0d" n))
827 (uid (+ first-uid n -1))
830 ;; guix-daemon expects GROUP to be listed as a
831 ;; supplementary group too:
832 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
833 (supplementary-groups (list group "kvm"))
835 (comment (format #f "Guix Build User ~2d" n))
836 (home-directory "/var/empty")
837 (shell #~(string-append #$shadow "/sbin/nologin"))))
841 (define (hydra-key-authorization guix)
842 "Return a gexp with code to register the hydra.gnu.org public key with
844 #~(unless (file-exists? "/etc/guix/acl")
845 (let ((pid (primitive-fork)))
848 (let* ((key (string-append #$guix
849 "/share/guix/hydra.gnu.org.pub"))
850 (port (open-file key "r0b")))
851 (format #t "registering public key '~a'...~%" key)
852 (close-port (current-input-port))
854 (execl (string-append #$guix "/bin/guix")
855 "guix" "archive" "--authorize")
858 (let ((status (cdr (waitpid pid))))
859 (unless (zero? status)
860 (format (current-error-port) "warning: \
861 failed to register hydra.gnu.org public key: ~a~%" status))))))))
863 (define-record-type* <guix-configuration>
864 guix-configuration make-guix-configuration
866 (guix guix-configuration-guix ;<package>
868 (build-group guix-configuration-build-group ;string
869 (default "guixbuild"))
870 (build-accounts guix-configuration-build-accounts ;integer
872 (authorize-key? guix-configuration-authorize-key? ;Boolean
874 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
876 (substitute-urls guix-configuration-substitute-urls ;list of strings
877 (default %default-substitute-urls))
878 (extra-options guix-configuration-extra-options ;list of strings
880 (lsof guix-configuration-lsof ;<package>
882 (lsh guix-configuration-lsh ;<package>
885 (define %default-guix-configuration
886 (guix-configuration))
888 (define (guix-shepherd-service config)
889 "Return a <shepherd-service> for the Guix daemon service with CONFIG."
891 (($ <guix-configuration> guix build-group build-accounts authorize-key?
892 use-substitutes? substitute-urls extra-options
894 (list (shepherd-service
895 (documentation "Run the Guix daemon.")
896 (provision '(guix-daemon))
897 (requirement '(user-processes))
899 #~(make-forkexec-constructor
900 (list (string-append #$guix "/bin/guix-daemon")
901 "--build-users-group" #$build-group
902 #$@(if use-substitutes?
904 '("--no-substitutes"))
905 "--substitute-urls" #$(string-join substitute-urls)
908 ;; Add 'lsof' (for the GC) and 'lsh' (for offloading) to the
910 #:environment-variables
911 (list (string-append "PATH=" #$lsof "/bin:" #$lsh "/bin"))))
912 (stop #~(make-kill-destructor)))))))
914 (define (guix-accounts config)
915 "Return the user accounts and user groups for CONFIG."
917 (($ <guix-configuration> _ build-group build-accounts)
922 ;; Use a fixed GID so that we can create the store with the right
925 (guix-build-accounts build-accounts
926 #:group build-group)))))
928 (define (guix-activation config)
929 "Return the activation gexp for CONFIG."
931 (($ <guix-configuration> guix build-group build-accounts authorize-key?)
932 ;; Assume that the store has BUILD-GROUP as its group. We could
933 ;; otherwise call 'chown' here, but the problem is that on a COW unionfs,
934 ;; chown leads to an entire copy of the tree, which is a bad idea.
936 ;; Optionally authorize hydra.gnu.org's key.
938 (hydra-key-authorization guix)))))
940 (define guix-service-type
944 (list (service-extension shepherd-root-service-type guix-shepherd-service)
945 (service-extension account-service-type guix-accounts)
946 (service-extension activation-service-type guix-activation)
947 (service-extension profile-service-type
948 (compose list guix-configuration-guix))))))
950 (define* (guix-service #:optional (config %default-guix-configuration))
951 "Return a service that runs the Guix build daemon according to
953 (service guix-service-type config))
956 (define-record-type* <guix-publish-configuration>
957 guix-publish-configuration make-guix-publish-configuration
958 guix-publish-configuration?
959 (guix guix-publish-configuration-guix ;package
961 (port guix-publish-configuration-port ;number
963 (host guix-publish-configuration-host ;string
964 (default "localhost")))
966 (define guix-publish-shepherd-service
968 (($ <guix-publish-configuration> guix port host)
969 (list (shepherd-service
970 (provision '(guix-publish))
971 (requirement '(guix-daemon))
972 (start #~(make-forkexec-constructor
973 (list (string-append #$guix "/bin/guix")
974 "publish" "-u" "guix-publish"
975 "-p" #$(number->string port)
976 (string-append "--listen=" #$host))))
977 (stop #~(make-kill-destructor)))))))
979 (define %guix-publish-accounts
980 (list (user-group (name "guix-publish") (system? #t))
982 (name "guix-publish")
983 (group "guix-publish")
985 (comment "guix publish user")
986 (home-directory "/var/empty")
987 (shell #~(string-append #$shadow "/sbin/nologin")))))
989 (define guix-publish-service-type
990 (service-type (name 'guix-publish)
992 (list (service-extension shepherd-root-service-type
993 guix-publish-shepherd-service)
994 (service-extension account-service-type
995 (const %guix-publish-accounts))))))
997 (define* (guix-publish-service #:key (guix guix) (port 80) (host "localhost"))
998 "Return a service that runs @command{guix publish} listening on @var{host}
999 and @var{port} (@pxref{Invoking guix publish}).
1001 This assumes that @file{/etc/guix} already contains a signing key pair as
1002 created by @command{guix archive --generate-key} (@pxref{Invoking guix
1003 archive}). If that is not the case, the service will fail to start."
1004 (service guix-publish-service-type
1005 (guix-publish-configuration (guix guix) (port port) (host host))))
1012 (define-record-type* <udev-configuration>
1013 udev-configuration make-udev-configuration
1015 (udev udev-configuration-udev ;<package>
1017 (rules udev-configuration-rules ;list of <package>
1020 (define (udev-rules-union packages)
1021 "Return the union of the @code{lib/udev/rules.d} directories found in each
1022 item of @var{packages}."
1025 (use-modules (guix build union)
1030 (define %standard-locations
1031 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
1033 (define (rules-sub-directory directory)
1034 ;; Return the sub-directory of DIRECTORY containing udev rules, or
1035 ;; #f if none was found.
1036 (find directory-exists?
1037 (map (cut string-append directory <>) %standard-locations)))
1039 (mkdir-p (string-append #$output "/lib/udev"))
1040 (union-build (string-append #$output "/lib/udev/rules.d")
1041 (filter-map rules-sub-directory '#$packages))))
1043 (computed-file "udev-rules" build
1044 #:modules '((guix build union)
1045 (guix build utils))))
1047 (define (udev-rule file-name contents)
1048 "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
1049 (computed-file file-name
1051 (use-modules (guix build utils))
1054 (string-append #$output "/lib/udev/rules.d"))
1057 (call-with-output-file
1058 (string-append rules.d "/" #$file-name)
1060 (display #$contents port))))
1061 #:modules '((guix build utils))))
1063 (define kvm-udev-rule
1064 ;; Return a directory with a udev rule that changes the group of /dev/kvm to
1065 ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
1066 ;; but now we have to add it by ourselves.
1068 ;; Build users are part of the "kvm" group, so we can fearlessly make
1069 ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
1070 (udev-rule "90-kvm.rules"
1071 "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
1073 (define udev-shepherd-service
1074 ;; Return a <shepherd-service> for UDEV with RULES.
1076 (($ <udev-configuration> udev rules)
1077 (let* ((rules (udev-rules-union (cons* udev kvm-udev-rule rules)))
1078 (udev.conf (computed-file "udev.conf"
1079 #~(call-with-output-file #$output
1082 "udev_rules=\"~a/lib/udev/rules.d\"\n"
1088 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
1090 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
1091 (requirement '(root-file-system))
1093 (documentation "Populate the /dev directory, dynamically.")
1096 (@ (srfi srfi-1) find))
1099 ;; Choose the right 'udevd'.
1101 (map (lambda (suffix)
1102 (string-append #$udev suffix))
1103 '("/libexec/udev/udevd" ;udev
1104 "/sbin/udevd")))) ;eudev
1106 (define (wait-for-udevd)
1107 ;; Wait until someone's listening on udevd's control
1109 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
1111 (catch 'system-error
1113 (connect sock PF_UNIX "/run/udev/control")
1116 (format #t "waiting for udevd...~%")
1120 ;; Allow udev to find the modules.
1121 (setenv "LINUX_MODULE_DIRECTORY"
1122 "/run/booted-system/kernel/lib/modules")
1124 ;; The first one is for udev, the second one for eudev.
1125 (setenv "UDEV_CONFIG_FILE" #$udev.conf)
1126 (setenv "EUDEV_RULES_DIRECTORY"
1127 (string-append #$rules "/lib/udev/rules.d"))
1129 (let ((pid (primitive-fork)))
1132 (exec-command (list udevd)))
1134 ;; Wait until udevd is up and running. This
1135 ;; appears to be needed so that the events
1136 ;; triggered below are actually handled.
1139 ;; Trigger device node creation.
1140 (system* (string-append #$udev "/bin/udevadm")
1141 "trigger" "--action=add")
1143 ;; Wait for things to settle down.
1144 (system* (string-append #$udev "/bin/udevadm")
1147 (stop #~(make-kill-destructor))
1149 ;; When halting the system, 'udev' is actually killed by
1150 ;; 'user-processes', i.e., before its own 'stop' method was called.
1151 ;; Thus, make sure it is not respawned.
1154 (define udev-service-type
1155 (service-type (name 'udev)
1157 (list (service-extension shepherd-root-service-type
1158 udev-shepherd-service)))
1160 (compose concatenate) ;concatenate the list of rules
1161 (extend (lambda (config rules)
1163 (($ <udev-configuration> udev initial-rules)
1166 (rules (append initial-rules rules)))))))))
1168 (define* (udev-service #:key (udev eudev) (rules '()))
1169 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
1170 extra rules from the packages listed in @var{rules}."
1171 (service udev-service-type
1172 (udev-configuration (udev udev) (rules rules))))
1174 (define device-mapping-service-type
1175 (shepherd-service-type
1178 ((target open close)
1180 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
1181 (requirement '(udev))
1182 (documentation "Map a device node using Linux's device mapper.")
1183 (start #~(lambda () #$open))
1184 (stop #~(lambda _ (not #$close)))
1187 (define (device-mapping-service target open close)
1188 "Return a service that maps device @var{target}, a string such as
1189 @code{\"home\"} (meaning @code{/dev/mapper/home}). Evaluate @var{open}, a
1190 gexp, to open it, and evaluate @var{close} to close it."
1191 (service device-mapping-service-type
1192 (list target open close)))
1194 (define swap-service-type
1195 (shepherd-service-type
1199 (if (string-prefix? "/dev/mapper/" device)
1200 (list (symbol-append 'device-mapping-
1201 (string->symbol (basename device))))
1205 (provision (list (symbol-append 'swap- (string->symbol device))))
1206 (requirement `(udev ,@requirement))
1207 (documentation "Enable the given swap device.")
1209 (restart-on-EINTR (swapon #$device))
1212 (restart-on-EINTR (swapoff #$device))
1216 (define (swap-service device)
1217 "Return a service that uses @var{device} as a swap device."
1218 (service swap-service-type device))
1221 (define-record-type* <gpm-configuration>
1222 gpm-configuration make-gpm-configuration gpm-configuration?
1223 (gpm gpm-configuration-gpm) ;package
1224 (options gpm-configuration-options)) ;list of strings
1226 (define gpm-shepherd-service
1228 (($ <gpm-configuration> gpm options)
1229 (list (shepherd-service
1230 (requirement '(udev))
1233 ;; 'gpm' runs in the background and sets a PID file.
1234 ;; Note that it requires running as "root".
1235 (false-if-exception (delete-file "/var/run/gpm.pid"))
1236 (fork+exec-command (list (string-append #$gpm "/sbin/gpm")
1239 ;; Wait for the PID file to appear; declare failure if
1240 ;; it doesn't show up.
1242 (or (file-exists? "/var/run/gpm.pid")
1250 ;; Return #f if successfully stopped.
1251 (not (zero? (system* (string-append #$gpm "/sbin/gpm")
1254 (define gpm-service-type
1255 (service-type (name 'gpm)
1257 (list (service-extension shepherd-root-service-type
1258 gpm-shepherd-service)))))
1260 (define* (gpm-service #:key (gpm gpm)
1261 (options '("-m" "/dev/input/mice" "-t" "ps2")))
1262 "Run @var{gpm}, the general-purpose mouse daemon, with the given
1263 command-line @var{options}. GPM allows users to use the mouse in the console,
1264 notably to select, copy, and paste text. The default value of @var{options}
1265 uses the @code{ps2} protocol, which works for both USB and PS/2 mice.
1267 This service is not part of @var{%base-services}."
1268 ;; To test in QEMU, use "-usbdevice mouse" and then, in the monitor, use
1269 ;; "info mice" and "mouse_set X" to use the right mouse.
1270 (service gpm-service-type
1271 (gpm-configuration (gpm gpm) (options options))))
1274 (define %base-services
1275 ;; Convenience variable holding the basic services.
1276 (let ((motd (plain-file "motd" "
1277 This is the GNU operating system, welcome!\n\n")))
1278 (list (console-font-service "tty1")
1279 (console-font-service "tty2")
1280 (console-font-service "tty3")
1281 (console-font-service "tty4")
1282 (console-font-service "tty5")
1283 (console-font-service "tty6")
1285 (mingetty-service (mingetty-configuration
1286 (tty "tty1") (motd motd)))
1287 (mingetty-service (mingetty-configuration
1288 (tty "tty2") (motd motd)))
1289 (mingetty-service (mingetty-configuration
1290 (tty "tty3") (motd motd)))
1291 (mingetty-service (mingetty-configuration
1292 (tty "tty4") (motd motd)))
1293 (mingetty-service (mingetty-configuration
1294 (tty "tty5") (motd motd)))
1295 (mingetty-service (mingetty-configuration
1296 (tty "tty6") (motd motd)))
1298 (static-networking-service "lo" "127.0.0.1"
1299 #:provision '(loopback))
1304 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
1305 ;; used, so enable them by default. The FUSE and ALSA rules are
1306 ;; less critical, but handy.
1307 (udev-service #:rules (list lvm2 fuse alsa-utils crda)))))
1309 ;;; base.scm ends here