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)
39 #:autoload (guix i18n) (G_)
40 #:use-module (gnu services)
41 #:use-module (gnu services admin)
42 #:use-module (gnu services shepherd)
43 #:use-module (gnu services sysctl)
44 #:use-module (gnu system pam)
45 #:use-module (gnu system shadow) ; 'user-account', etc.
46 #:use-module (gnu system uuid)
47 #:use-module (gnu system file-systems) ; 'file-system', etc.
48 #:use-module (gnu system keyboard)
49 #:use-module (gnu system mapped-devices)
50 #:use-module ((gnu system linux-initrd)
51 #:select (file-system-packages))
52 #:use-module (gnu packages admin)
53 #:use-module ((gnu packages linux)
54 #:select (alsa-utils crda eudev e2fsprogs fuse gpm kbd lvm2 rng-tools))
55 #:use-module (gnu packages bash)
56 #:use-module ((gnu packages base)
57 #:select (coreutils glibc glibc-utf8-locales))
58 #:autoload (gnu packages guile-xyz) (guile-netlink)
59 #:autoload (gnu packages hurd) (hurd)
60 #:use-module (gnu packages package-management)
61 #:use-module ((gnu packages gnupg) #:select (guile-gcrypt))
62 #:use-module (gnu packages linux)
63 #:use-module (gnu packages terminals)
64 #:use-module ((gnu build file-systems)
65 #:select (mount-flags->bit-mask
66 swap-space->flags-bit-mask))
67 #:use-module (guix gexp)
68 #:use-module (guix records)
69 #:use-module (guix modules)
70 #:use-module ((guix self) #:select (make-config.scm))
71 #:use-module (guix diagnostics)
72 #:use-module (guix i18n)
73 #:use-module (srfi srfi-1)
74 #:use-module (srfi srfi-26)
75 #:use-module (ice-9 match)
76 #:use-module (ice-9 format)
77 #:re-export (user-processes-service-type ;backwards compatibility
78 %default-substitute-urls)
79 #:export (fstab-service-type
80 root-file-system-service
81 file-system-service-type
85 console-font-service-type
87 virtual-terminal-service-type
91 static-networking-addresses
92 static-networking-links
93 static-networking-routes
94 static-networking-requirement
98 network-address-device
100 network-address-ipv6?
106 network-link-arguments
110 network-route-destination
114 network-route-gateway
116 static-networking-service
117 static-networking-service-type
119 %loopback-static-networking
120 %qemu-static-networking
124 udev-configuration-rules
137 agetty-configuration?
141 mingetty-configuration
142 mingetty-configuration-tty
143 mingetty-configuration-auto-login
144 mingetty-configuration-login-program
145 mingetty-configuration-login-pause?
146 mingetty-configuration-clear-on-logout?
147 mingetty-configuration-mingetty
148 mingetty-configuration?
150 mingetty-service-type
153 %nscd-default-configuration
165 syslog-configuration?
170 %default-authorized-guix-keys
174 guix-configuration-guix
175 guix-configuration-build-group
176 guix-configuration-build-accounts
177 guix-configuration-authorize-key?
178 guix-configuration-authorized-keys
179 guix-configuration-use-substitutes?
180 guix-configuration-substitute-urls
181 guix-configuration-extra-options
182 guix-configuration-log-file
185 guix-publish-configuration
186 guix-publish-configuration?
187 guix-publish-configuration-guix
188 guix-publish-configuration-port
189 guix-publish-configuration-host
190 guix-publish-configuration-compression
191 guix-publish-configuration-compression-level ;deprecated
192 guix-publish-configuration-nar-path
193 guix-publish-configuration-cache
194 guix-publish-configuration-ttl
195 guix-publish-service-type
201 urandom-seed-service-type
209 kmscon-configuration?
212 pam-limits-service-type
221 ;;; Base system services---i.e., services that 99% of the users will want to
232 (define (file-system->fstab-entry file-system)
233 "Return a @file{/etc/fstab} entry for @var{file-system}."
234 (string-append (match (file-system-device file-system)
235 ((? file-system-label? label)
236 (string-append "LABEL="
237 (file-system-label->string label)))
239 (string-append "UUID=" (uuid->string uuid)))
243 (file-system-mount-point file-system) "\t"
244 (file-system-type file-system) "\t"
245 (or (file-system-options file-system) "defaults") "\t"
247 ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
248 ;; don't have anything sensible to put in there.
251 (define (file-systems->fstab file-systems)
252 "Return a @file{/etc} entry for an @file{fstab} describing
254 `(("fstab" ,(plain-file "fstab"
257 # This file was generated from your Guix configuration. Any changes
258 # will be lost upon reboot or reconfiguration.\n\n"
259 (string-join (map file-system->fstab-entry
264 (define fstab-service-type
265 ;; The /etc/fstab service.
266 (service-type (name 'fstab)
268 (list (service-extension etc-service-type
269 file-systems->fstab)))
270 (compose concatenate)
273 "Populate the @file{/etc/fstab} based on the given file
276 (define %root-file-system-shepherd-service
278 (documentation "Take care of the root file system.")
279 (provision '(root-file-system))
282 ;; Return #f if successfully stopped.
285 (call-with-blocked-asyncs
287 (let ((null (%make-void-port "w")))
288 ;; Close 'shepherd.log'.
289 (display "closing log\n")
290 ((@ (shepherd comm) stop-logging))
292 ;; Redirect the default output ports..
293 (set-current-output-port null)
294 (set-current-error-port null)
296 ;; Close /dev/console.
297 (for-each close-fdes '(0 1 2))
299 ;; At this point, there are no open files left, so the
300 ;; root file system can be re-mounted read-only.
302 (logior MS_REMOUNT MS_RDONLY)
308 (define root-file-system-service-type
309 (shepherd-service-type 'root-file-system
310 (const %root-file-system-shepherd-service)
311 (description "Take care of syncing the root file
312 system and of remounting it read-only when the system shuts down.")))
314 (define (root-file-system-service)
315 "Return a service whose sole purpose is to re-mount read-only the root file
316 system upon shutdown (aka. cleanly \"umounting\" root.)
318 This service must be the root of the service dependency graph so that its
319 'stop' action is invoked when shepherd is the only process left."
320 (service root-file-system-service-type #f))
322 (define (file-system->shepherd-service-name file-system)
323 "Return the symbol that denotes the service mounting and unmounting
325 (symbol-append 'file-system-
326 (string->symbol (file-system-mount-point file-system))))
328 (define (mapped-device->shepherd-service-name md)
329 "Return the symbol that denotes the shepherd service of MD, a <mapped-device>."
330 (symbol-append 'device-mapping-
331 (string->symbol (string-join
332 (mapped-device-targets md) "-"))))
334 (define dependency->shepherd-service-name
336 ((? mapped-device? md)
337 (mapped-device->shepherd-service-name md))
339 (file-system->shepherd-service-name fs))))
341 (define (file-system-shepherd-service file-system)
342 "Return the shepherd service for @var{file-system}, or @code{#f} if
343 @var{file-system} is not auto-mounted or doesn't have its mount point created
345 (let ((target (file-system-mount-point file-system))
346 (create? (file-system-create-mount-point? file-system))
347 (mount? (file-system-mount? file-system))
348 (dependencies (file-system-dependencies file-system))
349 (packages (file-system-packages (list file-system))))
350 (and (or mount? create?)
351 (with-imported-modules (source-module-closure
352 '((gnu build file-systems)))
354 (provision (list (file-system->shepherd-service-name file-system)))
355 (requirement `(root-file-system
357 ,@(map dependency->shepherd-service-name dependencies)))
358 (documentation "Check, mount, and unmount the given file system.")
359 (start #~(lambda args
365 #~(let (($PATH (getenv "PATH")))
366 ;; Make sure fsck.ext2 & co. can be found.
369 ;; Don’t display the PATH settings.
370 (with-output-to-port (%make-void-port "w")
372 (set-path-environment-variable "PATH"
378 '#$(file-system->spec file-system))
381 (setenv "PATH" $PATH))))
385 ;; Normally there are no processes left at this point, so
386 ;; TARGET can be safely unmounted.
388 ;; Make sure PID 1 doesn't keep TARGET busy.
394 ;; We need additional modules.
395 (modules `(((gnu build file-systems)
396 #:select (mount-file-system))
397 (gnu system file-systems)
398 ,@%default-modules)))))))
400 (define (file-system-shepherd-services file-systems)
401 "Return the list of Shepherd services for FILE-SYSTEMS."
402 (let* ((file-systems (filter (lambda (x)
403 (or (file-system-mount? x)
404 (file-system-create-mount-point? x)))
408 (provision '(file-systems))
409 (requirement (cons* 'root-file-system 'user-file-systems
410 (map file-system->shepherd-service-name
412 (documentation "Target for all the initially-mounted file systems")
414 (stop #~(const #f))))
416 (define known-mount-points
417 (map file-system-mount-point file-systems))
421 (documentation "Unmount manually-mounted file systems.")
422 (provision '(user-file-systems))
425 (define (known? mount-point)
427 (cons* "/proc" "/sys" '#$known-mount-points)))
429 ;; Make sure we don't keep the user's mount points busy.
432 (for-each (lambda (mount-point)
433 (format #t "unmounting '~a'...~%" mount-point)
436 (umount mount-point))
438 (let ((errno (system-error-errno args)))
439 (format #t "failed to unmount '~a': ~a~%"
440 mount-point (strerror errno))))))
441 (filter (negate known?) (mount-points)))
444 (cons* sink user-unmount
445 (map file-system-shepherd-service file-systems))))
447 (define (file-system-fstab-entries file-systems)
448 "Return the subset of @var{file-systems} that should have an entry in
450 ;; /etc/fstab is about telling fsck(8), mount(8), and umount(8) about
451 ;; relevant file systems they'll have to deal with. That excludes "pseudo"
454 ;; In particular, things like GIO (part of GLib) use it to determine the set
455 ;; of mounts, which is then used by graphical file managers and desktop
456 ;; environments to display "volume" icons. Thus, we really need to exclude
457 ;; those pseudo file systems from the list.
458 (remove (lambda (file-system)
459 (or (member (file-system-type file-system)
460 %pseudo-file-system-types)
461 (memq 'bind-mount (file-system-flags file-system))))
464 (define file-system-service-type
465 (service-type (name 'file-systems)
467 (list (service-extension shepherd-root-service-type
468 file-system-shepherd-services)
469 (service-extension fstab-service-type
470 file-system-fstab-entries)
472 ;; Have 'user-processes' depend on 'file-systems'.
473 (service-extension user-processes-service-type
474 (const '(file-systems)))))
475 (compose concatenate)
478 "Provide Shepherd services to mount and unmount the given
479 file systems, as well as corresponding @file{/etc/fstab} entries.")))
484 ;;; Preserve entropy to seed /dev/urandom on boot.
487 (define %random-seed-file
488 "/var/lib/random-seed")
490 (define (urandom-seed-shepherd-service _)
491 "Return a shepherd service for the /dev/urandom seed."
492 (list (shepherd-service
493 (documentation "Preserve entropy across reboots for /dev/urandom.")
494 (provision '(urandom-seed))
496 ;; Depend on udev so that /dev/hwrng is available.
497 (requirement '(file-systems udev))
500 ;; On boot, write random seed into /dev/urandom.
501 (when (file-exists? #$%random-seed-file)
502 (call-with-input-file #$%random-seed-file
504 (call-with-output-file "/dev/urandom"
506 (dump-port seed urandom)
508 ;; Writing SEED to URANDOM isn't enough: we must
509 ;; also tell the kernel to account for these
510 ;; extra bits of entropy.
511 (let ((bits (* 8 (stat:size (stat seed)))))
512 (add-to-entropy-count urandom bits)))))))
514 ;; Try writing from /dev/hwrng into /dev/urandom.
515 ;; It seems that the file /dev/hwrng always exists, even
516 ;; when there is no hardware random number generator
517 ;; available. So, we handle a failed read or any other error
518 ;; reported by the operating system.
519 (let ((buf (catch 'system-error
521 (call-with-input-file "/dev/hwrng"
523 (get-bytevector-n hwrng 512))))
524 ;; Silence is golden...
527 (call-with-output-file "/dev/urandom"
529 (put-bytevector urandom buf)
530 (let ((bits (* 8 (bytevector-length buf))))
531 (add-to-entropy-count urandom bits))))))
533 ;; Immediately refresh the seed in case the system doesn't
534 ;; shut down cleanly.
535 (call-with-input-file "/dev/urandom"
537 (let ((previous-umask (umask #o077))
538 (buf (make-bytevector 512)))
539 (mkdir-p (dirname #$%random-seed-file))
540 (get-bytevector-n! urandom buf 0 512)
541 (call-with-output-file #$%random-seed-file
543 (put-bytevector seed buf)))
544 (umask previous-umask))))
547 ;; During shutdown, write from /dev/urandom into random seed.
548 (let ((buf (make-bytevector 512)))
549 (call-with-input-file "/dev/urandom"
551 (let ((previous-umask (umask #o077)))
552 (get-bytevector-n! urandom buf 0 512)
553 (mkdir-p (dirname #$%random-seed-file))
554 (call-with-output-file #$%random-seed-file
556 (put-bytevector seed buf)))
557 (umask previous-umask))
559 (modules `((rnrs bytevectors)
561 ,@%default-modules)))))
563 (define urandom-seed-service-type
564 (service-type (name 'urandom-seed)
566 (list (service-extension shepherd-root-service-type
567 urandom-seed-shepherd-service)
569 ;; Have 'user-processes' depend on 'urandom-seed'.
570 ;; This ensures that user processes and daemons don't
571 ;; start until we have seeded the PRNG.
572 (service-extension user-processes-service-type
573 (const '(urandom-seed)))))
576 "Seed the @file{/dev/urandom} pseudo-random number
577 generator (RNG) with the value recorded when the system was last shut
582 ;;; Add hardware random number generator to entropy pool.
585 (define-record-type* <rngd-configuration>
586 rngd-configuration make-rngd-configuration
588 (rng-tools rngd-configuration-rng-tools) ;file-like
589 (device rngd-configuration-device)) ;string
591 (define rngd-service-type
592 (shepherd-service-type
595 (define rng-tools (rngd-configuration-rng-tools config))
596 (define device (rngd-configuration-device config))
599 (list (file-append rng-tools "/sbin/rngd")
603 (documentation "Add TRNG to entropy pool.")
604 (requirement '(udev))
606 (start #~(make-forkexec-constructor '#$rngd-command))
607 (stop #~(make-kill-destructor))))
608 (description "Run the @command{rngd} random number generation daemon to
609 supply entropy to the kernel's pool.")))
611 (define* (rngd-service #:key
612 (rng-tools rng-tools)
613 (device "/dev/hwrng"))
614 "Return a service that runs the @command{rngd} program from @var{rng-tools}
615 to add @var{device} to the kernel's entropy pool. The service will fail if
616 @var{device} does not exist."
617 (service rngd-service-type
619 (rng-tools rng-tools)
627 (define host-name-service-type
628 (shepherd-service-type
632 (documentation "Initialize the machine's host name.")
633 (provision '(host-name))
635 (sethostname #$name)))
637 (description "Initialize the machine's host name.")))
639 (define (host-name-service name)
640 "Return a service that sets the host name to @var{name}."
641 (service host-name-service-type name))
643 (define virtual-terminal-service-type
644 ;; Ensure that virtual terminals run in UTF-8 mode. This is the case by
645 ;; default with recent Linux kernels, but this service allows us to ensure
646 ;; this. This service must start before any 'term-' service so that newly
647 ;; created terminals inherit this property. See
648 ;; <https://bugs.gnu.org/30505> for a discussion.
649 (shepherd-service-type
652 (let ((knob "/sys/module/vt/parameters/default_utf8"))
654 (documentation "Set virtual terminals in UTF-8 module.")
655 (provision '(virtual-terminal))
656 (requirement '(root-file-system))
658 ;; In containers /sys is read-only so don't insist on
659 ;; writing to this file.
660 (unless (= 1 (call-with-input-file #$knob read))
661 (call-with-output-file #$knob
665 (stop #~(const #f)))))
667 (description "Ensure the Linux virtual terminals run in UTF-8 mode.")))
669 (define console-keymap-service-type
670 (shepherd-service-type
674 (documentation (string-append "Load console keymap (loadkeys)."))
675 (provision '(console-keymap))
677 (zero? (system* #$(file-append kbd "/bin/loadkeys")
680 (description "@emph{This service is deprecated in favor of the
681 @code{keyboard-layout} field of @code{operating-system}.} Load the given list
682 of console keymaps with @command{loadkeys}.")))
684 (define %default-console-font
685 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
686 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
687 ;; codepoints notably found in the UTF-8 manual.
690 (define (console-font-shepherd-services tty+font)
691 "Return a list of Shepherd services for each pair in TTY+FONT."
694 (let ((device (string-append "/dev/" tty)))
696 (documentation "Load a Unicode console font.")
697 (provision (list (symbol-append 'console-font-
698 (string->symbol tty))))
700 ;; Start after mingetty has been started on TTY, otherwise the settings
702 (requirement (list (symbol-append 'term-
703 (string->symbol tty))))
706 ;; It could be that mingetty is not fully ready yet,
707 ;; which we check by calling 'ttyname'.
709 (unless (or (zero? i)
710 (call-with-input-file #$device
712 (false-if-exception (ttyname port)))))
716 ;; Assume the VT is already in UTF-8 mode, thanks to
717 ;; the 'virtual-terminal' service.
719 ;; 'setfont' returns EX_OSERR (71) when an
720 ;; KDFONTOP ioctl fails, for example. Like
721 ;; systemd's vconsole support, let's not treat
723 (case (status:exit-val
724 (system* #$(file-append kbd "/bin/setfont")
725 "-C" #$device #$font))
732 (define console-font-service-type
733 (service-type (name 'console-fonts)
735 (list (service-extension shepherd-root-service-type
736 console-font-shepherd-services)))
737 (compose concatenate)
740 "Install the given fonts on the specified ttys (fonts are per
741 virtual console on GNU/Linux). The value of this service is a list of
742 tty/font pairs. The font can be the name of a font provided by the @code{kbd}
743 package or any valid argument to @command{setfont}, as in this example:
746 `((\"tty1\" . \"LatGrkCyr-8x16\")
747 (\"tty2\" . ,(file-append
749 \"/share/kbd/consolefonts/TamzenForPowerline10x20.psf\"))
750 (\"tty3\" . ,(file-append
752 \"/share/consolefonts/ter-132n\"))) ; for HDPI
755 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
756 "This procedure is deprecated in favor of @code{console-font-service-type}.
758 Return a service that sets up Unicode support in @var{tty} and loads
759 @var{font} for that tty (fonts are per virtual console in Linux.)"
760 (simple-service (symbol-append 'console-font- (string->symbol tty))
761 console-font-service-type `((,tty . ,font))))
763 (define %default-motd
764 (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
766 (define-record-type* <login-configuration>
767 login-configuration make-login-configuration
769 (motd login-configuration-motd ;file-like
770 (default %default-motd))
771 ;; Allow empty passwords by default so that first-time users can log in when
772 ;; the 'root' account has just been created.
773 (allow-empty-passwords? login-configuration-allow-empty-passwords?
774 (default #t))) ;Boolean
776 (define (login-pam-service config)
777 "Return the list of PAM service needed for CONF."
778 ;; Let 'login' be known to PAM.
779 (list (unix-pam-service "login"
781 #:allow-empty-passwords?
782 (login-configuration-allow-empty-passwords? config)
784 (login-configuration-motd config))))
786 (define login-service-type
787 (service-type (name 'login)
788 (extensions (list (service-extension pam-root-service-type
790 (default-value (login-configuration))
792 "Provide a console log-in service as specified by its
793 configuration value, a @code{login-configuration} object.")))
795 (define* (login-service #:optional (config (login-configuration)))
796 "Return a service configure login according to @var{config}, which specifies
797 the message of the day, among other things."
798 (service login-service-type config))
800 (define-record-type* <agetty-configuration>
801 agetty-configuration make-agetty-configuration
802 agetty-configuration?
803 (agetty agetty-configuration-agetty ;file-like
804 (default util-linux))
805 (tty agetty-configuration-tty) ;string | #f
806 (term agetty-term ;string | #f
808 (baud-rate agetty-baud-rate ;string | #f
810 (auto-login agetty-auto-login ;list of strings | #f
812 (login-program agetty-login-program ;gexp
813 (default (file-append shadow "/bin/login")))
814 (login-pause? agetty-login-pause? ;Boolean
816 (eight-bits? agetty-eight-bits? ;Boolean
818 (no-reset? agetty-no-reset? ;Boolean
820 (remote? agetty-remote? ;Boolean
822 (flow-control? agetty-flow-control? ;Boolean
824 (host agetty-host ;string | #f
826 (no-issue? agetty-no-issue? ;Boolean
828 (init-string agetty-init-string ;string | #f
830 (no-clear? agetty-no-clear? ;Boolean
832 (local-line agetty-local-line ;always | never | auto
834 (extract-baud? agetty-extract-baud? ;Boolean
836 (skip-login? agetty-skip-login? ;Boolean
838 (no-newline? agetty-no-newline? ;Boolean
840 (login-options agetty-login-options ;string | #f
842 (chroot agetty-chroot ;string | #f
844 (hangup? agetty-hangup? ;Boolean
846 (keep-baud? agetty-keep-baud? ;Boolean
848 (timeout agetty-timeout ;integer | #f
850 (detect-case? agetty-detect-case? ;Boolean
852 (wait-cr? agetty-wait-cr? ;Boolean
854 (no-hints? agetty-no-hints? ;Boolean
856 (no-hostname? agetty-no hostname? ;Boolean
858 (long-hostname? agetty-long-hostname? ;Boolean
860 (erase-characters agetty-erase-characters ;string | #f
862 (kill-characters agetty-kill-characters ;string | #f
864 (chdir agetty-chdir ;string | #f
866 (delay agetty-delay ;integer | #f
868 (nice agetty-nice ;integer | #f
870 ;; "Escape hatch" for passing arbitrary command-line arguments.
871 (extra-options agetty-extra-options ;list of strings
873 ;;; XXX Unimplemented for now!
874 ;;; (issue-file agetty-issue-file ;file-like
878 (define (default-serial-port)
879 "Return a gexp that determines a reasonable default serial port
880 to use as the tty. This is primarily useful for headless systems."
881 (with-imported-modules (source-module-closure
882 '((gnu build linux-boot))) ;for 'find-long-options'
884 ;; console=device,options
885 ;; device: can be tty0, ttyS0, lp0, ttyUSB0 (serial).
886 ;; options: BBBBPNF. P n|o|e, N number of bits,
887 ;; F flow control (r RTS)
888 (let* ((not-comma (char-set-complement (char-set #\,)))
889 (command (linux-command-line))
890 (agetty-specs (find-long-options "agetty.tty" command))
891 (console-specs (filter (lambda (spec)
892 (and (string-prefix? "tty" spec)
894 (string-prefix? "tty0" spec)
895 (string-prefix? "tty1" spec)
896 (string-prefix? "tty2" spec)
897 (string-prefix? "tty3" spec)
898 (string-prefix? "tty4" spec)
899 (string-prefix? "tty5" spec)
900 (string-prefix? "tty6" spec)
901 (string-prefix? "tty7" spec)
902 (string-prefix? "tty8" spec)
903 (string-prefix? "tty9" spec)))))
904 (find-long-options "console" command)))
905 (specs (append agetty-specs console-specs)))
909 ;; Extract device name from first spec.
910 (match (string-tokenize spec not-comma)
914 (define agetty-shepherd-service
916 (($ <agetty-configuration> agetty tty term baud-rate auto-login
917 login-program login-pause? eight-bits? no-reset? remote? flow-control?
918 host no-issue? init-string no-clear? local-line extract-baud?
919 skip-login? no-newline? login-options chroot hangup? keep-baud? timeout
920 detect-case? wait-cr? no-hints? no-hostname? long-hostname?
921 erase-characters kill-characters chdir delay nice extra-options)
924 (documentation "Run agetty on a tty.")
925 (provision (list (symbol-append 'term- (string->symbol (or tty "auto")))))
927 ;; Since the login prompt shows the host name, wait for the 'host-name'
928 ;; service to be done. Also wait for udev essentially so that the tty
929 ;; text is not lost in the middle of kernel messages (see also
930 ;; mingetty-shepherd-service).
931 (requirement '(user-processes host-name udev))
933 (modules '((ice-9 match) (gnu build linux-boot)))
935 (with-imported-modules (source-module-closure
936 '((gnu build linux-boot)))
938 (let ((defaulted-tty #$(or tty (default-serial-port))))
941 (make-forkexec-constructor
942 (list #$(file-append util-linux "/sbin/agetty")
963 #~("--init-string" #$init-string)
968 ;;; FIXME This doesn't work as expected. According to agetty(8), if this option
969 ;;; is not passed, then the default is 'auto'. However, in my tests, when that
970 ;;; option is selected, agetty never presents the login prompt, and the
971 ;;; term-ttyS0 service respawns every few seconds.
973 #~(#$(match local-line
974 ('auto "--local-line=auto")
975 ('always "--local-line=always")
976 ('never "-local-line=never")))
991 #~("--login-options" #$login-options)
994 #~("--chroot" #$chroot)
1003 #~("--timeout" #$(number->string timeout))
1017 #$@(if long-hostname?
1018 #~("--long-hostname")
1020 #$@(if erase-characters
1021 #~("--erase-chars" #$erase-characters)
1023 #$@(if kill-characters
1024 #~("--kill-chars" #$kill-characters)
1027 #~("--chdir" #$chdir)
1030 #~("--delay" #$(number->string delay))
1033 #~("--nice" #$(number->string nice))
1036 (list "--autologin" auto-login)
1038 #$@(if login-program
1039 #~("--login-program" #$login-program)
1051 (const #f)) ; never start.
1053 (stop #~(make-kill-destructor)))))))
1055 (define agetty-service-type
1056 (service-type (name 'agetty)
1057 (extensions (list (service-extension shepherd-root-service-type
1058 agetty-shepherd-service)))
1060 "Provide console login using the @command{agetty}
1063 (define* (agetty-service config)
1064 "Return a service to run agetty according to @var{config}, which specifies
1065 the tty to run, among other things."
1066 (service agetty-service-type config))
1068 (define-record-type* <mingetty-configuration>
1069 mingetty-configuration make-mingetty-configuration
1070 mingetty-configuration?
1071 (mingetty mingetty-configuration-mingetty ;file-like
1073 (tty mingetty-configuration-tty) ;string
1074 (auto-login mingetty-auto-login ;string | #f
1076 (login-program mingetty-login-program ;gexp
1078 (login-pause? mingetty-login-pause? ;Boolean
1080 (clear-on-logout? mingetty-clear-on-logout? ;Boolean
1083 (define mingetty-shepherd-service
1085 (($ <mingetty-configuration> mingetty tty auto-login login-program
1086 login-pause? clear-on-logout?)
1089 (documentation "Run mingetty on an tty.")
1090 (provision (list (symbol-append 'term- (string->symbol tty))))
1092 ;; Since the login prompt shows the host name, wait for the 'host-name'
1093 ;; service to be done. Also wait for udev essentially so that the tty
1094 ;; text is not lost in the middle of kernel messages (XXX).
1095 (requirement '(user-processes host-name udev virtual-terminal))
1097 (start #~(make-forkexec-constructor
1098 (list #$(file-append mingetty "/sbin/mingetty")
1100 ;; Avoiding 'vhangup' allows us to avoid 'setfont'
1101 ;; errors down the path where various ioctls get
1102 ;; EIO--see 'hung_up_tty_ioctl' in driver/tty/tty_io.c
1106 #$@(if clear-on-logout?
1110 #~("--autologin" #$auto-login)
1112 #$@(if login-program
1113 #~("--loginprog" #$login-program)
1118 (stop #~(make-kill-destructor)))))))
1120 (define mingetty-service-type
1121 (service-type (name 'mingetty)
1122 (extensions (list (service-extension shepherd-root-service-type
1123 mingetty-shepherd-service)))
1125 "Provide console login using the @command{mingetty}
1128 (define* (mingetty-service config)
1129 "Return a service to run mingetty according to @var{config}, which specifies
1130 the tty to run, among other things."
1131 (service mingetty-service-type config))
1133 (define-record-type* <nscd-configuration> nscd-configuration
1134 make-nscd-configuration
1136 (log-file nscd-configuration-log-file ;string
1137 (default "/var/log/nscd.log"))
1138 (debug-level nscd-debug-level ;integer
1140 ;; TODO: See nscd.conf in glibc for other options to add.
1141 (caches nscd-configuration-caches ;list of <nscd-cache>
1142 (default %nscd-default-caches))
1143 (name-services nscd-configuration-name-services ;list of file-like
1145 (glibc nscd-configuration-glibc ;file-like
1148 (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
1150 (database nscd-cache-database) ;symbol
1151 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
1152 (negative-time-to-live nscd-cache-negative-time-to-live
1153 (default 20)) ;integer
1154 (suggested-size nscd-cache-suggested-size ;integer ("default module
1157 (check-files? nscd-cache-check-files? ;Boolean
1159 (persistent? nscd-cache-persistent? ;Boolean
1161 (shared? nscd-cache-shared? ;Boolean
1163 (max-database-size nscd-cache-max-database-size ;integer
1164 (default (* 32 (expt 2 20))))
1165 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
1168 (define %nscd-default-caches
1169 ;; Caches that we want to enable by default. Note that when providing an
1170 ;; empty nscd.conf, all caches are disabled.
1171 (list (nscd-cache (database 'hosts)
1173 ;; Aggressively cache the host name cache to improve
1174 ;; privacy and resilience.
1175 (positive-time-to-live (* 3600 12))
1176 (negative-time-to-live 20)
1179 (nscd-cache (database 'services)
1181 ;; Services are unlikely to change, so we can be even more
1183 (positive-time-to-live (* 3600 24))
1184 (negative-time-to-live 3600)
1185 (check-files? #t) ;check /etc/services changes
1188 (define %nscd-default-configuration
1189 ;; Default nscd configuration.
1190 (nscd-configuration))
1192 (define (nscd.conf-file config)
1193 "Return the @file{nscd.conf} configuration file for @var{config}, an
1194 @code{<nscd-configuration>} object."
1195 (define cache->config
1197 (($ <nscd-cache> (= symbol->string database)
1198 positive-ttl negative-ttl size check-files?
1199 persistent? shared? max-size propagate?)
1200 (string-append "\nenable-cache\t" database "\tyes\n"
1202 "positive-time-to-live\t" database "\t"
1203 (number->string positive-ttl) "\n"
1204 "negative-time-to-live\t" database "\t"
1205 (number->string negative-ttl) "\n"
1206 "suggested-size\t" database "\t"
1207 (number->string size) "\n"
1208 "check-files\t" database "\t"
1209 (if check-files? "yes\n" "no\n")
1210 "persistent\t" database "\t"
1211 (if persistent? "yes\n" "no\n")
1212 "shared\t" database "\t"
1213 (if shared? "yes\n" "no\n")
1214 "max-db-size\t" database "\t"
1215 (number->string max-size) "\n"
1216 "auto-propagate\t" database "\t"
1217 (if propagate? "yes\n" "no\n")))))
1220 (($ <nscd-configuration> log-file debug-level caches)
1221 (plain-file "nscd.conf"
1223 # Configuration of libc's name service cache daemon (nscd).\n\n"
1225 (string-append "logfile\t" log-file)
1229 (string-append "debug-level\t"
1230 (number->string debug-level))
1234 (map cache->config caches)))))))
1236 (define (nscd-action-procedure nscd config option)
1237 ;; XXX: This is duplicated from mcron; factorize.
1238 #~(lambda (_ . args)
1239 ;; Run 'nscd' in a pipe so we can explicitly redirect its output to
1240 ;; 'current-output-port', which at this stage is bound to the client
1242 (let ((pipe (apply open-pipe* OPEN_READ #$nscd
1243 "-f" #$config #$option args)))
1245 (match (read-line pipe 'concat)
1247 (catch 'system-error
1249 (zero? (close-pipe pipe)))
1251 ;; There's a race with the SIGCHLD handler, which could
1252 ;; call 'waitpid' before 'close-pipe' above does. If we
1253 ;; get ECHILD, that means we lost the race; in that case, we
1254 ;; cannot tell what the exit code was (FIXME).
1255 (or (= ECHILD (system-error-errno args))
1256 (apply throw args)))))
1261 (define (nscd-actions nscd config)
1262 "Return Shepherd actions for NSCD."
1263 ;; Make this functionality available as actions because that's a simple way
1264 ;; to run the right 'nscd' binary with the right config file.
1265 (list (shepherd-action
1267 (documentation "Display statistics about nscd usage.")
1268 (procedure (nscd-action-procedure nscd config "--statistics")))
1272 "Invalidate the given cache--e.g., 'hosts' for host name lookups.")
1273 (procedure (nscd-action-procedure nscd config "--invalidate")))))
1275 (define (nscd-shepherd-service config)
1276 "Return a shepherd service for CONFIG, an <nscd-configuration> object."
1277 (let ((nscd (file-append (nscd-configuration-glibc config)
1279 (nscd.conf (nscd.conf-file config))
1280 (name-services (nscd-configuration-name-services config)))
1281 (list (shepherd-service
1282 (documentation "Run libc's name service cache daemon (nscd).")
1284 (requirement '(user-processes))
1285 (start #~(make-forkexec-constructor
1286 (list #$nscd "-f" #$nscd.conf "--foreground")
1288 ;; Wait for the PID file. However, the PID file is
1289 ;; written before nscd is actually listening on its
1291 #:pid-file "/var/run/nscd/nscd.pid"
1293 #:environment-variables
1294 (list (string-append "LD_LIBRARY_PATH="
1297 (string-append dir "/lib"))
1298 (list #$@name-services))
1300 (stop #~(make-kill-destructor))
1301 (modules `((ice-9 popen) ;for the actions
1304 ,@%default-modules))
1305 (actions (nscd-actions nscd nscd.conf))))))
1307 (define nscd-activation
1308 ;; Actions to take before starting nscd.
1310 (use-modules (guix build utils))
1311 (mkdir-p "/var/run/nscd")
1312 (mkdir-p "/var/db/nscd") ;for the persistent cache
1314 ;; In libc 2.25 nscd uses inotify to watch /etc/resolv.conf, but only if
1315 ;; that file exists when it is started. Thus create it here. Note: on
1316 ;; some systems, such as when NetworkManager is used, /etc/resolv.conf
1317 ;; is a symlink, hence 'lstat'.
1318 (unless (false-if-exception (lstat "/etc/resolv.conf"))
1319 (call-with-output-file "/etc/resolv.conf"
1321 (display "# This is a placeholder.\n" port))))))
1323 (define nscd-service-type
1324 (service-type (name 'nscd)
1326 (list (service-extension activation-service-type
1327 (const nscd-activation))
1328 (service-extension shepherd-root-service-type
1329 nscd-shepherd-service)))
1331 ;; This can be extended by providing additional name services
1332 ;; such as nss-mdns.
1333 (compose concatenate)
1334 (extend (lambda (config name-services)
1337 (name-services (append
1338 (nscd-configuration-name-services config)
1340 (default-value %nscd-default-configuration)
1342 "Runs libc's @dfn{name service cache daemon} (nscd) with the
1343 given configuration---an @code{<nscd-configuration>} object. @xref{Name
1344 Service Switch}, for an example.")))
1346 (define* (nscd-service #:optional (config %nscd-default-configuration))
1347 "Return a service that runs libc's name service cache daemon (nscd) with the
1348 given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
1349 Service Switch}, for an example."
1350 (service nscd-service-type config))
1353 (define-record-type* <syslog-configuration>
1354 syslog-configuration make-syslog-configuration
1355 syslog-configuration?
1356 (syslogd syslog-configuration-syslogd
1357 (default (file-append inetutils "/libexec/syslogd")))
1358 (config-file syslog-configuration-config-file
1359 (default %default-syslog.conf)))
1361 (define syslog-service-type
1362 (shepherd-service-type
1366 (documentation "Run the syslog daemon (syslogd).")
1367 (provision '(syslogd))
1368 (requirement '(user-processes))
1369 (start #~(let ((spawn (make-forkexec-constructor
1370 (list #$(syslog-configuration-syslogd config)
1372 #$(syslog-configuration-config-file config))
1373 #:pid-file "/var/run/syslog.pid")))
1375 ;; Set the umask such that file permissions are #o640.
1376 (let ((mask (umask #o137))
1380 (stop #~(make-kill-destructor))))
1381 (description "Run the syslog daemon, @command{syslogd}, which is
1382 responsible for logging system messages.")))
1384 ;; Snippet adapted from the GNU inetutils manual.
1385 (define %default-syslog.conf
1386 (plain-file "syslog.conf" "
1387 # Log all error messages, authentication messages of
1388 # level notice or higher and anything of level err or
1389 # higher to the console.
1390 # Don't log private authentication messages!
1391 *.alert;auth.notice;authpriv.none /dev/console
1393 # Log anything (except mail) of level info or higher.
1394 # Don't log private authentication messages!
1395 *.info;mail.none;authpriv.none /var/log/messages
1397 # Like /var/log/messages, but also including \"debug\"-level logs.
1398 *.debug;mail.none;authpriv.none /var/log/debug
1400 # Same, in a different place.
1401 *.info;mail.none;authpriv.none /dev/tty12
1403 # The authpriv file has restricted access.
1404 authpriv.* /var/log/secure
1406 # Log all the mail messages in one place.
1407 mail.* /var/log/maillog
1410 (define* (syslog-service #:optional (config (syslog-configuration)))
1411 "Return a service that runs @command{syslogd} and takes
1412 @var{<syslog-configuration>} as a parameter.
1414 @xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
1415 information on the configuration file syntax."
1416 (service syslog-service-type config))
1419 (define pam-limits-service-type
1420 (let ((security-limits
1421 ;; Create /etc/security containing the provided "limits.conf" file.
1422 (lambda (limits-file)
1423 `(("security/limits.conf"
1427 (let ((pam-limits (pam-entry
1428 (control "required")
1429 (module "pam_limits.so")
1430 (arguments '("conf=/etc/security/limits.conf")))))
1431 (if (member (pam-service-name pam)
1432 '("login" "su" "slim" "gdm-password" "sddm"))
1435 (session (cons pam-limits
1436 (pam-service-session pam))))
1441 (list (service-extension etc-service-type security-limits)
1442 (service-extension pam-root-service-type
1443 (lambda _ (list pam-extension)))))
1445 "Install the specified resource usage limits by populating
1446 @file{/etc/security/limits.conf} and using the @code{pam_limits}
1447 authentication module."))))
1449 (define* (pam-limits-service #:optional (limits '()))
1450 "Return a service that makes selected programs respect the list of
1451 pam-limits-entry specified in LIMITS via pam_limits.so."
1452 (service pam-limits-service-type
1453 (plain-file "limits.conf"
1454 (string-join (map pam-limits-entry->string limits)
1462 (define* (guix-build-accounts count #:key
1465 "Return a list of COUNT user accounts for Guix build users with the given
1467 (unfold (cut > <> count)
1470 (name (format #f "guixbuilder~2,'0d" n))
1474 ;; guix-daemon expects GROUP to be listed as a
1475 ;; supplementary group too:
1476 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
1477 (supplementary-groups (list group "kvm"))
1479 (comment (format #f "Guix Build User ~2d" n))
1480 (home-directory "/var/empty")
1481 (shell (file-append shadow "/sbin/nologin"))))
1486 ;; Select (guix …) and (gnu …) modules, except (guix config).
1488 (('guix 'config) #f)
1489 (('guix rest ...) #t)
1490 (('gnu rest ...) #t)
1493 (define (substitute-key-authorization keys guix)
1494 "Return a gexp with code to register KEYS, a list of files containing 'guix
1495 archive' public keys, with GUIX."
1497 (with-extensions (list guile-gcrypt)
1498 (with-imported-modules `(((guix config) => ,(make-config.scm))
1499 ,@(source-module-closure '((guix pki))
1500 #:select? not-config?))
1501 (computed-file "acl"
1503 (use-modules (guix pki)
1509 (call-with-input-file file
1510 (compose string->canonical-sexp
1514 (call-with-output-file #$output
1516 (write-acl (public-keys->acl keys)
1519 (with-imported-modules '((guix build utils))
1521 (use-modules (guix build utils))
1523 ;; If the ACL already exists, move it out of the way. Create a backup
1524 ;; if it's a regular file: it's likely that the user manually updated
1525 ;; it with 'guix archive --authorize'.
1526 (if (file-exists? "/etc/guix/acl")
1527 (if (and (symbolic-link? "/etc/guix/acl")
1528 (store-file-name? (readlink "/etc/guix/acl")))
1529 (delete-file "/etc/guix/acl")
1530 (rename-file "/etc/guix/acl" "/etc/guix/acl.bak"))
1531 (mkdir-p "/etc/guix"))
1533 ;; Installed the declared ACL.
1534 (symlink #+default-acl "/etc/guix/acl"))))
1536 (define %default-authorized-guix-keys
1537 ;; List of authorized substitute keys.
1538 (list (file-append guix "/share/guix/berlin.guix.gnu.org.pub")
1539 (file-append guix "/share/guix/bordeaux.guix.gnu.org.pub")))
1541 (define-record-type* <guix-configuration>
1542 guix-configuration make-guix-configuration
1544 (guix guix-configuration-guix ;file-like
1546 (build-group guix-configuration-build-group ;string
1547 (default "guixbuild"))
1548 (build-accounts guix-configuration-build-accounts ;integer
1550 (authorize-key? guix-configuration-authorize-key? ;Boolean
1552 (authorized-keys guix-configuration-authorized-keys ;list of gexps
1553 (default %default-authorized-guix-keys))
1554 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
1556 (substitute-urls guix-configuration-substitute-urls ;list of strings
1557 (default %default-substitute-urls))
1558 (chroot-directories guix-configuration-chroot-directories ;list of file-like/strings
1560 (max-silent-time guix-configuration-max-silent-time ;integer
1562 (timeout guix-configuration-timeout ;integer
1564 (log-compression guix-configuration-log-compression
1566 (discover? guix-configuration-discover?
1568 (extra-options guix-configuration-extra-options ;list of strings
1570 (log-file guix-configuration-log-file ;string
1571 (default "/var/log/guix-daemon.log"))
1572 (http-proxy guix-http-proxy ;string | #f
1574 (tmpdir guix-tmpdir ;string | #f
1577 (define %default-guix-configuration
1578 (guix-configuration))
1580 (define shepherd-set-http-proxy-action
1581 ;; Shepherd action to change the HTTP(S) proxy.
1583 (name 'set-http-proxy)
1585 "Change the HTTP(S) proxy used by 'guix-daemon' and restart it.")
1586 (procedure #~(lambda* (_ #:optional proxy)
1587 (let ((environment (environ)))
1588 ;; A bit of a hack: communicate PROXY to the 'start'
1589 ;; method via environment variables.
1592 (format #t "changing HTTP/HTTPS \
1593 proxy of 'guix-daemon' to ~s...~%"
1595 (setenv "http_proxy" proxy))
1597 (format #t "clearing HTTP/HTTPS \
1598 proxy of 'guix-daemon'...~%")
1599 (unsetenv "http_proxy")))
1600 (action 'guix-daemon 'restart)
1601 (environ environment)
1604 (define shepherd-discover-action
1605 ;; Shepherd action to enable or disable substitute servers discovery.
1609 "Enable or disable substitute servers discovery and restart the
1611 (procedure #~(lambda* (_ status)
1612 (let ((environment (environ)))
1614 (string=? status "on"))
1616 (format #t "enable substitute servers discovery~%")
1617 (setenv "discover" "on"))
1619 (format #t "disable substitute servers discovery~%")
1620 (unsetenv "discover")))
1621 (action 'guix-daemon 'restart)
1622 (environ environment)
1625 (define (guix-shepherd-service config)
1626 "Return a <shepherd-service> for the Guix daemon service with CONFIG."
1627 (match-record config <guix-configuration>
1628 (guix build-group build-accounts authorize-key? authorized-keys
1629 use-substitutes? substitute-urls max-silent-time timeout
1630 log-compression discover? extra-options log-file
1631 http-proxy tmpdir chroot-directories)
1632 (list (shepherd-service
1633 (documentation "Run the Guix daemon.")
1634 (provision '(guix-daemon))
1635 (requirement '(user-processes))
1636 (actions (list shepherd-set-http-proxy-action
1637 shepherd-discover-action))
1638 (modules '((srfi srfi-1)
1640 (gnu build shepherd)))
1642 (with-imported-modules `(((guix config) => ,(make-config.scm))
1643 ,@(source-module-closure
1644 '((gnu build shepherd))
1645 #:select? not-config?))
1648 ;; HTTP/HTTPS proxy. The 'http_proxy' variable is set by
1649 ;; the 'set-http-proxy' action.
1650 (or (getenv "http_proxy") #$http-proxy))
1653 (or (getenv "discover") #$discover?))
1655 ;; Start the guix-daemon from a container, when supported,
1656 ;; to solve an installation issue. See the comment below for
1658 (fork+exec-command/container
1659 (cons* #$(file-append guix "/bin/guix-daemon")
1660 "--build-users-group" #$build-group
1662 #$(number->string max-silent-time)
1663 "--timeout" #$(number->string timeout)
1665 #$(symbol->string log-compression)
1666 #$@(if use-substitutes?
1668 '("--no-substitutes"))
1669 (string-append "--discover="
1670 (if discover? "yes" "no"))
1671 "--substitute-urls" #$(string-join substitute-urls)
1674 ;; Add CHROOT-DIRECTORIES and all their dependencies
1675 ;; (if these are store items) to the chroot.
1678 (append-map (lambda (directory)
1679 (list "--chroot-directory"
1681 (call-with-input-file file
1683 '#$(map references-file
1684 chroot-directories)))
1686 ;; When running the installer, we need guix-daemon to
1687 ;; operate from within the same MNT namespace as the
1688 ;; installation container. In that case only, enter the
1689 ;; namespace of the process PID passed as start argument.
1690 ;; Otherwise, for symmetry purposes enter the caller
1691 ;; namespaces which is a no-op.
1693 ((pid) (string->number pid))
1696 #:environment-variables
1697 (append (list #$@(if tmpdir
1698 (list (string-append "TMPDIR=" tmpdir))
1701 ;; Make sure we run in a UTF-8 locale so that
1702 ;; 'guix offload' correctly restores nars
1703 ;; that contain UTF-8 file names such as
1705 ;; <https://bugs.gnu.org/32942>.
1706 (string-append "GUIX_LOCPATH="
1707 #$glibc-utf8-locales
1709 "LC_ALL=en_US.utf8")
1711 (list (string-append "http_proxy=" proxy)
1712 (string-append "https_proxy=" proxy))
1715 #:log-file #$log-file))))
1716 (stop #~(make-kill-destructor))))))
1718 (define (guix-accounts config)
1719 "Return the user accounts and user groups for CONFIG."
1721 (($ <guix-configuration> _ build-group build-accounts)
1726 ;; Use a fixed GID so that we can create the store with the right
1729 (guix-build-accounts build-accounts
1730 #:group build-group)))))
1732 (define (guix-activation config)
1733 "Return the activation gexp for CONFIG."
1734 (match-record config <guix-configuration>
1735 (guix authorize-key? authorized-keys)
1737 ;; Assume that the store has BUILD-GROUP as its group. We could
1738 ;; otherwise call 'chown' here, but the problem is that on a COW overlayfs,
1739 ;; chown leads to an entire copy of the tree, which is a bad idea.
1741 ;; Generate a key pair and optionally authorize substitute server keys.
1742 (unless (file-exists? "/etc/guix/signing-key.pub")
1743 (system* #$(file-append guix "/bin/guix") "archive"
1746 #$(if authorize-key?
1747 (substitute-key-authorization authorized-keys guix)
1750 (define* (references-file item #:optional (name "references"))
1751 "Return a file that contains the list of references of ITEM."
1752 (if (struct? item) ;lowerable object
1754 (with-extensions (list guile-gcrypt) ;for store-copy
1755 (with-imported-modules (source-module-closure
1756 '((guix build store-copy)))
1758 (use-modules (guix build store-copy))
1760 (call-with-output-file #$output
1762 (write (map store-info-item
1763 (call-with-input-file "graph"
1764 read-reference-graph))
1766 #:options `(#:local-build? #f
1767 #:references-graphs (("graph" ,item))))
1768 (plain-file name "()")))
1770 (define guix-service-type
1774 (list (service-extension shepherd-root-service-type guix-shepherd-service)
1775 (service-extension account-service-type guix-accounts)
1776 (service-extension activation-service-type guix-activation)
1777 (service-extension profile-service-type
1778 (compose list guix-configuration-guix))))
1780 ;; Extensions can specify extra directories to add to the build chroot.
1781 (compose concatenate)
1782 (extend (lambda (config directories)
1786 (append (guix-configuration-chroot-directories config)
1789 (default-value (guix-configuration))
1791 "Run the build daemon of GNU@tie{}Guix, aka. @command{guix-daemon}.")))
1794 (define-record-type* <guix-publish-configuration>
1795 guix-publish-configuration make-guix-publish-configuration
1796 guix-publish-configuration?
1797 (guix guix-publish-configuration-guix ;file-like
1799 (port guix-publish-configuration-port ;number
1801 (host guix-publish-configuration-host ;string
1802 (default "localhost"))
1803 (advertise? guix-publish-advertise? ;boolean
1805 (compression guix-publish-configuration-compression
1807 (default (default-compression this-record
1808 (current-source-location))))
1809 (compression-level %guix-publish-configuration-compression-level ;deprecated
1811 (nar-path guix-publish-configuration-nar-path ;string
1813 (cache guix-publish-configuration-cache ;#f | string
1815 (cache-bypass-threshold guix-publish-configuration-cache-bypass-threshold
1816 (default (* 10 (expt 2 20)))) ;integer
1817 (workers guix-publish-configuration-workers ;#f | integer
1819 (ttl guix-publish-configuration-ttl ;#f | integer
1822 (define-deprecated (guix-publish-configuration-compression-level config)
1823 "Return a compression level, the old way."
1824 (match (guix-publish-configuration-compression config)
1825 (((_ level) _ ...) level)))
1827 (define (default-compression config properties)
1828 "Return the default 'guix publish' compression according to CONFIG, and
1829 raise a deprecation warning if the 'compression-level' field was used."
1830 (match (%guix-publish-configuration-compression-level config)
1832 ;; Default to low compression levels when there's no cache so that users
1833 ;; get good bandwidth by default.
1834 (if (guix-publish-configuration-cache config)
1835 '(("gzip" 5) ("zstd" 19))
1836 '(("gzip" 3) ("zstd" 3)))) ;zstd compresses faster
1838 (warn-about-deprecation 'compression-level properties
1839 #:replacement 'compression)
1840 `(("gzip" ,level)))))
1842 (define (guix-publish-shepherd-service config)
1843 (define (config->compression-options config)
1844 (match (guix-publish-configuration-compression config)
1845 (() ;empty list means "no compression"
1848 (append-map (match-lambda
1850 `("-C" ,(string-append type ":"
1851 (number->string level)))))
1854 (match-record config <guix-publish-configuration>
1855 (guix port host nar-path cache workers ttl cache-bypass-threshold
1857 (list (shepherd-service
1858 (provision '(guix-publish))
1859 (requirement `(user-processes
1861 ,@(if advertise? '(avahi-daemon) '())))
1862 (start #~(make-forkexec-constructor
1863 (list #$(file-append guix "/bin/guix")
1864 "publish" "-u" "guix-publish"
1865 "-p" #$(number->string port)
1866 #$@(config->compression-options config)
1867 (string-append "--nar-path=" #$nar-path)
1868 (string-append "--listen=" #$host)
1873 #~((string-append "--workers="
1878 #~((string-append "--ttl="
1879 #$(number->string ttl)
1883 #~((string-append "--cache=" #$cache)
1885 "--cache-bypass-threshold="
1887 cache-bypass-threshold)))
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 #:environment-variables
1894 (list (string-append "GUIX_LOCPATH="
1895 #$glibc-utf8-locales "/lib/locale")
1896 "LC_ALL=en_US.utf8")
1897 #:log-file "/var/log/guix-publish.log"))
1898 (stop #~(make-kill-destructor))))))
1900 (define %guix-publish-accounts
1901 (list (user-group (name "guix-publish") (system? #t))
1903 (name "guix-publish")
1904 (group "guix-publish")
1906 (comment "guix publish user")
1907 (home-directory "/var/empty")
1908 (shell (file-append shadow "/sbin/nologin")))))
1910 (define %guix-publish-log-rotations
1912 (files (list "/var/log/guix-publish.log")))))
1914 (define (guix-publish-activation config)
1915 (let ((cache (guix-publish-configuration-cache config)))
1917 (with-imported-modules '((guix build utils))
1919 (use-modules (guix build utils))
1922 (let* ((pw (getpw "guix-publish"))
1923 (uid (passwd:uid pw))
1924 (gid (passwd:gid pw)))
1925 (chown #$cache uid gid))))
1928 (define guix-publish-service-type
1929 (service-type (name 'guix-publish)
1931 (list (service-extension shepherd-root-service-type
1932 guix-publish-shepherd-service)
1933 (service-extension account-service-type
1934 (const %guix-publish-accounts))
1935 (service-extension rottlog-service-type
1936 (const %guix-publish-log-rotations))
1937 (service-extension activation-service-type
1938 guix-publish-activation)))
1939 (default-value (guix-publish-configuration))
1941 "Add a Shepherd service running @command{guix publish}, a
1942 command that allows you to share pre-built binaries with others over HTTP.")))
1949 (define-record-type* <udev-configuration>
1950 udev-configuration make-udev-configuration
1952 (udev udev-configuration-udev ;file-like
1954 (rules udev-configuration-rules ;list of file-like
1957 (define (udev-rules-union packages)
1958 "Return the union of the @code{lib/udev/rules.d} directories found in each
1959 item of @var{packages}."
1961 (with-imported-modules '((guix build union)
1964 (use-modules (guix build union)
1969 (define %standard-locations
1970 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
1972 (define (rules-sub-directory directory)
1973 ;; Return the sub-directory of DIRECTORY containing udev rules, or
1974 ;; #f if none was found.
1975 (find directory-exists?
1976 (map (cut string-append directory <>) %standard-locations)))
1978 (mkdir-p (string-append #$output "/lib/udev"))
1979 (union-build (string-append #$output "/lib/udev/rules.d")
1980 (filter-map rules-sub-directory '#$packages)))))
1982 (computed-file "udev-rules" build))
1984 (define (udev-rule file-name contents)
1985 "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
1986 (computed-file file-name
1987 (with-imported-modules '((guix build utils))
1989 (use-modules (guix build utils))
1992 (string-append #$output "/lib/udev/rules.d"))
1995 (call-with-output-file
1996 (string-append rules.d "/" #$file-name)
1998 (display #$contents port)))))))
2000 (define (file->udev-rule file-name file)
2001 "Return a directory with a udev rule file FILE-NAME which is a copy of FILE."
2002 (computed-file file-name
2003 (with-imported-modules '((guix build utils))
2005 (use-modules (guix build utils))
2008 (string-append #$output "/lib/udev/rules.d"))
2010 (define file-copy-dest
2011 (string-append rules.d "/" #$file-name))
2014 (copy-file #$file file-copy-dest)))))
2016 (define kvm-udev-rule
2017 ;; Return a directory with a udev rule that changes the group of /dev/kvm to
2018 ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
2019 ;; but now we have to add it by ourselves.
2021 ;; Build users are part of the "kvm" group, so we can fearlessly make
2022 ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
2023 (udev-rule "90-kvm.rules"
2024 "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
2026 (define udev-shepherd-service
2027 ;; Return a <shepherd-service> for UDEV with RULES.
2029 (($ <udev-configuration> udev rules)
2030 (let* ((rules (udev-rules-union (cons* udev kvm-udev-rule rules)))
2031 (udev.conf (computed-file "udev.conf"
2032 #~(call-with-output-file #$output
2035 "udev_rules=\"~a/lib/udev/rules.d\"\n"
2041 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
2043 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
2044 (requirement '(root-file-system))
2046 (documentation "Populate the /dev directory, dynamically.")
2048 (with-imported-modules (source-module-closure
2049 '((gnu build linux-boot)))
2052 ;; 'udevd' from eudev.
2053 #$(file-append udev "/sbin/udevd"))
2055 (define (wait-for-udevd)
2056 ;; Wait until someone's listening on udevd's control
2058 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
2060 (catch 'system-error
2062 (connect sock PF_UNIX "/run/udev/control")
2065 (format #t "waiting for udevd...~%")
2069 ;; Allow udev to find the modules.
2070 (setenv "LINUX_MODULE_DIRECTORY"
2071 "/run/booted-system/kernel/lib/modules")
2073 (let* ((kernel-release
2074 (utsname:release (uname)))
2075 (linux-module-directory
2076 (getenv "LINUX_MODULE_DIRECTORY"))
2078 (string-append linux-module-directory "/"
2080 (old-umask (umask #o022)))
2081 ;; If we're in a container, DIRECTORY might not exist,
2082 ;; for instance because the host runs a different
2083 ;; kernel. In that case, skip it; we'll just miss a few
2084 ;; nodes like /dev/fuse.
2085 (when (file-exists? directory)
2086 (make-static-device-nodes directory))
2089 (let ((pid (fork+exec-command (list udevd)
2090 #:environment-variables
2092 ;; The first one is for udev, the second one for
2094 (string-append "UDEV_CONFIG_FILE=" #$udev.conf)
2095 (string-append "EUDEV_RULES_DIRECTORY="
2097 rules "/lib/udev/rules.d"))
2098 (string-append "LINUX_MODULE_DIRECTORY="
2099 (getenv "LINUX_MODULE_DIRECTORY"))
2100 (default-environment-variables)))))
2101 ;; Wait until udevd is up and running. This appears to
2102 ;; be needed so that the events triggered below are
2103 ;; actually handled.
2106 ;; Trigger device node creation.
2107 (system* #$(file-append udev "/bin/udevadm")
2108 "trigger" "--action=add")
2110 ;; Wait for things to settle down.
2111 (system* #$(file-append udev "/bin/udevadm")
2114 (stop #~(make-kill-destructor))
2116 ;; When halting the system, 'udev' is actually killed by
2117 ;; 'user-processes', i.e., before its own 'stop' method was called.
2118 ;; Thus, make sure it is not respawned.
2120 ;; We need additional modules.
2121 (modules `((gnu build linux-boot) ;'make-static-device-nodes'
2122 ,@%default-modules))
2124 (actions (list (shepherd-action
2126 (documentation "Display the directory containing
2127 the udev rules in use.")
2128 (procedure #~(lambda (_)
2130 (newline))))))))))))
2132 (define udev-service-type
2133 (service-type (name 'udev)
2135 (list (service-extension shepherd-root-service-type
2136 udev-shepherd-service)))
2138 (compose concatenate) ;concatenate the list of rules
2139 (extend (lambda (config rules)
2141 (($ <udev-configuration> udev initial-rules)
2144 (rules (append initial-rules rules)))))))
2145 (default-value (udev-configuration))
2147 "Run @command{udev}, which populates the @file{/dev}
2148 directory dynamically. Get extra rules from the packages listed in the
2149 @code{rules} field of its value, @code{udev-configuration} object.")))
2151 (define* (udev-service #:key (udev eudev) (rules '()))
2152 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
2153 extra rules from the packages listed in @var{rules}."
2154 (service udev-service-type
2155 (udev-configuration (udev udev) (rules rules))))
2157 (define* (udev-rules-service name rules #:key (groups '()))
2158 "Return a service that extends udev-service-type with RULES and
2159 account-service-type with GROUPS as system groups. This works by creating a
2160 singleton service type NAME-udev-rules, of which the returned service is an
2162 (let* ((name (symbol-append name '-udev-rules))
2164 (const (map (lambda (group)
2165 (user-group (name group) (system? #t)))
2167 (udev-extension (const (list rules)))
2172 account-service-type account-extension)
2174 udev-service-type udev-extension))))))
2177 (define (swap-space->shepherd-service-name space)
2178 (let ((target (swap-space-target space)))
2179 (symbol-append 'swap-
2181 (cond ((uuid? target)
2182 (uuid->string target))
2183 ((file-system-label? target)
2184 (file-system-label->string target))
2188 ; TODO Remove after deprecation
2189 (define (swap-deprecated->shepherd-service-name sdep)
2190 (symbol-append 'swap-
2193 (string-take (uuid->string sdep) 6))
2194 ((file-system-label? sdep)
2195 (file-system-label->string sdep))
2199 (define swap->shepherd-service-name
2200 (match-lambda ((? swap-space? space)
2201 (swap-space->shepherd-service-name space))
2203 (swap-deprecated->shepherd-service-name sdep))))
2205 (define swap-service-type
2206 (shepherd-service-type
2209 (define requirements
2210 (cond ((swap-space? swap)
2211 (map dependency->shepherd-service-name
2212 (swap-space-dependencies swap)))
2213 ; TODO Remove after deprecation
2214 ((and (string? swap) (string-prefix? "/dev/mapper/" swap))
2215 (list (symbol-append 'device-mapping-
2216 (string->symbol (basename swap)))))
2220 (define device-lookup
2221 ;; The generic 'find-partition' procedures could return a partition
2222 ;; that's not swap space, but that's unlikely.
2223 (cond ((swap-space? swap)
2224 (let ((target (swap-space-target swap)))
2225 (cond ((uuid? target)
2226 #~(find-partition-by-uuid #$(uuid-bytevector target)))
2227 ((file-system-label? target)
2228 #~(find-partition-by-label
2229 #$(file-system-label->string target)))
2232 ; TODO Remove after deprecation
2234 #~(find-partition-by-uuid #$(uuid-bytevector swap)))
2235 ((file-system-label? swap)
2236 #~(find-partition-by-label
2237 #$(file-system-label->string swap)))
2241 (with-imported-modules (source-module-closure '((gnu build file-systems)))
2243 (provision (list (swap->shepherd-service-name swap)))
2244 (requirement `(udev ,@requirements))
2245 (documentation "Enable the given swap space.")
2246 (modules `((gnu build file-systems)
2247 ,@%default-modules))
2249 (let ((device #$device-lookup))
2252 (restart-on-EINTR (swapon device
2253 #$(if (swap-space? swap)
2254 (swap-space->flags-bit-mask
2259 (let ((device #$device-lookup))
2261 (restart-on-EINTR (swapoff device)))
2264 (description "Turn on the virtual memory swap area.")))
2266 (define (swap-service swap)
2267 "Return a service that uses @var{swap} as a swap space."
2268 (service swap-service-type swap))
2270 (define %default-gpm-options
2271 ;; Default options for GPM.
2272 '("-m" "/dev/input/mice" "-t" "ps2"))
2274 (define-record-type* <gpm-configuration>
2275 gpm-configuration make-gpm-configuration gpm-configuration?
2276 (gpm gpm-configuration-gpm ;file-like
2278 (options gpm-configuration-options ;list of strings
2279 (default %default-gpm-options)))
2281 (define gpm-shepherd-service
2283 (($ <gpm-configuration> gpm options)
2284 (list (shepherd-service
2285 (requirement '(udev))
2287 ;; 'gpm' runs in the background and sets a PID file.
2288 ;; Note that it requires running as "root".
2289 (start #~(make-forkexec-constructor
2290 (list #$(file-append gpm "/sbin/gpm")
2292 #:pid-file "/var/run/gpm.pid"
2293 #:pid-file-timeout 3))
2295 ;; Return #f if successfully stopped.
2296 (not (zero? (system* #$(file-append gpm "/sbin/gpm")
2299 (define gpm-service-type
2300 (service-type (name 'gpm)
2302 (list (service-extension shepherd-root-service-type
2303 gpm-shepherd-service)))
2304 (default-value (gpm-configuration))
2306 "Run GPM, the general-purpose mouse daemon, with the given
2307 command-line options. GPM allows users to use the mouse in the console,
2308 notably to select, copy, and paste text. The default options use the
2309 @code{ps2} protocol, which works for both USB and PS/2 mice.")))
2312 (define-record-type* <kmscon-configuration>
2313 kmscon-configuration make-kmscon-configuration
2314 kmscon-configuration?
2315 (kmscon kmscon-configuration-kmscon
2317 (virtual-terminal kmscon-configuration-virtual-terminal)
2318 (login-program kmscon-configuration-login-program
2319 (default (file-append shadow "/bin/login")))
2320 (login-arguments kmscon-configuration-login-arguments
2322 (auto-login kmscon-configuration-auto-login
2324 (hardware-acceleration? kmscon-configuration-hardware-acceleration?
2325 (default #f)) ; #t causes failure
2326 (font-engine kmscon-configuration-font-engine
2328 (font-size kmscon-configuration-font-size
2330 (keyboard-layout kmscon-configuration-keyboard-layout
2331 (default #f))) ; #f | <keyboard-layout>
2333 (define kmscon-service-type
2334 (shepherd-service-type
2337 (let ((kmscon (kmscon-configuration-kmscon config))
2338 (virtual-terminal (kmscon-configuration-virtual-terminal config))
2339 (login-program (kmscon-configuration-login-program config))
2340 (login-arguments (kmscon-configuration-login-arguments config))
2341 (auto-login (kmscon-configuration-auto-login config))
2342 (hardware-acceleration? (kmscon-configuration-hardware-acceleration? config))
2343 (font-engine (kmscon-configuration-font-engine config))
2344 (font-size (kmscon-configuration-font-size config))
2345 (keyboard-layout (kmscon-configuration-keyboard-layout config)))
2347 (define kmscon-command
2349 #$(file-append kmscon "/bin/kmscon") "--login"
2350 "--vt" #$virtual-terminal
2351 "--no-switchvt" ;Prevent a switch to the virtual terminal.
2352 "--font-engine" #$font-engine
2353 "--font-size" #$(number->string font-size)
2354 #$@(if keyboard-layout
2355 (let* ((layout (keyboard-layout-name keyboard-layout))
2356 (variant (keyboard-layout-variant keyboard-layout))
2357 (model (keyboard-layout-model keyboard-layout))
2358 (options (keyboard-layout-options keyboard-layout)))
2359 `("--xkb-layout" ,layout
2360 ,@(if variant `("--xkb-variant" ,variant) '())
2361 ,@(if model `("--xkb-model" ,model) '())
2362 ,@(if (null? options)
2364 `("--xkb-options" ,(string-join options ",")))))
2366 #$@(if hardware-acceleration? '("--hwaccel") '())
2368 #$login-program #$@login-arguments
2374 (documentation "kmscon virtual terminal")
2375 (requirement '(user-processes udev dbus-system))
2376 (provision (list (symbol-append 'term- (string->symbol virtual-terminal))))
2377 (start #~(make-forkexec-constructor #$kmscon-command))
2378 (stop #~(make-kill-destructor)))))
2379 (description "Start the @command{kmscon} virtual terminal emulator for the
2380 Linux @dfn{kernel mode setting} (KMS).")))
2384 ;;; Static networking.
2387 (define (ipv6-address? str)
2388 "Return true if STR denotes an IPv6 address."
2389 (false-if-exception (->bool (inet-pton AF_INET6 str))))
2391 (define-record-type* <static-networking>
2392 static-networking make-static-networking
2394 (addresses static-networking-addresses) ;list of <network-address>
2395 (links static-networking-links (default '())) ;list of <network-link>
2396 (routes static-networking-routes (default '())) ;list of <network-routes>
2397 (provision static-networking-provision
2398 (default '(networking)))
2399 (requirement static-networking-requirement
2401 (name-servers static-networking-name-servers ;FIXME: doesn't belong here
2404 (define-record-type* <network-address>
2405 network-address make-network-address
2407 (device network-address-device) ;string--e.g., "en01"
2408 (value network-address-value) ;string--CIDR notation
2409 (ipv6? network-address-ipv6? ;Boolean
2412 (ipv6-address? (cidr->ip (network-address-value this-record))))))
2414 (define-record-type* <network-link>
2415 network-link make-network-link
2417 (name network-link-name) ;string--e.g, "v0p0"
2418 (type network-link-type) ;symbol--e.g.,'veth
2419 (arguments network-link-arguments)) ;list
2421 (define-record-type* <network-route>
2422 network-route make-network-route
2424 (destination network-route-destination)
2425 (source network-route-source (default #f))
2426 (device network-route-device (default #f))
2427 (ipv6? network-route-ipv6? (thunked)
2429 (or (ipv6-address? (network-route-destination this-record))
2430 (and=> (network-route-gateway this-record)
2432 (gateway network-route-gateway (default #f)))
2434 (define* (cidr->netmask str #:optional (family AF_INET))
2435 "Given @var{str}, a string in CIDR notation (e.g., \"1.2.3.4/24\"), return
2436 the netmask as a string like \"255.255.255.0\"."
2437 (match (string-split str #\/)
2438 ((ip (= string->number bits))
2439 (let ((mask (ash (- (expt 2 bits) 1)
2440 (- (if (= family AF_INET6) 128 32)
2442 (inet-ntop family mask)))
2445 (define (cidr->ip str)
2446 "Strip the netmask bit of @var{str}, a CIDR-notation IP/netmask address."
2447 (match (string-split str #\/)
2451 (define* (ip+netmask->cidr ip netmask #:optional (family AF_INET))
2452 "Return the CIDR notation (a string) for @var{ip} and @var{netmask}, two
2453 @var{family} address strings, where @var{family} is @code{AF_INET} or
2455 (let* ((netmask (inet-pton family netmask))
2456 (bits (logcount netmask)))
2457 (string-append ip "/" (number->string bits))))
2459 (define (static-networking->hurd-pfinet-options config)
2460 "Return command-line options for the Hurd's pfinet translator corresponding
2462 (unless (null? (static-networking-links config))
2463 ;; XXX: Presumably this is not supported, or perhaps could be approximated
2464 ;; by running separate pfinet instances in some cases?
2465 (warning (G_ "network links are currently ignored on GNU/Hurd~%")))
2467 (match (static-networking-addresses config)
2468 ((and addresses (first _ ...))
2469 `("--ipv6" "/servers/socket/26"
2470 "--interface" ,(network-address-device first)
2471 ,@(append-map (lambda (address)
2472 `(,(if (network-address-ipv6? address)
2475 ,(cidr->ip (network-address-value address))
2476 ,@(match (cidr->netmask (network-address-value address)
2477 (if (network-address-ipv6? address)
2481 (mask (list "--netmask" mask)))))
2483 ,@(append-map (lambda (route)
2485 (($ <network-route> "default" #f device _ gateway)
2486 (if (network-route-ipv6? route)
2487 `("--gateway6" ,gateway)
2488 `("--gateway" ,gateway)))
2489 (($ <network-route> destination)
2490 (warning (G_ "ignoring network route for '~a'~%")
2493 (static-networking-routes config))))))
2495 (define (network-set-up/hurd config)
2496 "Set up networking for the Hurd."
2497 ;; The Hurd implements SIOCGIFADDR and other old-style ioctls, but the only
2498 ;; way to set up IPv6 is by starting pfinet with the right options.
2499 (if (equal? (static-networking-provision config) '(loopback))
2500 (scheme-file "set-up-pflocal" #~(begin 'nothing-to-do! #t))
2501 (scheme-file "set-up-pfinet"
2502 (with-imported-modules '((guix build utils))
2504 (use-modules (guix build utils)
2507 ;; TODO: Do that without forking.
2508 (let ((options '#$(static-networking->hurd-pfinet-options
2510 (format #t "starting '~a~{ ~s~}'~%"
2511 #$(file-append hurd "/hurd/pfinet")
2513 (apply invoke #$(file-append hurd "/bin/settrans") "-fac"
2515 #$(file-append hurd "/hurd/pfinet")
2518 (define (network-tear-down/hurd config)
2519 (scheme-file "tear-down-pfinet"
2520 (with-imported-modules '((guix build utils))
2522 (use-modules (guix build utils))
2524 ;; Forcefully terminate pfinet. XXX: In theory this
2525 ;; should just undo the addresses and routes of CONFIG;
2526 ;; this could be done using ioctls like SIOCDELRT, but
2527 ;; these are IPv4-only; another option would be to use
2528 ;; fsysopts but that seems to crash pfinet.
2529 (invoke #$(file-append hurd "/bin/settrans") "-fg"
2530 "/servers/socket/2")
2533 (define network-set-up/linux
2535 (($ <static-networking> addresses links routes)
2536 (scheme-file "set-up-network"
2537 (with-extensions (list guile-netlink)
2539 (use-modules (ip addr) (ip link) (ip route))
2541 #$@(map (lambda (address)
2543 (addr-add #$(network-address-device address)
2544 #$(network-address-value address)
2546 #$(network-address-ipv6? address))
2548 (link-set #$(network-address-device address)
2551 #$@(map (match-lambda
2552 (($ <network-link> name type arguments)
2553 #~(link-add #$name #$type
2554 #:type-args '#$arguments)))
2556 #$@(map (lambda (route)
2557 #~(route-add #$(network-route-destination route)
2559 #$(network-route-device route)
2561 #$(network-route-ipv6? route)
2563 #$(network-route-gateway route)
2565 #$(network-route-source route)))
2569 (define network-tear-down/linux
2571 (($ <static-networking> addresses links routes)
2572 (scheme-file "tear-down-network"
2573 (with-extensions (list guile-netlink)
2575 (use-modules (ip addr) (ip link) (ip route)
2579 (define-syntax-rule (false-if-netlink-error exp)
2580 (guard (c ((netlink-error? c) #f))
2583 ;; Wrap calls in 'false-if-netlink-error' so this
2584 ;; script goes as far as possible undoing the effects
2585 ;; of "set-up-network".
2587 #$@(map (lambda (route)
2588 #~(false-if-netlink-error
2589 (route-del #$(network-route-destination route)
2591 #$(network-route-device route)
2593 #$(network-route-ipv6? route)
2595 #$(network-route-gateway route)
2597 #$(network-route-source route))))
2599 #$@(map (match-lambda
2600 (($ <network-link> name type arguments)
2601 #~(false-if-netlink-error
2602 (link-del #$name))))
2604 #$@(map (lambda (address)
2605 #~(false-if-netlink-error
2606 (addr-del #$(network-address-device
2608 #$(network-address-value address)
2610 #$(network-address-ipv6? address))))
2614 (define (static-networking-shepherd-service config)
2616 (($ <static-networking> addresses links routes
2617 provision requirement name-servers)
2618 (let ((loopback? (and provision (memq 'loopback provision))))
2622 "Bring up the networking interface using a static IP address.")
2623 (requirement requirement)
2624 (provision provision)
2627 ;; Return #t if successfully started.
2628 (load #$(let-system (system target)
2629 (if (string-contains (or target system) "-linux")
2630 (network-set-up/linux config)
2631 (network-set-up/hurd config))))))
2633 ;; Return #f is successfully stopped.
2634 (load #$(let-system (system target)
2635 (if (string-contains (or target system) "-linux")
2636 (network-tear-down/linux config)
2637 (network-tear-down/hurd config))))))
2640 (define (static-networking-shepherd-services networks)
2641 (map static-networking-shepherd-service networks))
2643 (define (static-networking-etc-files interfaces)
2644 "Return a /etc/resolv.conf entry for INTERFACES or the empty list."
2645 (match (delete-duplicates
2646 (append-map static-networking-name-servers
2651 (let ((content (string-join
2652 (map (cut string-append "nameserver " <>)
2656 ,(plain-file "resolv.conf"
2658 # Generated by 'static-networking-service'.\n"
2661 (define static-networking-service-type
2662 ;; The service type for statically-defined network interfaces.
2663 (service-type (name 'static-networking)
2666 (service-extension shepherd-root-service-type
2667 static-networking-shepherd-services)
2668 (service-extension etc-service-type
2669 static-networking-etc-files)))
2670 (compose concatenate)
2673 "Turn up the specified network interfaces upon startup,
2674 with the given IP address, gateway, netmask, and so on. The value for
2675 services of this type is a list of @code{static-networking} objects, one per
2676 network interface.")))
2678 (define-deprecated (static-networking-service interface ip
2680 netmask gateway provision
2681 ;; Most interfaces require udev to be usable.
2682 (requirement '(udev))
2684 static-networking-service-type
2685 "Return a service that starts @var{interface} with address @var{ip}. If
2686 @var{netmask} is true, use it as the network mask. If @var{gateway} is true,
2687 it must be a string specifying the default network gateway.
2689 This procedure can be called several times, one for each network
2690 interface of interest. Behind the scenes what it does is extend
2691 @code{static-networking-service-type} with additional network interfaces
2693 (simple-service 'static-network-interface
2694 static-networking-service-type
2695 (list (static-networking
2697 (list (network-address
2700 (ip+netmask->cidr ip netmask)
2705 (list (network-route
2706 (destination "default")
2710 (requirement requirement)
2711 (provision (or provision '(networking)))
2712 (name-servers name-servers)))))
2714 (define %loopback-static-networking
2715 ;; The loopback device.
2717 (addresses (list (network-address
2719 (value "127.0.0.1"))))
2721 (provision '(loopback))))
2723 (define %qemu-static-networking
2724 ;; Networking configuration for QEMU's user-mode network stack (info "(QEMU)
2725 ;; Using the user mode network stack").
2727 (addresses (list (network-address
2729 (value "10.0.2.15/24"))))
2730 (routes (list (network-route
2731 (destination "default")
2732 (gateway "10.0.2.2"))))
2734 (provision '(networking))
2735 (name-servers '("10.0.2.3"))))
2738 (define %base-services
2739 ;; Convenience variable holding the basic services.
2740 (list (service login-service-type)
2742 (service virtual-terminal-service-type)
2743 (service console-font-service-type
2745 (cons tty %default-console-font))
2746 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
2748 (service agetty-service-type (agetty-configuration
2749 (extra-options '("-L")) ; no carrier detect
2751 (tty #f))) ; automatic
2753 (service mingetty-service-type (mingetty-configuration
2755 (service mingetty-service-type (mingetty-configuration
2757 (service mingetty-service-type (mingetty-configuration
2759 (service mingetty-service-type (mingetty-configuration
2761 (service mingetty-service-type (mingetty-configuration
2763 (service mingetty-service-type (mingetty-configuration
2766 (service static-networking-service-type
2767 (list %loopback-static-networking))
2769 (service urandom-seed-service-type)
2770 (service guix-service-type)
2771 (service nscd-service-type)
2773 (service rottlog-service-type)
2775 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
2776 ;; used, so enable them by default. The FUSE and ALSA rules are
2777 ;; less critical, but handy.
2778 (service udev-service-type
2780 (rules (list lvm2 fuse alsa-utils crda))))
2782 (service sysctl-service-type)
2784 (service special-files-service-type
2785 `(("/bin/sh" ,(file-append bash "/bin/sh"))
2786 ("/usr/bin/env" ,(file-append coreutils "/bin/env"))))))
2788 ;;; base.scm ends here