services: Define '%loopback-static-networking'.
[jackhill/guix/guix.git] / gnu / services / base.scm
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>
19 ;;;
20 ;;; This file is part of GNU Guix.
21 ;;;
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.
26 ;;;
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.
31 ;;;
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/>.
34
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
82 swap-service
83 host-name-service
84 %default-console-font
85 console-font-service-type
86 console-font-service
87 virtual-terminal-service-type
88
89 static-networking
90 static-networking?
91 static-networking-addresses
92 static-networking-links
93 static-networking-routes
94 static-networking-requirement
95
96 network-address
97 network-address?
98 network-address-device
99 network-address-value
100 network-address-ipv6?
101
102 network-link
103 network-link?
104 network-link-name
105 network-link-type
106 network-link-arguments
107
108 network-route
109 network-route?
110 network-route-destination
111 network-route-source
112 network-route-device
113 network-route-ipv6?
114 network-route-gateway
115
116 static-networking-service
117 static-networking-service-type
118
119 %loopback-static-networking
120 %qemu-static-networking
121
122 udev-configuration
123 udev-configuration?
124 udev-configuration-rules
125 udev-service-type
126 udev-service
127 udev-rule
128 file->udev-rule
129 udev-rules-service
130
131 login-configuration
132 login-configuration?
133 login-service-type
134 login-service
135
136 agetty-configuration
137 agetty-configuration?
138 agetty-service
139 agetty-service-type
140
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?
149 mingetty-service
150 mingetty-service-type
151
152 %nscd-default-caches
153 %nscd-default-configuration
154
155 nscd-configuration
156 nscd-configuration?
157
158 nscd-cache
159 nscd-cache?
160
161 nscd-service-type
162 nscd-service
163
164 syslog-configuration
165 syslog-configuration?
166 syslog-service
167 syslog-service-type
168 %default-syslog.conf
169
170 %default-authorized-guix-keys
171 guix-configuration
172 guix-configuration?
173
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
183
184 guix-service-type
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
196
197 gpm-configuration
198 gpm-configuration?
199 gpm-service-type
200
201 urandom-seed-service-type
202
203 rngd-configuration
204 rngd-configuration?
205 rngd-service-type
206 rngd-service
207
208 kmscon-configuration
209 kmscon-configuration?
210 kmscon-service-type
211
212 pam-limits-service-type
213 pam-limits-service
214
215 references-file
216
217 %base-services))
218
219 ;;; Commentary:
220 ;;;
221 ;;; Base system services---i.e., services that 99% of the users will want to
222 ;;; use.
223 ;;;
224 ;;; Code:
225
226
227 \f
228 ;;;
229 ;;; File systems.
230 ;;;
231
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)))
238 ((? uuid? uuid)
239 (string-append "UUID=" (uuid->string uuid)))
240 ((? string? device)
241 device))
242 "\t"
243 (file-system-mount-point file-system) "\t"
244 (file-system-type file-system) "\t"
245 (or (file-system-options file-system) "defaults") "\t"
246
247 ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
248 ;; don't have anything sensible to put in there.
249 ))
250
251 (define (file-systems->fstab file-systems)
252 "Return a @file{/etc} entry for an @file{fstab} describing
253 @var{file-systems}."
254 `(("fstab" ,(plain-file "fstab"
255 (string-append
256 "\
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
260 file-systems)
261 "\n")
262 "\n")))))
263
264 (define fstab-service-type
265 ;; The /etc/fstab service.
266 (service-type (name 'fstab)
267 (extensions
268 (list (service-extension etc-service-type
269 file-systems->fstab)))
270 (compose concatenate)
271 (extend append)
272 (description
273 "Populate the @file{/etc/fstab} based on the given file
274 system objects.")))
275
276 (define %root-file-system-shepherd-service
277 (shepherd-service
278 (documentation "Take care of the root file system.")
279 (provision '(root-file-system))
280 (start #~(const #t))
281 (stop #~(lambda _
282 ;; Return #f if successfully stopped.
283 (sync)
284
285 (call-with-blocked-asyncs
286 (lambda ()
287 (let ((null (%make-void-port "w")))
288 ;; Close 'shepherd.log'.
289 (display "closing log\n")
290 ((@ (shepherd comm) stop-logging))
291
292 ;; Redirect the default output ports..
293 (set-current-output-port null)
294 (set-current-error-port null)
295
296 ;; Close /dev/console.
297 (for-each close-fdes '(0 1 2))
298
299 ;; At this point, there are no open files left, so the
300 ;; root file system can be re-mounted read-only.
301 (mount #f "/" #f
302 (logior MS_REMOUNT MS_RDONLY)
303 #:update-mtab? #f)
304
305 #f)))))
306 (respawn? #f)))
307
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.")))
313
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.)
317
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))
321
322 (define (file-system->shepherd-service-name file-system)
323 "Return the symbol that denotes the service mounting and unmounting
324 FILE-SYSTEM."
325 (symbol-append 'file-system-
326 (string->symbol (file-system-mount-point file-system))))
327
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) "-"))))
333
334 (define dependency->shepherd-service-name
335 (match-lambda
336 ((? mapped-device? md)
337 (mapped-device->shepherd-service-name md))
338 ((? file-system? fs)
339 (file-system->shepherd-service-name fs))))
340
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
344 upon boot."
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)))
353 (shepherd-service
354 (provision (list (file-system->shepherd-service-name file-system)))
355 (requirement `(root-file-system
356 udev
357 ,@(map dependency->shepherd-service-name dependencies)))
358 (documentation "Check, mount, and unmount the given file system.")
359 (start #~(lambda args
360 #$(if create?
361 #~(mkdir-p #$target)
362 #t)
363
364 #$(if mount?
365 #~(let (($PATH (getenv "PATH")))
366 ;; Make sure fsck.ext2 & co. can be found.
367 (dynamic-wind
368 (lambda ()
369 ;; Don’t display the PATH settings.
370 (with-output-to-port (%make-void-port "w")
371 (lambda ()
372 (set-path-environment-variable "PATH"
373 '("bin" "sbin")
374 '#$packages))))
375 (lambda ()
376 (mount-file-system
377 (spec->file-system
378 '#$(file-system->spec file-system))
379 #:root "/"))
380 (lambda ()
381 (setenv "PATH" $PATH))))
382 #t)
383 #t))
384 (stop #~(lambda args
385 ;; Normally there are no processes left at this point, so
386 ;; TARGET can be safely unmounted.
387
388 ;; Make sure PID 1 doesn't keep TARGET busy.
389 (chdir "/")
390
391 (umount #$target)
392 #f))
393
394 ;; We need additional modules.
395 (modules `(((gnu build file-systems)
396 #:select (mount-file-system))
397 (gnu system file-systems)
398 ,@%default-modules)))))))
399
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)))
405 file-systems)))
406 (define sink
407 (shepherd-service
408 (provision '(file-systems))
409 (requirement (cons* 'root-file-system 'user-file-systems
410 (map file-system->shepherd-service-name
411 file-systems)))
412 (documentation "Target for all the initially-mounted file systems")
413 (start #~(const #t))
414 (stop #~(const #f))))
415
416 (define known-mount-points
417 (map file-system-mount-point file-systems))
418
419 (define user-unmount
420 (shepherd-service
421 (documentation "Unmount manually-mounted file systems.")
422 (provision '(user-file-systems))
423 (start #~(const #t))
424 (stop #~(lambda args
425 (define (known? mount-point)
426 (member mount-point
427 (cons* "/proc" "/sys" '#$known-mount-points)))
428
429 ;; Make sure we don't keep the user's mount points busy.
430 (chdir "/")
431
432 (for-each (lambda (mount-point)
433 (format #t "unmounting '~a'...~%" mount-point)
434 (catch 'system-error
435 (lambda ()
436 (umount mount-point))
437 (lambda args
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)))
442 #f))))
443
444 (cons* sink user-unmount
445 (map file-system-shepherd-service file-systems))))
446
447 (define (file-system-fstab-entries file-systems)
448 "Return the subset of @var{file-systems} that should have an entry in
449 @file{/etc/fstab}."
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"
452 ;; file systems.
453 ;;
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))))
462 file-systems))
463
464 (define file-system-service-type
465 (service-type (name 'file-systems)
466 (extensions
467 (list (service-extension shepherd-root-service-type
468 file-system-shepherd-services)
469 (service-extension fstab-service-type
470 file-system-fstab-entries)
471
472 ;; Have 'user-processes' depend on 'file-systems'.
473 (service-extension user-processes-service-type
474 (const '(file-systems)))))
475 (compose concatenate)
476 (extend append)
477 (description
478 "Provide Shepherd services to mount and unmount the given
479 file systems, as well as corresponding @file{/etc/fstab} entries.")))
480
481
482 \f
483 ;;;
484 ;;; Preserve entropy to seed /dev/urandom on boot.
485 ;;;
486
487 (define %random-seed-file
488 "/var/lib/random-seed")
489
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))
495
496 ;; Depend on udev so that /dev/hwrng is available.
497 (requirement '(file-systems udev))
498
499 (start #~(lambda _
500 ;; On boot, write random seed into /dev/urandom.
501 (when (file-exists? #$%random-seed-file)
502 (call-with-input-file #$%random-seed-file
503 (lambda (seed)
504 (call-with-output-file "/dev/urandom"
505 (lambda (urandom)
506 (dump-port seed urandom)
507
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)))))))
513
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
520 (lambda ()
521 (call-with-input-file "/dev/hwrng"
522 (lambda (hwrng)
523 (get-bytevector-n hwrng 512))))
524 ;; Silence is golden...
525 (const #f))))
526 (when buf
527 (call-with-output-file "/dev/urandom"
528 (lambda (urandom)
529 (put-bytevector urandom buf)
530 (let ((bits (* 8 (bytevector-length buf))))
531 (add-to-entropy-count urandom bits))))))
532
533 ;; Immediately refresh the seed in case the system doesn't
534 ;; shut down cleanly.
535 (call-with-input-file "/dev/urandom"
536 (lambda (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
542 (lambda (seed)
543 (put-bytevector seed buf)))
544 (umask previous-umask))))
545 #t))
546 (stop #~(lambda _
547 ;; During shutdown, write from /dev/urandom into random seed.
548 (let ((buf (make-bytevector 512)))
549 (call-with-input-file "/dev/urandom"
550 (lambda (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
555 (lambda (seed)
556 (put-bytevector seed buf)))
557 (umask previous-umask))
558 #t)))))
559 (modules `((rnrs bytevectors)
560 (rnrs io ports)
561 ,@%default-modules)))))
562
563 (define urandom-seed-service-type
564 (service-type (name 'urandom-seed)
565 (extensions
566 (list (service-extension shepherd-root-service-type
567 urandom-seed-shepherd-service)
568
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)))))
574 (default-value #f)
575 (description
576 "Seed the @file{/dev/urandom} pseudo-random number
577 generator (RNG) with the value recorded when the system was last shut
578 down.")))
579
580
581 ;;;
582 ;;; Add hardware random number generator to entropy pool.
583 ;;;
584
585 (define-record-type* <rngd-configuration>
586 rngd-configuration make-rngd-configuration
587 rngd-configuration?
588 (rng-tools rngd-configuration-rng-tools) ;file-like
589 (device rngd-configuration-device)) ;string
590
591 (define rngd-service-type
592 (shepherd-service-type
593 'rngd
594 (lambda (config)
595 (define rng-tools (rngd-configuration-rng-tools config))
596 (define device (rngd-configuration-device config))
597
598 (define rngd-command
599 (list (file-append rng-tools "/sbin/rngd")
600 "-f" "-r" device))
601
602 (shepherd-service
603 (documentation "Add TRNG to entropy pool.")
604 (requirement '(udev))
605 (provision '(trng))
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.")))
610
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
618 (rngd-configuration
619 (rng-tools rng-tools)
620 (device device))))
621
622 \f
623 ;;;
624 ;;; Console & co.
625 ;;;
626
627 (define host-name-service-type
628 (shepherd-service-type
629 'host-name
630 (lambda (name)
631 (shepherd-service
632 (documentation "Initialize the machine's host name.")
633 (provision '(host-name))
634 (start #~(lambda _
635 (sethostname #$name)))
636 (one-shot? #t)))
637 (description "Initialize the machine's host name.")))
638
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))
642
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
650 'virtual-terminal
651 (lambda (utf8?)
652 (let ((knob "/sys/module/vt/parameters/default_utf8"))
653 (shepherd-service
654 (documentation "Set virtual terminals in UTF-8 module.")
655 (provision '(virtual-terminal))
656 (requirement '(root-file-system))
657 (start #~(lambda _
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
662 (lambda (port)
663 (display 1 port))))
664 #t))
665 (stop #~(const #f)))))
666 #t ;default to UTF-8
667 (description "Ensure the Linux virtual terminals run in UTF-8 mode.")))
668
669 (define console-keymap-service-type
670 (shepherd-service-type
671 'console-keymap
672 (lambda (files)
673 (shepherd-service
674 (documentation (string-append "Load console keymap (loadkeys)."))
675 (provision '(console-keymap))
676 (start #~(lambda _
677 (zero? (system* #$(file-append kbd "/bin/loadkeys")
678 #$@files))))
679 (respawn? #f)))
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}.")))
683
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.
688 "LatGrkCyr-8x16")
689
690 (define (console-font-shepherd-services tty+font)
691 "Return a list of Shepherd services for each pair in TTY+FONT."
692 (map (match-lambda
693 ((tty . font)
694 (let ((device (string-append "/dev/" tty)))
695 (shepherd-service
696 (documentation "Load a Unicode console font.")
697 (provision (list (symbol-append 'console-font-
698 (string->symbol tty))))
699
700 ;; Start after mingetty has been started on TTY, otherwise the settings
701 ;; are ignored.
702 (requirement (list (symbol-append 'term-
703 (string->symbol tty))))
704
705 (start #~(lambda _
706 ;; It could be that mingetty is not fully ready yet,
707 ;; which we check by calling 'ttyname'.
708 (let loop ((i 10))
709 (unless (or (zero? i)
710 (call-with-input-file #$device
711 (lambda (port)
712 (false-if-exception (ttyname port)))))
713 (usleep 500)
714 (loop (- i 1))))
715
716 ;; Assume the VT is already in UTF-8 mode, thanks to
717 ;; the 'virtual-terminal' service.
718 ;;
719 ;; 'setfont' returns EX_OSERR (71) when an
720 ;; KDFONTOP ioctl fails, for example. Like
721 ;; systemd's vconsole support, let's not treat
722 ;; this as an error.
723 (case (status:exit-val
724 (system* #$(file-append kbd "/bin/setfont")
725 "-C" #$device #$font))
726 ((0 71) #t)
727 (else #f))))
728 (stop #~(const #t))
729 (respawn? #f)))))
730 tty+font))
731
732 (define console-font-service-type
733 (service-type (name 'console-fonts)
734 (extensions
735 (list (service-extension shepherd-root-service-type
736 console-font-shepherd-services)))
737 (compose concatenate)
738 (extend append)
739 (description
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:
744
745 @example
746 `((\"tty1\" . \"LatGrkCyr-8x16\")
747 (\"tty2\" . ,(file-append
748 font-tamzen
749 \"/share/kbd/consolefonts/TamzenForPowerline10x20.psf\"))
750 (\"tty3\" . ,(file-append
751 font-terminus
752 \"/share/consolefonts/ter-132n\"))) ; for HDPI
753 @end example\n")))
754
755 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
756 "This procedure is deprecated in favor of @code{console-font-service-type}.
757
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))))
762
763 (define %default-motd
764 (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
765
766 (define-record-type* <login-configuration>
767 login-configuration make-login-configuration
768 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
775
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"
780 #:login-uid? #t
781 #:allow-empty-passwords?
782 (login-configuration-allow-empty-passwords? config)
783 #:motd
784 (login-configuration-motd config))))
785
786 (define login-service-type
787 (service-type (name 'login)
788 (extensions (list (service-extension pam-root-service-type
789 login-pam-service)))
790 (default-value (login-configuration))
791 (description
792 "Provide a console log-in service as specified by its
793 configuration value, a @code{login-configuration} object.")))
794
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))
799
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
807 (default #f))
808 (baud-rate agetty-baud-rate ;string | #f
809 (default #f))
810 (auto-login agetty-auto-login ;list of strings | #f
811 (default #f))
812 (login-program agetty-login-program ;gexp
813 (default (file-append shadow "/bin/login")))
814 (login-pause? agetty-login-pause? ;Boolean
815 (default #f))
816 (eight-bits? agetty-eight-bits? ;Boolean
817 (default #f))
818 (no-reset? agetty-no-reset? ;Boolean
819 (default #f))
820 (remote? agetty-remote? ;Boolean
821 (default #f))
822 (flow-control? agetty-flow-control? ;Boolean
823 (default #f))
824 (host agetty-host ;string | #f
825 (default #f))
826 (no-issue? agetty-no-issue? ;Boolean
827 (default #f))
828 (init-string agetty-init-string ;string | #f
829 (default #f))
830 (no-clear? agetty-no-clear? ;Boolean
831 (default #f))
832 (local-line agetty-local-line ;always | never | auto
833 (default #f))
834 (extract-baud? agetty-extract-baud? ;Boolean
835 (default #f))
836 (skip-login? agetty-skip-login? ;Boolean
837 (default #f))
838 (no-newline? agetty-no-newline? ;Boolean
839 (default #f))
840 (login-options agetty-login-options ;string | #f
841 (default #f))
842 (chroot agetty-chroot ;string | #f
843 (default #f))
844 (hangup? agetty-hangup? ;Boolean
845 (default #f))
846 (keep-baud? agetty-keep-baud? ;Boolean
847 (default #f))
848 (timeout agetty-timeout ;integer | #f
849 (default #f))
850 (detect-case? agetty-detect-case? ;Boolean
851 (default #f))
852 (wait-cr? agetty-wait-cr? ;Boolean
853 (default #f))
854 (no-hints? agetty-no-hints? ;Boolean
855 (default #f))
856 (no-hostname? agetty-no hostname? ;Boolean
857 (default #f))
858 (long-hostname? agetty-long-hostname? ;Boolean
859 (default #f))
860 (erase-characters agetty-erase-characters ;string | #f
861 (default #f))
862 (kill-characters agetty-kill-characters ;string | #f
863 (default #f))
864 (chdir agetty-chdir ;string | #f
865 (default #f))
866 (delay agetty-delay ;integer | #f
867 (default #f))
868 (nice agetty-nice ;integer | #f
869 (default #f))
870 ;; "Escape hatch" for passing arbitrary command-line arguments.
871 (extra-options agetty-extra-options ;list of strings
872 (default '()))
873 ;;; XXX Unimplemented for now!
874 ;;; (issue-file agetty-issue-file ;file-like
875 ;;; (default #f))
876 )
877
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'
883 #~(begin
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)
893 (not (or
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)))
906 (match specs
907 (() #f)
908 ((spec _ ...)
909 ;; Extract device name from first spec.
910 (match (string-tokenize spec not-comma)
911 ((device-name _ ...)
912 device-name))))))))
913
914 (define agetty-shepherd-service
915 (match-lambda
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)
922 (list
923 (shepherd-service
924 (documentation "Run agetty on a tty.")
925 (provision (list (symbol-append 'term- (string->symbol (or tty "auto")))))
926
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))
932
933 (modules '((ice-9 match) (gnu build linux-boot)))
934 (start
935 (with-imported-modules (source-module-closure
936 '((gnu build linux-boot)))
937 #~(lambda args
938 (let ((defaulted-tty #$(or tty (default-serial-port))))
939 (apply
940 (if defaulted-tty
941 (make-forkexec-constructor
942 (list #$(file-append util-linux "/sbin/agetty")
943 #$@extra-options
944 #$@(if eight-bits?
945 #~("--8bits")
946 #~())
947 #$@(if no-reset?
948 #~("--noreset")
949 #~())
950 #$@(if remote?
951 #~("--remote")
952 #~())
953 #$@(if flow-control?
954 #~("--flow-control")
955 #~())
956 #$@(if host
957 #~("--host" #$host)
958 #~())
959 #$@(if no-issue?
960 #~("--noissue")
961 #~())
962 #$@(if init-string
963 #~("--init-string" #$init-string)
964 #~())
965 #$@(if no-clear?
966 #~("--noclear")
967 #~())
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.
972 #$@(if local-line
973 #~(#$(match local-line
974 ('auto "--local-line=auto")
975 ('always "--local-line=always")
976 ('never "-local-line=never")))
977 #~())
978 #$@(if tty
979 #~()
980 #~("--keep-baud"))
981 #$@(if extract-baud?
982 #~("--extract-baud")
983 #~())
984 #$@(if skip-login?
985 #~("--skip-login")
986 #~())
987 #$@(if no-newline?
988 #~("--nonewline")
989 #~())
990 #$@(if login-options
991 #~("--login-options" #$login-options)
992 #~())
993 #$@(if chroot
994 #~("--chroot" #$chroot)
995 #~())
996 #$@(if hangup?
997 #~("--hangup")
998 #~())
999 #$@(if keep-baud?
1000 #~("--keep-baud")
1001 #~())
1002 #$@(if timeout
1003 #~("--timeout" #$(number->string timeout))
1004 #~())
1005 #$@(if detect-case?
1006 #~("--detect-case")
1007 #~())
1008 #$@(if wait-cr?
1009 #~("--wait-cr")
1010 #~())
1011 #$@(if no-hints?
1012 #~("--nohints?")
1013 #~())
1014 #$@(if no-hostname?
1015 #~("--nohostname")
1016 #~())
1017 #$@(if long-hostname?
1018 #~("--long-hostname")
1019 #~())
1020 #$@(if erase-characters
1021 #~("--erase-chars" #$erase-characters)
1022 #~())
1023 #$@(if kill-characters
1024 #~("--kill-chars" #$kill-characters)
1025 #~())
1026 #$@(if chdir
1027 #~("--chdir" #$chdir)
1028 #~())
1029 #$@(if delay
1030 #~("--delay" #$(number->string delay))
1031 #~())
1032 #$@(if nice
1033 #~("--nice" #$(number->string nice))
1034 #~())
1035 #$@(if auto-login
1036 (list "--autologin" auto-login)
1037 '())
1038 #$@(if login-program
1039 #~("--login-program" #$login-program)
1040 #~())
1041 #$@(if login-pause?
1042 #~("--login-pause")
1043 #~())
1044 defaulted-tty
1045 #$@(if baud-rate
1046 #~(#$baud-rate)
1047 #~())
1048 #$@(if term
1049 #~(#$term)
1050 #~())))
1051 (const #f)) ; never start.
1052 args)))))
1053 (stop #~(make-kill-destructor)))))))
1054
1055 (define agetty-service-type
1056 (service-type (name 'agetty)
1057 (extensions (list (service-extension shepherd-root-service-type
1058 agetty-shepherd-service)))
1059 (description
1060 "Provide console login using the @command{agetty}
1061 program.")))
1062
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))
1067
1068 (define-record-type* <mingetty-configuration>
1069 mingetty-configuration make-mingetty-configuration
1070 mingetty-configuration?
1071 (mingetty mingetty-configuration-mingetty ;file-like
1072 (default mingetty))
1073 (tty mingetty-configuration-tty) ;string
1074 (auto-login mingetty-auto-login ;string | #f
1075 (default #f))
1076 (login-program mingetty-login-program ;gexp
1077 (default #f))
1078 (login-pause? mingetty-login-pause? ;Boolean
1079 (default #f))
1080 (clear-on-logout? mingetty-clear-on-logout? ;Boolean
1081 (default #t)))
1082
1083 (define mingetty-shepherd-service
1084 (match-lambda
1085 (($ <mingetty-configuration> mingetty tty auto-login login-program
1086 login-pause? clear-on-logout?)
1087 (list
1088 (shepherd-service
1089 (documentation "Run mingetty on an tty.")
1090 (provision (list (symbol-append 'term- (string->symbol tty))))
1091
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))
1096
1097 (start #~(make-forkexec-constructor
1098 (list #$(file-append mingetty "/sbin/mingetty")
1099
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
1103 ;; in Linux.
1104 "--nohangup" #$tty
1105
1106 #$@(if clear-on-logout?
1107 #~()
1108 #~("--noclear"))
1109 #$@(if auto-login
1110 #~("--autologin" #$auto-login)
1111 #~())
1112 #$@(if login-program
1113 #~("--loginprog" #$login-program)
1114 #~())
1115 #$@(if login-pause?
1116 #~("--loginpause")
1117 #~()))))
1118 (stop #~(make-kill-destructor)))))))
1119
1120 (define mingetty-service-type
1121 (service-type (name 'mingetty)
1122 (extensions (list (service-extension shepherd-root-service-type
1123 mingetty-shepherd-service)))
1124 (description
1125 "Provide console login using the @command{mingetty}
1126 program.")))
1127
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))
1132
1133 (define-record-type* <nscd-configuration> nscd-configuration
1134 make-nscd-configuration
1135 nscd-configuration?
1136 (log-file nscd-configuration-log-file ;string
1137 (default "/var/log/nscd.log"))
1138 (debug-level nscd-debug-level ;integer
1139 (default 0))
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
1144 (default '()))
1145 (glibc nscd-configuration-glibc ;file-like
1146 (default glibc)))
1147
1148 (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
1149 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
1155 ;of hash table")
1156 (default 211))
1157 (check-files? nscd-cache-check-files? ;Boolean
1158 (default #t))
1159 (persistent? nscd-cache-persistent? ;Boolean
1160 (default #t))
1161 (shared? nscd-cache-shared? ;Boolean
1162 (default #t))
1163 (max-database-size nscd-cache-max-database-size ;integer
1164 (default (* 32 (expt 2 20))))
1165 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
1166 (default #t)))
1167
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)
1172
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)
1177 (persistent? #t))
1178
1179 (nscd-cache (database 'services)
1180
1181 ;; Services are unlikely to change, so we can be even more
1182 ;; aggressive.
1183 (positive-time-to-live (* 3600 24))
1184 (negative-time-to-live 3600)
1185 (check-files? #t) ;check /etc/services changes
1186 (persistent? #t))))
1187
1188 (define %nscd-default-configuration
1189 ;; Default nscd configuration.
1190 (nscd-configuration))
1191
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
1196 (match-lambda
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"
1201
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")))))
1218
1219 (match config
1220 (($ <nscd-configuration> log-file debug-level caches)
1221 (plain-file "nscd.conf"
1222 (string-append "\
1223 # Configuration of libc's name service cache daemon (nscd).\n\n"
1224 (if log-file
1225 (string-append "logfile\t" log-file)
1226 "")
1227 "\n"
1228 (if debug-level
1229 (string-append "debug-level\t"
1230 (number->string debug-level))
1231 "")
1232 "\n"
1233 (string-concatenate
1234 (map cache->config caches)))))))
1235
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
1241 ;; connection.
1242 (let ((pipe (apply open-pipe* OPEN_READ #$nscd
1243 "-f" #$config #$option args)))
1244 (let loop ()
1245 (match (read-line pipe 'concat)
1246 ((? eof-object?)
1247 (catch 'system-error
1248 (lambda ()
1249 (zero? (close-pipe pipe)))
1250 (lambda args
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)))))
1257 (line
1258 (display line)
1259 (loop)))))))
1260
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
1266 (name 'statistics)
1267 (documentation "Display statistics about nscd usage.")
1268 (procedure (nscd-action-procedure nscd config "--statistics")))
1269 (shepherd-action
1270 (name 'invalidate)
1271 (documentation
1272 "Invalidate the given cache--e.g., 'hosts' for host name lookups.")
1273 (procedure (nscd-action-procedure nscd config "--invalidate")))))
1274
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)
1278 "/sbin/nscd"))
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).")
1283 (provision '(nscd))
1284 (requirement '(user-processes))
1285 (start #~(make-forkexec-constructor
1286 (list #$nscd "-f" #$nscd.conf "--foreground")
1287
1288 ;; Wait for the PID file. However, the PID file is
1289 ;; written before nscd is actually listening on its
1290 ;; socket (XXX).
1291 #:pid-file "/var/run/nscd/nscd.pid"
1292
1293 #:environment-variables
1294 (list (string-append "LD_LIBRARY_PATH="
1295 (string-join
1296 (map (lambda (dir)
1297 (string-append dir "/lib"))
1298 (list #$@name-services))
1299 ":")))))
1300 (stop #~(make-kill-destructor))
1301 (modules `((ice-9 popen) ;for the actions
1302 (ice-9 rdelim)
1303 (ice-9 match)
1304 ,@%default-modules))
1305 (actions (nscd-actions nscd nscd.conf))))))
1306
1307 (define nscd-activation
1308 ;; Actions to take before starting nscd.
1309 #~(begin
1310 (use-modules (guix build utils))
1311 (mkdir-p "/var/run/nscd")
1312 (mkdir-p "/var/db/nscd") ;for the persistent cache
1313
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"
1320 (lambda (port)
1321 (display "# This is a placeholder.\n" port))))))
1322
1323 (define nscd-service-type
1324 (service-type (name 'nscd)
1325 (extensions
1326 (list (service-extension activation-service-type
1327 (const nscd-activation))
1328 (service-extension shepherd-root-service-type
1329 nscd-shepherd-service)))
1330
1331 ;; This can be extended by providing additional name services
1332 ;; such as nss-mdns.
1333 (compose concatenate)
1334 (extend (lambda (config name-services)
1335 (nscd-configuration
1336 (inherit config)
1337 (name-services (append
1338 (nscd-configuration-name-services config)
1339 name-services)))))
1340 (default-value %nscd-default-configuration)
1341 (description
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.")))
1345
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))
1351
1352
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)))
1360
1361 (define syslog-service-type
1362 (shepherd-service-type
1363 'syslog
1364 (lambda (config)
1365 (shepherd-service
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)
1371 "--rcfile"
1372 #$(syslog-configuration-config-file config))
1373 #:pid-file "/var/run/syslog.pid")))
1374 (lambda ()
1375 ;; Set the umask such that file permissions are #o640.
1376 (let ((mask (umask #o137))
1377 (pid (spawn)))
1378 (umask mask)
1379 pid))))
1380 (stop #~(make-kill-destructor))))
1381 (description "Run the syslog daemon, @command{syslogd}, which is
1382 responsible for logging system messages.")))
1383
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
1392
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
1396
1397 # Like /var/log/messages, but also including \"debug\"-level logs.
1398 *.debug;mail.none;authpriv.none /var/log/debug
1399
1400 # Same, in a different place.
1401 *.info;mail.none;authpriv.none /dev/tty12
1402
1403 # The authpriv file has restricted access.
1404 authpriv.* /var/log/secure
1405
1406 # Log all the mail messages in one place.
1407 mail.* /var/log/maillog
1408 "))
1409
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.
1413
1414 @xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
1415 information on the configuration file syntax."
1416 (service syslog-service-type config))
1417
1418
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"
1424 ,limits-file))))
1425 (pam-extension
1426 (lambda (pam)
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"))
1433 (pam-service
1434 (inherit pam)
1435 (session (cons pam-limits
1436 (pam-service-session pam))))
1437 pam)))))
1438 (service-type
1439 (name 'limits)
1440 (extensions
1441 (list (service-extension etc-service-type security-limits)
1442 (service-extension pam-root-service-type
1443 (lambda _ (list pam-extension)))))
1444 (description
1445 "Install the specified resource usage limits by populating
1446 @file{/etc/security/limits.conf} and using the @code{pam_limits}
1447 authentication module."))))
1448
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)
1455 "\n"))))
1456
1457 \f
1458 ;;;
1459 ;;; Guix services.
1460 ;;;
1461
1462 (define* (guix-build-accounts count #:key
1463 (group "guixbuild")
1464 (shadow shadow))
1465 "Return a list of COUNT user accounts for Guix build users with the given
1466 GID."
1467 (unfold (cut > <> count)
1468 (lambda (n)
1469 (user-account
1470 (name (format #f "guixbuilder~2,'0d" n))
1471 (system? #t)
1472 (group group)
1473
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"))
1478
1479 (comment (format #f "Guix Build User ~2d" n))
1480 (home-directory "/var/empty")
1481 (shell (file-append shadow "/sbin/nologin"))))
1482 1+
1483 1))
1484
1485 (define not-config?
1486 ;; Select (guix …) and (gnu …) modules, except (guix config).
1487 (match-lambda
1488 (('guix 'config) #f)
1489 (('guix rest ...) #t)
1490 (('gnu rest ...) #t)
1491 (rest #f)))
1492
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."
1496 (define default-acl
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"
1502 #~(begin
1503 (use-modules (guix pki)
1504 (gcrypt pk-crypto)
1505 (ice-9 rdelim))
1506
1507 (define keys
1508 (map (lambda (file)
1509 (call-with-input-file file
1510 (compose string->canonical-sexp
1511 read-string)))
1512 '(#$@keys)))
1513
1514 (call-with-output-file #$output
1515 (lambda (port)
1516 (write-acl (public-keys->acl keys)
1517 port))))))))
1518
1519 (with-imported-modules '((guix build utils))
1520 #~(begin
1521 (use-modules (guix build utils))
1522
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"))
1532
1533 ;; Installed the declared ACL.
1534 (symlink #+default-acl "/etc/guix/acl"))))
1535
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")))
1540
1541 (define-record-type* <guix-configuration>
1542 guix-configuration make-guix-configuration
1543 guix-configuration?
1544 (guix guix-configuration-guix ;file-like
1545 (default guix))
1546 (build-group guix-configuration-build-group ;string
1547 (default "guixbuild"))
1548 (build-accounts guix-configuration-build-accounts ;integer
1549 (default 10))
1550 (authorize-key? guix-configuration-authorize-key? ;Boolean
1551 (default #t))
1552 (authorized-keys guix-configuration-authorized-keys ;list of gexps
1553 (default %default-authorized-guix-keys))
1554 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
1555 (default #t))
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
1559 (default '()))
1560 (max-silent-time guix-configuration-max-silent-time ;integer
1561 (default 0))
1562 (timeout guix-configuration-timeout ;integer
1563 (default 0))
1564 (log-compression guix-configuration-log-compression
1565 (default 'bzip2))
1566 (discover? guix-configuration-discover?
1567 (default #f))
1568 (extra-options guix-configuration-extra-options ;list of strings
1569 (default '()))
1570 (log-file guix-configuration-log-file ;string
1571 (default "/var/log/guix-daemon.log"))
1572 (http-proxy guix-http-proxy ;string | #f
1573 (default #f))
1574 (tmpdir guix-tmpdir ;string | #f
1575 (default #f)))
1576
1577 (define %default-guix-configuration
1578 (guix-configuration))
1579
1580 (define shepherd-set-http-proxy-action
1581 ;; Shepherd action to change the HTTP(S) proxy.
1582 (shepherd-action
1583 (name 'set-http-proxy)
1584 (documentation
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.
1590 (if proxy
1591 (begin
1592 (format #t "changing HTTP/HTTPS \
1593 proxy of 'guix-daemon' to ~s...~%"
1594 proxy)
1595 (setenv "http_proxy" proxy))
1596 (begin
1597 (format #t "clearing HTTP/HTTPS \
1598 proxy of 'guix-daemon'...~%")
1599 (unsetenv "http_proxy")))
1600 (action 'guix-daemon 'restart)
1601 (environ environment)
1602 #t)))))
1603
1604 (define shepherd-discover-action
1605 ;; Shepherd action to enable or disable substitute servers discovery.
1606 (shepherd-action
1607 (name 'discover)
1608 (documentation
1609 "Enable or disable substitute servers discovery and restart the
1610 'guix-daemon'.")
1611 (procedure #~(lambda* (_ status)
1612 (let ((environment (environ)))
1613 (if (and status
1614 (string=? status "on"))
1615 (begin
1616 (format #t "enable substitute servers discovery~%")
1617 (setenv "discover" "on"))
1618 (begin
1619 (format #t "disable substitute servers discovery~%")
1620 (unsetenv "discover")))
1621 (action 'guix-daemon 'restart)
1622 (environ environment)
1623 #t)))))
1624
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)
1639 (ice-9 match)
1640 (gnu build shepherd)))
1641 (start
1642 (with-imported-modules `(((guix config) => ,(make-config.scm))
1643 ,@(source-module-closure
1644 '((gnu build shepherd))
1645 #:select? not-config?))
1646 #~(lambda args
1647 (define proxy
1648 ;; HTTP/HTTPS proxy. The 'http_proxy' variable is set by
1649 ;; the 'set-http-proxy' action.
1650 (or (getenv "http_proxy") #$http-proxy))
1651
1652 (define discover?
1653 (or (getenv "discover") #$discover?))
1654
1655 ;; Start the guix-daemon from a container, when supported,
1656 ;; to solve an installation issue. See the comment below for
1657 ;; more details.
1658 (fork+exec-command/container
1659 (cons* #$(file-append guix "/bin/guix-daemon")
1660 "--build-users-group" #$build-group
1661 "--max-silent-time"
1662 #$(number->string max-silent-time)
1663 "--timeout" #$(number->string timeout)
1664 "--log-compression"
1665 #$(symbol->string log-compression)
1666 #$@(if use-substitutes?
1667 '()
1668 '("--no-substitutes"))
1669 (string-append "--discover="
1670 (if discover? "yes" "no"))
1671 "--substitute-urls" #$(string-join substitute-urls)
1672 #$@extra-options
1673
1674 ;; Add CHROOT-DIRECTORIES and all their dependencies
1675 ;; (if these are store items) to the chroot.
1676 (append-map
1677 (lambda (file)
1678 (append-map (lambda (directory)
1679 (list "--chroot-directory"
1680 directory))
1681 (call-with-input-file file
1682 read)))
1683 '#$(map references-file
1684 chroot-directories)))
1685
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.
1692 #:pid (match args
1693 ((pid) (string->number pid))
1694 (else (getpid)))
1695
1696 #:environment-variables
1697 (append (list #$@(if tmpdir
1698 (list (string-append "TMPDIR=" tmpdir))
1699 '())
1700
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
1704 ;; 'nss-certs'. See
1705 ;; <https://bugs.gnu.org/32942>.
1706 (string-append "GUIX_LOCPATH="
1707 #$glibc-utf8-locales
1708 "/lib/locale")
1709 "LC_ALL=en_US.utf8")
1710 (if proxy
1711 (list (string-append "http_proxy=" proxy)
1712 (string-append "https_proxy=" proxy))
1713 '()))
1714
1715 #:log-file #$log-file))))
1716 (stop #~(make-kill-destructor))))))
1717
1718 (define (guix-accounts config)
1719 "Return the user accounts and user groups for CONFIG."
1720 (match config
1721 (($ <guix-configuration> _ build-group build-accounts)
1722 (cons (user-group
1723 (name build-group)
1724 (system? #t)
1725
1726 ;; Use a fixed GID so that we can create the store with the right
1727 ;; owner.
1728 (id 30000))
1729 (guix-build-accounts build-accounts
1730 #:group build-group)))))
1731
1732 (define (guix-activation config)
1733 "Return the activation gexp for CONFIG."
1734 (match-record config <guix-configuration>
1735 (guix authorize-key? authorized-keys)
1736 #~(begin
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.
1740
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"
1744 "--generate-key"))
1745
1746 #$(if authorize-key?
1747 (substitute-key-authorization authorized-keys guix)
1748 #~#f))))
1749
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
1753 (computed-file name
1754 (with-extensions (list guile-gcrypt) ;for store-copy
1755 (with-imported-modules (source-module-closure
1756 '((guix build store-copy)))
1757 #~(begin
1758 (use-modules (guix build store-copy))
1759
1760 (call-with-output-file #$output
1761 (lambda (port)
1762 (write (map store-info-item
1763 (call-with-input-file "graph"
1764 read-reference-graph))
1765 port))))))
1766 #:options `(#:local-build? #f
1767 #:references-graphs (("graph" ,item))))
1768 (plain-file name "()")))
1769
1770 (define guix-service-type
1771 (service-type
1772 (name 'guix)
1773 (extensions
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))))
1779
1780 ;; Extensions can specify extra directories to add to the build chroot.
1781 (compose concatenate)
1782 (extend (lambda (config directories)
1783 (guix-configuration
1784 (inherit config)
1785 (chroot-directories
1786 (append (guix-configuration-chroot-directories config)
1787 directories)))))
1788
1789 (default-value (guix-configuration))
1790 (description
1791 "Run the build daemon of GNU@tie{}Guix, aka. @command{guix-daemon}.")))
1792
1793
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
1798 (default guix))
1799 (port guix-publish-configuration-port ;number
1800 (default 80))
1801 (host guix-publish-configuration-host ;string
1802 (default "localhost"))
1803 (advertise? guix-publish-advertise? ;boolean
1804 (default #f))
1805 (compression guix-publish-configuration-compression
1806 (thunked)
1807 (default (default-compression this-record
1808 (current-source-location))))
1809 (compression-level %guix-publish-configuration-compression-level ;deprecated
1810 (default #f))
1811 (nar-path guix-publish-configuration-nar-path ;string
1812 (default "nar"))
1813 (cache guix-publish-configuration-cache ;#f | string
1814 (default #f))
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
1818 (default #f))
1819 (ttl guix-publish-configuration-ttl ;#f | integer
1820 (default #f)))
1821
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)))
1826
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)
1831 (#f
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
1837 (level
1838 (warn-about-deprecation 'compression-level properties
1839 #:replacement 'compression)
1840 `(("gzip" ,level)))))
1841
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"
1846 '("-C0"))
1847 (lst
1848 (append-map (match-lambda
1849 ((type level)
1850 `("-C" ,(string-append type ":"
1851 (number->string level)))))
1852 lst))))
1853
1854 (match-record config <guix-publish-configuration>
1855 (guix port host nar-path cache workers ttl cache-bypass-threshold
1856 advertise?)
1857 (list (shepherd-service
1858 (provision '(guix-publish))
1859 (requirement `(user-processes
1860 guix-daemon
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)
1869 #$@(if advertise?
1870 #~("--advertise")
1871 #~())
1872 #$@(if workers
1873 #~((string-append "--workers="
1874 #$(number->string
1875 workers)))
1876 #~())
1877 #$@(if ttl
1878 #~((string-append "--ttl="
1879 #$(number->string ttl)
1880 "s"))
1881 #~())
1882 #$@(if cache
1883 #~((string-append "--cache=" #$cache)
1884 #$(string-append
1885 "--cache-bypass-threshold="
1886 (number->string
1887 cache-bypass-threshold)))
1888 #~()))
1889
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))))))
1899
1900 (define %guix-publish-accounts
1901 (list (user-group (name "guix-publish") (system? #t))
1902 (user-account
1903 (name "guix-publish")
1904 (group "guix-publish")
1905 (system? #t)
1906 (comment "guix publish user")
1907 (home-directory "/var/empty")
1908 (shell (file-append shadow "/sbin/nologin")))))
1909
1910 (define %guix-publish-log-rotations
1911 (list (log-rotation
1912 (files (list "/var/log/guix-publish.log")))))
1913
1914 (define (guix-publish-activation config)
1915 (let ((cache (guix-publish-configuration-cache config)))
1916 (if cache
1917 (with-imported-modules '((guix build utils))
1918 #~(begin
1919 (use-modules (guix build utils))
1920
1921 (mkdir-p #$cache)
1922 (let* ((pw (getpw "guix-publish"))
1923 (uid (passwd:uid pw))
1924 (gid (passwd:gid pw)))
1925 (chown #$cache uid gid))))
1926 #t)))
1927
1928 (define guix-publish-service-type
1929 (service-type (name 'guix-publish)
1930 (extensions
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))
1940 (description
1941 "Add a Shepherd service running @command{guix publish}, a
1942 command that allows you to share pre-built binaries with others over HTTP.")))
1943
1944 \f
1945 ;;;
1946 ;;; Udev.
1947 ;;;
1948
1949 (define-record-type* <udev-configuration>
1950 udev-configuration make-udev-configuration
1951 udev-configuration?
1952 (udev udev-configuration-udev ;file-like
1953 (default eudev))
1954 (rules udev-configuration-rules ;list of file-like
1955 (default '())))
1956
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}."
1960 (define build
1961 (with-imported-modules '((guix build union)
1962 (guix build utils))
1963 #~(begin
1964 (use-modules (guix build union)
1965 (guix build utils)
1966 (srfi srfi-1)
1967 (srfi srfi-26))
1968
1969 (define %standard-locations
1970 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
1971
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)))
1977
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)))))
1981
1982 (computed-file "udev-rules" build))
1983
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))
1988 #~(begin
1989 (use-modules (guix build utils))
1990
1991 (define rules.d
1992 (string-append #$output "/lib/udev/rules.d"))
1993
1994 (mkdir-p rules.d)
1995 (call-with-output-file
1996 (string-append rules.d "/" #$file-name)
1997 (lambda (port)
1998 (display #$contents port)))))))
1999
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))
2004 #~(begin
2005 (use-modules (guix build utils))
2006
2007 (define rules.d
2008 (string-append #$output "/lib/udev/rules.d"))
2009
2010 (define file-copy-dest
2011 (string-append rules.d "/" #$file-name))
2012
2013 (mkdir-p rules.d)
2014 (copy-file #$file file-copy-dest)))))
2015
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.
2020
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"))
2025
2026 (define udev-shepherd-service
2027 ;; Return a <shepherd-service> for UDEV with RULES.
2028 (match-lambda
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
2033 (lambda (port)
2034 (format port
2035 "udev_rules=\"~a/lib/udev/rules.d\"\n"
2036 #$rules))))))
2037 (list
2038 (shepherd-service
2039 (provision '(udev))
2040
2041 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
2042 ;; be added: see
2043 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
2044 (requirement '(root-file-system))
2045
2046 (documentation "Populate the /dev directory, dynamically.")
2047 (start
2048 (with-imported-modules (source-module-closure
2049 '((gnu build linux-boot)))
2050 #~(lambda ()
2051 (define udevd
2052 ;; 'udevd' from eudev.
2053 #$(file-append udev "/sbin/udevd"))
2054
2055 (define (wait-for-udevd)
2056 ;; Wait until someone's listening on udevd's control
2057 ;; socket.
2058 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
2059 (let try ()
2060 (catch 'system-error
2061 (lambda ()
2062 (connect sock PF_UNIX "/run/udev/control")
2063 (close-port sock))
2064 (lambda args
2065 (format #t "waiting for udevd...~%")
2066 (usleep 500000)
2067 (try))))))
2068
2069 ;; Allow udev to find the modules.
2070 (setenv "LINUX_MODULE_DIRECTORY"
2071 "/run/booted-system/kernel/lib/modules")
2072
2073 (let* ((kernel-release
2074 (utsname:release (uname)))
2075 (linux-module-directory
2076 (getenv "LINUX_MODULE_DIRECTORY"))
2077 (directory
2078 (string-append linux-module-directory "/"
2079 kernel-release))
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))
2087 (umask old-umask))
2088
2089 (let ((pid (fork+exec-command (list udevd)
2090 #:environment-variables
2091 (cons*
2092 ;; The first one is for udev, the second one for
2093 ;; eudev.
2094 (string-append "UDEV_CONFIG_FILE=" #$udev.conf)
2095 (string-append "EUDEV_RULES_DIRECTORY="
2096 #$(file-append
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.
2104 (wait-for-udevd)
2105
2106 ;; Trigger device node creation.
2107 (system* #$(file-append udev "/bin/udevadm")
2108 "trigger" "--action=add")
2109
2110 ;; Wait for things to settle down.
2111 (system* #$(file-append udev "/bin/udevadm")
2112 "settle")
2113 pid))))
2114 (stop #~(make-kill-destructor))
2115
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.
2119 (respawn? #f)
2120 ;; We need additional modules.
2121 (modules `((gnu build linux-boot) ;'make-static-device-nodes'
2122 ,@%default-modules))
2123
2124 (actions (list (shepherd-action
2125 (name 'rules)
2126 (documentation "Display the directory containing
2127 the udev rules in use.")
2128 (procedure #~(lambda (_)
2129 (display #$rules)
2130 (newline))))))))))))
2131
2132 (define udev-service-type
2133 (service-type (name 'udev)
2134 (extensions
2135 (list (service-extension shepherd-root-service-type
2136 udev-shepherd-service)))
2137
2138 (compose concatenate) ;concatenate the list of rules
2139 (extend (lambda (config rules)
2140 (match config
2141 (($ <udev-configuration> udev initial-rules)
2142 (udev-configuration
2143 (udev udev)
2144 (rules (append initial-rules rules)))))))
2145 (default-value (udev-configuration))
2146 (description
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.")))
2150
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))))
2156
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
2161 instance."
2162 (let* ((name (symbol-append name '-udev-rules))
2163 (account-extension
2164 (const (map (lambda (group)
2165 (user-group (name group) (system? #t)))
2166 groups)))
2167 (udev-extension (const (list rules)))
2168 (type (service-type
2169 (name name)
2170 (extensions (list
2171 (service-extension
2172 account-service-type account-extension)
2173 (service-extension
2174 udev-service-type udev-extension))))))
2175 (service type #f)))
2176
2177 (define (swap-space->shepherd-service-name space)
2178 (let ((target (swap-space-target space)))
2179 (symbol-append 'swap-
2180 (string->symbol
2181 (cond ((uuid? target)
2182 (uuid->string target))
2183 ((file-system-label? target)
2184 (file-system-label->string target))
2185 (else
2186 target))))))
2187
2188 ; TODO Remove after deprecation
2189 (define (swap-deprecated->shepherd-service-name sdep)
2190 (symbol-append 'swap-
2191 (string->symbol
2192 (cond ((uuid? sdep)
2193 (string-take (uuid->string sdep) 6))
2194 ((file-system-label? sdep)
2195 (file-system-label->string sdep))
2196 (else
2197 sdep)))))
2198
2199 (define swap->shepherd-service-name
2200 (match-lambda ((? swap-space? space)
2201 (swap-space->shepherd-service-name space))
2202 (sdep
2203 (swap-deprecated->shepherd-service-name sdep))))
2204
2205 (define swap-service-type
2206 (shepherd-service-type
2207 'swap
2208 (lambda (swap)
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)))))
2217 (else
2218 '())))
2219
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)))
2230 (else
2231 target))))
2232 ; TODO Remove after deprecation
2233 ((uuid? swap)
2234 #~(find-partition-by-uuid #$(uuid-bytevector swap)))
2235 ((file-system-label? swap)
2236 #~(find-partition-by-label
2237 #$(file-system-label->string swap)))
2238 (else
2239 swap)))
2240
2241 (with-imported-modules (source-module-closure '((gnu build file-systems)))
2242 (shepherd-service
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))
2248 (start #~(lambda ()
2249 (let ((device #$device-lookup))
2250 (and device
2251 (begin
2252 (restart-on-EINTR (swapon device
2253 #$(if (swap-space? swap)
2254 (swap-space->flags-bit-mask
2255 swap)
2256 0)))
2257 #t)))))
2258 (stop #~(lambda _
2259 (let ((device #$device-lookup))
2260 (when device
2261 (restart-on-EINTR (swapoff device)))
2262 #f)))
2263 (respawn? #f))))
2264 (description "Turn on the virtual memory swap area.")))
2265
2266 (define (swap-service swap)
2267 "Return a service that uses @var{swap} as a swap space."
2268 (service swap-service-type swap))
2269
2270 (define %default-gpm-options
2271 ;; Default options for GPM.
2272 '("-m" "/dev/input/mice" "-t" "ps2"))
2273
2274 (define-record-type* <gpm-configuration>
2275 gpm-configuration make-gpm-configuration gpm-configuration?
2276 (gpm gpm-configuration-gpm ;file-like
2277 (default gpm))
2278 (options gpm-configuration-options ;list of strings
2279 (default %default-gpm-options)))
2280
2281 (define gpm-shepherd-service
2282 (match-lambda
2283 (($ <gpm-configuration> gpm options)
2284 (list (shepherd-service
2285 (requirement '(udev))
2286 (provision '(gpm))
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")
2291 #$@options)
2292 #:pid-file "/var/run/gpm.pid"
2293 #:pid-file-timeout 3))
2294 (stop #~(lambda (_)
2295 ;; Return #f if successfully stopped.
2296 (not (zero? (system* #$(file-append gpm "/sbin/gpm")
2297 "-k"))))))))))
2298
2299 (define gpm-service-type
2300 (service-type (name 'gpm)
2301 (extensions
2302 (list (service-extension shepherd-root-service-type
2303 gpm-shepherd-service)))
2304 (default-value (gpm-configuration))
2305 (description
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.")))
2310
2311
2312 (define-record-type* <kmscon-configuration>
2313 kmscon-configuration make-kmscon-configuration
2314 kmscon-configuration?
2315 (kmscon kmscon-configuration-kmscon
2316 (default 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
2321 (default '("-p")))
2322 (auto-login kmscon-configuration-auto-login
2323 (default #f))
2324 (hardware-acceleration? kmscon-configuration-hardware-acceleration?
2325 (default #f)) ; #t causes failure
2326 (font-engine kmscon-configuration-font-engine
2327 (default "pango"))
2328 (font-size kmscon-configuration-font-size
2329 (default 12))
2330 (keyboard-layout kmscon-configuration-keyboard-layout
2331 (default #f))) ; #f | <keyboard-layout>
2332
2333 (define kmscon-service-type
2334 (shepherd-service-type
2335 'kmscon
2336 (lambda (config)
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)))
2346
2347 (define kmscon-command
2348 #~(list
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)
2363 '()
2364 `("--xkb-options" ,(string-join options ",")))))
2365 '())
2366 #$@(if hardware-acceleration? '("--hwaccel") '())
2367 "--login" "--"
2368 #$login-program #$@login-arguments
2369 #$@(if auto-login
2370 #~(#$auto-login)
2371 #~())))
2372
2373 (shepherd-service
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).")))
2381
2382 \f
2383 ;;;
2384 ;;; Static networking.
2385 ;;;
2386
2387 (define (ipv6-address? str)
2388 "Return true if STR denotes an IPv6 address."
2389 (false-if-exception (->bool (inet-pton AF_INET6 str))))
2390
2391 (define-record-type* <static-networking>
2392 static-networking make-static-networking
2393 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
2400 (default '(udev)))
2401 (name-servers static-networking-name-servers ;FIXME: doesn't belong here
2402 (default '())))
2403
2404 (define-record-type* <network-address>
2405 network-address make-network-address
2406 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
2410 (thunked)
2411 (default
2412 (ipv6-address? (cidr->ip (network-address-value this-record))))))
2413
2414 (define-record-type* <network-link>
2415 network-link make-network-link
2416 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
2420
2421 (define-record-type* <network-route>
2422 network-route make-network-route
2423 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)
2428 (default
2429 (or (ipv6-address? (network-route-destination this-record))
2430 (and=> (network-route-gateway this-record)
2431 ipv6-address?))))
2432 (gateway network-route-gateway (default #f)))
2433
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)
2441 bits))))
2442 (inet-ntop family mask)))
2443 (_ #f)))
2444
2445 (define (cidr->ip str)
2446 "Strip the netmask bit of @var{str}, a CIDR-notation IP/netmask address."
2447 (match (string-split str #\/)
2448 ((or (ip _) (ip))
2449 ip)))
2450
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
2454 @code{AF_INET6}."
2455 (let* ((netmask (inet-pton family netmask))
2456 (bits (logcount netmask)))
2457 (string-append ip "/" (number->string bits))))
2458
2459 (define (static-networking->hurd-pfinet-options config)
2460 "Return command-line options for the Hurd's pfinet translator corresponding
2461 to CONFIG."
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~%")))
2466
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)
2473 "--address6"
2474 "--address")
2475 ,(cidr->ip (network-address-value address))
2476 ,@(match (cidr->netmask (network-address-value address)
2477 (if (network-address-ipv6? address)
2478 AF_INET6
2479 AF_INET))
2480 (#f '())
2481 (mask (list "--netmask" mask)))))
2482 addresses)
2483 ,@(append-map (lambda (route)
2484 (match 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'~%")
2491 destination)
2492 '())))
2493 (static-networking-routes config))))))
2494
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))
2503 #~(begin
2504 (use-modules (guix build utils)
2505 (ice-9 format))
2506
2507 ;; TODO: Do that without forking.
2508 (let ((options '#$(static-networking->hurd-pfinet-options
2509 config)))
2510 (format #t "starting '~a~{ ~s~}'~%"
2511 #$(file-append hurd "/hurd/pfinet")
2512 options)
2513 (apply invoke #$(file-append hurd "/bin/settrans") "-fac"
2514 "/servers/socket/2"
2515 #$(file-append hurd "/hurd/pfinet")
2516 options)))))))
2517
2518 (define (network-tear-down/hurd config)
2519 (scheme-file "tear-down-pfinet"
2520 (with-imported-modules '((guix build utils))
2521 #~(begin
2522 (use-modules (guix build utils))
2523
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")
2531 #f))))
2532
2533 (define network-set-up/linux
2534 (match-lambda
2535 (($ <static-networking> addresses links routes)
2536 (scheme-file "set-up-network"
2537 (with-extensions (list guile-netlink)
2538 #~(begin
2539 (use-modules (ip addr) (ip link) (ip route))
2540
2541 #$@(map (lambda (address)
2542 #~(begin
2543 (addr-add #$(network-address-device address)
2544 #$(network-address-value address)
2545 #:ipv6?
2546 #$(network-address-ipv6? address))
2547 ;; FIXME: loopback?
2548 (link-set #$(network-address-device address)
2549 #:up #t)))
2550 addresses)
2551 #$@(map (match-lambda
2552 (($ <network-link> name type arguments)
2553 #~(link-add #$name #$type
2554 #:type-args '#$arguments)))
2555 links)
2556 #$@(map (lambda (route)
2557 #~(route-add #$(network-route-destination route)
2558 #:device
2559 #$(network-route-device route)
2560 #:ipv6?
2561 #$(network-route-ipv6? route)
2562 #:via
2563 #$(network-route-gateway route)
2564 #:src
2565 #$(network-route-source route)))
2566 routes)
2567 #t))))))
2568
2569 (define network-tear-down/linux
2570 (match-lambda
2571 (($ <static-networking> addresses links routes)
2572 (scheme-file "tear-down-network"
2573 (with-extensions (list guile-netlink)
2574 #~(begin
2575 (use-modules (ip addr) (ip link) (ip route)
2576 (netlink error)
2577 (srfi srfi-34))
2578
2579 (define-syntax-rule (false-if-netlink-error exp)
2580 (guard (c ((netlink-error? c) #f))
2581 exp))
2582
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".
2586
2587 #$@(map (lambda (route)
2588 #~(false-if-netlink-error
2589 (route-del #$(network-route-destination route)
2590 #:device
2591 #$(network-route-device route)
2592 #:ipv6?
2593 #$(network-route-ipv6? route)
2594 #:via
2595 #$(network-route-gateway route)
2596 #:src
2597 #$(network-route-source route))))
2598 routes)
2599 #$@(map (match-lambda
2600 (($ <network-link> name type arguments)
2601 #~(false-if-netlink-error
2602 (link-del #$name))))
2603 links)
2604 #$@(map (lambda (address)
2605 #~(false-if-netlink-error
2606 (addr-del #$(network-address-device
2607 address)
2608 #$(network-address-value address)
2609 #:ipv6?
2610 #$(network-address-ipv6? address))))
2611 addresses)
2612 #f))))))
2613
2614 (define (static-networking-shepherd-service config)
2615 (match config
2616 (($ <static-networking> addresses links routes
2617 provision requirement name-servers)
2618 (let ((loopback? (and provision (memq 'loopback provision))))
2619 (shepherd-service
2620
2621 (documentation
2622 "Bring up the networking interface using a static IP address.")
2623 (requirement requirement)
2624 (provision provision)
2625
2626 (start #~(lambda _
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))))))
2632 (stop #~(lambda _
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))))))
2638 (respawn? #f))))))
2639
2640 (define (static-networking-shepherd-services networks)
2641 (map static-networking-shepherd-service networks))
2642
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
2647 interfaces))
2648 (()
2649 '())
2650 ((name-servers ...)
2651 (let ((content (string-join
2652 (map (cut string-append "nameserver " <>)
2653 name-servers)
2654 "\n" 'suffix)))
2655 `(("resolv.conf"
2656 ,(plain-file "resolv.conf"
2657 (string-append "\
2658 # Generated by 'static-networking-service'.\n"
2659 content))))))))
2660
2661 (define static-networking-service-type
2662 ;; The service type for statically-defined network interfaces.
2663 (service-type (name 'static-networking)
2664 (extensions
2665 (list
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)
2671 (extend append)
2672 (description
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.")))
2677
2678 (define-deprecated (static-networking-service interface ip
2679 #:key
2680 netmask gateway provision
2681 ;; Most interfaces require udev to be usable.
2682 (requirement '(udev))
2683 (name-servers '()))
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.
2688
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
2692 to handle."
2693 (simple-service 'static-network-interface
2694 static-networking-service-type
2695 (list (static-networking
2696 (addresses
2697 (list (network-address
2698 (device interface)
2699 (value (if netmask
2700 (ip+netmask->cidr ip netmask)
2701 ip))
2702 (ipv6? #f))))
2703 (routes
2704 (if gateway
2705 (list (network-route
2706 (destination "default")
2707 (gateway gateway)
2708 (ipv6? #f)))
2709 '()))
2710 (requirement requirement)
2711 (provision (or provision '(networking)))
2712 (name-servers name-servers)))))
2713
2714 (define %loopback-static-networking
2715 ;; The loopback device.
2716 (static-networking
2717 (addresses (list (network-address
2718 (device "lo")
2719 (value "127.0.0.1"))))
2720 (requirement '())
2721 (provision '(loopback))))
2722
2723 (define %qemu-static-networking
2724 ;; Networking configuration for QEMU's user-mode network stack (info "(QEMU)
2725 ;; Using the user mode network stack").
2726 (static-networking
2727 (addresses (list (network-address
2728 (device "eth0")
2729 (value "10.0.2.15/24"))))
2730 (routes (list (network-route
2731 (destination "default")
2732 (gateway "10.0.2.2"))))
2733 (requirement '())
2734 (provision '(networking))
2735 (name-servers '("10.0.2.3"))))
2736
2737 \f
2738 (define %base-services
2739 ;; Convenience variable holding the basic services.
2740 (list (service login-service-type)
2741
2742 (service virtual-terminal-service-type)
2743 (service console-font-service-type
2744 (map (lambda (tty)
2745 (cons tty %default-console-font))
2746 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
2747
2748 (service agetty-service-type (agetty-configuration
2749 (extra-options '("-L")) ; no carrier detect
2750 (term "vt100")
2751 (tty #f))) ; automatic
2752
2753 (service mingetty-service-type (mingetty-configuration
2754 (tty "tty1")))
2755 (service mingetty-service-type (mingetty-configuration
2756 (tty "tty2")))
2757 (service mingetty-service-type (mingetty-configuration
2758 (tty "tty3")))
2759 (service mingetty-service-type (mingetty-configuration
2760 (tty "tty4")))
2761 (service mingetty-service-type (mingetty-configuration
2762 (tty "tty5")))
2763 (service mingetty-service-type (mingetty-configuration
2764 (tty "tty6")))
2765
2766 (service static-networking-service-type
2767 (list %loopback-static-networking))
2768 (syslog-service)
2769 (service urandom-seed-service-type)
2770 (service guix-service-type)
2771 (service nscd-service-type)
2772
2773 (service rottlog-service-type)
2774
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
2779 (udev-configuration
2780 (rules (list lvm2 fuse alsa-utils crda))))
2781
2782 (service sysctl-service-type)
2783
2784 (service special-files-service-type
2785 `(("/bin/sh" ,(file-append bash "/bin/sh"))
2786 ("/usr/bin/env" ,(file-append coreutils "/bin/env"))))))
2787
2788 ;;; base.scm ends here