1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
4 ;;; Copyright © 2015, 2016, 2020 Mark H Weaver <mhw@netris.org>
5 ;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
6 ;;; Copyright © 2016, 2017 Leo Famulari <leo@famulari.name>
7 ;;; Copyright © 2016 David Craven <david@craven.ch>
8 ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
9 ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
10 ;;; Copyright © 2019 Efraim Flashner <efraim@flashner.co.il>
11 ;;; Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr>
12 ;;; Copyright © 2019 John Soo <jsoo1@asu.edu>
13 ;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
14 ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
15 ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
17 ;;; This file is part of GNU Guix.
19 ;;; GNU Guix is free software; you can redistribute it and/or modify it
20 ;;; under the terms of the GNU General Public License as published by
21 ;;; the Free Software Foundation; either version 3 of the License, or (at
22 ;;; your option) any later version.
24 ;;; GNU Guix is distributed in the hope that it will be useful, but
25 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;;; GNU General Public License for more details.
29 ;;; You should have received a copy of the GNU General Public License
30 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
32 (define-module (gnu services base)
33 #:use-module (guix store)
34 #:use-module (guix deprecation)
35 #:use-module (gnu services)
36 #:use-module (gnu services admin)
37 #:use-module (gnu services shepherd)
38 #:use-module (gnu system pam)
39 #:use-module (gnu system shadow) ; 'user-account', etc.
40 #:use-module (gnu system uuid)
41 #:use-module (gnu system file-systems) ; 'file-system', etc.
42 #:use-module (gnu system mapped-devices)
43 #:use-module ((gnu system linux-initrd)
44 #:select (file-system-packages))
45 #:use-module (gnu packages admin)
46 #:use-module ((gnu packages linux)
47 #:select (alsa-utils crda eudev e2fsprogs fuse gpm kbd lvm2 rng-tools))
48 #:use-module (gnu packages bash)
49 #:use-module ((gnu packages base)
50 #:select (coreutils glibc glibc-utf8-locales))
51 #:use-module (gnu packages package-management)
52 #:use-module ((gnu packages gnupg) #:select (guile-gcrypt))
53 #:use-module (gnu packages linux)
54 #:use-module (gnu packages terminals)
55 #:use-module ((gnu build file-systems)
56 #:select (mount-flags->bit-mask))
57 #:use-module (guix gexp)
58 #:use-module (guix records)
59 #:use-module (guix modules)
60 #:use-module ((guix self) #:select (make-config.scm))
61 #:use-module (srfi srfi-1)
62 #:use-module (srfi srfi-26)
63 #:use-module (ice-9 match)
64 #:use-module (ice-9 format)
65 #:re-export (user-processes-service-type ;backwards compatibility
66 %default-substitute-urls)
67 #:export (fstab-service-type
68 root-file-system-service
69 file-system-service-type
72 console-keymap-service
74 console-font-service-type
76 virtual-terminal-service-type
81 static-networking-interface
83 static-networking-netmask
84 static-networking-gateway
85 static-networking-requirement
87 static-networking-service
88 static-networking-service-type
92 udev-configuration-rules
105 agetty-configuration?
109 mingetty-configuration
110 mingetty-configuration-tty
111 mingetty-configuration-auto-login
112 mingetty-configuration-login-program
113 mingetty-configuration-login-pause?
114 mingetty-configuration-clear-on-logout?
115 mingetty-configuration-mingetty
116 mingetty-configuration?
118 mingetty-service-type
121 %nscd-default-configuration
133 syslog-configuration?
138 %default-authorized-guix-keys
142 guix-configuration-guix
143 guix-configuration-build-group
144 guix-configuration-build-accounts
145 guix-configuration-authorize-key?
146 guix-configuration-authorized-keys
147 guix-configuration-use-substitutes?
148 guix-configuration-substitute-urls
149 guix-configuration-extra-options
150 guix-configuration-log-file
154 guix-publish-configuration
155 guix-publish-configuration?
156 guix-publish-configuration-guix
157 guix-publish-configuration-port
158 guix-publish-configuration-host
159 guix-publish-configuration-compression
160 guix-publish-configuration-compression-level ;deprecated
161 guix-publish-configuration-nar-path
162 guix-publish-configuration-cache
163 guix-publish-configuration-ttl
165 guix-publish-service-type
172 urandom-seed-service-type
181 kmscon-configuration?
184 pam-limits-service-type
193 ;;; Base system services---i.e., services that 99% of the users will want to
204 (define (file-system->fstab-entry file-system)
205 "Return a @file{/etc/fstab} entry for @var{file-system}."
206 (string-append (match (file-system-device file-system)
207 ((? file-system-label? label)
208 (string-append "LABEL="
209 (file-system-label->string label)))
211 (string-append "UUID=" (uuid->string uuid)))
215 (file-system-mount-point file-system) "\t"
216 (file-system-type file-system) "\t"
217 (or (file-system-options file-system) "defaults") "\t"
219 ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
220 ;; don't have anything sensible to put in there.
223 (define (file-systems->fstab file-systems)
224 "Return a @file{/etc} entry for an @file{fstab} describing
226 `(("fstab" ,(plain-file "fstab"
229 # This file was generated from your Guix configuration. Any changes
230 # will be lost upon reboot or reconfiguration.\n\n"
231 (string-join (map file-system->fstab-entry
236 (define fstab-service-type
237 ;; The /etc/fstab service.
238 (service-type (name 'fstab)
240 (list (service-extension etc-service-type
241 file-systems->fstab)))
242 (compose concatenate)
245 "Populate the @file{/etc/fstab} based on the given file
248 (define %root-file-system-shepherd-service
250 (documentation "Take care of the root file system.")
251 (provision '(root-file-system))
254 ;; Return #f if successfully stopped.
257 (call-with-blocked-asyncs
259 (let ((null (%make-void-port "w")))
260 ;; Close 'shepherd.log'.
261 (display "closing log\n")
262 ((@ (shepherd comm) stop-logging))
264 ;; Redirect the default output ports..
265 (set-current-output-port null)
266 (set-current-error-port null)
268 ;; Close /dev/console.
269 (for-each close-fdes '(0 1 2))
271 ;; At this point, there are no open files left, so the
272 ;; root file system can be re-mounted read-only.
274 (logior MS_REMOUNT MS_RDONLY)
280 (define root-file-system-service-type
281 (shepherd-service-type 'root-file-system
282 (const %root-file-system-shepherd-service)))
284 (define (root-file-system-service)
285 "Return a service whose sole purpose is to re-mount read-only the root file
286 system upon shutdown (aka. cleanly \"umounting\" root.)
288 This service must be the root of the service dependency graph so that its
289 'stop' action is invoked when shepherd is the only process left."
290 (service root-file-system-service-type #f))
292 (define (file-system->shepherd-service-name file-system)
293 "Return the symbol that denotes the service mounting and unmounting
295 (symbol-append 'file-system-
296 (string->symbol (file-system-mount-point file-system))))
298 (define (mapped-device->shepherd-service-name md)
299 "Return the symbol that denotes the shepherd service of MD, a <mapped-device>."
300 (symbol-append 'device-mapping-
301 (string->symbol (string-join
302 (mapped-device-targets md) "-"))))
304 (define dependency->shepherd-service-name
306 ((? mapped-device? md)
307 (mapped-device->shepherd-service-name md))
309 (file-system->shepherd-service-name fs))))
311 (define (file-system-shepherd-service file-system)
312 "Return the shepherd service for @var{file-system}, or @code{#f} if
313 @var{file-system} is not auto-mounted upon boot."
314 (let ((target (file-system-mount-point file-system))
315 (create? (file-system-create-mount-point? file-system))
316 (dependencies (file-system-dependencies file-system))
317 (packages (file-system-packages (list file-system))))
318 (and (file-system-mount? file-system)
319 (with-imported-modules (source-module-closure
320 '((gnu build file-systems)))
322 (provision (list (file-system->shepherd-service-name file-system)))
323 (requirement `(root-file-system udev
324 ,@(map dependency->shepherd-service-name dependencies)))
325 (documentation "Check, mount, and unmount the given file system.")
326 (start #~(lambda args
331 (let (($PATH (getenv "PATH")))
332 ;; Make sure fsck.ext2 & co. can be found.
335 ;; Don’t display the PATH settings.
336 (with-output-to-port (%make-void-port "w")
338 (set-path-environment-variable "PATH"
344 '#$(file-system->spec file-system))
347 (setenv "PATH" $PATH)))
350 ;; Normally there are no processes left at this point, so
351 ;; TARGET can be safely unmounted.
353 ;; Make sure PID 1 doesn't keep TARGET busy.
359 ;; We need additional modules.
360 (modules `(((gnu build file-systems)
361 #:select (mount-file-system))
362 (gnu system file-systems)
363 ,@%default-modules)))))))
365 (define (file-system-shepherd-services file-systems)
366 "Return the list of Shepherd services for FILE-SYSTEMS."
367 (let* ((file-systems (filter file-system-mount? file-systems)))
370 (provision '(file-systems))
371 (requirement (cons* 'root-file-system 'user-file-systems
372 (map file-system->shepherd-service-name
374 (documentation "Target for all the initially-mounted file systems")
376 (stop #~(const #f))))
378 (define known-mount-points
379 (map file-system-mount-point file-systems))
383 (documentation "Unmount manually-mounted file systems.")
384 (provision '(user-file-systems))
387 (define (known? mount-point)
389 (cons* "/proc" "/sys" '#$known-mount-points)))
391 ;; Make sure we don't keep the user's mount points busy.
394 (for-each (lambda (mount-point)
395 (format #t "unmounting '~a'...~%" mount-point)
398 (umount mount-point))
400 (let ((errno (system-error-errno args)))
401 (format #t "failed to unmount '~a': ~a~%"
402 mount-point (strerror errno))))))
403 (filter (negate known?) (mount-points)))
406 (cons* sink user-unmount
407 (map file-system-shepherd-service file-systems))))
409 (define (file-system-fstab-entries file-systems)
410 "Return the subset of @var{file-systems} that should have an entry in
412 ;; /etc/fstab is about telling fsck(8), mount(8), and umount(8) about
413 ;; relevant file systems they'll have to deal with. That excludes "pseudo"
416 ;; In particular, things like GIO (part of GLib) use it to determine the set
417 ;; of mounts, which is then used by graphical file managers and desktop
418 ;; environments to display "volume" icons. Thus, we really need to exclude
419 ;; those pseudo file systems from the list.
420 (remove (lambda (file-system)
421 (or (member (file-system-type file-system)
422 %pseudo-file-system-types)
423 (memq 'bind-mount (file-system-flags file-system))))
426 (define file-system-service-type
427 (service-type (name 'file-systems)
429 (list (service-extension shepherd-root-service-type
430 file-system-shepherd-services)
431 (service-extension fstab-service-type
432 file-system-fstab-entries)
434 ;; Have 'user-processes' depend on 'file-systems'.
435 (service-extension user-processes-service-type
436 (const '(file-systems)))))
437 (compose concatenate)
440 "Provide Shepherd services to mount and unmount the given
441 file systems, as well as corresponding @file{/etc/fstab} entries.")))
446 ;;; Preserve entropy to seed /dev/urandom on boot.
449 (define %random-seed-file
450 "/var/lib/random-seed")
452 (define (urandom-seed-shepherd-service _)
453 "Return a shepherd service for the /dev/urandom seed."
454 (list (shepherd-service
455 (documentation "Preserve entropy across reboots for /dev/urandom.")
456 (provision '(urandom-seed))
458 ;; Depend on udev so that /dev/hwrng is available.
459 (requirement '(file-systems udev))
462 ;; On boot, write random seed into /dev/urandom.
463 (when (file-exists? #$%random-seed-file)
464 (call-with-input-file #$%random-seed-file
466 (call-with-output-file "/dev/urandom"
468 (dump-port seed urandom)
470 ;; Writing SEED to URANDOM isn't enough: we must
471 ;; also tell the kernel to account for these
472 ;; extra bits of entropy.
473 (let ((bits (* 8 (stat:size (stat seed)))))
474 (add-to-entropy-count urandom bits)))))))
476 ;; Try writing from /dev/hwrng into /dev/urandom.
477 ;; It seems that the file /dev/hwrng always exists, even
478 ;; when there is no hardware random number generator
479 ;; available. So, we handle a failed read or any other error
480 ;; reported by the operating system.
481 (let ((buf (catch 'system-error
483 (call-with-input-file "/dev/hwrng"
485 (get-bytevector-n hwrng 512))))
486 ;; Silence is golden...
489 (call-with-output-file "/dev/urandom"
491 (put-bytevector urandom buf)
492 (let ((bits (* 8 (bytevector-length buf))))
493 (add-to-entropy-count urandom bits))))))
495 ;; Immediately refresh the seed in case the system doesn't
496 ;; shut down cleanly.
497 (call-with-input-file "/dev/urandom"
499 (let ((previous-umask (umask #o077))
500 (buf (make-bytevector 512)))
501 (mkdir-p (dirname #$%random-seed-file))
502 (get-bytevector-n! urandom buf 0 512)
503 (call-with-output-file #$%random-seed-file
505 (put-bytevector seed buf)))
506 (umask previous-umask))))
509 ;; During shutdown, write from /dev/urandom into random seed.
510 (let ((buf (make-bytevector 512)))
511 (call-with-input-file "/dev/urandom"
513 (let ((previous-umask (umask #o077)))
514 (get-bytevector-n! urandom buf 0 512)
515 (mkdir-p (dirname #$%random-seed-file))
516 (call-with-output-file #$%random-seed-file
518 (put-bytevector seed buf)))
519 (umask previous-umask))
521 (modules `((rnrs bytevectors)
523 ,@%default-modules)))))
525 (define urandom-seed-service-type
526 (service-type (name 'urandom-seed)
528 (list (service-extension shepherd-root-service-type
529 urandom-seed-shepherd-service)
531 ;; Have 'user-processes' depend on 'urandom-seed'.
532 ;; This ensures that user processes and daemons don't
533 ;; start until we have seeded the PRNG.
534 (service-extension user-processes-service-type
535 (const '(urandom-seed)))))
538 "Seed the @file{/dev/urandom} pseudo-random number
539 generator (RNG) with the value recorded when the system was last shut
542 (define-deprecated (urandom-seed-service)
543 urandom-seed-service-type
544 (service urandom-seed-service-type))
548 ;;; Add hardware random number generator to entropy pool.
551 (define-record-type* <rngd-configuration>
552 rngd-configuration make-rngd-configuration
554 (rng-tools rngd-configuration-rng-tools) ;package
555 (device rngd-configuration-device)) ;string
557 (define rngd-service-type
558 (shepherd-service-type
561 (define rng-tools (rngd-configuration-rng-tools config))
562 (define device (rngd-configuration-device config))
565 (list (file-append rng-tools "/sbin/rngd")
569 (documentation "Add TRNG to entropy pool.")
570 (requirement '(udev))
572 (start #~(make-forkexec-constructor '#$rngd-command))
573 (stop #~(make-kill-destructor))))))
575 (define* (rngd-service #:key
576 (rng-tools rng-tools)
577 (device "/dev/hwrng"))
578 "Return a service that runs the @command{rngd} program from @var{rng-tools}
579 to add @var{device} to the kernel's entropy pool. The service will fail if
580 @var{device} does not exist."
581 (service rngd-service-type
583 (rng-tools rng-tools)
591 (define host-name-service-type
592 (shepherd-service-type
596 (documentation "Initialize the machine's host name.")
597 (provision '(host-name))
599 (sethostname #$name)))
602 (define (host-name-service name)
603 "Return a service that sets the host name to @var{name}."
604 (service host-name-service-type name))
606 (define virtual-terminal-service-type
607 ;; Ensure that virtual terminals run in UTF-8 mode. This is the case by
608 ;; default with recent Linux kernels, but this service allows us to ensure
609 ;; this. This service must start before any 'term-' service so that newly
610 ;; created terminals inherit this property. See
611 ;; <https://bugs.gnu.org/30505> for a discussion.
612 (shepherd-service-type
615 (let ((knob "/sys/module/vt/parameters/default_utf8"))
617 (documentation "Set virtual terminals in UTF-8 module.")
618 (provision '(virtual-terminal))
619 (requirement '(root-file-system))
621 ;; In containers /sys is read-only so don't insist on
622 ;; writing to this file.
623 (unless (= 1 (call-with-input-file #$knob read))
624 (call-with-output-file #$knob
628 (stop #~(const #f)))))
629 #t)) ;default to UTF-8
631 (define console-keymap-service-type
632 (shepherd-service-type
636 (documentation (string-append "Load console keymap (loadkeys)."))
637 (provision '(console-keymap))
639 (zero? (system* #$(file-append kbd "/bin/loadkeys")
643 (define-deprecated (console-keymap-service #:rest files)
645 "Return a service to load console keymaps from @var{files}."
646 (service console-keymap-service-type files))
648 (define %default-console-font
649 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
650 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
651 ;; codepoints notably found in the UTF-8 manual.
654 (define (console-font-shepherd-services tty+font)
655 "Return a list of Shepherd services for each pair in TTY+FONT."
658 (let ((device (string-append "/dev/" tty)))
660 (documentation "Load a Unicode console font.")
661 (provision (list (symbol-append 'console-font-
662 (string->symbol tty))))
664 ;; Start after mingetty has been started on TTY, otherwise the settings
666 (requirement (list (symbol-append 'term-
667 (string->symbol tty))))
670 ;; It could be that mingetty is not fully ready yet,
671 ;; which we check by calling 'ttyname'.
673 (unless (or (zero? i)
674 (call-with-input-file #$device
676 (false-if-exception (ttyname port)))))
680 ;; Assume the VT is already in UTF-8 mode, thanks to
681 ;; the 'virtual-terminal' service.
683 ;; 'setfont' returns EX_OSERR (71) when an
684 ;; KDFONTOP ioctl fails, for example. Like
685 ;; systemd's vconsole support, let's not treat
687 (case (status:exit-val
688 (system* #$(file-append kbd "/bin/setfont")
689 "-C" #$device #$font))
696 (define console-font-service-type
697 (service-type (name 'console-fonts)
699 (list (service-extension shepherd-root-service-type
700 console-font-shepherd-services)))
701 (compose concatenate)
704 "Install the given fonts on the specified ttys (fonts are per
705 virtual console on GNU/Linux). The value of this service is a list of
706 tty/font pairs. The font can be the name of a font provided by the @code{kbd}
707 package or any valid argument to @command{setfont}, as in this example:
710 `((\"tty1\" . \"LatGrkCyr-8x16\")
711 (\"tty2\" . ,(file-append
713 \"/share/kbd/consolefonts/TamzenForPowerline10x20.psf\"))
714 (\"tty3\" . ,(file-append
716 \"/share/consolefonts/ter-132n\"))) ; for HDPI
719 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
720 "This procedure is deprecated in favor of @code{console-font-service-type}.
722 Return a service that sets up Unicode support in @var{tty} and loads
723 @var{font} for that tty (fonts are per virtual console in Linux.)"
724 (simple-service (symbol-append 'console-font- (string->symbol tty))
725 console-font-service-type `((,tty . ,font))))
727 (define %default-motd
728 (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
730 (define-record-type* <login-configuration>
731 login-configuration make-login-configuration
733 (motd login-configuration-motd ;file-like
734 (default %default-motd))
735 ;; Allow empty passwords by default so that first-time users can log in when
736 ;; the 'root' account has just been created.
737 (allow-empty-passwords? login-configuration-allow-empty-passwords?
738 (default #t))) ;Boolean
740 (define (login-pam-service config)
741 "Return the list of PAM service needed for CONF."
742 ;; Let 'login' be known to PAM.
743 (list (unix-pam-service "login"
745 #:allow-empty-passwords?
746 (login-configuration-allow-empty-passwords? config)
748 (login-configuration-motd config))))
750 (define login-service-type
751 (service-type (name 'login)
752 (extensions (list (service-extension pam-root-service-type
754 (default-value (login-configuration))
756 "Provide a console log-in service as specified by its
757 configuration value, a @code{login-configuration} object.")))
759 (define* (login-service #:optional (config (login-configuration)))
760 "Return a service configure login according to @var{config}, which specifies
761 the message of the day, among other things."
762 (service login-service-type config))
764 (define-record-type* <agetty-configuration>
765 agetty-configuration make-agetty-configuration
766 agetty-configuration?
767 (agetty agetty-configuration-agetty ;<package>
768 (default util-linux))
769 (tty agetty-configuration-tty) ;string | #f
770 (term agetty-term ;string | #f
772 (baud-rate agetty-baud-rate ;string | #f
774 (auto-login agetty-auto-login ;list of strings | #f
776 (login-program agetty-login-program ;gexp
777 (default (file-append shadow "/bin/login")))
778 (login-pause? agetty-login-pause? ;Boolean
780 (eight-bits? agetty-eight-bits? ;Boolean
782 (no-reset? agetty-no-reset? ;Boolean
784 (remote? agetty-remote? ;Boolean
786 (flow-control? agetty-flow-control? ;Boolean
788 (host agetty-host ;string | #f
790 (no-issue? agetty-no-issue? ;Boolean
792 (init-string agetty-init-string ;string | #f
794 (no-clear? agetty-no-clear? ;Boolean
796 (local-line agetty-local-line ;always | never | auto
798 (extract-baud? agetty-extract-baud? ;Boolean
800 (skip-login? agetty-skip-login? ;Boolean
802 (no-newline? agetty-no-newline? ;Boolean
804 (login-options agetty-login-options ;string | #f
806 (chroot agetty-chroot ;string | #f
808 (hangup? agetty-hangup? ;Boolean
810 (keep-baud? agetty-keep-baud? ;Boolean
812 (timeout agetty-timeout ;integer | #f
814 (detect-case? agetty-detect-case? ;Boolean
816 (wait-cr? agetty-wait-cr? ;Boolean
818 (no-hints? agetty-no-hints? ;Boolean
820 (no-hostname? agetty-no hostname? ;Boolean
822 (long-hostname? agetty-long-hostname? ;Boolean
824 (erase-characters agetty-erase-characters ;string | #f
826 (kill-characters agetty-kill-characters ;string | #f
828 (chdir agetty-chdir ;string | #f
830 (delay agetty-delay ;integer | #f
832 (nice agetty-nice ;integer | #f
834 ;; "Escape hatch" for passing arbitrary command-line arguments.
835 (extra-options agetty-extra-options ;list of strings
837 ;;; XXX Unimplemented for now!
838 ;;; (issue-file agetty-issue-file ;file-like
842 (define (default-serial-port)
843 "Return a gexp that determines a reasonable default serial port
844 to use as the tty. This is primarily useful for headless systems."
845 (with-imported-modules (source-module-closure
846 '((gnu build linux-boot))) ;for 'find-long-options'
848 ;; console=device,options
849 ;; device: can be tty0, ttyS0, lp0, ttyUSB0 (serial).
850 ;; options: BBBBPNF. P n|o|e, N number of bits,
851 ;; F flow control (r RTS)
852 (let* ((not-comma (char-set-complement (char-set #\,)))
853 (command (linux-command-line))
854 (agetty-specs (find-long-options "agetty.tty" command))
855 (console-specs (filter (lambda (spec)
856 (and (string-prefix? "tty" spec)
858 (string-prefix? "tty0" spec)
859 (string-prefix? "tty1" spec)
860 (string-prefix? "tty2" spec)
861 (string-prefix? "tty3" spec)
862 (string-prefix? "tty4" spec)
863 (string-prefix? "tty5" spec)
864 (string-prefix? "tty6" spec)
865 (string-prefix? "tty7" spec)
866 (string-prefix? "tty8" spec)
867 (string-prefix? "tty9" spec)))))
868 (find-long-options "console" command)))
869 (specs (append agetty-specs console-specs)))
873 ;; Extract device name from first spec.
874 (match (string-tokenize spec not-comma)
878 (define agetty-shepherd-service
880 (($ <agetty-configuration> agetty tty term baud-rate auto-login
881 login-program login-pause? eight-bits? no-reset? remote? flow-control?
882 host no-issue? init-string no-clear? local-line extract-baud?
883 skip-login? no-newline? login-options chroot hangup? keep-baud? timeout
884 detect-case? wait-cr? no-hints? no-hostname? long-hostname?
885 erase-characters kill-characters chdir delay nice extra-options)
888 (documentation "Run agetty on a tty.")
889 (provision (list (symbol-append 'term- (string->symbol (or tty "auto")))))
891 ;; Since the login prompt shows the host name, wait for the 'host-name'
892 ;; service to be done. Also wait for udev essentially so that the tty
893 ;; text is not lost in the middle of kernel messages (see also
894 ;; mingetty-shepherd-service).
895 (requirement '(user-processes host-name udev))
897 (modules '((ice-9 match) (gnu build linux-boot)))
899 (with-imported-modules (source-module-closure
900 '((gnu build linux-boot)))
902 (let ((defaulted-tty #$(or tty (default-serial-port))))
905 (make-forkexec-constructor
906 (list #$(file-append util-linux "/sbin/agetty")
927 #~("--init-string" #$init-string)
932 ;;; FIXME This doesn't work as expected. According to agetty(8), if this option
933 ;;; is not passed, then the default is 'auto'. However, in my tests, when that
934 ;;; option is selected, agetty never presents the login prompt, and the
935 ;;; term-ttyS0 service respawns every few seconds.
937 #~(#$(match local-line
938 ('auto "--local-line=auto")
939 ('always "--local-line=always")
940 ('never "-local-line=never")))
955 #~("--login-options" #$login-options)
958 #~("--chroot" #$chroot)
967 #~("--timeout" #$(number->string timeout))
981 #$@(if long-hostname?
982 #~("--long-hostname")
984 #$@(if erase-characters
985 #~("--erase-chars" #$erase-characters)
987 #$@(if kill-characters
988 #~("--kill-chars" #$kill-characters)
991 #~("--chdir" #$chdir)
994 #~("--delay" #$(number->string delay))
997 #~("--nice" #$(number->string nice))
1000 (list "--autologin" auto-login)
1002 #$@(if login-program
1003 #~("--login-program" #$login-program)
1015 (const #f)) ; never start.
1017 (stop #~(make-kill-destructor)))))))
1019 (define agetty-service-type
1020 (service-type (name 'agetty)
1021 (extensions (list (service-extension shepherd-root-service-type
1022 agetty-shepherd-service)))
1024 "Provide console login using the @command{agetty}
1027 (define* (agetty-service config)
1028 "Return a service to run agetty according to @var{config}, which specifies
1029 the tty to run, among other things."
1030 (service agetty-service-type config))
1032 (define-record-type* <mingetty-configuration>
1033 mingetty-configuration make-mingetty-configuration
1034 mingetty-configuration?
1035 (mingetty mingetty-configuration-mingetty ;<package>
1037 (tty mingetty-configuration-tty) ;string
1038 (auto-login mingetty-auto-login ;string | #f
1040 (login-program mingetty-login-program ;gexp
1042 (login-pause? mingetty-login-pause? ;Boolean
1044 (clear-on-logout? mingetty-clear-on-logout? ;Boolean
1047 (define mingetty-shepherd-service
1049 (($ <mingetty-configuration> mingetty tty auto-login login-program
1050 login-pause? clear-on-logout?)
1053 (documentation "Run mingetty on an tty.")
1054 (provision (list (symbol-append 'term- (string->symbol tty))))
1056 ;; Since the login prompt shows the host name, wait for the 'host-name'
1057 ;; service to be done. Also wait for udev essentially so that the tty
1058 ;; text is not lost in the middle of kernel messages (XXX).
1059 (requirement '(user-processes host-name udev virtual-terminal))
1061 (start #~(make-forkexec-constructor
1062 (list #$(file-append mingetty "/sbin/mingetty")
1064 ;; Avoiding 'vhangup' allows us to avoid 'setfont'
1065 ;; errors down the path where various ioctls get
1066 ;; EIO--see 'hung_up_tty_ioctl' in driver/tty/tty_io.c
1070 #$@(if clear-on-logout?
1074 #~("--autologin" #$auto-login)
1076 #$@(if login-program
1077 #~("--loginprog" #$login-program)
1082 (stop #~(make-kill-destructor)))))))
1084 (define mingetty-service-type
1085 (service-type (name 'mingetty)
1086 (extensions (list (service-extension shepherd-root-service-type
1087 mingetty-shepherd-service)))
1089 "Provide console login using the @command{mingetty}
1092 (define* (mingetty-service config)
1093 "Return a service to run mingetty according to @var{config}, which specifies
1094 the tty to run, among other things."
1095 (service mingetty-service-type config))
1097 (define-record-type* <nscd-configuration> nscd-configuration
1098 make-nscd-configuration
1100 (log-file nscd-configuration-log-file ;string
1101 (default "/var/log/nscd.log"))
1102 (debug-level nscd-debug-level ;integer
1104 ;; TODO: See nscd.conf in glibc for other options to add.
1105 (caches nscd-configuration-caches ;list of <nscd-cache>
1106 (default %nscd-default-caches))
1107 (name-services nscd-configuration-name-services ;list of <packages>
1109 (glibc nscd-configuration-glibc ;<package>
1112 (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
1114 (database nscd-cache-database) ;symbol
1115 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
1116 (negative-time-to-live nscd-cache-negative-time-to-live
1117 (default 20)) ;integer
1118 (suggested-size nscd-cache-suggested-size ;integer ("default module
1121 (check-files? nscd-cache-check-files? ;Boolean
1123 (persistent? nscd-cache-persistent? ;Boolean
1125 (shared? nscd-cache-shared? ;Boolean
1127 (max-database-size nscd-cache-max-database-size ;integer
1128 (default (* 32 (expt 2 20))))
1129 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
1132 (define %nscd-default-caches
1133 ;; Caches that we want to enable by default. Note that when providing an
1134 ;; empty nscd.conf, all caches are disabled.
1135 (list (nscd-cache (database 'hosts)
1137 ;; Aggressively cache the host name cache to improve
1138 ;; privacy and resilience.
1139 (positive-time-to-live (* 3600 12))
1140 (negative-time-to-live 20)
1143 (nscd-cache (database 'services)
1145 ;; Services are unlikely to change, so we can be even more
1147 (positive-time-to-live (* 3600 24))
1148 (negative-time-to-live 3600)
1149 (check-files? #t) ;check /etc/services changes
1152 (define %nscd-default-configuration
1153 ;; Default nscd configuration.
1154 (nscd-configuration))
1156 (define (nscd.conf-file config)
1157 "Return the @file{nscd.conf} configuration file for @var{config}, an
1158 @code{<nscd-configuration>} object."
1159 (define cache->config
1161 (($ <nscd-cache> (= symbol->string database)
1162 positive-ttl negative-ttl size check-files?
1163 persistent? shared? max-size propagate?)
1164 (string-append "\nenable-cache\t" database "\tyes\n"
1166 "positive-time-to-live\t" database "\t"
1167 (number->string positive-ttl) "\n"
1168 "negative-time-to-live\t" database "\t"
1169 (number->string negative-ttl) "\n"
1170 "suggested-size\t" database "\t"
1171 (number->string size) "\n"
1172 "check-files\t" database "\t"
1173 (if check-files? "yes\n" "no\n")
1174 "persistent\t" database "\t"
1175 (if persistent? "yes\n" "no\n")
1176 "shared\t" database "\t"
1177 (if shared? "yes\n" "no\n")
1178 "max-db-size\t" database "\t"
1179 (number->string max-size) "\n"
1180 "auto-propagate\t" database "\t"
1181 (if propagate? "yes\n" "no\n")))))
1184 (($ <nscd-configuration> log-file debug-level caches)
1185 (plain-file "nscd.conf"
1187 # Configuration of libc's name service cache daemon (nscd).\n\n"
1189 (string-append "logfile\t" log-file)
1193 (string-append "debug-level\t"
1194 (number->string debug-level))
1198 (map cache->config caches)))))))
1200 (define (nscd-action-procedure nscd config option)
1201 ;; XXX: This is duplicated from mcron; factorize.
1202 #~(lambda (_ . args)
1203 ;; Run 'nscd' in a pipe so we can explicitly redirect its output to
1204 ;; 'current-output-port', which at this stage is bound to the client
1206 (let ((pipe (apply open-pipe* OPEN_READ #$nscd
1207 "-f" #$config #$option args)))
1209 (match (read-line pipe 'concat)
1211 (catch 'system-error
1213 (zero? (close-pipe pipe)))
1215 ;; There's a race with the SIGCHLD handler, which could
1216 ;; call 'waitpid' before 'close-pipe' above does. If we
1217 ;; get ECHILD, that means we lost the race; in that case, we
1218 ;; cannot tell what the exit code was (FIXME).
1219 (or (= ECHILD (system-error-errno args))
1220 (apply throw args)))))
1225 (define (nscd-actions nscd config)
1226 "Return Shepherd actions for NSCD."
1227 ;; Make this functionality available as actions because that's a simple way
1228 ;; to run the right 'nscd' binary with the right config file.
1229 (list (shepherd-action
1231 (documentation "Display statistics about nscd usage.")
1232 (procedure (nscd-action-procedure nscd config "--statistics")))
1236 "Invalidate the given cache--e.g., 'hosts' for host name lookups.")
1237 (procedure (nscd-action-procedure nscd config "--invalidate")))))
1239 (define (nscd-shepherd-service config)
1240 "Return a shepherd service for CONFIG, an <nscd-configuration> object."
1241 (let ((nscd (file-append (nscd-configuration-glibc config)
1243 (nscd.conf (nscd.conf-file config))
1244 (name-services (nscd-configuration-name-services config)))
1245 (list (shepherd-service
1246 (documentation "Run libc's name service cache daemon (nscd).")
1248 (requirement '(user-processes))
1249 (start #~(make-forkexec-constructor
1250 (list #$nscd "-f" #$nscd.conf "--foreground")
1252 ;; Wait for the PID file. However, the PID file is
1253 ;; written before nscd is actually listening on its
1255 #:pid-file "/var/run/nscd/nscd.pid"
1257 #:environment-variables
1258 (list (string-append "LD_LIBRARY_PATH="
1261 (string-append dir "/lib"))
1262 (list #$@name-services))
1264 (stop #~(make-kill-destructor))
1265 (modules `((ice-9 popen) ;for the actions
1268 ,@%default-modules))
1269 (actions (nscd-actions nscd nscd.conf))))))
1271 (define nscd-activation
1272 ;; Actions to take before starting nscd.
1274 (use-modules (guix build utils))
1275 (mkdir-p "/var/run/nscd")
1276 (mkdir-p "/var/db/nscd") ;for the persistent cache
1278 ;; In libc 2.25 nscd uses inotify to watch /etc/resolv.conf, but only if
1279 ;; that file exists when it is started. Thus create it here. Note: on
1280 ;; some systems, such as when NetworkManager is used, /etc/resolv.conf
1281 ;; is a symlink, hence 'lstat'.
1282 (unless (false-if-exception (lstat "/etc/resolv.conf"))
1283 (call-with-output-file "/etc/resolv.conf"
1285 (display "# This is a placeholder.\n" port))))))
1287 (define nscd-service-type
1288 (service-type (name 'nscd)
1290 (list (service-extension activation-service-type
1291 (const nscd-activation))
1292 (service-extension shepherd-root-service-type
1293 nscd-shepherd-service)))
1295 ;; This can be extended by providing additional name services
1296 ;; such as nss-mdns.
1297 (compose concatenate)
1298 (extend (lambda (config name-services)
1301 (name-services (append
1302 (nscd-configuration-name-services config)
1304 (default-value %nscd-default-configuration)
1306 "Runs libc's @dfn{name service cache daemon} (nscd) with the
1307 given configuration---an @code{<nscd-configuration>} object. @xref{Name
1308 Service Switch}, for an example.")))
1310 (define* (nscd-service #:optional (config %nscd-default-configuration))
1311 "Return a service that runs libc's name service cache daemon (nscd) with the
1312 given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
1313 Service Switch}, for an example."
1314 (service nscd-service-type config))
1317 (define-record-type* <syslog-configuration>
1318 syslog-configuration make-syslog-configuration
1319 syslog-configuration?
1320 (syslogd syslog-configuration-syslogd
1321 (default (file-append inetutils "/libexec/syslogd")))
1322 (config-file syslog-configuration-config-file
1323 (default %default-syslog.conf)))
1325 (define syslog-service-type
1326 (shepherd-service-type
1330 (documentation "Run the syslog daemon (syslogd).")
1331 (provision '(syslogd))
1332 (requirement '(user-processes))
1333 (start #~(let ((spawn (make-forkexec-constructor
1334 (list #$(syslog-configuration-syslogd config)
1336 #$(syslog-configuration-config-file config))
1337 #:pid-file "/var/run/syslog.pid")))
1339 ;; Set the umask such that file permissions are #o640.
1340 (let ((mask (umask #o137))
1344 (stop #~(make-kill-destructor))))))
1346 ;; Snippet adapted from the GNU inetutils manual.
1347 (define %default-syslog.conf
1348 (plain-file "syslog.conf" "
1349 # Log all error messages, authentication messages of
1350 # level notice or higher and anything of level err or
1351 # higher to the console.
1352 # Don't log private authentication messages!
1353 *.alert;auth.notice;authpriv.none /dev/console
1355 # Log anything (except mail) of level info or higher.
1356 # Don't log private authentication messages!
1357 *.info;mail.none;authpriv.none /var/log/messages
1359 # Like /var/log/messages, but also including \"debug\"-level logs.
1360 *.debug;mail.none;authpriv.none /var/log/debug
1362 # Same, in a different place.
1363 *.info;mail.none;authpriv.none /dev/tty12
1365 # The authpriv file has restricted access.
1366 authpriv.* /var/log/secure
1368 # Log all the mail messages in one place.
1369 mail.* /var/log/maillog
1372 (define* (syslog-service #:optional (config (syslog-configuration)))
1373 "Return a service that runs @command{syslogd} and takes
1374 @var{<syslog-configuration>} as a parameter.
1376 @xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
1377 information on the configuration file syntax."
1378 (service syslog-service-type config))
1381 (define pam-limits-service-type
1382 (let ((security-limits
1383 ;; Create /etc/security containing the provided "limits.conf" file.
1384 (lambda (limits-file)
1390 (stat #$limits-file)
1391 (symlink #$limits-file
1392 (string-append #$output "/limits.conf"))))))))
1395 (let ((pam-limits (pam-entry
1396 (control "required")
1397 (module "pam_limits.so")
1398 (arguments '("conf=/etc/security/limits.conf")))))
1399 (if (member (pam-service-name pam)
1400 '("login" "su" "slim" "gdm-password" "sddm"))
1403 (session (cons pam-limits
1404 (pam-service-session pam))))
1409 (list (service-extension etc-service-type security-limits)
1410 (service-extension pam-root-service-type
1411 (lambda _ (list pam-extension)))))
1413 "Install the specified resource usage limits by populating
1414 @file{/etc/security/limits.conf} and using the @code{pam_limits}
1415 authentication module."))))
1417 (define* (pam-limits-service #:optional (limits '()))
1418 "Return a service that makes selected programs respect the list of
1419 pam-limits-entry specified in LIMITS via pam_limits.so."
1420 (service pam-limits-service-type
1421 (plain-file "limits.conf"
1422 (string-join (map pam-limits-entry->string limits)
1430 (define* (guix-build-accounts count #:key
1433 "Return a list of COUNT user accounts for Guix build users with the given
1435 (unfold (cut > <> count)
1438 (name (format #f "guixbuilder~2,'0d" n))
1442 ;; guix-daemon expects GROUP to be listed as a
1443 ;; supplementary group too:
1444 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
1445 (supplementary-groups (list group "kvm"))
1447 (comment (format #f "Guix Build User ~2d" n))
1448 (home-directory "/var/empty")
1449 (shell (file-append shadow "/sbin/nologin"))))
1454 ;; Select (guix …) and (gnu …) modules, except (guix config).
1456 (('guix 'config) #f)
1457 (('guix rest ...) #t)
1458 (('gnu rest ...) #t)
1461 (define (substitute-key-authorization keys guix)
1462 "Return a gexp with code to register KEYS, a list of files containing 'guix
1463 archive' public keys, with GUIX."
1465 (with-extensions (list guile-gcrypt)
1466 (with-imported-modules `(((guix config) => ,(make-config.scm))
1467 ,@(source-module-closure '((guix pki))
1468 #:select? not-config?))
1469 (computed-file "acl"
1471 (use-modules (guix pki)
1477 (call-with-input-file file
1478 (compose string->canonical-sexp
1482 (call-with-output-file #$output
1484 (write-acl (public-keys->acl keys)
1487 (with-imported-modules '((guix build utils))
1489 (use-modules (guix build utils))
1491 ;; If the ACL already exists, move it out of the way. Create a backup
1492 ;; if it's a regular file: it's likely that the user manually updated
1493 ;; it with 'guix archive --authorize'.
1494 (if (file-exists? "/etc/guix/acl")
1495 (if (and (symbolic-link? "/etc/guix/acl")
1496 (store-file-name? (readlink "/etc/guix/acl")))
1497 (delete-file "/etc/guix/acl")
1498 (rename-file "/etc/guix/acl" "/etc/guix/acl.bak"))
1499 (mkdir-p "/etc/guix"))
1501 ;; Installed the declared ACL.
1502 (symlink #+default-acl "/etc/guix/acl"))))
1504 (define %default-authorized-guix-keys
1505 ;; List of authorized substitute keys.
1506 (list (file-append guix "/share/guix/berlin.guix.gnu.org.pub")))
1508 (define-record-type* <guix-configuration>
1509 guix-configuration make-guix-configuration
1511 (guix guix-configuration-guix ;<package>
1513 (build-group guix-configuration-build-group ;string
1514 (default "guixbuild"))
1515 (build-accounts guix-configuration-build-accounts ;integer
1517 (authorize-key? guix-configuration-authorize-key? ;Boolean
1519 (authorized-keys guix-configuration-authorized-keys ;list of gexps
1520 (default %default-authorized-guix-keys))
1521 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
1523 (substitute-urls guix-configuration-substitute-urls ;list of strings
1524 (default %default-substitute-urls))
1525 (chroot-directories guix-configuration-chroot-directories ;list of file-like/strings
1527 (max-silent-time guix-configuration-max-silent-time ;integer
1529 (timeout guix-configuration-timeout ;integer
1531 (log-compression guix-configuration-log-compression
1533 (extra-options guix-configuration-extra-options ;list of strings
1535 (log-file guix-configuration-log-file ;string
1536 (default "/var/log/guix-daemon.log"))
1537 (http-proxy guix-http-proxy ;string | #f
1539 (tmpdir guix-tmpdir ;string | #f
1542 (define %default-guix-configuration
1543 (guix-configuration))
1545 (define shepherd-set-http-proxy-action
1546 ;; Shepherd action to change the HTTP(S) proxy.
1548 (name 'set-http-proxy)
1550 "Change the HTTP(S) proxy used by 'guix-daemon' and restart it.")
1551 (procedure #~(lambda* (_ #:optional proxy)
1552 (let ((environment (environ)))
1553 ;; A bit of a hack: communicate PROXY to the 'start'
1554 ;; method via environment variables.
1557 (format #t "changing HTTP/HTTPS \
1558 proxy of 'guix-daemon' to ~s...~%"
1560 (setenv "http_proxy" proxy))
1562 (format #t "clearing HTTP/HTTPS \
1563 proxy of 'guix-daemon'...~%")
1564 (unsetenv "http_proxy")))
1565 (action 'guix-daemon 'restart)
1566 (environ environment)
1569 (define (guix-shepherd-service config)
1570 "Return a <shepherd-service> for the Guix daemon service with CONFIG."
1571 (match-record config <guix-configuration>
1572 (guix build-group build-accounts authorize-key? authorized-keys
1573 use-substitutes? substitute-urls max-silent-time timeout
1574 log-compression extra-options log-file http-proxy tmpdir
1576 (list (shepherd-service
1577 (documentation "Run the Guix daemon.")
1578 (provision '(guix-daemon))
1579 (requirement '(user-processes))
1580 (actions (list shepherd-set-http-proxy-action))
1581 (modules '((srfi srfi-1)
1583 (gnu build shepherd)))
1585 (with-imported-modules `(((guix config) => ,(make-config.scm))
1586 ,@(source-module-closure
1587 '((gnu build shepherd))
1588 #:select? not-config?))
1591 ;; HTTP/HTTPS proxy. The 'http_proxy' variable is set by
1592 ;; the 'set-http-proxy' action.
1593 (or (getenv "http_proxy") #$http-proxy))
1595 ;; Start the guix-daemon from a container, when supported,
1596 ;; to solve an installation issue. See the comment below for
1598 (fork+exec-command/container
1599 (cons* #$(file-append guix "/bin/guix-daemon")
1600 "--build-users-group" #$build-group
1602 #$(number->string max-silent-time)
1603 "--timeout" #$(number->string timeout)
1605 #$(symbol->string log-compression)
1606 #$@(if use-substitutes?
1608 '("--no-substitutes"))
1609 "--substitute-urls" #$(string-join substitute-urls)
1612 ;; Add CHROOT-DIRECTORIES and all their dependencies
1613 ;; (if these are store items) to the chroot.
1616 (append-map (lambda (directory)
1617 (list "--chroot-directory"
1619 (call-with-input-file file
1621 '#$(map references-file
1622 chroot-directories)))
1624 ;; When running the installer, we need guix-daemon to
1625 ;; operate from within the same MNT namespace as the
1626 ;; installation container. In that case only, enter the
1627 ;; namespace of the process PID passed as start argument.
1628 ;; Otherwise, for symmetry purposes enter the caller
1629 ;; namespaces which is a no-op.
1631 ((pid) (string->number pid))
1634 #:environment-variables
1635 (append (list #$@(if tmpdir
1636 (list (string-append "TMPDIR=" tmpdir))
1639 ;; Make sure we run in a UTF-8 locale so that
1640 ;; 'guix offload' correctly restores nars
1641 ;; that contain UTF-8 file names such as
1643 ;; <https://bugs.gnu.org/32942>.
1644 (string-append "GUIX_LOCPATH="
1645 #$glibc-utf8-locales
1647 "LC_ALL=en_US.utf8")
1649 (list (string-append "http_proxy=" proxy)
1650 (string-append "https_proxy=" proxy))
1653 #:log-file #$log-file))))
1654 (stop #~(make-kill-destructor))))))
1656 (define (guix-accounts config)
1657 "Return the user accounts and user groups for CONFIG."
1659 (($ <guix-configuration> _ build-group build-accounts)
1664 ;; Use a fixed GID so that we can create the store with the right
1667 (guix-build-accounts build-accounts
1668 #:group build-group)))))
1670 (define (guix-activation config)
1671 "Return the activation gexp for CONFIG."
1673 (($ <guix-configuration> guix build-group build-accounts authorize-key? keys)
1674 ;; Assume that the store has BUILD-GROUP as its group. We could
1675 ;; otherwise call 'chown' here, but the problem is that on a COW overlayfs,
1676 ;; chown leads to an entire copy of the tree, which is a bad idea.
1678 ;; Generate a key pair and optionally authorize substitute server keys.
1680 (unless (file-exists? "/etc/guix/signing-key.pub")
1681 (system* #$(file-append guix "/bin/guix") "archive"
1684 #$(if authorize-key?
1685 (substitute-key-authorization keys guix)
1688 (define* (references-file item #:optional (name "references"))
1689 "Return a file that contains the list of references of ITEM."
1690 (if (struct? item) ;lowerable object
1692 (with-imported-modules (source-module-closure
1693 '((guix build store-copy)))
1695 (use-modules (guix build store-copy))
1697 (call-with-output-file #$output
1699 (write (map store-info-item
1700 (call-with-input-file "graph"
1701 read-reference-graph))
1703 #:options `(#:local-build? #f
1704 #:references-graphs (("graph" ,item))))
1705 (plain-file name "()")))
1707 (define guix-service-type
1711 (list (service-extension shepherd-root-service-type guix-shepherd-service)
1712 (service-extension account-service-type guix-accounts)
1713 (service-extension activation-service-type guix-activation)
1714 (service-extension profile-service-type
1715 (compose list guix-configuration-guix))))
1717 ;; Extensions can specify extra directories to add to the build chroot.
1718 (compose concatenate)
1719 (extend (lambda (config directories)
1723 (append (guix-configuration-chroot-directories config)
1726 (default-value (guix-configuration))
1728 "Run the build daemon of GNU@tie{}Guix, aka. @command{guix-daemon}.")))
1730 (define-deprecated (guix-service #:optional
1731 (config %default-guix-configuration))
1733 "Return a service that runs the Guix build daemon according to
1735 (service guix-service-type config))
1738 (define-record-type* <guix-publish-configuration>
1739 guix-publish-configuration make-guix-publish-configuration
1740 guix-publish-configuration?
1741 (guix guix-publish-configuration-guix ;package
1743 (port guix-publish-configuration-port ;number
1745 (host guix-publish-configuration-host ;string
1746 (default "localhost"))
1747 (compression guix-publish-configuration-compression
1749 (default (default-compression this-record
1750 (current-source-location))))
1751 (compression-level %guix-publish-configuration-compression-level ;deprecated
1753 (nar-path guix-publish-configuration-nar-path ;string
1755 (cache guix-publish-configuration-cache ;#f | string
1757 (cache-bypass-threshold guix-publish-configuration-cache-bypass-threshold
1758 (default (* 10 (expt 2 20)))) ;integer
1759 (workers guix-publish-configuration-workers ;#f | integer
1761 (ttl guix-publish-configuration-ttl ;#f | integer
1764 (define-deprecated (guix-publish-configuration-compression-level config)
1765 "Return a compression level, the old way."
1766 (match (guix-publish-configuration-compression config)
1767 (((_ level) _ ...) level)))
1769 (define (default-compression config properties)
1770 "Return the default 'guix publish' compression according to CONFIG, and
1771 raise a deprecation warning if the 'compression-level' field was used."
1772 (match (%guix-publish-configuration-compression-level config)
1776 (warn-about-deprecation 'compression-level properties
1777 #:replacement 'compression)
1778 `(("gzip" ,level)))))
1780 (define (guix-publish-shepherd-service config)
1781 (define (config->compression-options config)
1782 (match (guix-publish-configuration-compression config)
1783 (() ;empty list means "no compression"
1786 (append-map (match-lambda
1788 `("-C" ,(string-append type ":"
1789 (number->string level)))))
1792 (match-record config <guix-publish-configuration>
1793 (guix port host nar-path cache workers ttl cache-bypass-threshold)
1794 (list (shepherd-service
1795 (provision '(guix-publish))
1796 (requirement '(guix-daemon))
1797 (start #~(make-forkexec-constructor
1798 (list #$(file-append guix "/bin/guix")
1799 "publish" "-u" "guix-publish"
1800 "-p" #$(number->string port)
1801 #$@(config->compression-options config)
1802 (string-append "--nar-path=" #$nar-path)
1803 (string-append "--listen=" #$host)
1805 #~((string-append "--workers="
1810 #~((string-append "--ttl="
1811 #$(number->string ttl)
1815 #~((string-append "--cache=" #$cache)
1817 "--cache-bypass-threshold="
1819 cache-bypass-threshold)))
1822 ;; Make sure we run in a UTF-8 locale so we can produce
1823 ;; nars for packages that contain UTF-8 file names such
1824 ;; as 'nss-certs'. See <https://bugs.gnu.org/26948>.
1825 #:environment-variables
1826 (list (string-append "GUIX_LOCPATH="
1827 #$glibc-utf8-locales "/lib/locale")
1828 "LC_ALL=en_US.utf8")
1829 #:log-file "/var/log/guix-publish.log"))
1830 (stop #~(make-kill-destructor))))))
1832 (define %guix-publish-accounts
1833 (list (user-group (name "guix-publish") (system? #t))
1835 (name "guix-publish")
1836 (group "guix-publish")
1838 (comment "guix publish user")
1839 (home-directory "/var/empty")
1840 (shell (file-append shadow "/sbin/nologin")))))
1842 (define %guix-publish-log-rotations
1844 (files (list "/var/log/guix-publish.log")))))
1846 (define (guix-publish-activation config)
1847 (let ((cache (guix-publish-configuration-cache config)))
1849 (with-imported-modules '((guix build utils))
1851 (use-modules (guix build utils))
1854 (let* ((pw (getpw "guix-publish"))
1855 (uid (passwd:uid pw))
1856 (gid (passwd:gid pw)))
1857 (chown #$cache uid gid))))
1860 (define guix-publish-service-type
1861 (service-type (name 'guix-publish)
1863 (list (service-extension shepherd-root-service-type
1864 guix-publish-shepherd-service)
1865 (service-extension account-service-type
1866 (const %guix-publish-accounts))
1867 (service-extension rottlog-service-type
1868 (const %guix-publish-log-rotations))
1869 (service-extension activation-service-type
1870 guix-publish-activation)))
1871 (default-value (guix-publish-configuration))
1873 "Add a Shepherd service running @command{guix publish}, a
1874 command that allows you to share pre-built binaries with others over HTTP.")))
1876 (define-deprecated (guix-publish-service #:key (guix guix)
1877 (port 80) (host "localhost"))
1878 guix-publish-service-type
1879 "Return a service that runs @command{guix publish} listening on @var{host}
1880 and @var{port} (@pxref{Invoking guix publish}).
1882 This assumes that @file{/etc/guix} already contains a signing key pair as
1883 created by @command{guix archive --generate-key} (@pxref{Invoking guix
1884 archive}). If that is not the case, the service will fail to start."
1886 (service guix-publish-service-type
1887 (guix-publish-configuration (guix guix) (port port) (host host))))
1894 (define-record-type* <udev-configuration>
1895 udev-configuration make-udev-configuration
1897 (udev udev-configuration-udev ;<package>
1899 (rules udev-configuration-rules ;list of <package>
1902 (define (udev-rules-union packages)
1903 "Return the union of the @code{lib/udev/rules.d} directories found in each
1904 item of @var{packages}."
1906 (with-imported-modules '((guix build union)
1909 (use-modules (guix build union)
1914 (define %standard-locations
1915 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
1917 (define (rules-sub-directory directory)
1918 ;; Return the sub-directory of DIRECTORY containing udev rules, or
1919 ;; #f if none was found.
1920 (find directory-exists?
1921 (map (cut string-append directory <>) %standard-locations)))
1923 (mkdir-p (string-append #$output "/lib/udev"))
1924 (union-build (string-append #$output "/lib/udev/rules.d")
1925 (filter-map rules-sub-directory '#$packages)))))
1927 (computed-file "udev-rules" build))
1929 (define (udev-rule file-name contents)
1930 "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
1931 (computed-file file-name
1932 (with-imported-modules '((guix build utils))
1934 (use-modules (guix build utils))
1937 (string-append #$output "/lib/udev/rules.d"))
1940 (call-with-output-file
1941 (string-append rules.d "/" #$file-name)
1943 (display #$contents port)))))))
1945 (define (file->udev-rule file-name file)
1946 "Return a directory with a udev rule file FILE-NAME which is a copy of FILE."
1947 (computed-file file-name
1948 (with-imported-modules '((guix build utils))
1950 (use-modules (guix build utils))
1953 (string-append #$output "/lib/udev/rules.d"))
1955 (define file-copy-dest
1956 (string-append rules.d "/" #$file-name))
1959 (copy-file #$file file-copy-dest)))))
1961 (define kvm-udev-rule
1962 ;; Return a directory with a udev rule that changes the group of /dev/kvm to
1963 ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
1964 ;; but now we have to add it by ourselves.
1966 ;; Build users are part of the "kvm" group, so we can fearlessly make
1967 ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
1968 (udev-rule "90-kvm.rules"
1969 "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
1971 (define udev-shepherd-service
1972 ;; Return a <shepherd-service> for UDEV with RULES.
1974 (($ <udev-configuration> udev rules)
1975 (let* ((rules (udev-rules-union (cons* udev kvm-udev-rule rules)))
1976 (udev.conf (computed-file "udev.conf"
1977 #~(call-with-output-file #$output
1980 "udev_rules=\"~a/lib/udev/rules.d\"\n"
1986 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
1988 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
1989 (requirement '(root-file-system))
1991 (documentation "Populate the /dev directory, dynamically.")
1993 (with-imported-modules (source-module-closure
1994 '((gnu build linux-boot)))
1997 ;; 'udevd' from eudev.
1998 #$(file-append udev "/sbin/udevd"))
2000 (define (wait-for-udevd)
2001 ;; Wait until someone's listening on udevd's control
2003 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
2005 (catch 'system-error
2007 (connect sock PF_UNIX "/run/udev/control")
2010 (format #t "waiting for udevd...~%")
2014 ;; Allow udev to find the modules.
2015 (setenv "LINUX_MODULE_DIRECTORY"
2016 "/run/booted-system/kernel/lib/modules")
2018 (let* ((kernel-release
2019 (utsname:release (uname)))
2020 (linux-module-directory
2021 (getenv "LINUX_MODULE_DIRECTORY"))
2023 (string-append linux-module-directory "/"
2025 (old-umask (umask #o022)))
2026 ;; If we're in a container, DIRECTORY might not exist,
2027 ;; for instance because the host runs a different
2028 ;; kernel. In that case, skip it; we'll just miss a few
2029 ;; nodes like /dev/fuse.
2030 (when (file-exists? directory)
2031 (make-static-device-nodes directory))
2034 (let ((pid (fork+exec-command (list udevd)
2035 #:environment-variables
2037 ;; The first one is for udev, the second one for
2039 (string-append "UDEV_CONFIG_FILE=" #$udev.conf)
2040 (string-append "EUDEV_RULES_DIRECTORY="
2042 rules "/lib/udev/rules.d"))
2043 (string-append "LINUX_MODULE_DIRECTORY="
2044 (getenv "LINUX_MODULE_DIRECTORY"))
2045 (default-environment-variables)))))
2046 ;; Wait until udevd is up and running. This appears to
2047 ;; be needed so that the events triggered below are
2048 ;; actually handled.
2051 ;; Trigger device node creation.
2052 (system* #$(file-append udev "/bin/udevadm")
2053 "trigger" "--action=add")
2055 ;; Wait for things to settle down.
2056 (system* #$(file-append udev "/bin/udevadm")
2059 (stop #~(make-kill-destructor))
2061 ;; When halting the system, 'udev' is actually killed by
2062 ;; 'user-processes', i.e., before its own 'stop' method was called.
2063 ;; Thus, make sure it is not respawned.
2065 ;; We need additional modules.
2066 (modules `((gnu build linux-boot) ;'make-static-device-nodes'
2067 ,@%default-modules))
2069 (actions (list (shepherd-action
2071 (documentation "Display the directory containing
2072 the udev rules in use.")
2073 (procedure #~(lambda (_)
2075 (newline))))))))))))
2077 (define udev-service-type
2078 (service-type (name 'udev)
2080 (list (service-extension shepherd-root-service-type
2081 udev-shepherd-service)))
2083 (compose concatenate) ;concatenate the list of rules
2084 (extend (lambda (config rules)
2086 (($ <udev-configuration> udev initial-rules)
2089 (rules (append initial-rules rules)))))))
2090 (default-value (udev-configuration))
2092 "Run @command{udev}, which populates the @file{/dev}
2093 directory dynamically. Get extra rules from the packages listed in the
2094 @code{rules} field of its value, @code{udev-configuration} object.")))
2096 (define* (udev-service #:key (udev eudev) (rules '()))
2097 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
2098 extra rules from the packages listed in @var{rules}."
2099 (service udev-service-type
2100 (udev-configuration (udev udev) (rules rules))))
2102 (define* (udev-rules-service name rules #:key (groups '()))
2103 "Return a service that extends udev-service-type with RULES and
2104 account-service-type with GROUPS as system groups. This works by creating a
2105 singleton service type NAME-udev-rules, of which the returned service is an
2107 (let* ((name (symbol-append name '-udev-rules))
2109 (const (map (lambda (group)
2110 (user-group (name group) (system? #t)))
2112 (udev-extension (const (list rules)))
2117 account-service-type account-extension)
2119 udev-service-type udev-extension))))))
2122 (define swap-service-type
2123 (shepherd-service-type
2127 (if (and (string? device)
2128 (string-prefix? "/dev/mapper/" device))
2129 (list (symbol-append 'device-mapping-
2130 (string->symbol (basename device))))
2133 (define (device-lookup device)
2134 ;; The generic 'find-partition' procedures could return a partition
2135 ;; that's not swap space, but that's unlikely.
2136 (cond ((uuid? device)
2137 #~(find-partition-by-uuid #$(uuid-bytevector device)))
2138 ((file-system-label? device)
2139 #~(find-partition-by-label
2140 #$(file-system-label->string device)))
2144 (define service-name
2145 (symbol-append 'swap-
2147 (cond ((uuid? device)
2148 (string-take (uuid->string device) 6))
2149 ((file-system-label? device)
2150 (file-system-label->string device))
2154 (with-imported-modules (source-module-closure '((gnu build file-systems)))
2156 (provision (list service-name))
2157 (requirement `(udev ,@requirement))
2158 (documentation "Enable the given swap device.")
2159 (modules `((gnu build file-systems)
2160 ,@%default-modules))
2162 (let ((device #$(device-lookup device)))
2165 (restart-on-EINTR (swapon device))
2168 (let ((device #$(device-lookup device)))
2170 (restart-on-EINTR (swapoff device)))
2174 (define (swap-service device)
2175 "Return a service that uses @var{device} as a swap device."
2176 (service swap-service-type device))
2178 (define %default-gpm-options
2179 ;; Default options for GPM.
2180 '("-m" "/dev/input/mice" "-t" "ps2"))
2182 (define-record-type* <gpm-configuration>
2183 gpm-configuration make-gpm-configuration gpm-configuration?
2184 (gpm gpm-configuration-gpm ;package
2186 (options gpm-configuration-options ;list of strings
2187 (default %default-gpm-options)))
2189 (define gpm-shepherd-service
2191 (($ <gpm-configuration> gpm options)
2192 (list (shepherd-service
2193 (requirement '(udev))
2196 ;; 'gpm' runs in the background and sets a PID file.
2197 ;; Note that it requires running as "root".
2198 (false-if-exception (delete-file "/var/run/gpm.pid"))
2199 (fork+exec-command (list #$(file-append gpm "/sbin/gpm")
2202 ;; Wait for the PID file to appear; declare failure if
2203 ;; it doesn't show up.
2205 (or (file-exists? "/var/run/gpm.pid")
2213 ;; Return #f if successfully stopped.
2214 (not (zero? (system* #$(file-append gpm "/sbin/gpm")
2217 (define gpm-service-type
2218 (service-type (name 'gpm)
2220 (list (service-extension shepherd-root-service-type
2221 gpm-shepherd-service)))
2222 (default-value (gpm-configuration))
2224 "Run GPM, the general-purpose mouse daemon, with the given
2225 command-line options. GPM allows users to use the mouse in the console,
2226 notably to select, copy, and paste text. The default options use the
2227 @code{ps2} protocol, which works for both USB and PS/2 mice.")))
2229 (define-deprecated (gpm-service #:key (gpm gpm)
2230 (options %default-gpm-options))
2232 "Run @var{gpm}, the general-purpose mouse daemon, with the given
2233 command-line @var{options}. GPM allows users to use the mouse in the console,
2234 notably to select, copy, and paste text. The default value of @var{options}
2235 uses the @code{ps2} protocol, which works for both USB and PS/2 mice.
2237 This service is not part of @var{%base-services}."
2238 ;; To test in QEMU, use "-usbdevice mouse" and then, in the monitor, use
2239 ;; "info mice" and "mouse_set X" to use the right mouse.
2240 (service gpm-service-type
2241 (gpm-configuration (gpm gpm) (options options))))
2243 (define-record-type* <kmscon-configuration>
2244 kmscon-configuration make-kmscon-configuration
2245 kmscon-configuration?
2246 (kmscon kmscon-configuration-kmscon
2248 (virtual-terminal kmscon-configuration-virtual-terminal)
2249 (login-program kmscon-configuration-login-program
2250 (default (file-append shadow "/bin/login")))
2251 (login-arguments kmscon-configuration-login-arguments
2253 (auto-login kmscon-configuration-auto-login
2255 (hardware-acceleration? kmscon-configuration-hardware-acceleration?
2256 (default #f))) ; #t causes failure
2258 (define kmscon-service-type
2259 (shepherd-service-type
2262 (let ((kmscon (kmscon-configuration-kmscon config))
2263 (virtual-terminal (kmscon-configuration-virtual-terminal config))
2264 (login-program (kmscon-configuration-login-program config))
2265 (login-arguments (kmscon-configuration-login-arguments config))
2266 (auto-login (kmscon-configuration-auto-login config))
2267 (hardware-acceleration? (kmscon-configuration-hardware-acceleration? config)))
2269 (define kmscon-command
2271 #$(file-append kmscon "/bin/kmscon") "--login"
2272 "--vt" #$virtual-terminal
2273 "--no-switchvt" ;Prevent a switch to the virtual terminal.
2274 #$@(if hardware-acceleration? '("--hwaccel") '())
2276 #$login-program #$@login-arguments
2282 (documentation "kmscon virtual terminal")
2283 (requirement '(user-processes udev dbus-system))
2284 (provision (list (symbol-append 'term- (string->symbol virtual-terminal))))
2285 (start #~(make-forkexec-constructor #$kmscon-command))
2286 (stop #~(make-kill-destructor)))))))
2288 (define-record-type* <static-networking>
2289 static-networking make-static-networking
2291 (interface static-networking-interface)
2292 (ip static-networking-ip)
2293 (netmask static-networking-netmask
2295 (gateway static-networking-gateway ;FIXME: doesn't belong here
2297 (provision static-networking-provision
2299 (requirement static-networking-requirement
2301 (name-servers static-networking-name-servers ;FIXME: doesn't belong here
2304 (define static-networking-shepherd-service
2306 (($ <static-networking> interface ip netmask gateway provision
2307 requirement name-servers)
2308 (let ((loopback? (and provision (memq 'loopback provision))))
2312 "Bring up the networking interface using a static IP address.")
2313 (requirement requirement)
2314 (provision (or provision
2315 (list (symbol-append 'networking-
2316 (string->symbol interface)))))
2319 ;; Return #t if successfully started.
2320 (let* ((addr (inet-pton AF_INET #$ip))
2321 (sockaddr (make-socket-address AF_INET addr 0))
2322 (mask (and #$netmask
2323 (inet-pton AF_INET #$netmask)))
2325 (make-socket-address AF_INET
2327 (gateway (and #$gateway
2328 (inet-pton AF_INET #$gateway)))
2329 (gatewayaddr (and gateway
2330 (make-socket-address AF_INET
2332 (configure-network-interface #$interface sockaddr
2339 (let ((sock (socket AF_INET SOCK_DGRAM 0)))
2340 (add-network-route/gateway sock gatewayaddr)
2341 (close-port sock))))))
2343 ;; Return #f is successfully stopped.
2344 (let ((sock (socket AF_INET SOCK_STREAM 0)))
2346 (delete-network-route sock
2347 (make-socket-address
2348 AF_INET INADDR_ANY 0)))
2349 (set-network-interface-flags sock #$interface 0)
2354 (define (static-networking-etc-files interfaces)
2355 "Return a /etc/resolv.conf entry for INTERFACES or the empty list."
2356 (match (delete-duplicates
2357 (append-map static-networking-name-servers
2362 (let ((content (string-join
2363 (map (cut string-append "nameserver " <>)
2367 ,(plain-file "resolv.conf"
2369 # Generated by 'static-networking-service'.\n"
2372 (define (static-networking-shepherd-services interfaces)
2373 "Return the list of Shepherd services to bring up INTERFACES, a list of
2374 <static-networking> objects."
2375 (define (loopback? service)
2376 (memq 'loopback (shepherd-service-provision service)))
2378 (let ((services (map static-networking-shepherd-service interfaces)))
2379 (match (remove loopback? services)
2381 ;; There's no interface other than 'loopback', so we assume that the
2382 ;; 'networking' service will be provided by dhclient or similar.
2385 ;; Assume we're providing all the interfaces, and thus, provide a
2386 ;; 'networking' service.
2387 (cons (shepherd-service
2388 (provision '(networking))
2389 (requirement (append-map shepherd-service-provision
2391 (start #~(const #t))
2393 (documentation "Bring up all the networking interfaces."))
2396 (define static-networking-service-type
2397 ;; The service type for statically-defined network interfaces.
2398 (service-type (name 'static-networking)
2401 (service-extension shepherd-root-service-type
2402 static-networking-shepherd-services)
2403 (service-extension etc-service-type
2404 static-networking-etc-files)))
2405 (compose concatenate)
2408 "Turn up the specified network interfaces upon startup,
2409 with the given IP address, gateway, netmask, and so on. The value for
2410 services of this type is a list of @code{static-networking} objects, one per
2411 network interface.")))
2413 (define* (static-networking-service interface ip
2415 netmask gateway provision
2416 ;; Most interfaces require udev to be usable.
2417 (requirement '(udev))
2419 "Return a service that starts @var{interface} with address @var{ip}. If
2420 @var{netmask} is true, use it as the network mask. If @var{gateway} is true,
2421 it must be a string specifying the default network gateway.
2423 This procedure can be called several times, one for each network
2424 interface of interest. Behind the scenes what it does is extend
2425 @code{static-networking-service-type} with additional network interfaces
2427 (simple-service 'static-network-interface
2428 static-networking-service-type
2429 (list (static-networking (interface interface) (ip ip)
2430 (netmask netmask) (gateway gateway)
2431 (provision provision)
2432 (requirement requirement)
2433 (name-servers name-servers)))))
2436 (define %base-services
2437 ;; Convenience variable holding the basic services.
2438 (list (service login-service-type)
2440 (service virtual-terminal-service-type)
2441 (service console-font-service-type
2443 (cons tty %default-console-font))
2444 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
2446 (service agetty-service-type (agetty-configuration
2447 (extra-options '("-L")) ; no carrier detect
2449 (tty #f))) ; automatic
2451 (service mingetty-service-type (mingetty-configuration
2453 (service mingetty-service-type (mingetty-configuration
2455 (service mingetty-service-type (mingetty-configuration
2457 (service mingetty-service-type (mingetty-configuration
2459 (service mingetty-service-type (mingetty-configuration
2461 (service mingetty-service-type (mingetty-configuration
2464 (service static-networking-service-type
2465 (list (static-networking (interface "lo")
2468 (provision '(loopback)))))
2470 (service urandom-seed-service-type)
2471 (service guix-service-type)
2472 (service nscd-service-type)
2474 (service rottlog-service-type)
2476 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
2477 ;; used, so enable them by default. The FUSE and ALSA rules are
2478 ;; less critical, but handy.
2479 (service udev-service-type
2481 (rules (list lvm2 fuse alsa-utils crda))))
2483 (service special-files-service-type
2484 `(("/bin/sh" ,(file-append bash "/bin/sh"))
2485 ("/usr/bin/env" ,(file-append coreutils "/bin/env"))))))
2487 ;;; base.scm ends here