1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013-2022 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, 2021 Brice Waegeneire <brice@waegenei.re>
16 ;;; Copyright © 2021 qblade <qblade@protonmail.com>
17 ;;; Copyright © 2021 Hui Lu <luhuins@163.com>
18 ;;; Copyright © 2021, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
19 ;;; Copyright © 2022 Guillaume Le Vaillant <glv@posteo.net>
21 ;;; This file is part of GNU Guix.
23 ;;; GNU Guix is free software; you can redistribute it and/or modify it
24 ;;; under the terms of the GNU General Public License as published by
25 ;;; the Free Software Foundation; either version 3 of the License, or (at
26 ;;; your option) any later version.
28 ;;; GNU Guix is distributed in the hope that it will be useful, but
29 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
30 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 ;;; GNU General Public License for more details.
33 ;;; You should have received a copy of the GNU General Public License
34 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
36 (define-module (gnu services base)
37 #:use-module (guix store)
38 #:use-module (guix deprecation)
39 #:autoload (guix diagnostics) (warning &fix-hint)
40 #:autoload (guix i18n) (G_)
41 #:use-module (guix combinators)
42 #:use-module (gnu services)
43 #:use-module (gnu services admin)
44 #:use-module (gnu services shepherd)
45 #:use-module (gnu services sysctl)
46 #:use-module (gnu system pam)
47 #:use-module (gnu system shadow) ; 'user-account', etc.
48 #:use-module (gnu system uuid)
49 #:use-module (gnu system file-systems) ; 'file-system', etc.
50 #:use-module (gnu system keyboard)
51 #:use-module (gnu system mapped-devices)
52 #:use-module ((gnu system linux-initrd)
53 #:select (file-system-packages))
54 #:use-module (gnu packages admin)
55 #:use-module ((gnu packages linux)
56 #:select (alsa-utils crda eudev e2fsprogs fuse gpm kbd lvm2 rng-tools))
57 #:use-module (gnu packages bash)
58 #:use-module ((gnu packages base)
59 #:select (coreutils glibc glibc-utf8-locales tar))
60 #:use-module ((gnu packages compression) #:select (gzip))
61 #:autoload (gnu packages guile-xyz) (guile-netlink)
62 #:autoload (gnu packages hurd) (hurd)
63 #:use-module (gnu packages package-management)
64 #:use-module ((gnu packages gnupg) #:select (guile-gcrypt))
65 #:use-module (gnu packages linux)
66 #:use-module (gnu packages terminals)
67 #:use-module ((gnu build file-systems)
68 #:select (mount-flags->bit-mask
69 swap-space->flags-bit-mask))
70 #:use-module (guix gexp)
71 #:use-module (guix records)
72 #:use-module (guix modules)
73 #:use-module ((guix self) #:select (make-config.scm))
74 #:use-module (guix diagnostics)
75 #:use-module (guix i18n)
76 #:use-module (srfi srfi-1)
77 #:use-module (srfi srfi-26)
78 #:use-module (srfi srfi-34)
79 #:use-module (srfi srfi-35)
80 #:use-module (ice-9 match)
81 #:use-module (ice-9 format)
82 #:re-export (user-processes-service-type ;backwards compatibility
83 %default-substitute-urls)
84 #:export (fstab-service-type
85 root-file-system-service
86 file-system-service-type
90 console-font-service-type
92 virtual-terminal-service-type
96 static-networking-addresses
97 static-networking-links
98 static-networking-routes
99 static-networking-requirement
103 network-address-device
104 network-address-value
105 network-address-ipv6?
111 network-link-arguments
115 network-route-destination
119 network-route-gateway
121 static-networking-service
122 static-networking-service-type
124 %loopback-static-networking
125 %qemu-static-networking
129 udev-configuration-rules
142 agetty-configuration?
146 mingetty-configuration
147 mingetty-configuration-tty
148 mingetty-configuration-auto-login
149 mingetty-configuration-login-program
150 mingetty-configuration-login-pause?
151 mingetty-configuration-clear-on-logout?
152 mingetty-configuration-mingetty
153 mingetty-configuration?
155 mingetty-service-type
158 %nscd-default-configuration
170 syslog-configuration?
175 %default-authorized-guix-keys
179 guix-configuration-guix
180 guix-configuration-build-group
181 guix-configuration-build-accounts
182 guix-configuration-authorize-key?
183 guix-configuration-authorized-keys
184 guix-configuration-use-substitutes?
185 guix-configuration-substitute-urls
186 guix-configuration-generate-substitute-key?
187 guix-configuration-extra-options
188 guix-configuration-log-file
191 guix-publish-configuration
192 guix-publish-configuration?
193 guix-publish-configuration-guix
194 guix-publish-configuration-port
195 guix-publish-configuration-host
196 guix-publish-configuration-compression
197 guix-publish-configuration-compression-level ;deprecated
198 guix-publish-configuration-nar-path
199 guix-publish-configuration-cache
200 guix-publish-configuration-ttl
201 guix-publish-configuration-negative-ttl
202 guix-publish-service-type
208 urandom-seed-service-type
216 kmscon-configuration?
219 pam-limits-service-type
226 ;;; Base system services---i.e., services that 99% of the users will want to
237 (define (file-system->fstab-entry file-system)
238 "Return a @file{/etc/fstab} entry for @var{file-system}."
239 (string-append (match (file-system-device file-system)
240 ((? file-system-label? label)
241 (string-append "LABEL="
242 (file-system-label->string label)))
244 (string-append "UUID=" (uuid->string uuid)))
248 (file-system-mount-point file-system) "\t"
249 (file-system-type file-system) "\t"
250 (or (file-system-options file-system) "defaults") "\t"
252 ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
253 ;; don't have anything sensible to put in there.
256 (define (file-systems->fstab file-systems)
257 "Return a @file{/etc} entry for an @file{fstab} describing
259 `(("fstab" ,(plain-file "fstab"
262 # This file was generated from your Guix configuration. Any changes
263 # will be lost upon reboot or reconfiguration.\n\n"
264 (string-join (map file-system->fstab-entry
269 (define fstab-service-type
270 ;; The /etc/fstab service.
271 (service-type (name 'fstab)
273 (list (service-extension etc-service-type
274 file-systems->fstab)))
275 (compose concatenate)
278 "Populate the @file{/etc/fstab} based on the given file
281 (define %root-file-system-shepherd-service
283 (documentation "Take care of the root file system.")
284 (provision '(root-file-system))
287 ;; Return #f if successfully stopped.
290 (call-with-blocked-asyncs
292 (let ((null (%make-void-port "w")))
293 ;; Close 'shepherd.log'.
294 (display "closing log\n")
295 ((@ (shepherd comm) stop-logging))
297 ;; Redirect the default output ports..
298 (set-current-output-port null)
299 (set-current-error-port null)
301 ;; Close /dev/console.
302 (for-each close-fdes '(0 1 2))
304 ;; At this point, there are no open files left, so the
305 ;; root file system can be re-mounted read-only.
307 (logior MS_REMOUNT MS_RDONLY)
313 (define root-file-system-service-type
314 (shepherd-service-type 'root-file-system
315 (const %root-file-system-shepherd-service)
316 (description "Take care of syncing the root file
317 system and of remounting it read-only when the system shuts down.")))
319 (define (root-file-system-service)
320 "Return a service whose sole purpose is to re-mount read-only the root file
321 system upon shutdown (aka. cleanly \"umounting\" root.)
323 This service must be the root of the service dependency graph so that its
324 'stop' action is invoked when shepherd is the only process left."
325 (service root-file-system-service-type #f))
327 (define (file-system->shepherd-service-name file-system)
328 "Return the symbol that denotes the service mounting and unmounting
330 (symbol-append 'file-system-
331 (string->symbol (file-system-mount-point file-system))))
333 (define (mapped-device->shepherd-service-name md)
334 "Return the symbol that denotes the shepherd service of MD, a <mapped-device>."
335 (symbol-append 'device-mapping-
336 (string->symbol (string-join
337 (mapped-device-targets md) "-"))))
339 (define dependency->shepherd-service-name
341 ((? mapped-device? md)
342 (mapped-device->shepherd-service-name md))
344 (file-system->shepherd-service-name fs))))
346 (define (file-system-shepherd-service file-system)
347 "Return the shepherd service for @var{file-system}, or @code{#f} if
348 @var{file-system} is not auto-mounted or doesn't have its mount point created
350 (let ((target (file-system-mount-point file-system))
351 (create? (file-system-create-mount-point? file-system))
352 (mount? (file-system-mount? file-system))
353 (dependencies (file-system-dependencies file-system))
354 (packages (file-system-packages (list file-system))))
355 (and (or mount? create?)
356 (with-imported-modules (source-module-closure
357 '((gnu build file-systems)))
359 (provision (list (file-system->shepherd-service-name file-system)))
360 (requirement `(root-file-system
362 ,@(map dependency->shepherd-service-name dependencies)))
363 (documentation "Check, mount, and unmount the given file system.")
364 (start #~(lambda args
370 #~(let (($PATH (getenv "PATH")))
371 ;; Make sure fsck.ext2 & co. can be found.
374 ;; Don’t display the PATH settings.
375 (with-output-to-port (%make-void-port "w")
377 (set-path-environment-variable "PATH"
383 '#$(file-system->spec file-system))
386 (setenv "PATH" $PATH))))
390 ;; Normally there are no processes left at this point, so
391 ;; TARGET can be safely unmounted.
393 ;; Make sure PID 1 doesn't keep TARGET busy.
399 ;; We need additional modules.
400 (modules `(((gnu build file-systems)
401 #:select (mount-file-system))
402 (gnu system file-systems)
403 ,@%default-modules)))))))
405 (define (file-system-shepherd-services file-systems)
406 "Return the list of Shepherd services for FILE-SYSTEMS."
407 (let* ((file-systems (filter (lambda (x)
408 (or (file-system-mount? x)
409 (file-system-create-mount-point? x)))
413 (provision '(file-systems))
414 (requirement (cons* 'root-file-system 'user-file-systems
415 (map file-system->shepherd-service-name
417 (documentation "Target for all the initially-mounted file systems")
419 (stop #~(const #f))))
421 (define known-mount-points
422 (map file-system-mount-point file-systems))
426 (documentation "Unmount manually-mounted file systems.")
427 (provision '(user-file-systems))
430 (define (known? mount-point)
432 (cons* "/proc" "/sys" '#$known-mount-points)))
434 ;; Make sure we don't keep the user's mount points busy.
437 (for-each (lambda (mount-point)
438 (format #t "unmounting '~a'...~%" mount-point)
441 (umount mount-point))
443 (let ((errno (system-error-errno args)))
444 (format #t "failed to unmount '~a': ~a~%"
445 mount-point (strerror errno))))))
446 (filter (negate known?) (mount-points)))
449 (cons* sink user-unmount
450 (map file-system-shepherd-service file-systems))))
452 (define (file-system-fstab-entries file-systems)
453 "Return the subset of @var{file-systems} that should have an entry in
455 ;; /etc/fstab is about telling fsck(8), mount(8), and umount(8) about
456 ;; relevant file systems they'll have to deal with. That excludes "pseudo"
459 ;; In particular, things like GIO (part of GLib) use it to determine the set
460 ;; of mounts, which is then used by graphical file managers and desktop
461 ;; environments to display "volume" icons. Thus, we really need to exclude
462 ;; those pseudo file systems from the list.
463 (remove (lambda (file-system)
464 (or (member (file-system-type file-system)
465 %pseudo-file-system-types)
466 (memq 'bind-mount (file-system-flags file-system))))
469 (define file-system-service-type
470 (service-type (name 'file-systems)
472 (list (service-extension shepherd-root-service-type
473 file-system-shepherd-services)
474 (service-extension fstab-service-type
475 file-system-fstab-entries)
477 ;; Have 'user-processes' depend on 'file-systems'.
478 (service-extension user-processes-service-type
479 (const '(file-systems)))))
480 (compose concatenate)
483 "Provide Shepherd services to mount and unmount the given
484 file systems, as well as corresponding @file{/etc/fstab} entries.")))
489 ;;; Preserve entropy to seed /dev/urandom on boot.
492 (define %random-seed-file
493 "/var/lib/random-seed")
495 (define (urandom-seed-shepherd-service _)
496 "Return a shepherd service for the /dev/urandom seed."
497 (list (shepherd-service
498 (documentation "Preserve entropy across reboots for /dev/urandom.")
499 (provision '(urandom-seed))
501 ;; Depend on udev so that /dev/hwrng is available.
502 (requirement '(file-systems udev))
505 ;; On boot, write random seed into /dev/urandom.
506 (when (file-exists? #$%random-seed-file)
507 (call-with-input-file #$%random-seed-file
509 (call-with-output-file "/dev/urandom"
511 (dump-port seed urandom)
513 ;; Writing SEED to URANDOM isn't enough: we must
514 ;; also tell the kernel to account for these
515 ;; extra bits of entropy.
516 (let ((bits (* 8 (stat:size (stat seed)))))
517 (add-to-entropy-count urandom bits)))))))
519 ;; Try writing from /dev/hwrng into /dev/urandom.
520 ;; It seems that the file /dev/hwrng always exists, even
521 ;; when there is no hardware random number generator
522 ;; available. So, we handle a failed read or any other error
523 ;; reported by the operating system.
524 (let ((buf (catch 'system-error
526 (call-with-input-file "/dev/hwrng"
528 (get-bytevector-n hwrng 512))))
529 ;; Silence is golden...
532 (call-with-output-file "/dev/urandom"
534 (put-bytevector urandom buf)
535 (let ((bits (* 8 (bytevector-length buf))))
536 (add-to-entropy-count urandom bits))))))
538 ;; Immediately refresh the seed in case the system doesn't
539 ;; shut down cleanly.
540 (call-with-input-file "/dev/urandom"
542 (let ((previous-umask (umask #o077))
543 (buf (make-bytevector 512)))
544 (mkdir-p (dirname #$%random-seed-file))
545 (get-bytevector-n! urandom buf 0 512)
546 (call-with-output-file #$%random-seed-file
548 (put-bytevector seed buf)))
549 (umask previous-umask))))
552 ;; During shutdown, write from /dev/urandom into random seed.
553 (let ((buf (make-bytevector 512)))
554 (call-with-input-file "/dev/urandom"
556 (let ((previous-umask (umask #o077)))
557 (get-bytevector-n! urandom buf 0 512)
558 (mkdir-p (dirname #$%random-seed-file))
559 (call-with-output-file #$%random-seed-file
561 (put-bytevector seed buf)))
562 (umask previous-umask))
564 (modules `((rnrs bytevectors)
566 ,@%default-modules)))))
568 (define urandom-seed-service-type
569 (service-type (name 'urandom-seed)
571 (list (service-extension shepherd-root-service-type
572 urandom-seed-shepherd-service)
574 ;; Have 'user-processes' depend on 'urandom-seed'.
575 ;; This ensures that user processes and daemons don't
576 ;; start until we have seeded the PRNG.
577 (service-extension user-processes-service-type
578 (const '(urandom-seed)))))
581 "Seed the @file{/dev/urandom} pseudo-random number
582 generator (RNG) with the value recorded when the system was last shut
587 ;;; Add hardware random number generator to entropy pool.
590 (define-record-type* <rngd-configuration>
591 rngd-configuration make-rngd-configuration
593 (rng-tools rngd-configuration-rng-tools) ;file-like
594 (device rngd-configuration-device)) ;string
596 (define rngd-service-type
597 (shepherd-service-type
600 (define rng-tools (rngd-configuration-rng-tools config))
601 (define device (rngd-configuration-device config))
604 (list (file-append rng-tools "/sbin/rngd")
608 (documentation "Add TRNG to entropy pool.")
609 (requirement '(udev))
611 (start #~(make-forkexec-constructor '#$rngd-command))
612 (stop #~(make-kill-destructor))))
613 (description "Run the @command{rngd} random number generation daemon to
614 supply entropy to the kernel's pool.")))
616 (define* (rngd-service #:key
617 (rng-tools rng-tools)
618 (device "/dev/hwrng"))
619 "Return a service that runs the @command{rngd} program from @var{rng-tools}
620 to add @var{device} to the kernel's entropy pool. The service will fail if
621 @var{device} does not exist."
622 (service rngd-service-type
624 (rng-tools rng-tools)
632 (define host-name-service-type
633 (shepherd-service-type
637 (documentation "Initialize the machine's host name.")
638 (provision '(host-name))
640 (sethostname #$name)))
642 (description "Initialize the machine's host name.")))
644 (define (host-name-service name)
645 "Return a service that sets the host name to @var{name}."
646 (service host-name-service-type name))
648 (define virtual-terminal-service-type
649 ;; Ensure that virtual terminals run in UTF-8 mode. This is the case by
650 ;; default with recent Linux kernels, but this service allows us to ensure
651 ;; this. This service must start before any 'term-' service so that newly
652 ;; created terminals inherit this property. See
653 ;; <https://bugs.gnu.org/30505> for a discussion.
654 (shepherd-service-type
657 (let ((knob "/sys/module/vt/parameters/default_utf8"))
659 (documentation "Set virtual terminals in UTF-8 module.")
660 (provision '(virtual-terminal))
661 (requirement '(root-file-system))
663 ;; In containers /sys is read-only so don't insist on
664 ;; writing to this file.
665 (unless (= 1 (call-with-input-file #$knob read))
666 (call-with-output-file #$knob
670 (stop #~(const #f)))))
672 (description "Ensure the Linux virtual terminals run in UTF-8 mode.")))
674 (define console-keymap-service-type
675 (shepherd-service-type
679 (documentation (string-append "Load console keymap (loadkeys)."))
680 (provision '(console-keymap))
682 (zero? (system* #$(file-append kbd "/bin/loadkeys")
685 (description "@emph{This service is deprecated in favor of the
686 @code{keyboard-layout} field of @code{operating-system}.} Load the given list
687 of console keymaps with @command{loadkeys}.")))
689 (define %default-console-font
690 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
691 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
692 ;; codepoints notably found in the UTF-8 manual.
695 (define (console-font-shepherd-services tty+font)
696 "Return a list of Shepherd services for each pair in TTY+FONT."
699 (let ((device (string-append "/dev/" tty)))
701 (documentation "Load a Unicode console font.")
702 (provision (list (symbol-append 'console-font-
703 (string->symbol tty))))
705 ;; Start after mingetty has been started on TTY, otherwise the settings
707 (requirement (list (symbol-append 'term-
708 (string->symbol tty))))
711 ;; It could be that mingetty is not fully ready yet,
712 ;; which we check by calling 'ttyname'.
714 (unless (or (zero? i)
715 (call-with-input-file #$device
717 (false-if-exception (ttyname port)))))
721 ;; Assume the VT is already in UTF-8 mode, thanks to
722 ;; the 'virtual-terminal' service.
724 ;; 'setfont' returns EX_OSERR (71) when an
725 ;; KDFONTOP ioctl fails, for example. Like
726 ;; systemd's vconsole support, let's not treat
728 (case (status:exit-val
729 (system* #$(file-append kbd "/bin/setfont")
730 "-C" #$device #$font))
737 (define console-font-service-type
738 (service-type (name 'console-fonts)
740 (list (service-extension shepherd-root-service-type
741 console-font-shepherd-services)))
742 (compose concatenate)
745 "Install the given fonts on the specified ttys (fonts are per
746 virtual console on GNU/Linux). The value of this service is a list of
747 tty/font pairs. The font can be the name of a font provided by the @code{kbd}
748 package or any valid argument to @command{setfont}, as in this example:
751 `((\"tty1\" . \"LatGrkCyr-8x16\")
752 (\"tty2\" . ,(file-append
754 \"/share/kbd/consolefonts/TamzenForPowerline10x20.psf\"))
755 (\"tty3\" . ,(file-append
757 \"/share/consolefonts/ter-132n\"))) ; for HDPI
760 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
761 "This procedure is deprecated in favor of @code{console-font-service-type}.
763 Return a service that sets up Unicode support in @var{tty} and loads
764 @var{font} for that tty (fonts are per virtual console in Linux.)"
765 (simple-service (symbol-append 'console-font- (string->symbol tty))
766 console-font-service-type `((,tty . ,font))))
768 (define %default-motd
769 (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
771 (define-record-type* <login-configuration>
772 login-configuration make-login-configuration
774 (motd login-configuration-motd ;file-like
775 (default %default-motd))
776 ;; Allow empty passwords by default so that first-time users can log in when
777 ;; the 'root' account has just been created.
778 (allow-empty-passwords? login-configuration-allow-empty-passwords?
779 (default #t))) ;Boolean
781 (define (login-pam-service config)
782 "Return the list of PAM service needed for CONF."
783 ;; Let 'login' be known to PAM.
784 (list (unix-pam-service "login"
786 #:allow-empty-passwords?
787 (login-configuration-allow-empty-passwords? config)
789 (login-configuration-motd config))))
791 (define login-service-type
792 (service-type (name 'login)
793 (extensions (list (service-extension pam-root-service-type
795 (default-value (login-configuration))
797 "Provide a console log-in service as specified by its
798 configuration value, a @code{login-configuration} object.")))
800 (define* (login-service #:optional (config (login-configuration)))
801 "Return a service configure login according to @var{config}, which specifies
802 the message of the day, among other things."
803 (service login-service-type config))
805 (define-record-type* <agetty-configuration>
806 agetty-configuration make-agetty-configuration
807 agetty-configuration?
808 (agetty agetty-configuration-agetty ;file-like
809 (default util-linux))
810 (tty agetty-configuration-tty) ;string | #f
811 (term agetty-term ;string | #f
813 (baud-rate agetty-baud-rate ;string | #f
815 (auto-login agetty-auto-login ;list of strings | #f
817 (login-program agetty-login-program ;gexp
818 (default (file-append shadow "/bin/login")))
819 (login-pause? agetty-login-pause? ;Boolean
821 (eight-bits? agetty-eight-bits? ;Boolean
823 (no-reset? agetty-no-reset? ;Boolean
825 (remote? agetty-remote? ;Boolean
827 (flow-control? agetty-flow-control? ;Boolean
829 (host agetty-host ;string | #f
831 (no-issue? agetty-no-issue? ;Boolean
833 (init-string agetty-init-string ;string | #f
835 (no-clear? agetty-no-clear? ;Boolean
837 (local-line agetty-local-line ;always | never | auto
839 (extract-baud? agetty-extract-baud? ;Boolean
841 (skip-login? agetty-skip-login? ;Boolean
843 (no-newline? agetty-no-newline? ;Boolean
845 (login-options agetty-login-options ;string | #f
847 (chroot agetty-chroot ;string | #f
849 (hangup? agetty-hangup? ;Boolean
851 (keep-baud? agetty-keep-baud? ;Boolean
853 (timeout agetty-timeout ;integer | #f
855 (detect-case? agetty-detect-case? ;Boolean
857 (wait-cr? agetty-wait-cr? ;Boolean
859 (no-hints? agetty-no-hints? ;Boolean
861 (no-hostname? agetty-no hostname? ;Boolean
863 (long-hostname? agetty-long-hostname? ;Boolean
865 (erase-characters agetty-erase-characters ;string | #f
867 (kill-characters agetty-kill-characters ;string | #f
869 (chdir agetty-chdir ;string | #f
871 (delay agetty-delay ;integer | #f
873 (nice agetty-nice ;integer | #f
875 ;; "Escape hatch" for passing arbitrary command-line arguments.
876 (extra-options agetty-extra-options ;list of strings
878 (shepherd-requirement agetty-shepherd-requirement ;list of SHEPHERD requirements
880 ;;; XXX Unimplemented for now!
881 ;;; (issue-file agetty-issue-file ;file-like
885 (define (default-serial-port)
886 "Return a gexp that determines a reasonable default serial port
887 to use as the tty. This is primarily useful for headless systems."
888 (with-imported-modules (source-module-closure
889 '((gnu build linux-boot))) ;for 'find-long-options'
891 ;; console=device,options
892 ;; device: can be tty0, ttyS0, lp0, ttyUSB0 (serial).
893 ;; options: BBBBPNF. P n|o|e, N number of bits,
894 ;; F flow control (r RTS)
895 (let* ((not-comma (char-set-complement (char-set #\,)))
896 (command (linux-command-line))
897 (agetty-specs (find-long-options "agetty.tty" command))
898 (console-specs (filter (lambda (spec)
899 (and (string-prefix? "tty" spec)
901 (string-prefix? "tty0" spec)
902 (string-prefix? "tty1" spec)
903 (string-prefix? "tty2" spec)
904 (string-prefix? "tty3" spec)
905 (string-prefix? "tty4" spec)
906 (string-prefix? "tty5" spec)
907 (string-prefix? "tty6" spec)
908 (string-prefix? "tty7" spec)
909 (string-prefix? "tty8" spec)
910 (string-prefix? "tty9" spec)))))
911 (find-long-options "console" command)))
912 (specs (append agetty-specs console-specs)))
916 ;; Extract device name from first spec.
917 (match (string-tokenize spec not-comma)
921 (define agetty-shepherd-service
923 (($ <agetty-configuration> agetty tty term baud-rate auto-login
924 login-program login-pause? eight-bits? no-reset? remote? flow-control?
925 host no-issue? init-string no-clear? local-line extract-baud?
926 skip-login? no-newline? login-options chroot hangup? keep-baud? timeout
927 detect-case? wait-cr? no-hints? no-hostname? long-hostname?
928 erase-characters kill-characters chdir delay nice extra-options
929 shepherd-requirement)
932 (documentation "Run agetty on a tty.")
933 (provision (list (symbol-append 'term- (string->symbol (or tty "console")))))
935 ;; Since the login prompt shows the host name, wait for the 'host-name'
936 ;; service to be done. Also wait for udev essentially so that the tty
937 ;; text is not lost in the middle of kernel messages (see also
938 ;; mingetty-shepherd-service).
939 (requirement (cons* 'user-processes 'host-name 'udev
940 shepherd-requirement))
942 (modules '((ice-9 match) (gnu build linux-boot)))
944 (with-imported-modules (source-module-closure
945 '((gnu build linux-boot)))
947 (let ((defaulted-tty #$(or tty (default-serial-port))))
950 (make-forkexec-constructor
951 (list #$(file-append util-linux "/sbin/agetty")
972 #~("--init-string" #$init-string)
977 ;;; FIXME This doesn't work as expected. According to agetty(8), if this option
978 ;;; is not passed, then the default is 'auto'. However, in my tests, when that
979 ;;; option is selected, agetty never presents the login prompt, and the
980 ;;; term-ttyS0 service respawns every few seconds.
982 #~(#$(match local-line
983 ('auto "--local-line=auto")
984 ('always "--local-line=always")
985 ('never "-local-line=never")))
1000 #~("--login-options" #$login-options)
1003 #~("--chroot" #$chroot)
1012 #~("--timeout" #$(number->string timeout))
1026 #$@(if long-hostname?
1027 #~("--long-hostname")
1029 #$@(if erase-characters
1030 #~("--erase-chars" #$erase-characters)
1032 #$@(if kill-characters
1033 #~("--kill-chars" #$kill-characters)
1036 #~("--chdir" #$chdir)
1039 #~("--delay" #$(number->string delay))
1042 #~("--nice" #$(number->string nice))
1045 (list "--autologin" auto-login)
1047 #$@(if login-program
1048 #~("--login-program" #$login-program)
1060 (const #f)) ; never start.
1062 (stop #~(make-kill-destructor)))))))
1064 (define agetty-service-type
1065 (service-type (name 'agetty)
1066 (extensions (list (service-extension shepherd-root-service-type
1067 agetty-shepherd-service)))
1069 "Provide console login using the @command{agetty}
1072 (define* (agetty-service config)
1073 "Return a service to run agetty according to @var{config}, which specifies
1074 the tty to run, among other things."
1075 (service agetty-service-type config))
1077 (define-record-type* <mingetty-configuration>
1078 mingetty-configuration make-mingetty-configuration
1079 mingetty-configuration?
1080 (mingetty mingetty-configuration-mingetty ;file-like
1082 (tty mingetty-configuration-tty) ;string
1083 (auto-login mingetty-auto-login ;string | #f
1085 (login-program mingetty-login-program ;gexp
1087 (login-pause? mingetty-login-pause? ;Boolean
1089 (clear-on-logout? mingetty-clear-on-logout? ;Boolean
1092 (define mingetty-shepherd-service
1094 (($ <mingetty-configuration> mingetty tty auto-login login-program
1095 login-pause? clear-on-logout?)
1098 (documentation "Run mingetty on an tty.")
1099 (provision (list (symbol-append 'term- (string->symbol tty))))
1101 ;; Since the login prompt shows the host name, wait for the 'host-name'
1102 ;; service to be done. Also wait for udev essentially so that the tty
1103 ;; text is not lost in the middle of kernel messages (XXX).
1104 (requirement '(user-processes host-name udev virtual-terminal))
1106 (start #~(make-forkexec-constructor
1107 (list #$(file-append mingetty "/sbin/mingetty")
1109 ;; Avoiding 'vhangup' allows us to avoid 'setfont'
1110 ;; errors down the path where various ioctls get
1111 ;; EIO--see 'hung_up_tty_ioctl' in driver/tty/tty_io.c
1115 #$@(if clear-on-logout?
1119 #~("--autologin" #$auto-login)
1121 #$@(if login-program
1122 #~("--loginprog" #$login-program)
1127 (stop #~(make-kill-destructor)))))))
1129 (define mingetty-service-type
1130 (service-type (name 'mingetty)
1131 (extensions (list (service-extension shepherd-root-service-type
1132 mingetty-shepherd-service)))
1134 "Provide console login using the @command{mingetty}
1137 (define* (mingetty-service config)
1138 "Return a service to run mingetty according to @var{config}, which specifies
1139 the tty to run, among other things."
1140 (service mingetty-service-type config))
1142 (define-record-type* <nscd-configuration> nscd-configuration
1143 make-nscd-configuration
1145 (log-file nscd-configuration-log-file ;string
1146 (default "/var/log/nscd.log"))
1147 (debug-level nscd-debug-level ;integer
1149 ;; TODO: See nscd.conf in glibc for other options to add.
1150 (caches nscd-configuration-caches ;list of <nscd-cache>
1151 (default %nscd-default-caches))
1152 (name-services nscd-configuration-name-services ;list of file-like
1154 (glibc nscd-configuration-glibc ;file-like
1157 (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
1159 (database nscd-cache-database) ;symbol
1160 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
1161 (negative-time-to-live nscd-cache-negative-time-to-live
1162 (default 20)) ;integer
1163 (suggested-size nscd-cache-suggested-size ;integer ("default module
1166 (check-files? nscd-cache-check-files? ;Boolean
1168 (persistent? nscd-cache-persistent? ;Boolean
1170 (shared? nscd-cache-shared? ;Boolean
1172 (max-database-size nscd-cache-max-database-size ;integer
1173 (default (* 32 (expt 2 20))))
1174 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
1177 (define %nscd-default-caches
1178 ;; Caches that we want to enable by default. Note that when providing an
1179 ;; empty nscd.conf, all caches are disabled.
1180 (list (nscd-cache (database 'hosts)
1182 ;; Aggressively cache the host name cache to improve
1183 ;; privacy and resilience.
1184 (positive-time-to-live (* 3600 12))
1185 (negative-time-to-live 20)
1188 (nscd-cache (database 'services)
1190 ;; Services are unlikely to change, so we can be even more
1192 (positive-time-to-live (* 3600 24))
1193 (negative-time-to-live 3600)
1194 (check-files? #t) ;check /etc/services changes
1197 (define %nscd-default-configuration
1198 ;; Default nscd configuration.
1199 (nscd-configuration))
1201 (define (nscd.conf-file config)
1202 "Return the @file{nscd.conf} configuration file for @var{config}, an
1203 @code{<nscd-configuration>} object."
1204 (define cache->config
1206 (($ <nscd-cache> (= symbol->string database)
1207 positive-ttl negative-ttl size check-files?
1208 persistent? shared? max-size propagate?)
1209 (string-append "\nenable-cache\t" database "\tyes\n"
1211 "positive-time-to-live\t" database "\t"
1212 (number->string positive-ttl) "\n"
1213 "negative-time-to-live\t" database "\t"
1214 (number->string negative-ttl) "\n"
1215 "suggested-size\t" database "\t"
1216 (number->string size) "\n"
1217 "check-files\t" database "\t"
1218 (if check-files? "yes\n" "no\n")
1219 "persistent\t" database "\t"
1220 (if persistent? "yes\n" "no\n")
1221 "shared\t" database "\t"
1222 (if shared? "yes\n" "no\n")
1223 "max-db-size\t" database "\t"
1224 (number->string max-size) "\n"
1225 "auto-propagate\t" database "\t"
1226 (if propagate? "yes\n" "no\n")))))
1229 (($ <nscd-configuration> log-file debug-level caches)
1230 (plain-file "nscd.conf"
1232 # Configuration of libc's name service cache daemon (nscd).\n\n"
1234 (string-append "logfile\t" log-file)
1238 (string-append "debug-level\t"
1239 (number->string debug-level))
1243 (map cache->config caches)))))))
1245 (define (nscd-action-procedure nscd config option)
1246 ;; XXX: This is duplicated from mcron; factorize.
1247 #~(lambda (_ . args)
1248 ;; Run 'nscd' in a pipe so we can explicitly redirect its output to
1249 ;; 'current-output-port', which at this stage is bound to the client
1251 (let ((pipe (apply open-pipe* OPEN_READ #$nscd
1252 "-f" #$config #$option args)))
1254 (match (read-line pipe 'concat)
1256 (catch 'system-error
1258 (zero? (close-pipe pipe)))
1260 ;; There's a race with the SIGCHLD handler, which could
1261 ;; call 'waitpid' before 'close-pipe' above does. If we
1262 ;; get ECHILD, that means we lost the race; in that case, we
1263 ;; cannot tell what the exit code was (FIXME).
1264 (or (= ECHILD (system-error-errno args))
1265 (apply throw args)))))
1270 (define (nscd-actions nscd config)
1271 "Return Shepherd actions for NSCD."
1272 ;; Make this functionality available as actions because that's a simple way
1273 ;; to run the right 'nscd' binary with the right config file.
1274 (list (shepherd-action
1276 (documentation "Display statistics about nscd usage.")
1277 (procedure (nscd-action-procedure nscd config "--statistics")))
1281 "Invalidate the given cache--e.g., 'hosts' for host name lookups.")
1282 (procedure (nscd-action-procedure nscd config "--invalidate")))))
1284 (define (nscd-shepherd-service config)
1285 "Return a shepherd service for CONFIG, an <nscd-configuration> object."
1286 (let ((nscd (file-append (nscd-configuration-glibc config)
1288 (nscd.conf (nscd.conf-file config))
1289 (name-services (nscd-configuration-name-services config)))
1290 (list (shepherd-service
1291 (documentation "Run libc's name service cache daemon (nscd).")
1293 (requirement '(user-processes))
1294 (start #~(make-forkexec-constructor
1295 (list #$nscd "-f" #$nscd.conf "--foreground")
1297 ;; Wait for the PID file. However, the PID file is
1298 ;; written before nscd is actually listening on its
1300 #:pid-file "/var/run/nscd/nscd.pid"
1302 #:environment-variables
1303 (list (string-append "LD_LIBRARY_PATH="
1306 (string-append dir "/lib"))
1307 (list #$@name-services))
1309 (stop #~(make-kill-destructor))
1310 (modules `((ice-9 popen) ;for the actions
1313 ,@%default-modules))
1314 (actions (nscd-actions nscd nscd.conf))))))
1316 (define nscd-activation
1317 ;; Actions to take before starting nscd.
1319 (use-modules (guix build utils))
1320 (mkdir-p "/var/run/nscd")
1321 (mkdir-p "/var/db/nscd") ;for the persistent cache
1323 ;; In libc 2.25 nscd uses inotify to watch /etc/resolv.conf, but only if
1324 ;; that file exists when it is started. Thus create it here. Note: on
1325 ;; some systems, such as when NetworkManager is used, /etc/resolv.conf
1326 ;; is a symlink, hence 'lstat'.
1327 (unless (false-if-exception (lstat "/etc/resolv.conf"))
1328 (call-with-output-file "/etc/resolv.conf"
1330 (display "# This is a placeholder.\n" port))))))
1332 (define nscd-service-type
1333 (service-type (name 'nscd)
1335 (list (service-extension activation-service-type
1336 (const nscd-activation))
1337 (service-extension shepherd-root-service-type
1338 nscd-shepherd-service)))
1340 ;; This can be extended by providing additional name services
1341 ;; such as nss-mdns.
1342 (compose concatenate)
1343 (extend (lambda (config name-services)
1346 (name-services (append
1347 (nscd-configuration-name-services config)
1349 (default-value %nscd-default-configuration)
1351 "Runs libc's @dfn{name service cache daemon} (nscd) with the
1352 given configuration---an @code{<nscd-configuration>} object. @xref{Name
1353 Service Switch}, for an example.")))
1355 (define* (nscd-service #:optional (config %nscd-default-configuration))
1356 "Return a service that runs libc's name service cache daemon (nscd) with the
1357 given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
1358 Service Switch}, for an example."
1359 (service nscd-service-type config))
1362 (define-record-type* <syslog-configuration>
1363 syslog-configuration make-syslog-configuration
1364 syslog-configuration?
1365 (syslogd syslog-configuration-syslogd
1366 (default (file-append inetutils "/libexec/syslogd")))
1367 (config-file syslog-configuration-config-file
1368 (default %default-syslog.conf)))
1370 (define syslog-service-type
1371 (shepherd-service-type
1375 (documentation "Run the syslog daemon (syslogd).")
1376 (provision '(syslogd))
1377 (requirement '(user-processes))
1378 (start #~(let ((spawn (make-forkexec-constructor
1379 (list #$(syslog-configuration-syslogd config)
1381 #$(syslog-configuration-config-file config))
1382 #:pid-file "/var/run/syslog.pid")))
1384 ;; Set the umask such that file permissions are #o640.
1385 (let ((mask (umask #o137))
1389 (stop #~(make-kill-destructor))))
1390 (description "Run the syslog daemon, @command{syslogd}, which is
1391 responsible for logging system messages.")))
1393 ;; Snippet adapted from the GNU inetutils manual.
1394 (define %default-syslog.conf
1395 (plain-file "syslog.conf" "
1396 # Log all error messages, authentication messages of
1397 # level notice or higher and anything of level err or
1398 # higher to the console.
1399 # Don't log private authentication messages!
1400 *.alert;auth.notice;authpriv.none /dev/console
1402 # Log anything (except mail) of level info or higher.
1403 # Don't log private authentication messages!
1404 *.info;mail.none;authpriv.none /var/log/messages
1406 # Like /var/log/messages, but also including \"debug\"-level logs.
1407 *.debug;mail.none;authpriv.none /var/log/debug
1409 # Same, in a different place.
1410 *.info;mail.none;authpriv.none /dev/tty12
1412 # The authpriv file has restricted access.
1413 authpriv.* /var/log/secure
1415 # Log all the mail messages in one place.
1416 mail.* /var/log/maillog
1419 (define* (syslog-service #:optional (config (syslog-configuration)))
1420 "Return a service that runs @command{syslogd} and takes
1421 @var{<syslog-configuration>} as a parameter.
1423 @xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
1424 information on the configuration file syntax."
1425 (service syslog-service-type config))
1428 (define pam-limits-service-type
1429 (let ((security-limits
1430 ;; Create /etc/security containing the provided "limits.conf" file.
1431 (lambda (limits-file)
1432 `(("security/limits.conf"
1436 (let ((pam-limits (pam-entry
1437 (control "required")
1438 (module "pam_limits.so")
1439 (arguments '("conf=/etc/security/limits.conf")))))
1440 (if (member (pam-service-name pam)
1441 '("login" "su" "slim" "gdm-password" "sddm"))
1444 (session (cons pam-limits
1445 (pam-service-session pam))))
1450 (list (service-extension etc-service-type security-limits)
1451 (service-extension pam-root-service-type
1452 (lambda _ (list pam-extension)))))
1454 "Install the specified resource usage limits by populating
1455 @file{/etc/security/limits.conf} and using the @code{pam_limits}
1456 authentication module."))))
1458 (define* (pam-limits-service #:optional (limits '()))
1459 "Return a service that makes selected programs respect the list of
1460 pam-limits-entry specified in LIMITS via pam_limits.so."
1461 (service pam-limits-service-type
1462 (plain-file "limits.conf"
1463 (string-join (map pam-limits-entry->string limits)
1471 (define* (guix-build-accounts count #:key
1474 "Return a list of COUNT user accounts for Guix build users with the given
1476 (unfold (cut > <> count)
1479 (name (format #f "guixbuilder~2,'0d" n))
1483 ;; guix-daemon expects GROUP to be listed as a
1484 ;; supplementary group too:
1485 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
1486 (supplementary-groups (list group "kvm"))
1488 (comment (format #f "Guix Build User ~2d" n))
1489 (home-directory "/var/empty")
1490 (shell (file-append shadow "/sbin/nologin"))))
1495 ;; Select (guix …) and (gnu …) modules, except (guix config).
1497 (('guix 'config) #f)
1498 (('guix rest ...) #t)
1499 (('gnu rest ...) #t)
1502 (define (substitute-key-authorization keys guix)
1503 "Return a gexp with code to register KEYS, a list of files containing 'guix
1504 archive' public keys, with GUIX."
1506 (with-extensions (list guile-gcrypt)
1507 (with-imported-modules `(((guix config) => ,(make-config.scm))
1508 ,@(source-module-closure '((guix pki))
1509 #:select? not-config?))
1510 (computed-file "acl"
1512 (use-modules (guix pki)
1518 (call-with-input-file file
1519 (compose string->canonical-sexp
1523 (call-with-output-file #$output
1525 (write-acl (public-keys->acl keys)
1528 (with-imported-modules '((guix build utils))
1530 (use-modules (guix build utils))
1532 ;; If the ACL already exists, move it out of the way. Create a backup
1533 ;; if it's a regular file: it's likely that the user manually updated
1534 ;; it with 'guix archive --authorize'.
1535 (if (file-exists? "/etc/guix/acl")
1536 (if (and (symbolic-link? "/etc/guix/acl")
1537 (store-file-name? (readlink "/etc/guix/acl")))
1538 (delete-file "/etc/guix/acl")
1539 (rename-file "/etc/guix/acl" "/etc/guix/acl.bak"))
1540 (mkdir-p "/etc/guix"))
1542 ;; Installed the declared ACL.
1543 (symlink #+default-acl "/etc/guix/acl"))))
1545 (define %default-authorized-guix-keys
1546 ;; List of authorized substitute keys.
1547 (list (file-append guix "/share/guix/berlin.guix.gnu.org.pub")
1548 (file-append guix "/share/guix/bordeaux.guix.gnu.org.pub")))
1550 (define-record-type* <guix-configuration>
1551 guix-configuration make-guix-configuration
1553 (guix guix-configuration-guix ;file-like
1555 (build-group guix-configuration-build-group ;string
1556 (default "guixbuild"))
1557 (build-accounts guix-configuration-build-accounts ;integer
1559 (authorize-key? guix-configuration-authorize-key? ;Boolean
1561 (authorized-keys guix-configuration-authorized-keys ;list of gexps
1562 (default %default-authorized-guix-keys))
1563 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
1565 (substitute-urls guix-configuration-substitute-urls ;list of strings
1566 (default %default-substitute-urls))
1567 (generate-substitute-key? guix-configuration-generate-substitute-key?
1568 (default #t)) ;Boolean
1569 (chroot-directories guix-configuration-chroot-directories ;list of file-like/strings
1571 (max-silent-time guix-configuration-max-silent-time ;integer
1573 (timeout guix-configuration-timeout ;integer
1575 (log-compression guix-configuration-log-compression
1577 (discover? guix-configuration-discover?
1579 (extra-options guix-configuration-extra-options ;list of strings
1581 (log-file guix-configuration-log-file ;string
1582 (default "/var/log/guix-daemon.log"))
1583 (http-proxy guix-http-proxy ;string | #f
1585 (tmpdir guix-tmpdir ;string | #f
1588 (define %default-guix-configuration
1589 (guix-configuration))
1591 (define shepherd-set-http-proxy-action
1592 ;; Shepherd action to change the HTTP(S) proxy.
1594 (name 'set-http-proxy)
1596 "Change the HTTP(S) proxy used by 'guix-daemon' and restart it.")
1597 (procedure #~(lambda* (_ #:optional proxy)
1598 (let ((environment (environ)))
1599 ;; A bit of a hack: communicate PROXY to the 'start'
1600 ;; method via environment variables.
1603 (format #t "changing HTTP/HTTPS \
1604 proxy of 'guix-daemon' to ~s...~%"
1606 (setenv "http_proxy" proxy))
1608 (format #t "clearing HTTP/HTTPS \
1609 proxy of 'guix-daemon'...~%")
1610 (unsetenv "http_proxy")))
1611 (action 'guix-daemon 'restart)
1612 (environ environment)
1615 (define shepherd-discover-action
1616 ;; Shepherd action to enable or disable substitute servers discovery.
1620 "Enable or disable substitute servers discovery and restart the
1622 (procedure #~(lambda* (_ status)
1623 (let ((environment (environ)))
1625 (string=? status "on"))
1627 (format #t "enable substitute servers discovery~%")
1628 (setenv "discover" "on"))
1630 (format #t "disable substitute servers discovery~%")
1631 (unsetenv "discover")))
1632 (action 'guix-daemon 'restart)
1633 (environ environment)
1636 (define (guix-shepherd-service config)
1637 "Return a <shepherd-service> for the Guix daemon service with CONFIG."
1638 (match-record config <guix-configuration>
1639 (guix build-group build-accounts authorize-key? authorized-keys
1640 use-substitutes? substitute-urls max-silent-time timeout
1641 log-compression discover? extra-options log-file
1642 http-proxy tmpdir chroot-directories)
1643 (list (shepherd-service
1644 (documentation "Run the Guix daemon.")
1645 (provision '(guix-daemon))
1646 (requirement '(user-processes))
1647 (actions (list shepherd-set-http-proxy-action
1648 shepherd-discover-action))
1649 (modules '((srfi srfi-1)
1651 (gnu build shepherd)))
1653 (with-imported-modules `(((guix config) => ,(make-config.scm))
1654 ,@(source-module-closure
1655 '((gnu build shepherd))
1656 #:select? not-config?))
1659 ;; HTTP/HTTPS proxy. The 'http_proxy' variable is set by
1660 ;; the 'set-http-proxy' action.
1661 (or (getenv "http_proxy") #$http-proxy))
1664 (or (getenv "discover") #$discover?))
1666 ;; Start the guix-daemon from a container, when supported,
1667 ;; to solve an installation issue. See the comment below for
1669 (fork+exec-command/container
1670 (cons* #$(file-append guix "/bin/guix-daemon")
1671 "--build-users-group" #$build-group
1673 #$(number->string max-silent-time)
1674 "--timeout" #$(number->string timeout)
1676 #$(symbol->string log-compression)
1677 #$@(if use-substitutes?
1679 '("--no-substitutes"))
1680 (string-append "--discover="
1681 (if discover? "yes" "no"))
1682 "--substitute-urls" #$(string-join substitute-urls)
1685 ;; Add CHROOT-DIRECTORIES and all their dependencies
1686 ;; (if these are store items) to the chroot.
1689 (append-map (lambda (directory)
1690 (list "--chroot-directory"
1692 (call-with-input-file file
1694 '#$(map references-file
1695 chroot-directories)))
1697 ;; When running the installer, we need guix-daemon to
1698 ;; operate from within the same MNT namespace as the
1699 ;; installation container. In that case only, enter the
1700 ;; namespace of the process PID passed as start argument.
1701 ;; Otherwise, for symmetry purposes enter the caller
1702 ;; namespaces which is a no-op.
1704 ((pid) (string->number pid))
1707 #:environment-variables
1708 (append (list #$@(if tmpdir
1709 (list (string-append "TMPDIR=" tmpdir))
1712 ;; Make sure we run in a UTF-8 locale so that
1713 ;; 'guix offload' correctly restores nars
1714 ;; that contain UTF-8 file names such as
1716 ;; <https://bugs.gnu.org/32942>.
1717 (string-append "GUIX_LOCPATH="
1718 #$glibc-utf8-locales
1721 ;; Make 'tar' and 'gzip' available so
1722 ;; that 'guix perform-download' can use
1723 ;; them when downloading from Software
1724 ;; Heritage via '(guix swh)'.
1725 (string-append "PATH="
1726 #$(file-append tar "/bin") ":"
1727 #$(file-append gzip "/bin")))
1729 (list (string-append "http_proxy=" proxy)
1730 (string-append "https_proxy=" proxy))
1733 #:log-file #$log-file))))
1734 (stop #~(make-kill-destructor))))))
1736 (define (guix-accounts config)
1737 "Return the user accounts and user groups for CONFIG."
1739 (($ <guix-configuration> _ build-group build-accounts)
1744 ;; Use a fixed GID so that we can create the store with the right
1747 (guix-build-accounts build-accounts
1748 #:group build-group)))))
1750 (define (guix-activation config)
1751 "Return the activation gexp for CONFIG."
1752 (match-record config <guix-configuration>
1753 (guix generate-substitute-key? authorize-key? authorized-keys)
1755 ;; Assume that the store has BUILD-GROUP as its group. We could
1756 ;; otherwise call 'chown' here, but the problem is that on a COW overlayfs,
1757 ;; chown leads to an entire copy of the tree, which is a bad idea.
1759 ;; Generate a key pair and optionally authorize substitute server keys.
1760 (unless (or #$(not generate-substitute-key?)
1761 (file-exists? "/etc/guix/signing-key.pub"))
1762 (system* #$(file-append guix "/bin/guix") "archive"
1765 #$(if authorize-key?
1766 (substitute-key-authorization authorized-keys guix)
1769 (define guix-service-type
1773 (list (service-extension shepherd-root-service-type guix-shepherd-service)
1774 (service-extension account-service-type guix-accounts)
1775 (service-extension activation-service-type guix-activation)
1776 (service-extension profile-service-type
1777 (compose list guix-configuration-guix))))
1779 ;; Extensions can specify extra directories to add to the build chroot.
1780 (compose concatenate)
1781 (extend (lambda (config directories)
1785 (append (guix-configuration-chroot-directories config)
1788 (default-value (guix-configuration))
1790 "Run the build daemon of GNU@tie{}Guix, aka. @command{guix-daemon}.")))
1793 (define-record-type* <guix-publish-configuration>
1794 guix-publish-configuration make-guix-publish-configuration
1795 guix-publish-configuration?
1796 (guix guix-publish-configuration-guix ;file-like
1798 (port guix-publish-configuration-port ;number
1800 (host guix-publish-configuration-host ;string
1801 (default "localhost"))
1802 (advertise? guix-publish-advertise? ;boolean
1804 (compression guix-publish-configuration-compression
1806 (default (default-compression this-record
1807 (current-source-location))))
1808 (compression-level %guix-publish-configuration-compression-level ;deprecated
1810 (nar-path guix-publish-configuration-nar-path ;string
1812 (cache guix-publish-configuration-cache ;#f | string
1814 (cache-bypass-threshold guix-publish-configuration-cache-bypass-threshold
1815 (default (* 10 (expt 2 20)))) ;integer
1816 (workers guix-publish-configuration-workers ;#f | integer
1818 (ttl guix-publish-configuration-ttl ;#f | integer
1820 (negative-ttl guix-publish-configuration-negative-ttl ;#f | integer
1823 (define-deprecated (guix-publish-configuration-compression-level config)
1824 "Return a compression level, the old way."
1825 (match (guix-publish-configuration-compression config)
1826 (((_ level) _ ...) level)))
1828 (define (default-compression config properties)
1829 "Return the default 'guix publish' compression according to CONFIG, and
1830 raise a deprecation warning if the 'compression-level' field was used."
1831 (match (%guix-publish-configuration-compression-level config)
1833 ;; Default to low compression levels when there's no cache so that users
1834 ;; get good bandwidth by default.
1835 (if (guix-publish-configuration-cache config)
1836 '(("gzip" 5) ("zstd" 19))
1837 '(("gzip" 3) ("zstd" 3)))) ;zstd compresses faster
1839 (warn-about-deprecation 'compression-level properties
1840 #:replacement 'compression)
1841 `(("gzip" ,level)))))
1843 (define (guix-publish-shepherd-service config)
1844 (define (config->compression-options config)
1845 (match (guix-publish-configuration-compression config)
1846 (() ;empty list means "no compression"
1849 (append-map (match-lambda
1851 `("-C" ,(string-append type ":"
1852 (number->string level)))))
1855 (match-record config <guix-publish-configuration>
1856 (guix port host nar-path cache workers ttl negative-ttl
1857 cache-bypass-threshold advertise?)
1858 (let ((command #~(list #$(file-append guix "/bin/guix")
1859 "publish" "-u" "guix-publish"
1860 "-p" #$(number->string port)
1861 #$@(config->compression-options config)
1862 (string-append "--nar-path=" #$nar-path)
1863 (string-append "--listen=" #$host)
1868 #~((string-append "--workers="
1873 #~((string-append "--ttl="
1874 #$(number->string ttl)
1878 #~((string-append "--negative-ttl="
1879 #$(number->string negative-ttl)
1883 #~((string-append "--cache=" #$cache)
1885 "--cache-bypass-threshold="
1887 cache-bypass-threshold)))
1889 (options #~(#:environment-variables
1890 ;; Make sure we run in a UTF-8 locale so we can produce
1891 ;; nars for packages that contain UTF-8 file names such
1892 ;; as 'nss-certs'. See <https://bugs.gnu.org/26948>.
1893 (list (string-append "GUIX_LOCPATH="
1894 #$glibc-utf8-locales "/lib/locale")
1895 "LC_ALL=en_US.utf8")
1896 #:log-file "/var/log/guix-publish.log"))
1897 (endpoints #~(let ((ai (false-if-exception
1899 #$(number->string port)
1902 (list (endpoint (addrinfo:addr (car ai))))
1904 (list (shepherd-service
1905 (provision '(guix-publish))
1906 (requirement `(user-processes
1908 ,@(if advertise? '(avahi-daemon) '())))
1910 ;; Use lazy socket activation unless ADVERTISE? is true: in that
1911 ;; case the process should start right away to advertise itself.
1912 (start #~(if (and (defined? 'make-systemd-constructor) ;> 0.9.0?
1914 (make-systemd-constructor
1915 #$command #$endpoints #$@options)
1916 (make-forkexec-constructor #$command #$@options)))
1917 (stop #~(if (and (defined? 'make-systemd-destructor)
1919 (make-systemd-destructor)
1920 (make-kill-destructor))))))))
1922 (define %guix-publish-accounts
1923 (list (user-group (name "guix-publish") (system? #t))
1925 (name "guix-publish")
1926 (group "guix-publish")
1928 (comment "guix publish user")
1929 (home-directory "/var/empty")
1930 (shell (file-append shadow "/sbin/nologin")))))
1932 (define %guix-publish-log-rotations
1934 (files (list "/var/log/guix-publish.log")))))
1936 (define (guix-publish-activation config)
1937 (let ((cache (guix-publish-configuration-cache config)))
1939 (with-imported-modules '((guix build utils))
1941 (use-modules (guix build utils))
1944 (let* ((pw (getpw "guix-publish"))
1945 (uid (passwd:uid pw))
1946 (gid (passwd:gid pw)))
1947 (chown #$cache uid gid))))
1950 (define guix-publish-service-type
1951 (service-type (name 'guix-publish)
1953 (list (service-extension shepherd-root-service-type
1954 guix-publish-shepherd-service)
1955 (service-extension account-service-type
1956 (const %guix-publish-accounts))
1957 (service-extension rottlog-service-type
1958 (const %guix-publish-log-rotations))
1959 (service-extension activation-service-type
1960 guix-publish-activation)))
1961 (default-value (guix-publish-configuration))
1963 "Add a Shepherd service running @command{guix publish}, a
1964 command that allows you to share pre-built binaries with others over HTTP.")))
1971 (define-record-type* <udev-configuration>
1972 udev-configuration make-udev-configuration
1974 (udev udev-configuration-udev ;file-like
1976 (rules udev-configuration-rules ;list of file-like
1979 (define (udev-rules-union packages)
1980 "Return the union of the @code{lib/udev/rules.d} directories found in each
1981 item of @var{packages}."
1983 (with-imported-modules '((guix build union)
1986 (use-modules (guix build union)
1991 (define %standard-locations
1992 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
1994 (define (rules-sub-directory directory)
1995 ;; Return the sub-directory of DIRECTORY containing udev rules, or
1996 ;; #f if none was found.
1997 (find directory-exists?
1998 (map (cut string-append directory <>) %standard-locations)))
2000 (union-build #$output
2001 (filter-map rules-sub-directory '#$packages)))))
2003 (computed-file "udev-rules" build))
2005 (define (udev-rule file-name contents)
2006 "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
2007 (computed-file file-name
2008 (with-imported-modules '((guix build utils))
2010 (use-modules (guix build utils))
2013 (string-append #$output "/lib/udev/rules.d"))
2016 (call-with-output-file
2017 (string-append rules.d "/" #$file-name)
2019 (display #$contents port)))))))
2021 (define (file->udev-rule file-name file)
2022 "Return a directory with a udev rule file FILE-NAME which is a copy of FILE."
2023 (computed-file file-name
2024 (with-imported-modules '((guix build utils))
2026 (use-modules (guix build utils))
2029 (string-append #$output "/lib/udev/rules.d"))
2031 (define file-copy-dest
2032 (string-append rules.d "/" #$file-name))
2035 (copy-file #$file file-copy-dest)))))
2037 (define kvm-udev-rule
2038 ;; Return a directory with a udev rule that changes the group of /dev/kvm to
2039 ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
2040 ;; but now we have to add it by ourselves.
2042 ;; Build users are part of the "kvm" group, so we can fearlessly make
2043 ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
2044 (udev-rule "90-kvm.rules"
2045 "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
2047 (define udev-shepherd-service
2048 ;; Return a <shepherd-service> for UDEV with RULES.
2050 (($ <udev-configuration> udev)
2055 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
2057 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
2058 (requirement '(root-file-system))
2060 (documentation "Populate the /dev directory, dynamically.")
2062 (with-imported-modules (source-module-closure
2063 '((gnu build linux-boot)))
2066 ;; 'udevd' from eudev.
2067 #$(file-append udev "/sbin/udevd"))
2069 (define (wait-for-udevd)
2070 ;; Wait until someone's listening on udevd's control
2072 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
2074 (catch 'system-error
2076 (connect sock PF_UNIX "/run/udev/control")
2079 (format #t "waiting for udevd...~%")
2083 ;; Allow udev to find the modules.
2084 (setenv "LINUX_MODULE_DIRECTORY"
2085 "/run/booted-system/kernel/lib/modules")
2087 (let* ((kernel-release
2088 (utsname:release (uname)))
2089 (linux-module-directory
2090 (getenv "LINUX_MODULE_DIRECTORY"))
2092 (string-append linux-module-directory "/"
2094 (old-umask (umask #o022)))
2095 ;; If we're in a container, DIRECTORY might not exist,
2096 ;; for instance because the host runs a different
2097 ;; kernel. In that case, skip it; we'll just miss a few
2098 ;; nodes like /dev/fuse.
2099 (when (file-exists? directory)
2100 (make-static-device-nodes directory))
2103 (let ((pid (fork+exec-command
2105 #:environment-variables
2107 ;; The first one is for udev, the second one for
2109 "UDEV_CONFIG_FILE=/etc/udev/udev.conf"
2110 "EUDEV_RULES_DIRECTORY=/etc/udev/rules.d"
2111 (string-append "LINUX_MODULE_DIRECTORY="
2112 (getenv "LINUX_MODULE_DIRECTORY"))
2113 (default-environment-variables)))))
2114 ;; Wait until udevd is up and running. This appears to
2115 ;; be needed so that the events triggered below are
2116 ;; actually handled.
2119 ;; Trigger device node creation.
2120 (system* #$(file-append udev "/bin/udevadm")
2121 "trigger" "--action=add")
2123 ;; Wait for things to settle down.
2124 (system* #$(file-append udev "/bin/udevadm")
2127 (stop #~(make-kill-destructor))
2129 ;; When halting the system, 'udev' is actually killed by
2130 ;; 'user-processes', i.e., before its own 'stop' method was called.
2131 ;; Thus, make sure it is not respawned.
2133 ;; We need additional modules.
2134 (modules `((gnu build linux-boot) ;'make-static-device-nodes'
2135 ,@%default-modules)))))))
2138 (computed-file "udev.conf"
2139 #~(call-with-output-file #$output
2141 (format port "udev_rules=\"/etc/udev/rules.d\"~%")))))
2145 (($ <udev-configuration> udev rules)
2148 "udev" `(("udev.conf" ,udev.conf)
2149 ("rules.d" ,(udev-rules-union (cons* udev kvm-udev-rule
2152 (define udev-service-type
2153 (service-type (name 'udev)
2155 (list (service-extension shepherd-root-service-type
2156 udev-shepherd-service)
2157 (service-extension etc-service-type udev-etc)))
2158 (compose concatenate) ;concatenate the list of rules
2159 (extend (lambda (config rules)
2161 (($ <udev-configuration> udev initial-rules)
2164 (rules (append initial-rules rules)))))))
2165 (default-value (udev-configuration))
2167 "Run @command{udev}, which populates the @file{/dev}
2168 directory dynamically. Get extra rules from the packages listed in the
2169 @code{rules} field of its value, @code{udev-configuration} object.")))
2171 (define* (udev-service #:key (udev eudev) (rules '()))
2172 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
2173 extra rules from the packages listed in @var{rules}."
2174 (service udev-service-type
2175 (udev-configuration (udev udev) (rules rules))))
2177 (define* (udev-rules-service name rules #:key (groups '()))
2178 "Return a service that extends udev-service-type with RULES and
2179 account-service-type with GROUPS as system groups. This works by creating a
2180 singleton service type NAME-udev-rules, of which the returned service is an
2182 (let* ((name (symbol-append name '-udev-rules))
2184 (const (map (lambda (group)
2185 (user-group (name group) (system? #t)))
2187 (udev-extension (const (list rules)))
2192 account-service-type account-extension)
2194 udev-service-type udev-extension)))
2195 (description "This service adds udev rules."))))
2198 (define (swap-space->shepherd-service-name space)
2199 (let ((target (swap-space-target space)))
2200 (symbol-append 'swap-
2202 (cond ((uuid? target)
2203 (uuid->string target))
2204 ((file-system-label? target)
2205 (file-system-label->string target))
2209 ; TODO Remove after deprecation
2210 (define (swap-deprecated->shepherd-service-name sdep)
2211 (symbol-append 'swap-
2214 (string-take (uuid->string sdep) 6))
2215 ((file-system-label? sdep)
2216 (file-system-label->string sdep))
2220 (define swap->shepherd-service-name
2221 (match-lambda ((? swap-space? space)
2222 (swap-space->shepherd-service-name space))
2224 (swap-deprecated->shepherd-service-name sdep))))
2226 (define swap-service-type
2227 (shepherd-service-type
2230 (define requirements
2231 (cond ((swap-space? swap)
2232 (map dependency->shepherd-service-name
2233 (swap-space-dependencies swap)))
2234 ; TODO Remove after deprecation
2235 ((and (string? swap) (string-prefix? "/dev/mapper/" swap))
2236 (list (symbol-append 'device-mapping-
2237 (string->symbol (basename swap)))))
2241 (define device-lookup
2242 ;; The generic 'find-partition' procedures could return a partition
2243 ;; that's not swap space, but that's unlikely.
2244 (cond ((swap-space? swap)
2245 (let ((target (swap-space-target swap)))
2246 (cond ((uuid? target)
2247 #~(find-partition-by-uuid #$(uuid-bytevector target)))
2248 ((file-system-label? target)
2249 #~(find-partition-by-label
2250 #$(file-system-label->string target)))
2253 ; TODO Remove after deprecation
2255 #~(find-partition-by-uuid #$(uuid-bytevector swap)))
2256 ((file-system-label? swap)
2257 #~(find-partition-by-label
2258 #$(file-system-label->string swap)))
2262 (with-imported-modules (source-module-closure '((gnu build file-systems)))
2264 (provision (list (swap->shepherd-service-name swap)))
2265 (requirement `(udev ,@requirements))
2266 (documentation "Enable the given swap space.")
2267 (modules `((gnu build file-systems)
2268 ,@%default-modules))
2270 (let ((device #$device-lookup))
2273 (restart-on-EINTR (swapon device
2274 #$(if (swap-space? swap)
2275 (swap-space->flags-bit-mask
2280 (let ((device #$device-lookup))
2282 (restart-on-EINTR (swapoff device)))
2285 (description "Turn on the virtual memory swap area.")))
2287 (define (swap-service swap)
2288 "Return a service that uses @var{swap} as a swap space."
2289 (service swap-service-type swap))
2291 (define %default-gpm-options
2292 ;; Default options for GPM.
2293 '("-m" "/dev/input/mice" "-t" "ps2"))
2295 (define-record-type* <gpm-configuration>
2296 gpm-configuration make-gpm-configuration gpm-configuration?
2297 (gpm gpm-configuration-gpm ;file-like
2299 (options gpm-configuration-options ;list of strings
2300 (default %default-gpm-options)))
2302 (define gpm-shepherd-service
2304 (($ <gpm-configuration> gpm options)
2305 (list (shepherd-service
2306 (requirement '(udev))
2308 ;; 'gpm' runs in the background and sets a PID file.
2309 ;; Note that it requires running as "root".
2310 (start #~(make-forkexec-constructor
2311 (list #$(file-append gpm "/sbin/gpm")
2313 #:pid-file "/var/run/gpm.pid"
2314 #:pid-file-timeout 3))
2316 ;; Return #f if successfully stopped.
2317 (not (zero? (system* #$(file-append gpm "/sbin/gpm")
2320 (define gpm-service-type
2321 (service-type (name 'gpm)
2323 (list (service-extension shepherd-root-service-type
2324 gpm-shepherd-service)))
2325 (default-value (gpm-configuration))
2327 "Run GPM, the general-purpose mouse daemon, with the given
2328 command-line options. GPM allows users to use the mouse in the console,
2329 notably to select, copy, and paste text. The default options use the
2330 @code{ps2} protocol, which works for both USB and PS/2 mice.")))
2333 (define-record-type* <kmscon-configuration>
2334 kmscon-configuration make-kmscon-configuration
2335 kmscon-configuration?
2336 (kmscon kmscon-configuration-kmscon
2338 (virtual-terminal kmscon-configuration-virtual-terminal)
2339 (login-program kmscon-configuration-login-program
2340 (default (file-append shadow "/bin/login")))
2341 (login-arguments kmscon-configuration-login-arguments
2343 (auto-login kmscon-configuration-auto-login
2345 (hardware-acceleration? kmscon-configuration-hardware-acceleration?
2346 (default #f)) ; #t causes failure
2347 (font-engine kmscon-configuration-font-engine
2349 (font-size kmscon-configuration-font-size
2351 (keyboard-layout kmscon-configuration-keyboard-layout
2352 (default #f))) ; #f | <keyboard-layout>
2354 (define kmscon-service-type
2355 (shepherd-service-type
2358 (let ((kmscon (kmscon-configuration-kmscon config))
2359 (virtual-terminal (kmscon-configuration-virtual-terminal config))
2360 (login-program (kmscon-configuration-login-program config))
2361 (login-arguments (kmscon-configuration-login-arguments config))
2362 (auto-login (kmscon-configuration-auto-login config))
2363 (hardware-acceleration? (kmscon-configuration-hardware-acceleration? config))
2364 (font-engine (kmscon-configuration-font-engine config))
2365 (font-size (kmscon-configuration-font-size config))
2366 (keyboard-layout (kmscon-configuration-keyboard-layout config)))
2368 (define kmscon-command
2370 #$(file-append kmscon "/bin/kmscon") "--login"
2371 "--vt" #$virtual-terminal
2372 "--no-switchvt" ;Prevent a switch to the virtual terminal.
2373 "--font-engine" #$font-engine
2374 "--font-size" #$(number->string font-size)
2375 #$@(if keyboard-layout
2376 (let* ((layout (keyboard-layout-name keyboard-layout))
2377 (variant (keyboard-layout-variant keyboard-layout))
2378 (model (keyboard-layout-model keyboard-layout))
2379 (options (keyboard-layout-options keyboard-layout)))
2380 `("--xkb-layout" ,layout
2381 ,@(if variant `("--xkb-variant" ,variant) '())
2382 ,@(if model `("--xkb-model" ,model) '())
2383 ,@(if (null? options)
2385 `("--xkb-options" ,(string-join options ",")))))
2387 #$@(if hardware-acceleration? '("--hwaccel") '())
2389 #$login-program #$@login-arguments
2395 (documentation "kmscon virtual terminal")
2396 (requirement '(user-processes udev dbus-system))
2397 (provision (list (symbol-append 'term- (string->symbol virtual-terminal))))
2398 (start #~(make-forkexec-constructor #$kmscon-command))
2399 (stop #~(make-kill-destructor)))))
2400 (description "Start the @command{kmscon} virtual terminal emulator for the
2401 Linux @dfn{kernel mode setting} (KMS).")))
2405 ;;; Static networking.
2408 (define (ipv6-address? str)
2409 "Return true if STR denotes an IPv6 address."
2410 (false-if-exception (->bool (inet-pton AF_INET6 str))))
2412 (define-compile-time-procedure (assert-valid-address (address string?))
2413 "Ensure ADDRESS has a valid netmask."
2414 (unless (cidr->netmask address)
2416 (make-compound-condition
2417 (formatted-message (G_ "address '~a' lacks a network mask")
2419 (condition (&error-location
2421 (source-properties->location procedure-call-location))))
2422 (condition (&fix-hint
2423 (hint (format #f (G_ "\
2424 Write, say, @samp{\"~a/24\"} for a 24-bit network mask.")
2428 (define-record-type* <static-networking>
2429 static-networking make-static-networking
2431 (addresses static-networking-addresses) ;list of <network-address>
2432 (links static-networking-links (default '())) ;list of <network-link>
2433 (routes static-networking-routes (default '())) ;list of <network-routes>
2434 (provision static-networking-provision
2435 (default '(networking)))
2436 (requirement static-networking-requirement
2438 (name-servers static-networking-name-servers ;FIXME: doesn't belong here
2441 (define-record-type* <network-address>
2442 network-address make-network-address
2444 (device network-address-device) ;string--e.g., "en01"
2445 (value network-address-value ;string--CIDR notation
2446 (sanitize assert-valid-address))
2447 (ipv6? network-address-ipv6? ;Boolean
2450 (ipv6-address? (cidr->ip (network-address-value this-record))))))
2452 (define-record-type* <network-link>
2453 network-link make-network-link
2455 (name network-link-name) ;string--e.g, "v0p0"
2456 (type network-link-type) ;symbol--e.g.,'veth
2457 (arguments network-link-arguments)) ;list
2459 (define-record-type* <network-route>
2460 network-route make-network-route
2462 (destination network-route-destination)
2463 (source network-route-source (default #f))
2464 (device network-route-device (default #f))
2465 (ipv6? network-route-ipv6? (thunked)
2467 (or (ipv6-address? (network-route-destination this-record))
2468 (and=> (network-route-gateway this-record)
2470 (gateway network-route-gateway (default #f)))
2472 (define* (cidr->netmask str #:optional (family AF_INET))
2473 "Given @var{str}, a string in CIDR notation (e.g., \"1.2.3.4/24\"), return
2474 the netmask as a string like \"255.255.255.0\"."
2475 (match (string-split str #\/)
2476 ((ip (= string->number bits))
2477 (let ((mask (ash (- (expt 2 bits) 1)
2478 (- (if (= family AF_INET6) 128 32)
2480 (inet-ntop family mask)))
2483 (define (cidr->ip str)
2484 "Strip the netmask bit of @var{str}, a CIDR-notation IP/netmask address."
2485 (match (string-split str #\/)
2489 (define* (ip+netmask->cidr ip netmask #:optional (family AF_INET))
2490 "Return the CIDR notation (a string) for @var{ip} and @var{netmask}, two
2491 @var{family} address strings, where @var{family} is @code{AF_INET} or
2493 (let* ((netmask (inet-pton family netmask))
2494 (bits (logcount netmask)))
2495 (string-append ip "/" (number->string bits))))
2497 (define (static-networking->hurd-pfinet-options config)
2498 "Return command-line options for the Hurd's pfinet translator corresponding
2500 (unless (null? (static-networking-links config))
2501 ;; XXX: Presumably this is not supported, or perhaps could be approximated
2502 ;; by running separate pfinet instances in some cases?
2503 (warning (G_ "network links are currently ignored on GNU/Hurd~%")))
2505 (match (static-networking-addresses config)
2506 ((and addresses (first _ ...))
2507 `("--ipv6" "/servers/socket/26"
2508 "--interface" ,(network-address-device first)
2509 ,@(append-map (lambda (address)
2510 `(,(if (network-address-ipv6? address)
2513 ,(cidr->ip (network-address-value address))
2514 ,@(match (cidr->netmask (network-address-value address)
2515 (if (network-address-ipv6? address)
2519 (mask (list "--netmask" mask)))))
2521 ,@(append-map (lambda (route)
2523 (($ <network-route> "default" #f device _ gateway)
2524 (if (network-route-ipv6? route)
2525 `("--gateway6" ,gateway)
2526 `("--gateway" ,gateway)))
2527 (($ <network-route> destination)
2528 (warning (G_ "ignoring network route for '~a'~%")
2531 (static-networking-routes config))))))
2533 (define (network-set-up/hurd config)
2534 "Set up networking for the Hurd."
2535 ;; The Hurd implements SIOCGIFADDR and other old-style ioctls, but the only
2536 ;; way to set up IPv6 is by starting pfinet with the right options.
2537 (if (equal? (static-networking-provision config) '(loopback))
2538 (scheme-file "set-up-pflocal" #~(begin 'nothing-to-do! #t))
2539 (scheme-file "set-up-pfinet"
2540 (with-imported-modules '((guix build utils))
2542 (use-modules (guix build utils)
2545 ;; TODO: Do that without forking.
2546 (let ((options '#$(static-networking->hurd-pfinet-options
2548 (format #t "starting '~a~{ ~s~}'~%"
2549 #$(file-append hurd "/hurd/pfinet")
2551 (apply invoke #$(file-append hurd "/bin/settrans") "-fac"
2553 #$(file-append hurd "/hurd/pfinet")
2556 (define (network-tear-down/hurd config)
2557 (scheme-file "tear-down-pfinet"
2558 (with-imported-modules '((guix build utils))
2560 (use-modules (guix build utils))
2562 ;; Forcefully terminate pfinet. XXX: In theory this
2563 ;; should just undo the addresses and routes of CONFIG;
2564 ;; this could be done using ioctls like SIOCDELRT, but
2565 ;; these are IPv4-only; another option would be to use
2566 ;; fsysopts but that seems to crash pfinet.
2567 (invoke #$(file-append hurd "/bin/settrans") "-fg"
2568 "/servers/socket/2")
2571 (define network-set-up/linux
2573 (($ <static-networking> addresses links routes)
2574 (scheme-file "set-up-network"
2575 (with-extensions (list guile-netlink)
2577 (use-modules (ip addr) (ip link) (ip route))
2579 #$@(map (lambda (address)
2581 (addr-add #$(network-address-device address)
2582 #$(network-address-value address)
2584 #$(network-address-ipv6? address))
2586 (link-set #$(network-address-device address)
2590 #$@(map (match-lambda
2591 (($ <network-link> name type arguments)
2592 #~(link-add #$name #$type
2593 #:type-args '#$arguments)))
2595 #$@(map (lambda (route)
2596 #~(route-add #$(network-route-destination route)
2598 #$(network-route-device route)
2600 #$(network-route-ipv6? route)
2602 #$(network-route-gateway route)
2604 #$(network-route-source route)))
2608 (define network-tear-down/linux
2610 (($ <static-networking> addresses links routes)
2611 (scheme-file "tear-down-network"
2612 (with-extensions (list guile-netlink)
2614 (use-modules (ip addr) (ip link) (ip route)
2618 (define-syntax-rule (false-if-netlink-error exp)
2619 (guard (c ((netlink-error? c) #f))
2622 ;; Wrap calls in 'false-if-netlink-error' so this
2623 ;; script goes as far as possible undoing the effects
2624 ;; of "set-up-network".
2626 #$@(map (lambda (route)
2627 #~(false-if-netlink-error
2628 (route-del #$(network-route-destination route)
2630 #$(network-route-device route)
2632 #$(network-route-ipv6? route)
2634 #$(network-route-gateway route)
2636 #$(network-route-source route))))
2638 #$@(map (match-lambda
2639 (($ <network-link> name type arguments)
2640 #~(false-if-netlink-error
2641 (link-del #$name))))
2643 #$@(map (lambda (address)
2644 #~(false-if-netlink-error
2645 (addr-del #$(network-address-device
2647 #$(network-address-value address)
2649 #$(network-address-ipv6? address))))
2653 (define (static-networking-shepherd-service config)
2655 (($ <static-networking> addresses links routes
2656 provision requirement name-servers)
2657 (let ((loopback? (and provision (memq 'loopback provision))))
2661 "Bring up the networking interface using a static IP address.")
2662 (requirement requirement)
2663 (provision provision)
2666 ;; Return #t if successfully started.
2667 (load #$(let-system (system target)
2668 (if (string-contains (or target system) "-linux")
2669 (network-set-up/linux config)
2670 (network-set-up/hurd config))))))
2672 ;; Return #f is successfully stopped.
2673 (load #$(let-system (system target)
2674 (if (string-contains (or target system) "-linux")
2675 (network-tear-down/linux config)
2676 (network-tear-down/hurd config))))))
2679 (define (static-networking-shepherd-services networks)
2680 (map static-networking-shepherd-service networks))
2682 (define (static-networking-etc-files interfaces)
2683 "Return a /etc/resolv.conf entry for INTERFACES or the empty list."
2684 (match (delete-duplicates
2685 (append-map static-networking-name-servers
2690 (let ((content (string-join
2691 (map (cut string-append "nameserver " <>)
2695 ,(plain-file "resolv.conf"
2697 # Generated by 'static-networking-service'.\n"
2700 (define static-networking-service-type
2701 ;; The service type for statically-defined network interfaces.
2702 (service-type (name 'static-networking)
2705 (service-extension shepherd-root-service-type
2706 static-networking-shepherd-services)
2707 (service-extension etc-service-type
2708 static-networking-etc-files)))
2709 (compose concatenate)
2712 "Turn up the specified network interfaces upon startup,
2713 with the given IP address, gateway, netmask, and so on. The value for
2714 services of this type is a list of @code{static-networking} objects, one per
2715 network interface.")))
2717 (define-deprecated (static-networking-service interface ip
2719 netmask gateway provision
2720 ;; Most interfaces require udev to be usable.
2721 (requirement '(udev))
2723 static-networking-service-type
2724 "Return a service that starts @var{interface} with address @var{ip}. If
2725 @var{netmask} is true, use it as the network mask. If @var{gateway} is true,
2726 it must be a string specifying the default network gateway.
2728 This procedure can be called several times, one for each network
2729 interface of interest. Behind the scenes what it does is extend
2730 @code{static-networking-service-type} with additional network interfaces
2732 (simple-service 'static-network-interface
2733 static-networking-service-type
2734 (list (static-networking
2736 (list (network-address
2739 (ip+netmask->cidr ip netmask)
2744 (list (network-route
2745 (destination "default")
2749 (requirement requirement)
2750 (provision (or provision '(networking)))
2751 (name-servers name-servers)))))
2753 (define %loopback-static-networking
2754 ;; The loopback device.
2756 (addresses (list (network-address
2758 (value "127.0.0.1/8"))))
2760 (provision '(loopback))))
2762 (define %qemu-static-networking
2763 ;; Networking configuration for QEMU's user-mode network stack (info "(QEMU)
2764 ;; Using the user mode network stack").
2766 (addresses (list (network-address
2768 (value "10.0.2.15/24"))))
2769 (routes (list (network-route
2770 (destination "default")
2771 (gateway "10.0.2.2"))))
2773 (provision '(networking))
2774 (name-servers '("10.0.2.3"))))
2777 (define %base-services
2778 ;; Convenience variable holding the basic services.
2779 (list (service login-service-type)
2781 (service virtual-terminal-service-type)
2782 (service console-font-service-type
2784 (cons tty %default-console-font))
2785 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
2788 (service agetty-service-type (agetty-configuration
2789 (extra-options '("-L")) ; no carrier detect
2791 (tty #f) ; automatic
2792 (shepherd-requirement '(syslogd))))
2794 (service mingetty-service-type (mingetty-configuration
2796 (service mingetty-service-type (mingetty-configuration
2798 (service mingetty-service-type (mingetty-configuration
2800 (service mingetty-service-type (mingetty-configuration
2802 (service mingetty-service-type (mingetty-configuration
2804 (service mingetty-service-type (mingetty-configuration
2807 (service static-networking-service-type
2808 (list %loopback-static-networking))
2809 (service urandom-seed-service-type)
2810 (service guix-service-type)
2811 (service nscd-service-type)
2813 (service rottlog-service-type)
2815 ;; Periodically delete old build logs.
2816 (service log-cleanup-service-type
2817 (log-cleanup-configuration
2818 (directory "/var/log/guix/drvs")))
2820 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
2821 ;; used, so enable them by default. The FUSE and ALSA rules are
2822 ;; less critical, but handy.
2823 (service udev-service-type
2825 (rules (list lvm2 fuse alsa-utils crda))))
2827 (service sysctl-service-type)
2829 (service special-files-service-type
2830 `(("/bin/sh" ,(file-append bash "/bin/sh"))
2831 ("/usr/bin/env" ,(file-append coreutils "/bin/env"))))))
2833 ;;; base.scm ends here