1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 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 Maxim Cournoyer <maxim.cournoyer@gmail.com>
20 ;;; This file is part of GNU Guix.
22 ;;; GNU Guix is free software; you can redistribute it and/or modify it
23 ;;; under the terms of the GNU General Public License as published by
24 ;;; the Free Software Foundation; either version 3 of the License, or (at
25 ;;; your option) any later version.
27 ;;; GNU Guix is distributed in the hope that it will be useful, but
28 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
29 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 ;;; GNU General Public License for more details.
32 ;;; You should have received a copy of the GNU General Public License
33 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
35 (define-module (gnu services base)
36 #:use-module (guix store)
37 #:use-module (guix deprecation)
38 #:autoload (guix diagnostics) (warning &fix-hint)
39 #:autoload (guix i18n) (G_)
40 #:use-module (guix combinators)
41 #:use-module (gnu services)
42 #:use-module (gnu services admin)
43 #:use-module (gnu services shepherd)
44 #:use-module (gnu services sysctl)
45 #:use-module (gnu system pam)
46 #:use-module (gnu system shadow) ; 'user-account', etc.
47 #:use-module (gnu system uuid)
48 #:use-module (gnu system file-systems) ; 'file-system', etc.
49 #:use-module (gnu system keyboard)
50 #:use-module (gnu system mapped-devices)
51 #:use-module ((gnu system linux-initrd)
52 #:select (file-system-packages))
53 #:use-module (gnu packages admin)
54 #:use-module ((gnu packages linux)
55 #:select (alsa-utils crda eudev e2fsprogs fuse gpm kbd lvm2 rng-tools))
56 #:use-module (gnu packages bash)
57 #:use-module ((gnu packages base)
58 #:select (coreutils glibc glibc-utf8-locales))
59 #:autoload (gnu packages guile-xyz) (guile-netlink)
60 #:autoload (gnu packages hurd) (hurd)
61 #:use-module (gnu packages package-management)
62 #:use-module ((gnu packages gnupg) #:select (guile-gcrypt))
63 #:use-module (gnu packages linux)
64 #:use-module (gnu packages terminals)
65 #:use-module ((gnu build file-systems)
66 #:select (mount-flags->bit-mask
67 swap-space->flags-bit-mask))
68 #:use-module (guix gexp)
69 #:use-module (guix records)
70 #:use-module (guix modules)
71 #:use-module ((guix self) #:select (make-config.scm))
72 #:use-module (guix diagnostics)
73 #:use-module (guix i18n)
74 #:use-module (srfi srfi-1)
75 #:use-module (srfi srfi-26)
76 #:use-module (srfi srfi-34)
77 #:use-module (srfi srfi-35)
78 #:use-module (ice-9 match)
79 #:use-module (ice-9 format)
80 #:re-export (user-processes-service-type ;backwards compatibility
81 %default-substitute-urls)
82 #:export (fstab-service-type
83 root-file-system-service
84 file-system-service-type
88 console-font-service-type
90 virtual-terminal-service-type
94 static-networking-addresses
95 static-networking-links
96 static-networking-routes
97 static-networking-requirement
101 network-address-device
102 network-address-value
103 network-address-ipv6?
109 network-link-arguments
113 network-route-destination
117 network-route-gateway
119 static-networking-service
120 static-networking-service-type
122 %loopback-static-networking
123 %qemu-static-networking
127 udev-configuration-rules
140 agetty-configuration?
144 mingetty-configuration
145 mingetty-configuration-tty
146 mingetty-configuration-auto-login
147 mingetty-configuration-login-program
148 mingetty-configuration-login-pause?
149 mingetty-configuration-clear-on-logout?
150 mingetty-configuration-mingetty
151 mingetty-configuration?
153 mingetty-service-type
156 %nscd-default-configuration
168 syslog-configuration?
173 %default-authorized-guix-keys
177 guix-configuration-guix
178 guix-configuration-build-group
179 guix-configuration-build-accounts
180 guix-configuration-authorize-key?
181 guix-configuration-authorized-keys
182 guix-configuration-use-substitutes?
183 guix-configuration-substitute-urls
184 guix-configuration-extra-options
185 guix-configuration-log-file
188 guix-publish-configuration
189 guix-publish-configuration?
190 guix-publish-configuration-guix
191 guix-publish-configuration-port
192 guix-publish-configuration-host
193 guix-publish-configuration-compression
194 guix-publish-configuration-compression-level ;deprecated
195 guix-publish-configuration-nar-path
196 guix-publish-configuration-cache
197 guix-publish-configuration-ttl
198 guix-publish-service-type
204 urandom-seed-service-type
212 kmscon-configuration?
215 pam-limits-service-type
224 ;;; Base system services---i.e., services that 99% of the users will want to
235 (define (file-system->fstab-entry file-system)
236 "Return a @file{/etc/fstab} entry for @var{file-system}."
237 (string-append (match (file-system-device file-system)
238 ((? file-system-label? label)
239 (string-append "LABEL="
240 (file-system-label->string label)))
242 (string-append "UUID=" (uuid->string uuid)))
246 (file-system-mount-point file-system) "\t"
247 (file-system-type file-system) "\t"
248 (or (file-system-options file-system) "defaults") "\t"
250 ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
251 ;; don't have anything sensible to put in there.
254 (define (file-systems->fstab file-systems)
255 "Return a @file{/etc} entry for an @file{fstab} describing
257 `(("fstab" ,(plain-file "fstab"
260 # This file was generated from your Guix configuration. Any changes
261 # will be lost upon reboot or reconfiguration.\n\n"
262 (string-join (map file-system->fstab-entry
267 (define fstab-service-type
268 ;; The /etc/fstab service.
269 (service-type (name 'fstab)
271 (list (service-extension etc-service-type
272 file-systems->fstab)))
273 (compose concatenate)
276 "Populate the @file{/etc/fstab} based on the given file
279 (define %root-file-system-shepherd-service
281 (documentation "Take care of the root file system.")
282 (provision '(root-file-system))
285 ;; Return #f if successfully stopped.
288 (call-with-blocked-asyncs
290 (let ((null (%make-void-port "w")))
291 ;; Close 'shepherd.log'.
292 (display "closing log\n")
293 ((@ (shepherd comm) stop-logging))
295 ;; Redirect the default output ports..
296 (set-current-output-port null)
297 (set-current-error-port null)
299 ;; Close /dev/console.
300 (for-each close-fdes '(0 1 2))
302 ;; At this point, there are no open files left, so the
303 ;; root file system can be re-mounted read-only.
305 (logior MS_REMOUNT MS_RDONLY)
311 (define root-file-system-service-type
312 (shepherd-service-type 'root-file-system
313 (const %root-file-system-shepherd-service)
314 (description "Take care of syncing the root file
315 system and of remounting it read-only when the system shuts down.")))
317 (define (root-file-system-service)
318 "Return a service whose sole purpose is to re-mount read-only the root file
319 system upon shutdown (aka. cleanly \"umounting\" root.)
321 This service must be the root of the service dependency graph so that its
322 'stop' action is invoked when shepherd is the only process left."
323 (service root-file-system-service-type #f))
325 (define (file-system->shepherd-service-name file-system)
326 "Return the symbol that denotes the service mounting and unmounting
328 (symbol-append 'file-system-
329 (string->symbol (file-system-mount-point file-system))))
331 (define (mapped-device->shepherd-service-name md)
332 "Return the symbol that denotes the shepherd service of MD, a <mapped-device>."
333 (symbol-append 'device-mapping-
334 (string->symbol (string-join
335 (mapped-device-targets md) "-"))))
337 (define dependency->shepherd-service-name
339 ((? mapped-device? md)
340 (mapped-device->shepherd-service-name md))
342 (file-system->shepherd-service-name fs))))
344 (define (file-system-shepherd-service file-system)
345 "Return the shepherd service for @var{file-system}, or @code{#f} if
346 @var{file-system} is not auto-mounted or doesn't have its mount point created
348 (let ((target (file-system-mount-point file-system))
349 (create? (file-system-create-mount-point? file-system))
350 (mount? (file-system-mount? file-system))
351 (dependencies (file-system-dependencies file-system))
352 (packages (file-system-packages (list file-system))))
353 (and (or mount? create?)
354 (with-imported-modules (source-module-closure
355 '((gnu build file-systems)))
357 (provision (list (file-system->shepherd-service-name file-system)))
358 (requirement `(root-file-system
360 ,@(map dependency->shepherd-service-name dependencies)))
361 (documentation "Check, mount, and unmount the given file system.")
362 (start #~(lambda args
368 #~(let (($PATH (getenv "PATH")))
369 ;; Make sure fsck.ext2 & co. can be found.
372 ;; Don’t display the PATH settings.
373 (with-output-to-port (%make-void-port "w")
375 (set-path-environment-variable "PATH"
381 '#$(file-system->spec file-system))
384 (setenv "PATH" $PATH))))
388 ;; Normally there are no processes left at this point, so
389 ;; TARGET can be safely unmounted.
391 ;; Make sure PID 1 doesn't keep TARGET busy.
397 ;; We need additional modules.
398 (modules `(((gnu build file-systems)
399 #:select (mount-file-system))
400 (gnu system file-systems)
401 ,@%default-modules)))))))
403 (define (file-system-shepherd-services file-systems)
404 "Return the list of Shepherd services for FILE-SYSTEMS."
405 (let* ((file-systems (filter (lambda (x)
406 (or (file-system-mount? x)
407 (file-system-create-mount-point? x)))
411 (provision '(file-systems))
412 (requirement (cons* 'root-file-system 'user-file-systems
413 (map file-system->shepherd-service-name
415 (documentation "Target for all the initially-mounted file systems")
417 (stop #~(const #f))))
419 (define known-mount-points
420 (map file-system-mount-point file-systems))
424 (documentation "Unmount manually-mounted file systems.")
425 (provision '(user-file-systems))
428 (define (known? mount-point)
430 (cons* "/proc" "/sys" '#$known-mount-points)))
432 ;; Make sure we don't keep the user's mount points busy.
435 (for-each (lambda (mount-point)
436 (format #t "unmounting '~a'...~%" mount-point)
439 (umount mount-point))
441 (let ((errno (system-error-errno args)))
442 (format #t "failed to unmount '~a': ~a~%"
443 mount-point (strerror errno))))))
444 (filter (negate known?) (mount-points)))
447 (cons* sink user-unmount
448 (map file-system-shepherd-service file-systems))))
450 (define (file-system-fstab-entries file-systems)
451 "Return the subset of @var{file-systems} that should have an entry in
453 ;; /etc/fstab is about telling fsck(8), mount(8), and umount(8) about
454 ;; relevant file systems they'll have to deal with. That excludes "pseudo"
457 ;; In particular, things like GIO (part of GLib) use it to determine the set
458 ;; of mounts, which is then used by graphical file managers and desktop
459 ;; environments to display "volume" icons. Thus, we really need to exclude
460 ;; those pseudo file systems from the list.
461 (remove (lambda (file-system)
462 (or (member (file-system-type file-system)
463 %pseudo-file-system-types)
464 (memq 'bind-mount (file-system-flags file-system))))
467 (define file-system-service-type
468 (service-type (name 'file-systems)
470 (list (service-extension shepherd-root-service-type
471 file-system-shepherd-services)
472 (service-extension fstab-service-type
473 file-system-fstab-entries)
475 ;; Have 'user-processes' depend on 'file-systems'.
476 (service-extension user-processes-service-type
477 (const '(file-systems)))))
478 (compose concatenate)
481 "Provide Shepherd services to mount and unmount the given
482 file systems, as well as corresponding @file{/etc/fstab} entries.")))
487 ;;; Preserve entropy to seed /dev/urandom on boot.
490 (define %random-seed-file
491 "/var/lib/random-seed")
493 (define (urandom-seed-shepherd-service _)
494 "Return a shepherd service for the /dev/urandom seed."
495 (list (shepherd-service
496 (documentation "Preserve entropy across reboots for /dev/urandom.")
497 (provision '(urandom-seed))
499 ;; Depend on udev so that /dev/hwrng is available.
500 (requirement '(file-systems udev))
503 ;; On boot, write random seed into /dev/urandom.
504 (when (file-exists? #$%random-seed-file)
505 (call-with-input-file #$%random-seed-file
507 (call-with-output-file "/dev/urandom"
509 (dump-port seed urandom)
511 ;; Writing SEED to URANDOM isn't enough: we must
512 ;; also tell the kernel to account for these
513 ;; extra bits of entropy.
514 (let ((bits (* 8 (stat:size (stat seed)))))
515 (add-to-entropy-count urandom bits)))))))
517 ;; Try writing from /dev/hwrng into /dev/urandom.
518 ;; It seems that the file /dev/hwrng always exists, even
519 ;; when there is no hardware random number generator
520 ;; available. So, we handle a failed read or any other error
521 ;; reported by the operating system.
522 (let ((buf (catch 'system-error
524 (call-with-input-file "/dev/hwrng"
526 (get-bytevector-n hwrng 512))))
527 ;; Silence is golden...
530 (call-with-output-file "/dev/urandom"
532 (put-bytevector urandom buf)
533 (let ((bits (* 8 (bytevector-length buf))))
534 (add-to-entropy-count urandom bits))))))
536 ;; Immediately refresh the seed in case the system doesn't
537 ;; shut down cleanly.
538 (call-with-input-file "/dev/urandom"
540 (let ((previous-umask (umask #o077))
541 (buf (make-bytevector 512)))
542 (mkdir-p (dirname #$%random-seed-file))
543 (get-bytevector-n! urandom buf 0 512)
544 (call-with-output-file #$%random-seed-file
546 (put-bytevector seed buf)))
547 (umask previous-umask))))
550 ;; During shutdown, write from /dev/urandom into random seed.
551 (let ((buf (make-bytevector 512)))
552 (call-with-input-file "/dev/urandom"
554 (let ((previous-umask (umask #o077)))
555 (get-bytevector-n! urandom buf 0 512)
556 (mkdir-p (dirname #$%random-seed-file))
557 (call-with-output-file #$%random-seed-file
559 (put-bytevector seed buf)))
560 (umask previous-umask))
562 (modules `((rnrs bytevectors)
564 ,@%default-modules)))))
566 (define urandom-seed-service-type
567 (service-type (name 'urandom-seed)
569 (list (service-extension shepherd-root-service-type
570 urandom-seed-shepherd-service)
572 ;; Have 'user-processes' depend on 'urandom-seed'.
573 ;; This ensures that user processes and daemons don't
574 ;; start until we have seeded the PRNG.
575 (service-extension user-processes-service-type
576 (const '(urandom-seed)))))
579 "Seed the @file{/dev/urandom} pseudo-random number
580 generator (RNG) with the value recorded when the system was last shut
585 ;;; Add hardware random number generator to entropy pool.
588 (define-record-type* <rngd-configuration>
589 rngd-configuration make-rngd-configuration
591 (rng-tools rngd-configuration-rng-tools) ;file-like
592 (device rngd-configuration-device)) ;string
594 (define rngd-service-type
595 (shepherd-service-type
598 (define rng-tools (rngd-configuration-rng-tools config))
599 (define device (rngd-configuration-device config))
602 (list (file-append rng-tools "/sbin/rngd")
606 (documentation "Add TRNG to entropy pool.")
607 (requirement '(udev))
609 (start #~(make-forkexec-constructor '#$rngd-command))
610 (stop #~(make-kill-destructor))))
611 (description "Run the @command{rngd} random number generation daemon to
612 supply entropy to the kernel's pool.")))
614 (define* (rngd-service #:key
615 (rng-tools rng-tools)
616 (device "/dev/hwrng"))
617 "Return a service that runs the @command{rngd} program from @var{rng-tools}
618 to add @var{device} to the kernel's entropy pool. The service will fail if
619 @var{device} does not exist."
620 (service rngd-service-type
622 (rng-tools rng-tools)
630 (define host-name-service-type
631 (shepherd-service-type
635 (documentation "Initialize the machine's host name.")
636 (provision '(host-name))
638 (sethostname #$name)))
640 (description "Initialize the machine's host name.")))
642 (define (host-name-service name)
643 "Return a service that sets the host name to @var{name}."
644 (service host-name-service-type name))
646 (define virtual-terminal-service-type
647 ;; Ensure that virtual terminals run in UTF-8 mode. This is the case by
648 ;; default with recent Linux kernels, but this service allows us to ensure
649 ;; this. This service must start before any 'term-' service so that newly
650 ;; created terminals inherit this property. See
651 ;; <https://bugs.gnu.org/30505> for a discussion.
652 (shepherd-service-type
655 (let ((knob "/sys/module/vt/parameters/default_utf8"))
657 (documentation "Set virtual terminals in UTF-8 module.")
658 (provision '(virtual-terminal))
659 (requirement '(root-file-system))
661 ;; In containers /sys is read-only so don't insist on
662 ;; writing to this file.
663 (unless (= 1 (call-with-input-file #$knob read))
664 (call-with-output-file #$knob
668 (stop #~(const #f)))))
670 (description "Ensure the Linux virtual terminals run in UTF-8 mode.")))
672 (define console-keymap-service-type
673 (shepherd-service-type
677 (documentation (string-append "Load console keymap (loadkeys)."))
678 (provision '(console-keymap))
680 (zero? (system* #$(file-append kbd "/bin/loadkeys")
683 (description "@emph{This service is deprecated in favor of the
684 @code{keyboard-layout} field of @code{operating-system}.} Load the given list
685 of console keymaps with @command{loadkeys}.")))
687 (define %default-console-font
688 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
689 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
690 ;; codepoints notably found in the UTF-8 manual.
693 (define (console-font-shepherd-services tty+font)
694 "Return a list of Shepherd services for each pair in TTY+FONT."
697 (let ((device (string-append "/dev/" tty)))
699 (documentation "Load a Unicode console font.")
700 (provision (list (symbol-append 'console-font-
701 (string->symbol tty))))
703 ;; Start after mingetty has been started on TTY, otherwise the settings
705 (requirement (list (symbol-append 'term-
706 (string->symbol tty))))
709 ;; It could be that mingetty is not fully ready yet,
710 ;; which we check by calling 'ttyname'.
712 (unless (or (zero? i)
713 (call-with-input-file #$device
715 (false-if-exception (ttyname port)))))
719 ;; Assume the VT is already in UTF-8 mode, thanks to
720 ;; the 'virtual-terminal' service.
722 ;; 'setfont' returns EX_OSERR (71) when an
723 ;; KDFONTOP ioctl fails, for example. Like
724 ;; systemd's vconsole support, let's not treat
726 (case (status:exit-val
727 (system* #$(file-append kbd "/bin/setfont")
728 "-C" #$device #$font))
735 (define console-font-service-type
736 (service-type (name 'console-fonts)
738 (list (service-extension shepherd-root-service-type
739 console-font-shepherd-services)))
740 (compose concatenate)
743 "Install the given fonts on the specified ttys (fonts are per
744 virtual console on GNU/Linux). The value of this service is a list of
745 tty/font pairs. The font can be the name of a font provided by the @code{kbd}
746 package or any valid argument to @command{setfont}, as in this example:
749 `((\"tty1\" . \"LatGrkCyr-8x16\")
750 (\"tty2\" . ,(file-append
752 \"/share/kbd/consolefonts/TamzenForPowerline10x20.psf\"))
753 (\"tty3\" . ,(file-append
755 \"/share/consolefonts/ter-132n\"))) ; for HDPI
758 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
759 "This procedure is deprecated in favor of @code{console-font-service-type}.
761 Return a service that sets up Unicode support in @var{tty} and loads
762 @var{font} for that tty (fonts are per virtual console in Linux.)"
763 (simple-service (symbol-append 'console-font- (string->symbol tty))
764 console-font-service-type `((,tty . ,font))))
766 (define %default-motd
767 (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
769 (define-record-type* <login-configuration>
770 login-configuration make-login-configuration
772 (motd login-configuration-motd ;file-like
773 (default %default-motd))
774 ;; Allow empty passwords by default so that first-time users can log in when
775 ;; the 'root' account has just been created.
776 (allow-empty-passwords? login-configuration-allow-empty-passwords?
777 (default #t))) ;Boolean
779 (define (login-pam-service config)
780 "Return the list of PAM service needed for CONF."
781 ;; Let 'login' be known to PAM.
782 (list (unix-pam-service "login"
784 #:allow-empty-passwords?
785 (login-configuration-allow-empty-passwords? config)
787 (login-configuration-motd config))))
789 (define login-service-type
790 (service-type (name 'login)
791 (extensions (list (service-extension pam-root-service-type
793 (default-value (login-configuration))
795 "Provide a console log-in service as specified by its
796 configuration value, a @code{login-configuration} object.")))
798 (define* (login-service #:optional (config (login-configuration)))
799 "Return a service configure login according to @var{config}, which specifies
800 the message of the day, among other things."
801 (service login-service-type config))
803 (define-record-type* <agetty-configuration>
804 agetty-configuration make-agetty-configuration
805 agetty-configuration?
806 (agetty agetty-configuration-agetty ;file-like
807 (default util-linux))
808 (tty agetty-configuration-tty) ;string | #f
809 (term agetty-term ;string | #f
811 (baud-rate agetty-baud-rate ;string | #f
813 (auto-login agetty-auto-login ;list of strings | #f
815 (login-program agetty-login-program ;gexp
816 (default (file-append shadow "/bin/login")))
817 (login-pause? agetty-login-pause? ;Boolean
819 (eight-bits? agetty-eight-bits? ;Boolean
821 (no-reset? agetty-no-reset? ;Boolean
823 (remote? agetty-remote? ;Boolean
825 (flow-control? agetty-flow-control? ;Boolean
827 (host agetty-host ;string | #f
829 (no-issue? agetty-no-issue? ;Boolean
831 (init-string agetty-init-string ;string | #f
833 (no-clear? agetty-no-clear? ;Boolean
835 (local-line agetty-local-line ;always | never | auto
837 (extract-baud? agetty-extract-baud? ;Boolean
839 (skip-login? agetty-skip-login? ;Boolean
841 (no-newline? agetty-no-newline? ;Boolean
843 (login-options agetty-login-options ;string | #f
845 (chroot agetty-chroot ;string | #f
847 (hangup? agetty-hangup? ;Boolean
849 (keep-baud? agetty-keep-baud? ;Boolean
851 (timeout agetty-timeout ;integer | #f
853 (detect-case? agetty-detect-case? ;Boolean
855 (wait-cr? agetty-wait-cr? ;Boolean
857 (no-hints? agetty-no-hints? ;Boolean
859 (no-hostname? agetty-no hostname? ;Boolean
861 (long-hostname? agetty-long-hostname? ;Boolean
863 (erase-characters agetty-erase-characters ;string | #f
865 (kill-characters agetty-kill-characters ;string | #f
867 (chdir agetty-chdir ;string | #f
869 (delay agetty-delay ;integer | #f
871 (nice agetty-nice ;integer | #f
873 ;; "Escape hatch" for passing arbitrary command-line arguments.
874 (extra-options agetty-extra-options ;list of strings
876 ;;; XXX Unimplemented for now!
877 ;;; (issue-file agetty-issue-file ;file-like
881 (define (default-serial-port)
882 "Return a gexp that determines a reasonable default serial port
883 to use as the tty. This is primarily useful for headless systems."
884 (with-imported-modules (source-module-closure
885 '((gnu build linux-boot))) ;for 'find-long-options'
887 ;; console=device,options
888 ;; device: can be tty0, ttyS0, lp0, ttyUSB0 (serial).
889 ;; options: BBBBPNF. P n|o|e, N number of bits,
890 ;; F flow control (r RTS)
891 (let* ((not-comma (char-set-complement (char-set #\,)))
892 (command (linux-command-line))
893 (agetty-specs (find-long-options "agetty.tty" command))
894 (console-specs (filter (lambda (spec)
895 (and (string-prefix? "tty" spec)
897 (string-prefix? "tty0" spec)
898 (string-prefix? "tty1" spec)
899 (string-prefix? "tty2" spec)
900 (string-prefix? "tty3" spec)
901 (string-prefix? "tty4" spec)
902 (string-prefix? "tty5" spec)
903 (string-prefix? "tty6" spec)
904 (string-prefix? "tty7" spec)
905 (string-prefix? "tty8" spec)
906 (string-prefix? "tty9" spec)))))
907 (find-long-options "console" command)))
908 (specs (append agetty-specs console-specs)))
912 ;; Extract device name from first spec.
913 (match (string-tokenize spec not-comma)
917 (define agetty-shepherd-service
919 (($ <agetty-configuration> agetty tty term baud-rate auto-login
920 login-program login-pause? eight-bits? no-reset? remote? flow-control?
921 host no-issue? init-string no-clear? local-line extract-baud?
922 skip-login? no-newline? login-options chroot hangup? keep-baud? timeout
923 detect-case? wait-cr? no-hints? no-hostname? long-hostname?
924 erase-characters kill-characters chdir delay nice extra-options)
927 (documentation "Run agetty on a tty.")
928 (provision (list (symbol-append 'term- (string->symbol (or tty "auto")))))
930 ;; Since the login prompt shows the host name, wait for the 'host-name'
931 ;; service to be done. Also wait for udev essentially so that the tty
932 ;; text is not lost in the middle of kernel messages (see also
933 ;; mingetty-shepherd-service).
934 (requirement '(user-processes host-name udev))
936 (modules '((ice-9 match) (gnu build linux-boot)))
938 (with-imported-modules (source-module-closure
939 '((gnu build linux-boot)))
941 (let ((defaulted-tty #$(or tty (default-serial-port))))
944 (make-forkexec-constructor
945 (list #$(file-append util-linux "/sbin/agetty")
966 #~("--init-string" #$init-string)
971 ;;; FIXME This doesn't work as expected. According to agetty(8), if this option
972 ;;; is not passed, then the default is 'auto'. However, in my tests, when that
973 ;;; option is selected, agetty never presents the login prompt, and the
974 ;;; term-ttyS0 service respawns every few seconds.
976 #~(#$(match local-line
977 ('auto "--local-line=auto")
978 ('always "--local-line=always")
979 ('never "-local-line=never")))
994 #~("--login-options" #$login-options)
997 #~("--chroot" #$chroot)
1006 #~("--timeout" #$(number->string timeout))
1020 #$@(if long-hostname?
1021 #~("--long-hostname")
1023 #$@(if erase-characters
1024 #~("--erase-chars" #$erase-characters)
1026 #$@(if kill-characters
1027 #~("--kill-chars" #$kill-characters)
1030 #~("--chdir" #$chdir)
1033 #~("--delay" #$(number->string delay))
1036 #~("--nice" #$(number->string nice))
1039 (list "--autologin" auto-login)
1041 #$@(if login-program
1042 #~("--login-program" #$login-program)
1054 (const #f)) ; never start.
1056 (stop #~(make-kill-destructor)))))))
1058 (define agetty-service-type
1059 (service-type (name 'agetty)
1060 (extensions (list (service-extension shepherd-root-service-type
1061 agetty-shepherd-service)))
1063 "Provide console login using the @command{agetty}
1066 (define* (agetty-service config)
1067 "Return a service to run agetty according to @var{config}, which specifies
1068 the tty to run, among other things."
1069 (service agetty-service-type config))
1071 (define-record-type* <mingetty-configuration>
1072 mingetty-configuration make-mingetty-configuration
1073 mingetty-configuration?
1074 (mingetty mingetty-configuration-mingetty ;file-like
1076 (tty mingetty-configuration-tty) ;string
1077 (auto-login mingetty-auto-login ;string | #f
1079 (login-program mingetty-login-program ;gexp
1081 (login-pause? mingetty-login-pause? ;Boolean
1083 (clear-on-logout? mingetty-clear-on-logout? ;Boolean
1086 (define mingetty-shepherd-service
1088 (($ <mingetty-configuration> mingetty tty auto-login login-program
1089 login-pause? clear-on-logout?)
1092 (documentation "Run mingetty on an tty.")
1093 (provision (list (symbol-append 'term- (string->symbol tty))))
1095 ;; Since the login prompt shows the host name, wait for the 'host-name'
1096 ;; service to be done. Also wait for udev essentially so that the tty
1097 ;; text is not lost in the middle of kernel messages (XXX).
1098 (requirement '(user-processes host-name udev virtual-terminal))
1100 (start #~(make-forkexec-constructor
1101 (list #$(file-append mingetty "/sbin/mingetty")
1103 ;; Avoiding 'vhangup' allows us to avoid 'setfont'
1104 ;; errors down the path where various ioctls get
1105 ;; EIO--see 'hung_up_tty_ioctl' in driver/tty/tty_io.c
1109 #$@(if clear-on-logout?
1113 #~("--autologin" #$auto-login)
1115 #$@(if login-program
1116 #~("--loginprog" #$login-program)
1121 (stop #~(make-kill-destructor)))))))
1123 (define mingetty-service-type
1124 (service-type (name 'mingetty)
1125 (extensions (list (service-extension shepherd-root-service-type
1126 mingetty-shepherd-service)))
1128 "Provide console login using the @command{mingetty}
1131 (define* (mingetty-service config)
1132 "Return a service to run mingetty according to @var{config}, which specifies
1133 the tty to run, among other things."
1134 (service mingetty-service-type config))
1136 (define-record-type* <nscd-configuration> nscd-configuration
1137 make-nscd-configuration
1139 (log-file nscd-configuration-log-file ;string
1140 (default "/var/log/nscd.log"))
1141 (debug-level nscd-debug-level ;integer
1143 ;; TODO: See nscd.conf in glibc for other options to add.
1144 (caches nscd-configuration-caches ;list of <nscd-cache>
1145 (default %nscd-default-caches))
1146 (name-services nscd-configuration-name-services ;list of file-like
1148 (glibc nscd-configuration-glibc ;file-like
1151 (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
1153 (database nscd-cache-database) ;symbol
1154 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
1155 (negative-time-to-live nscd-cache-negative-time-to-live
1156 (default 20)) ;integer
1157 (suggested-size nscd-cache-suggested-size ;integer ("default module
1160 (check-files? nscd-cache-check-files? ;Boolean
1162 (persistent? nscd-cache-persistent? ;Boolean
1164 (shared? nscd-cache-shared? ;Boolean
1166 (max-database-size nscd-cache-max-database-size ;integer
1167 (default (* 32 (expt 2 20))))
1168 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
1171 (define %nscd-default-caches
1172 ;; Caches that we want to enable by default. Note that when providing an
1173 ;; empty nscd.conf, all caches are disabled.
1174 (list (nscd-cache (database 'hosts)
1176 ;; Aggressively cache the host name cache to improve
1177 ;; privacy and resilience.
1178 (positive-time-to-live (* 3600 12))
1179 (negative-time-to-live 20)
1182 (nscd-cache (database 'services)
1184 ;; Services are unlikely to change, so we can be even more
1186 (positive-time-to-live (* 3600 24))
1187 (negative-time-to-live 3600)
1188 (check-files? #t) ;check /etc/services changes
1191 (define %nscd-default-configuration
1192 ;; Default nscd configuration.
1193 (nscd-configuration))
1195 (define (nscd.conf-file config)
1196 "Return the @file{nscd.conf} configuration file for @var{config}, an
1197 @code{<nscd-configuration>} object."
1198 (define cache->config
1200 (($ <nscd-cache> (= symbol->string database)
1201 positive-ttl negative-ttl size check-files?
1202 persistent? shared? max-size propagate?)
1203 (string-append "\nenable-cache\t" database "\tyes\n"
1205 "positive-time-to-live\t" database "\t"
1206 (number->string positive-ttl) "\n"
1207 "negative-time-to-live\t" database "\t"
1208 (number->string negative-ttl) "\n"
1209 "suggested-size\t" database "\t"
1210 (number->string size) "\n"
1211 "check-files\t" database "\t"
1212 (if check-files? "yes\n" "no\n")
1213 "persistent\t" database "\t"
1214 (if persistent? "yes\n" "no\n")
1215 "shared\t" database "\t"
1216 (if shared? "yes\n" "no\n")
1217 "max-db-size\t" database "\t"
1218 (number->string max-size) "\n"
1219 "auto-propagate\t" database "\t"
1220 (if propagate? "yes\n" "no\n")))))
1223 (($ <nscd-configuration> log-file debug-level caches)
1224 (plain-file "nscd.conf"
1226 # Configuration of libc's name service cache daemon (nscd).\n\n"
1228 (string-append "logfile\t" log-file)
1232 (string-append "debug-level\t"
1233 (number->string debug-level))
1237 (map cache->config caches)))))))
1239 (define (nscd-action-procedure nscd config option)
1240 ;; XXX: This is duplicated from mcron; factorize.
1241 #~(lambda (_ . args)
1242 ;; Run 'nscd' in a pipe so we can explicitly redirect its output to
1243 ;; 'current-output-port', which at this stage is bound to the client
1245 (let ((pipe (apply open-pipe* OPEN_READ #$nscd
1246 "-f" #$config #$option args)))
1248 (match (read-line pipe 'concat)
1250 (catch 'system-error
1252 (zero? (close-pipe pipe)))
1254 ;; There's a race with the SIGCHLD handler, which could
1255 ;; call 'waitpid' before 'close-pipe' above does. If we
1256 ;; get ECHILD, that means we lost the race; in that case, we
1257 ;; cannot tell what the exit code was (FIXME).
1258 (or (= ECHILD (system-error-errno args))
1259 (apply throw args)))))
1264 (define (nscd-actions nscd config)
1265 "Return Shepherd actions for NSCD."
1266 ;; Make this functionality available as actions because that's a simple way
1267 ;; to run the right 'nscd' binary with the right config file.
1268 (list (shepherd-action
1270 (documentation "Display statistics about nscd usage.")
1271 (procedure (nscd-action-procedure nscd config "--statistics")))
1275 "Invalidate the given cache--e.g., 'hosts' for host name lookups.")
1276 (procedure (nscd-action-procedure nscd config "--invalidate")))))
1278 (define (nscd-shepherd-service config)
1279 "Return a shepherd service for CONFIG, an <nscd-configuration> object."
1280 (let ((nscd (file-append (nscd-configuration-glibc config)
1282 (nscd.conf (nscd.conf-file config))
1283 (name-services (nscd-configuration-name-services config)))
1284 (list (shepherd-service
1285 (documentation "Run libc's name service cache daemon (nscd).")
1287 (requirement '(user-processes))
1288 (start #~(make-forkexec-constructor
1289 (list #$nscd "-f" #$nscd.conf "--foreground")
1291 ;; Wait for the PID file. However, the PID file is
1292 ;; written before nscd is actually listening on its
1294 #:pid-file "/var/run/nscd/nscd.pid"
1296 #:environment-variables
1297 (list (string-append "LD_LIBRARY_PATH="
1300 (string-append dir "/lib"))
1301 (list #$@name-services))
1303 (stop #~(make-kill-destructor))
1304 (modules `((ice-9 popen) ;for the actions
1307 ,@%default-modules))
1308 (actions (nscd-actions nscd nscd.conf))))))
1310 (define nscd-activation
1311 ;; Actions to take before starting nscd.
1313 (use-modules (guix build utils))
1314 (mkdir-p "/var/run/nscd")
1315 (mkdir-p "/var/db/nscd") ;for the persistent cache
1317 ;; In libc 2.25 nscd uses inotify to watch /etc/resolv.conf, but only if
1318 ;; that file exists when it is started. Thus create it here. Note: on
1319 ;; some systems, such as when NetworkManager is used, /etc/resolv.conf
1320 ;; is a symlink, hence 'lstat'.
1321 (unless (false-if-exception (lstat "/etc/resolv.conf"))
1322 (call-with-output-file "/etc/resolv.conf"
1324 (display "# This is a placeholder.\n" port))))))
1326 (define nscd-service-type
1327 (service-type (name 'nscd)
1329 (list (service-extension activation-service-type
1330 (const nscd-activation))
1331 (service-extension shepherd-root-service-type
1332 nscd-shepherd-service)))
1334 ;; This can be extended by providing additional name services
1335 ;; such as nss-mdns.
1336 (compose concatenate)
1337 (extend (lambda (config name-services)
1340 (name-services (append
1341 (nscd-configuration-name-services config)
1343 (default-value %nscd-default-configuration)
1345 "Runs libc's @dfn{name service cache daemon} (nscd) with the
1346 given configuration---an @code{<nscd-configuration>} object. @xref{Name
1347 Service Switch}, for an example.")))
1349 (define* (nscd-service #:optional (config %nscd-default-configuration))
1350 "Return a service that runs libc's name service cache daemon (nscd) with the
1351 given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
1352 Service Switch}, for an example."
1353 (service nscd-service-type config))
1356 (define-record-type* <syslog-configuration>
1357 syslog-configuration make-syslog-configuration
1358 syslog-configuration?
1359 (syslogd syslog-configuration-syslogd
1360 (default (file-append inetutils "/libexec/syslogd")))
1361 (config-file syslog-configuration-config-file
1362 (default %default-syslog.conf)))
1364 (define syslog-service-type
1365 (shepherd-service-type
1369 (documentation "Run the syslog daemon (syslogd).")
1370 (provision '(syslogd))
1371 (requirement '(user-processes))
1372 (start #~(let ((spawn (make-forkexec-constructor
1373 (list #$(syslog-configuration-syslogd config)
1375 #$(syslog-configuration-config-file config))
1376 #:pid-file "/var/run/syslog.pid")))
1378 ;; Set the umask such that file permissions are #o640.
1379 (let ((mask (umask #o137))
1383 (stop #~(make-kill-destructor))))
1384 (description "Run the syslog daemon, @command{syslogd}, which is
1385 responsible for logging system messages.")))
1387 ;; Snippet adapted from the GNU inetutils manual.
1388 (define %default-syslog.conf
1389 (plain-file "syslog.conf" "
1390 # Log all error messages, authentication messages of
1391 # level notice or higher and anything of level err or
1392 # higher to the console.
1393 # Don't log private authentication messages!
1394 *.alert;auth.notice;authpriv.none /dev/console
1396 # Log anything (except mail) of level info or higher.
1397 # Don't log private authentication messages!
1398 *.info;mail.none;authpriv.none /var/log/messages
1400 # Like /var/log/messages, but also including \"debug\"-level logs.
1401 *.debug;mail.none;authpriv.none /var/log/debug
1403 # Same, in a different place.
1404 *.info;mail.none;authpriv.none /dev/tty12
1406 # The authpriv file has restricted access.
1407 authpriv.* /var/log/secure
1409 # Log all the mail messages in one place.
1410 mail.* /var/log/maillog
1413 (define* (syslog-service #:optional (config (syslog-configuration)))
1414 "Return a service that runs @command{syslogd} and takes
1415 @var{<syslog-configuration>} as a parameter.
1417 @xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
1418 information on the configuration file syntax."
1419 (service syslog-service-type config))
1422 (define pam-limits-service-type
1423 (let ((security-limits
1424 ;; Create /etc/security containing the provided "limits.conf" file.
1425 (lambda (limits-file)
1426 `(("security/limits.conf"
1430 (let ((pam-limits (pam-entry
1431 (control "required")
1432 (module "pam_limits.so")
1433 (arguments '("conf=/etc/security/limits.conf")))))
1434 (if (member (pam-service-name pam)
1435 '("login" "su" "slim" "gdm-password" "sddm"))
1438 (session (cons pam-limits
1439 (pam-service-session pam))))
1444 (list (service-extension etc-service-type security-limits)
1445 (service-extension pam-root-service-type
1446 (lambda _ (list pam-extension)))))
1448 "Install the specified resource usage limits by populating
1449 @file{/etc/security/limits.conf} and using the @code{pam_limits}
1450 authentication module."))))
1452 (define* (pam-limits-service #:optional (limits '()))
1453 "Return a service that makes selected programs respect the list of
1454 pam-limits-entry specified in LIMITS via pam_limits.so."
1455 (service pam-limits-service-type
1456 (plain-file "limits.conf"
1457 (string-join (map pam-limits-entry->string limits)
1465 (define* (guix-build-accounts count #:key
1468 "Return a list of COUNT user accounts for Guix build users with the given
1470 (unfold (cut > <> count)
1473 (name (format #f "guixbuilder~2,'0d" n))
1477 ;; guix-daemon expects GROUP to be listed as a
1478 ;; supplementary group too:
1479 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
1480 (supplementary-groups (list group "kvm"))
1482 (comment (format #f "Guix Build User ~2d" n))
1483 (home-directory "/var/empty")
1484 (shell (file-append shadow "/sbin/nologin"))))
1489 ;; Select (guix …) and (gnu …) modules, except (guix config).
1491 (('guix 'config) #f)
1492 (('guix rest ...) #t)
1493 (('gnu rest ...) #t)
1496 (define (substitute-key-authorization keys guix)
1497 "Return a gexp with code to register KEYS, a list of files containing 'guix
1498 archive' public keys, with GUIX."
1500 (with-extensions (list guile-gcrypt)
1501 (with-imported-modules `(((guix config) => ,(make-config.scm))
1502 ,@(source-module-closure '((guix pki))
1503 #:select? not-config?))
1504 (computed-file "acl"
1506 (use-modules (guix pki)
1512 (call-with-input-file file
1513 (compose string->canonical-sexp
1517 (call-with-output-file #$output
1519 (write-acl (public-keys->acl keys)
1522 (with-imported-modules '((guix build utils))
1524 (use-modules (guix build utils))
1526 ;; If the ACL already exists, move it out of the way. Create a backup
1527 ;; if it's a regular file: it's likely that the user manually updated
1528 ;; it with 'guix archive --authorize'.
1529 (if (file-exists? "/etc/guix/acl")
1530 (if (and (symbolic-link? "/etc/guix/acl")
1531 (store-file-name? (readlink "/etc/guix/acl")))
1532 (delete-file "/etc/guix/acl")
1533 (rename-file "/etc/guix/acl" "/etc/guix/acl.bak"))
1534 (mkdir-p "/etc/guix"))
1536 ;; Installed the declared ACL.
1537 (symlink #+default-acl "/etc/guix/acl"))))
1539 (define %default-authorized-guix-keys
1540 ;; List of authorized substitute keys.
1541 (list (file-append guix "/share/guix/berlin.guix.gnu.org.pub")
1542 (file-append guix "/share/guix/bordeaux.guix.gnu.org.pub")))
1544 (define-record-type* <guix-configuration>
1545 guix-configuration make-guix-configuration
1547 (guix guix-configuration-guix ;file-like
1549 (build-group guix-configuration-build-group ;string
1550 (default "guixbuild"))
1551 (build-accounts guix-configuration-build-accounts ;integer
1553 (authorize-key? guix-configuration-authorize-key? ;Boolean
1555 (authorized-keys guix-configuration-authorized-keys ;list of gexps
1556 (default %default-authorized-guix-keys))
1557 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
1559 (substitute-urls guix-configuration-substitute-urls ;list of strings
1560 (default %default-substitute-urls))
1561 (chroot-directories guix-configuration-chroot-directories ;list of file-like/strings
1563 (max-silent-time guix-configuration-max-silent-time ;integer
1565 (timeout guix-configuration-timeout ;integer
1567 (log-compression guix-configuration-log-compression
1569 (discover? guix-configuration-discover?
1571 (extra-options guix-configuration-extra-options ;list of strings
1573 (log-file guix-configuration-log-file ;string
1574 (default "/var/log/guix-daemon.log"))
1575 (http-proxy guix-http-proxy ;string | #f
1577 (tmpdir guix-tmpdir ;string | #f
1580 (define %default-guix-configuration
1581 (guix-configuration))
1583 (define shepherd-set-http-proxy-action
1584 ;; Shepherd action to change the HTTP(S) proxy.
1586 (name 'set-http-proxy)
1588 "Change the HTTP(S) proxy used by 'guix-daemon' and restart it.")
1589 (procedure #~(lambda* (_ #:optional proxy)
1590 (let ((environment (environ)))
1591 ;; A bit of a hack: communicate PROXY to the 'start'
1592 ;; method via environment variables.
1595 (format #t "changing HTTP/HTTPS \
1596 proxy of 'guix-daemon' to ~s...~%"
1598 (setenv "http_proxy" proxy))
1600 (format #t "clearing HTTP/HTTPS \
1601 proxy of 'guix-daemon'...~%")
1602 (unsetenv "http_proxy")))
1603 (action 'guix-daemon 'restart)
1604 (environ environment)
1607 (define shepherd-discover-action
1608 ;; Shepherd action to enable or disable substitute servers discovery.
1612 "Enable or disable substitute servers discovery and restart the
1614 (procedure #~(lambda* (_ status)
1615 (let ((environment (environ)))
1617 (string=? status "on"))
1619 (format #t "enable substitute servers discovery~%")
1620 (setenv "discover" "on"))
1622 (format #t "disable substitute servers discovery~%")
1623 (unsetenv "discover")))
1624 (action 'guix-daemon 'restart)
1625 (environ environment)
1628 (define (guix-shepherd-service config)
1629 "Return a <shepherd-service> for the Guix daemon service with CONFIG."
1630 (match-record config <guix-configuration>
1631 (guix build-group build-accounts authorize-key? authorized-keys
1632 use-substitutes? substitute-urls max-silent-time timeout
1633 log-compression discover? extra-options log-file
1634 http-proxy tmpdir chroot-directories)
1635 (list (shepherd-service
1636 (documentation "Run the Guix daemon.")
1637 (provision '(guix-daemon))
1638 (requirement '(user-processes))
1639 (actions (list shepherd-set-http-proxy-action
1640 shepherd-discover-action))
1641 (modules '((srfi srfi-1)
1643 (gnu build shepherd)))
1645 (with-imported-modules `(((guix config) => ,(make-config.scm))
1646 ,@(source-module-closure
1647 '((gnu build shepherd))
1648 #:select? not-config?))
1651 ;; HTTP/HTTPS proxy. The 'http_proxy' variable is set by
1652 ;; the 'set-http-proxy' action.
1653 (or (getenv "http_proxy") #$http-proxy))
1656 (or (getenv "discover") #$discover?))
1658 ;; Start the guix-daemon from a container, when supported,
1659 ;; to solve an installation issue. See the comment below for
1661 (fork+exec-command/container
1662 (cons* #$(file-append guix "/bin/guix-daemon")
1663 "--build-users-group" #$build-group
1665 #$(number->string max-silent-time)
1666 "--timeout" #$(number->string timeout)
1668 #$(symbol->string log-compression)
1669 #$@(if use-substitutes?
1671 '("--no-substitutes"))
1672 (string-append "--discover="
1673 (if discover? "yes" "no"))
1674 "--substitute-urls" #$(string-join substitute-urls)
1677 ;; Add CHROOT-DIRECTORIES and all their dependencies
1678 ;; (if these are store items) to the chroot.
1681 (append-map (lambda (directory)
1682 (list "--chroot-directory"
1684 (call-with-input-file file
1686 '#$(map references-file
1687 chroot-directories)))
1689 ;; When running the installer, we need guix-daemon to
1690 ;; operate from within the same MNT namespace as the
1691 ;; installation container. In that case only, enter the
1692 ;; namespace of the process PID passed as start argument.
1693 ;; Otherwise, for symmetry purposes enter the caller
1694 ;; namespaces which is a no-op.
1696 ((pid) (string->number pid))
1699 #:environment-variables
1700 (append (list #$@(if tmpdir
1701 (list (string-append "TMPDIR=" tmpdir))
1704 ;; Make sure we run in a UTF-8 locale so that
1705 ;; 'guix offload' correctly restores nars
1706 ;; that contain UTF-8 file names such as
1708 ;; <https://bugs.gnu.org/32942>.
1709 (string-append "GUIX_LOCPATH="
1710 #$glibc-utf8-locales
1712 "LC_ALL=en_US.utf8")
1714 (list (string-append "http_proxy=" proxy)
1715 (string-append "https_proxy=" proxy))
1718 #:log-file #$log-file))))
1719 (stop #~(make-kill-destructor))))))
1721 (define (guix-accounts config)
1722 "Return the user accounts and user groups for CONFIG."
1724 (($ <guix-configuration> _ build-group build-accounts)
1729 ;; Use a fixed GID so that we can create the store with the right
1732 (guix-build-accounts build-accounts
1733 #:group build-group)))))
1735 (define (guix-activation config)
1736 "Return the activation gexp for CONFIG."
1737 (match-record config <guix-configuration>
1738 (guix authorize-key? authorized-keys)
1740 ;; Assume that the store has BUILD-GROUP as its group. We could
1741 ;; otherwise call 'chown' here, but the problem is that on a COW overlayfs,
1742 ;; chown leads to an entire copy of the tree, which is a bad idea.
1744 ;; Generate a key pair and optionally authorize substitute server keys.
1745 (unless (file-exists? "/etc/guix/signing-key.pub")
1746 (system* #$(file-append guix "/bin/guix") "archive"
1749 #$(if authorize-key?
1750 (substitute-key-authorization authorized-keys guix)
1753 (define* (references-file item #:optional (name "references"))
1754 "Return a file that contains the list of references of ITEM."
1755 (if (struct? item) ;lowerable object
1757 (with-extensions (list guile-gcrypt) ;for store-copy
1758 (with-imported-modules (source-module-closure
1759 '((guix build store-copy)))
1761 (use-modules (guix build store-copy))
1763 (call-with-output-file #$output
1765 (write (map store-info-item
1766 (call-with-input-file "graph"
1767 read-reference-graph))
1769 #:options `(#:local-build? #f
1770 #:references-graphs (("graph" ,item))))
1771 (plain-file name "()")))
1773 (define guix-service-type
1777 (list (service-extension shepherd-root-service-type guix-shepherd-service)
1778 (service-extension account-service-type guix-accounts)
1779 (service-extension activation-service-type guix-activation)
1780 (service-extension profile-service-type
1781 (compose list guix-configuration-guix))))
1783 ;; Extensions can specify extra directories to add to the build chroot.
1784 (compose concatenate)
1785 (extend (lambda (config directories)
1789 (append (guix-configuration-chroot-directories config)
1792 (default-value (guix-configuration))
1794 "Run the build daemon of GNU@tie{}Guix, aka. @command{guix-daemon}.")))
1797 (define-record-type* <guix-publish-configuration>
1798 guix-publish-configuration make-guix-publish-configuration
1799 guix-publish-configuration?
1800 (guix guix-publish-configuration-guix ;file-like
1802 (port guix-publish-configuration-port ;number
1804 (host guix-publish-configuration-host ;string
1805 (default "localhost"))
1806 (advertise? guix-publish-advertise? ;boolean
1808 (compression guix-publish-configuration-compression
1810 (default (default-compression this-record
1811 (current-source-location))))
1812 (compression-level %guix-publish-configuration-compression-level ;deprecated
1814 (nar-path guix-publish-configuration-nar-path ;string
1816 (cache guix-publish-configuration-cache ;#f | string
1818 (cache-bypass-threshold guix-publish-configuration-cache-bypass-threshold
1819 (default (* 10 (expt 2 20)))) ;integer
1820 (workers guix-publish-configuration-workers ;#f | integer
1822 (ttl guix-publish-configuration-ttl ;#f | integer
1825 (define-deprecated (guix-publish-configuration-compression-level config)
1826 "Return a compression level, the old way."
1827 (match (guix-publish-configuration-compression config)
1828 (((_ level) _ ...) level)))
1830 (define (default-compression config properties)
1831 "Return the default 'guix publish' compression according to CONFIG, and
1832 raise a deprecation warning if the 'compression-level' field was used."
1833 (match (%guix-publish-configuration-compression-level config)
1835 ;; Default to low compression levels when there's no cache so that users
1836 ;; get good bandwidth by default.
1837 (if (guix-publish-configuration-cache config)
1838 '(("gzip" 5) ("zstd" 19))
1839 '(("gzip" 3) ("zstd" 3)))) ;zstd compresses faster
1841 (warn-about-deprecation 'compression-level properties
1842 #:replacement 'compression)
1843 `(("gzip" ,level)))))
1845 (define (guix-publish-shepherd-service config)
1846 (define (config->compression-options config)
1847 (match (guix-publish-configuration-compression config)
1848 (() ;empty list means "no compression"
1851 (append-map (match-lambda
1853 `("-C" ,(string-append type ":"
1854 (number->string level)))))
1857 (match-record config <guix-publish-configuration>
1858 (guix port host nar-path cache workers ttl cache-bypass-threshold
1860 (list (shepherd-service
1861 (provision '(guix-publish))
1862 (requirement `(user-processes
1864 ,@(if advertise? '(avahi-daemon) '())))
1865 (start #~(make-forkexec-constructor
1866 (list #$(file-append guix "/bin/guix")
1867 "publish" "-u" "guix-publish"
1868 "-p" #$(number->string port)
1869 #$@(config->compression-options config)
1870 (string-append "--nar-path=" #$nar-path)
1871 (string-append "--listen=" #$host)
1876 #~((string-append "--workers="
1881 #~((string-append "--ttl="
1882 #$(number->string ttl)
1886 #~((string-append "--cache=" #$cache)
1888 "--cache-bypass-threshold="
1890 cache-bypass-threshold)))
1893 ;; Make sure we run in a UTF-8 locale so we can produce
1894 ;; nars for packages that contain UTF-8 file names such
1895 ;; as 'nss-certs'. See <https://bugs.gnu.org/26948>.
1896 #:environment-variables
1897 (list (string-append "GUIX_LOCPATH="
1898 #$glibc-utf8-locales "/lib/locale")
1899 "LC_ALL=en_US.utf8")
1900 #:log-file "/var/log/guix-publish.log"))
1901 (stop #~(make-kill-destructor))))))
1903 (define %guix-publish-accounts
1904 (list (user-group (name "guix-publish") (system? #t))
1906 (name "guix-publish")
1907 (group "guix-publish")
1909 (comment "guix publish user")
1910 (home-directory "/var/empty")
1911 (shell (file-append shadow "/sbin/nologin")))))
1913 (define %guix-publish-log-rotations
1915 (files (list "/var/log/guix-publish.log")))))
1917 (define (guix-publish-activation config)
1918 (let ((cache (guix-publish-configuration-cache config)))
1920 (with-imported-modules '((guix build utils))
1922 (use-modules (guix build utils))
1925 (let* ((pw (getpw "guix-publish"))
1926 (uid (passwd:uid pw))
1927 (gid (passwd:gid pw)))
1928 (chown #$cache uid gid))))
1931 (define guix-publish-service-type
1932 (service-type (name 'guix-publish)
1934 (list (service-extension shepherd-root-service-type
1935 guix-publish-shepherd-service)
1936 (service-extension account-service-type
1937 (const %guix-publish-accounts))
1938 (service-extension rottlog-service-type
1939 (const %guix-publish-log-rotations))
1940 (service-extension activation-service-type
1941 guix-publish-activation)))
1942 (default-value (guix-publish-configuration))
1944 "Add a Shepherd service running @command{guix publish}, a
1945 command that allows you to share pre-built binaries with others over HTTP.")))
1952 (define-record-type* <udev-configuration>
1953 udev-configuration make-udev-configuration
1955 (udev udev-configuration-udev ;file-like
1957 (rules udev-configuration-rules ;list of file-like
1960 (define (udev-rules-union packages)
1961 "Return the union of the @code{lib/udev/rules.d} directories found in each
1962 item of @var{packages}."
1964 (with-imported-modules '((guix build union)
1967 (use-modules (guix build union)
1972 (define %standard-locations
1973 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
1975 (define (rules-sub-directory directory)
1976 ;; Return the sub-directory of DIRECTORY containing udev rules, or
1977 ;; #f if none was found.
1978 (find directory-exists?
1979 (map (cut string-append directory <>) %standard-locations)))
1981 (mkdir-p (string-append #$output "/lib/udev"))
1982 (union-build (string-append #$output "/lib/udev/rules.d")
1983 (filter-map rules-sub-directory '#$packages)))))
1985 (computed-file "udev-rules" build))
1987 (define (udev-rule file-name contents)
1988 "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
1989 (computed-file file-name
1990 (with-imported-modules '((guix build utils))
1992 (use-modules (guix build utils))
1995 (string-append #$output "/lib/udev/rules.d"))
1998 (call-with-output-file
1999 (string-append rules.d "/" #$file-name)
2001 (display #$contents port)))))))
2003 (define (file->udev-rule file-name file)
2004 "Return a directory with a udev rule file FILE-NAME which is a copy of FILE."
2005 (computed-file file-name
2006 (with-imported-modules '((guix build utils))
2008 (use-modules (guix build utils))
2011 (string-append #$output "/lib/udev/rules.d"))
2013 (define file-copy-dest
2014 (string-append rules.d "/" #$file-name))
2017 (copy-file #$file file-copy-dest)))))
2019 (define kvm-udev-rule
2020 ;; Return a directory with a udev rule that changes the group of /dev/kvm to
2021 ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
2022 ;; but now we have to add it by ourselves.
2024 ;; Build users are part of the "kvm" group, so we can fearlessly make
2025 ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
2026 (udev-rule "90-kvm.rules"
2027 "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
2029 (define udev-shepherd-service
2030 ;; Return a <shepherd-service> for UDEV with RULES.
2032 (($ <udev-configuration> udev rules)
2033 (let* ((rules (udev-rules-union (cons* udev kvm-udev-rule rules)))
2034 (udev.conf (computed-file "udev.conf"
2035 #~(call-with-output-file #$output
2038 "udev_rules=\"~a/lib/udev/rules.d\"\n"
2044 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
2046 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
2047 (requirement '(root-file-system))
2049 (documentation "Populate the /dev directory, dynamically.")
2051 (with-imported-modules (source-module-closure
2052 '((gnu build linux-boot)))
2055 ;; 'udevd' from eudev.
2056 #$(file-append udev "/sbin/udevd"))
2058 (define (wait-for-udevd)
2059 ;; Wait until someone's listening on udevd's control
2061 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
2063 (catch 'system-error
2065 (connect sock PF_UNIX "/run/udev/control")
2068 (format #t "waiting for udevd...~%")
2072 ;; Allow udev to find the modules.
2073 (setenv "LINUX_MODULE_DIRECTORY"
2074 "/run/booted-system/kernel/lib/modules")
2076 (let* ((kernel-release
2077 (utsname:release (uname)))
2078 (linux-module-directory
2079 (getenv "LINUX_MODULE_DIRECTORY"))
2081 (string-append linux-module-directory "/"
2083 (old-umask (umask #o022)))
2084 ;; If we're in a container, DIRECTORY might not exist,
2085 ;; for instance because the host runs a different
2086 ;; kernel. In that case, skip it; we'll just miss a few
2087 ;; nodes like /dev/fuse.
2088 (when (file-exists? directory)
2089 (make-static-device-nodes directory))
2092 (let ((pid (fork+exec-command (list udevd)
2093 #:environment-variables
2095 ;; The first one is for udev, the second one for
2097 (string-append "UDEV_CONFIG_FILE=" #$udev.conf)
2098 (string-append "EUDEV_RULES_DIRECTORY="
2100 rules "/lib/udev/rules.d"))
2101 (string-append "LINUX_MODULE_DIRECTORY="
2102 (getenv "LINUX_MODULE_DIRECTORY"))
2103 (default-environment-variables)))))
2104 ;; Wait until udevd is up and running. This appears to
2105 ;; be needed so that the events triggered below are
2106 ;; actually handled.
2109 ;; Trigger device node creation.
2110 (system* #$(file-append udev "/bin/udevadm")
2111 "trigger" "--action=add")
2113 ;; Wait for things to settle down.
2114 (system* #$(file-append udev "/bin/udevadm")
2117 (stop #~(make-kill-destructor))
2119 ;; When halting the system, 'udev' is actually killed by
2120 ;; 'user-processes', i.e., before its own 'stop' method was called.
2121 ;; Thus, make sure it is not respawned.
2123 ;; We need additional modules.
2124 (modules `((gnu build linux-boot) ;'make-static-device-nodes'
2125 ,@%default-modules))
2127 (actions (list (shepherd-action
2129 (documentation "Display the directory containing
2130 the udev rules in use.")
2131 (procedure #~(lambda (_)
2133 (newline))))))))))))
2135 (define udev-service-type
2136 (service-type (name 'udev)
2138 (list (service-extension shepherd-root-service-type
2139 udev-shepherd-service)))
2141 (compose concatenate) ;concatenate the list of rules
2142 (extend (lambda (config rules)
2144 (($ <udev-configuration> udev initial-rules)
2147 (rules (append initial-rules rules)))))))
2148 (default-value (udev-configuration))
2150 "Run @command{udev}, which populates the @file{/dev}
2151 directory dynamically. Get extra rules from the packages listed in the
2152 @code{rules} field of its value, @code{udev-configuration} object.")))
2154 (define* (udev-service #:key (udev eudev) (rules '()))
2155 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
2156 extra rules from the packages listed in @var{rules}."
2157 (service udev-service-type
2158 (udev-configuration (udev udev) (rules rules))))
2160 (define* (udev-rules-service name rules #:key (groups '()))
2161 "Return a service that extends udev-service-type with RULES and
2162 account-service-type with GROUPS as system groups. This works by creating a
2163 singleton service type NAME-udev-rules, of which the returned service is an
2165 (let* ((name (symbol-append name '-udev-rules))
2167 (const (map (lambda (group)
2168 (user-group (name group) (system? #t)))
2170 (udev-extension (const (list rules)))
2175 account-service-type account-extension)
2177 udev-service-type udev-extension))))))
2180 (define (swap-space->shepherd-service-name space)
2181 (let ((target (swap-space-target space)))
2182 (symbol-append 'swap-
2184 (cond ((uuid? target)
2185 (uuid->string target))
2186 ((file-system-label? target)
2187 (file-system-label->string target))
2191 ; TODO Remove after deprecation
2192 (define (swap-deprecated->shepherd-service-name sdep)
2193 (symbol-append 'swap-
2196 (string-take (uuid->string sdep) 6))
2197 ((file-system-label? sdep)
2198 (file-system-label->string sdep))
2202 (define swap->shepherd-service-name
2203 (match-lambda ((? swap-space? space)
2204 (swap-space->shepherd-service-name space))
2206 (swap-deprecated->shepherd-service-name sdep))))
2208 (define swap-service-type
2209 (shepherd-service-type
2212 (define requirements
2213 (cond ((swap-space? swap)
2214 (map dependency->shepherd-service-name
2215 (swap-space-dependencies swap)))
2216 ; TODO Remove after deprecation
2217 ((and (string? swap) (string-prefix? "/dev/mapper/" swap))
2218 (list (symbol-append 'device-mapping-
2219 (string->symbol (basename swap)))))
2223 (define device-lookup
2224 ;; The generic 'find-partition' procedures could return a partition
2225 ;; that's not swap space, but that's unlikely.
2226 (cond ((swap-space? swap)
2227 (let ((target (swap-space-target swap)))
2228 (cond ((uuid? target)
2229 #~(find-partition-by-uuid #$(uuid-bytevector target)))
2230 ((file-system-label? target)
2231 #~(find-partition-by-label
2232 #$(file-system-label->string target)))
2235 ; TODO Remove after deprecation
2237 #~(find-partition-by-uuid #$(uuid-bytevector swap)))
2238 ((file-system-label? swap)
2239 #~(find-partition-by-label
2240 #$(file-system-label->string swap)))
2244 (with-imported-modules (source-module-closure '((gnu build file-systems)))
2246 (provision (list (swap->shepherd-service-name swap)))
2247 (requirement `(udev ,@requirements))
2248 (documentation "Enable the given swap space.")
2249 (modules `((gnu build file-systems)
2250 ,@%default-modules))
2252 (let ((device #$device-lookup))
2255 (restart-on-EINTR (swapon device
2256 #$(if (swap-space? swap)
2257 (swap-space->flags-bit-mask
2262 (let ((device #$device-lookup))
2264 (restart-on-EINTR (swapoff device)))
2267 (description "Turn on the virtual memory swap area.")))
2269 (define (swap-service swap)
2270 "Return a service that uses @var{swap} as a swap space."
2271 (service swap-service-type swap))
2273 (define %default-gpm-options
2274 ;; Default options for GPM.
2275 '("-m" "/dev/input/mice" "-t" "ps2"))
2277 (define-record-type* <gpm-configuration>
2278 gpm-configuration make-gpm-configuration gpm-configuration?
2279 (gpm gpm-configuration-gpm ;file-like
2281 (options gpm-configuration-options ;list of strings
2282 (default %default-gpm-options)))
2284 (define gpm-shepherd-service
2286 (($ <gpm-configuration> gpm options)
2287 (list (shepherd-service
2288 (requirement '(udev))
2290 ;; 'gpm' runs in the background and sets a PID file.
2291 ;; Note that it requires running as "root".
2292 (start #~(make-forkexec-constructor
2293 (list #$(file-append gpm "/sbin/gpm")
2295 #:pid-file "/var/run/gpm.pid"
2296 #:pid-file-timeout 3))
2298 ;; Return #f if successfully stopped.
2299 (not (zero? (system* #$(file-append gpm "/sbin/gpm")
2302 (define gpm-service-type
2303 (service-type (name 'gpm)
2305 (list (service-extension shepherd-root-service-type
2306 gpm-shepherd-service)))
2307 (default-value (gpm-configuration))
2309 "Run GPM, the general-purpose mouse daemon, with the given
2310 command-line options. GPM allows users to use the mouse in the console,
2311 notably to select, copy, and paste text. The default options use the
2312 @code{ps2} protocol, which works for both USB and PS/2 mice.")))
2315 (define-record-type* <kmscon-configuration>
2316 kmscon-configuration make-kmscon-configuration
2317 kmscon-configuration?
2318 (kmscon kmscon-configuration-kmscon
2320 (virtual-terminal kmscon-configuration-virtual-terminal)
2321 (login-program kmscon-configuration-login-program
2322 (default (file-append shadow "/bin/login")))
2323 (login-arguments kmscon-configuration-login-arguments
2325 (auto-login kmscon-configuration-auto-login
2327 (hardware-acceleration? kmscon-configuration-hardware-acceleration?
2328 (default #f)) ; #t causes failure
2329 (font-engine kmscon-configuration-font-engine
2331 (font-size kmscon-configuration-font-size
2333 (keyboard-layout kmscon-configuration-keyboard-layout
2334 (default #f))) ; #f | <keyboard-layout>
2336 (define kmscon-service-type
2337 (shepherd-service-type
2340 (let ((kmscon (kmscon-configuration-kmscon config))
2341 (virtual-terminal (kmscon-configuration-virtual-terminal config))
2342 (login-program (kmscon-configuration-login-program config))
2343 (login-arguments (kmscon-configuration-login-arguments config))
2344 (auto-login (kmscon-configuration-auto-login config))
2345 (hardware-acceleration? (kmscon-configuration-hardware-acceleration? config))
2346 (font-engine (kmscon-configuration-font-engine config))
2347 (font-size (kmscon-configuration-font-size config))
2348 (keyboard-layout (kmscon-configuration-keyboard-layout config)))
2350 (define kmscon-command
2352 #$(file-append kmscon "/bin/kmscon") "--login"
2353 "--vt" #$virtual-terminal
2354 "--no-switchvt" ;Prevent a switch to the virtual terminal.
2355 "--font-engine" #$font-engine
2356 "--font-size" #$(number->string font-size)
2357 #$@(if keyboard-layout
2358 (let* ((layout (keyboard-layout-name keyboard-layout))
2359 (variant (keyboard-layout-variant keyboard-layout))
2360 (model (keyboard-layout-model keyboard-layout))
2361 (options (keyboard-layout-options keyboard-layout)))
2362 `("--xkb-layout" ,layout
2363 ,@(if variant `("--xkb-variant" ,variant) '())
2364 ,@(if model `("--xkb-model" ,model) '())
2365 ,@(if (null? options)
2367 `("--xkb-options" ,(string-join options ",")))))
2369 #$@(if hardware-acceleration? '("--hwaccel") '())
2371 #$login-program #$@login-arguments
2377 (documentation "kmscon virtual terminal")
2378 (requirement '(user-processes udev dbus-system))
2379 (provision (list (symbol-append 'term- (string->symbol virtual-terminal))))
2380 (start #~(make-forkexec-constructor #$kmscon-command))
2381 (stop #~(make-kill-destructor)))))
2382 (description "Start the @command{kmscon} virtual terminal emulator for the
2383 Linux @dfn{kernel mode setting} (KMS).")))
2387 ;;; Static networking.
2390 (define (ipv6-address? str)
2391 "Return true if STR denotes an IPv6 address."
2392 (false-if-exception (->bool (inet-pton AF_INET6 str))))
2394 (define-compile-time-procedure (assert-valid-address (address string?))
2395 "Ensure ADDRESS has a valid netmask."
2396 (unless (or (cidr->netmask address)
2397 (and=> (false-if-exception (inet-pton AF_INET address))
2398 (cut = INADDR_LOOPBACK <>))
2399 (and=> (false-if-exception (inet-pton AF_INET6 address))
2402 (make-compound-condition
2403 (formatted-message (G_ "address '~a' lacks a network mask")
2405 (condition (&error-location
2407 (source-properties->location procedure-call-location))))
2408 (condition (&fix-hint
2409 (hint (format #f (G_ "\
2410 Write, say, @samp{\"~a/24\"} for a 24-bit network mask.")
2414 (define-record-type* <static-networking>
2415 static-networking make-static-networking
2417 (addresses static-networking-addresses) ;list of <network-address>
2418 (links static-networking-links (default '())) ;list of <network-link>
2419 (routes static-networking-routes (default '())) ;list of <network-routes>
2420 (provision static-networking-provision
2421 (default '(networking)))
2422 (requirement static-networking-requirement
2424 (name-servers static-networking-name-servers ;FIXME: doesn't belong here
2427 (define-record-type* <network-address>
2428 network-address make-network-address
2430 (device network-address-device) ;string--e.g., "en01"
2431 (value network-address-value ;string--CIDR notation
2432 (sanitize assert-valid-address))
2433 (ipv6? network-address-ipv6? ;Boolean
2436 (ipv6-address? (cidr->ip (network-address-value this-record))))))
2438 (define-record-type* <network-link>
2439 network-link make-network-link
2441 (name network-link-name) ;string--e.g, "v0p0"
2442 (type network-link-type) ;symbol--e.g.,'veth
2443 (arguments network-link-arguments)) ;list
2445 (define-record-type* <network-route>
2446 network-route make-network-route
2448 (destination network-route-destination)
2449 (source network-route-source (default #f))
2450 (device network-route-device (default #f))
2451 (ipv6? network-route-ipv6? (thunked)
2453 (or (ipv6-address? (network-route-destination this-record))
2454 (and=> (network-route-gateway this-record)
2456 (gateway network-route-gateway (default #f)))
2458 (define* (cidr->netmask str #:optional (family AF_INET))
2459 "Given @var{str}, a string in CIDR notation (e.g., \"1.2.3.4/24\"), return
2460 the netmask as a string like \"255.255.255.0\"."
2461 (match (string-split str #\/)
2462 ((ip (= string->number bits))
2463 (let ((mask (ash (- (expt 2 bits) 1)
2464 (- (if (= family AF_INET6) 128 32)
2466 (inet-ntop family mask)))
2469 (define (cidr->ip str)
2470 "Strip the netmask bit of @var{str}, a CIDR-notation IP/netmask address."
2471 (match (string-split str #\/)
2475 (define* (ip+netmask->cidr ip netmask #:optional (family AF_INET))
2476 "Return the CIDR notation (a string) for @var{ip} and @var{netmask}, two
2477 @var{family} address strings, where @var{family} is @code{AF_INET} or
2479 (let* ((netmask (inet-pton family netmask))
2480 (bits (logcount netmask)))
2481 (string-append ip "/" (number->string bits))))
2483 (define (static-networking->hurd-pfinet-options config)
2484 "Return command-line options for the Hurd's pfinet translator corresponding
2486 (unless (null? (static-networking-links config))
2487 ;; XXX: Presumably this is not supported, or perhaps could be approximated
2488 ;; by running separate pfinet instances in some cases?
2489 (warning (G_ "network links are currently ignored on GNU/Hurd~%")))
2491 (match (static-networking-addresses config)
2492 ((and addresses (first _ ...))
2493 `("--ipv6" "/servers/socket/26"
2494 "--interface" ,(network-address-device first)
2495 ,@(append-map (lambda (address)
2496 `(,(if (network-address-ipv6? address)
2499 ,(cidr->ip (network-address-value address))
2500 ,@(match (cidr->netmask (network-address-value address)
2501 (if (network-address-ipv6? address)
2505 (mask (list "--netmask" mask)))))
2507 ,@(append-map (lambda (route)
2509 (($ <network-route> "default" #f device _ gateway)
2510 (if (network-route-ipv6? route)
2511 `("--gateway6" ,gateway)
2512 `("--gateway" ,gateway)))
2513 (($ <network-route> destination)
2514 (warning (G_ "ignoring network route for '~a'~%")
2517 (static-networking-routes config))))))
2519 (define (network-set-up/hurd config)
2520 "Set up networking for the Hurd."
2521 ;; The Hurd implements SIOCGIFADDR and other old-style ioctls, but the only
2522 ;; way to set up IPv6 is by starting pfinet with the right options.
2523 (if (equal? (static-networking-provision config) '(loopback))
2524 (scheme-file "set-up-pflocal" #~(begin 'nothing-to-do! #t))
2525 (scheme-file "set-up-pfinet"
2526 (with-imported-modules '((guix build utils))
2528 (use-modules (guix build utils)
2531 ;; TODO: Do that without forking.
2532 (let ((options '#$(static-networking->hurd-pfinet-options
2534 (format #t "starting '~a~{ ~s~}'~%"
2535 #$(file-append hurd "/hurd/pfinet")
2537 (apply invoke #$(file-append hurd "/bin/settrans") "-fac"
2539 #$(file-append hurd "/hurd/pfinet")
2542 (define (network-tear-down/hurd config)
2543 (scheme-file "tear-down-pfinet"
2544 (with-imported-modules '((guix build utils))
2546 (use-modules (guix build utils))
2548 ;; Forcefully terminate pfinet. XXX: In theory this
2549 ;; should just undo the addresses and routes of CONFIG;
2550 ;; this could be done using ioctls like SIOCDELRT, but
2551 ;; these are IPv4-only; another option would be to use
2552 ;; fsysopts but that seems to crash pfinet.
2553 (invoke #$(file-append hurd "/bin/settrans") "-fg"
2554 "/servers/socket/2")
2557 (define network-set-up/linux
2559 (($ <static-networking> addresses links routes)
2560 (scheme-file "set-up-network"
2561 (with-extensions (list guile-netlink)
2563 (use-modules (ip addr) (ip link) (ip route))
2565 #$@(map (lambda (address)
2567 (addr-add #$(network-address-device address)
2568 #$(network-address-value address)
2570 #$(network-address-ipv6? address))
2572 (link-set #$(network-address-device address)
2575 #$@(map (match-lambda
2576 (($ <network-link> name type arguments)
2577 #~(link-add #$name #$type
2578 #:type-args '#$arguments)))
2580 #$@(map (lambda (route)
2581 #~(route-add #$(network-route-destination route)
2583 #$(network-route-device route)
2585 #$(network-route-ipv6? route)
2587 #$(network-route-gateway route)
2589 #$(network-route-source route)))
2593 (define network-tear-down/linux
2595 (($ <static-networking> addresses links routes)
2596 (scheme-file "tear-down-network"
2597 (with-extensions (list guile-netlink)
2599 (use-modules (ip addr) (ip link) (ip route)
2603 (define-syntax-rule (false-if-netlink-error exp)
2604 (guard (c ((netlink-error? c) #f))
2607 ;; Wrap calls in 'false-if-netlink-error' so this
2608 ;; script goes as far as possible undoing the effects
2609 ;; of "set-up-network".
2611 #$@(map (lambda (route)
2612 #~(false-if-netlink-error
2613 (route-del #$(network-route-destination route)
2615 #$(network-route-device route)
2617 #$(network-route-ipv6? route)
2619 #$(network-route-gateway route)
2621 #$(network-route-source route))))
2623 #$@(map (match-lambda
2624 (($ <network-link> name type arguments)
2625 #~(false-if-netlink-error
2626 (link-del #$name))))
2628 #$@(map (lambda (address)
2629 #~(false-if-netlink-error
2630 (addr-del #$(network-address-device
2632 #$(network-address-value address)
2634 #$(network-address-ipv6? address))))
2638 (define (static-networking-shepherd-service config)
2640 (($ <static-networking> addresses links routes
2641 provision requirement name-servers)
2642 (let ((loopback? (and provision (memq 'loopback provision))))
2646 "Bring up the networking interface using a static IP address.")
2647 (requirement requirement)
2648 (provision provision)
2651 ;; Return #t if successfully started.
2652 (load #$(let-system (system target)
2653 (if (string-contains (or target system) "-linux")
2654 (network-set-up/linux config)
2655 (network-set-up/hurd config))))))
2657 ;; Return #f is successfully stopped.
2658 (load #$(let-system (system target)
2659 (if (string-contains (or target system) "-linux")
2660 (network-tear-down/linux config)
2661 (network-tear-down/hurd config))))))
2664 (define (static-networking-shepherd-services networks)
2665 (map static-networking-shepherd-service networks))
2667 (define (static-networking-etc-files interfaces)
2668 "Return a /etc/resolv.conf entry for INTERFACES or the empty list."
2669 (match (delete-duplicates
2670 (append-map static-networking-name-servers
2675 (let ((content (string-join
2676 (map (cut string-append "nameserver " <>)
2680 ,(plain-file "resolv.conf"
2682 # Generated by 'static-networking-service'.\n"
2685 (define static-networking-service-type
2686 ;; The service type for statically-defined network interfaces.
2687 (service-type (name 'static-networking)
2690 (service-extension shepherd-root-service-type
2691 static-networking-shepherd-services)
2692 (service-extension etc-service-type
2693 static-networking-etc-files)))
2694 (compose concatenate)
2697 "Turn up the specified network interfaces upon startup,
2698 with the given IP address, gateway, netmask, and so on. The value for
2699 services of this type is a list of @code{static-networking} objects, one per
2700 network interface.")))
2702 (define-deprecated (static-networking-service interface ip
2704 netmask gateway provision
2705 ;; Most interfaces require udev to be usable.
2706 (requirement '(udev))
2708 static-networking-service-type
2709 "Return a service that starts @var{interface} with address @var{ip}. If
2710 @var{netmask} is true, use it as the network mask. If @var{gateway} is true,
2711 it must be a string specifying the default network gateway.
2713 This procedure can be called several times, one for each network
2714 interface of interest. Behind the scenes what it does is extend
2715 @code{static-networking-service-type} with additional network interfaces
2717 (simple-service 'static-network-interface
2718 static-networking-service-type
2719 (list (static-networking
2721 (list (network-address
2724 (ip+netmask->cidr ip netmask)
2729 (list (network-route
2730 (destination "default")
2734 (requirement requirement)
2735 (provision (or provision '(networking)))
2736 (name-servers name-servers)))))
2738 (define %loopback-static-networking
2739 ;; The loopback device.
2741 (addresses (list (network-address
2743 (value "127.0.0.1"))))
2745 (provision '(loopback))))
2747 (define %qemu-static-networking
2748 ;; Networking configuration for QEMU's user-mode network stack (info "(QEMU)
2749 ;; Using the user mode network stack").
2751 (addresses (list (network-address
2753 (value "10.0.2.15/24"))))
2754 (routes (list (network-route
2755 (destination "default")
2756 (gateway "10.0.2.2"))))
2758 (provision '(networking))
2759 (name-servers '("10.0.2.3"))))
2762 (define %base-services
2763 ;; Convenience variable holding the basic services.
2764 (list (service login-service-type)
2766 (service virtual-terminal-service-type)
2767 (service console-font-service-type
2769 (cons tty %default-console-font))
2770 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
2772 (service agetty-service-type (agetty-configuration
2773 (extra-options '("-L")) ; no carrier detect
2775 (tty #f))) ; automatic
2777 (service mingetty-service-type (mingetty-configuration
2779 (service mingetty-service-type (mingetty-configuration
2781 (service mingetty-service-type (mingetty-configuration
2783 (service mingetty-service-type (mingetty-configuration
2785 (service mingetty-service-type (mingetty-configuration
2787 (service mingetty-service-type (mingetty-configuration
2790 (service static-networking-service-type
2791 (list %loopback-static-networking))
2793 (service urandom-seed-service-type)
2794 (service guix-service-type)
2795 (service nscd-service-type)
2797 (service rottlog-service-type)
2799 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
2800 ;; used, so enable them by default. The FUSE and ALSA rules are
2801 ;; less critical, but handy.
2802 (service udev-service-type
2804 (rules (list lvm2 fuse alsa-utils crda))))
2806 (service sysctl-service-type)
2808 (service special-files-service-type
2809 `(("/bin/sh" ,(file-append bash "/bin/sh"))
2810 ("/usr/bin/env" ,(file-append coreutils "/bin/env"))))))
2812 ;;; base.scm ends here