services: static-networking: Sanitize <network-address> values.
[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 &fix-hint)
39 #:autoload (guix i18n) (G_)
40 #:use-module (guix combinators)
41 #:use-module (gnu services)
42 #:use-module (gnu services admin)
43 #:use-module (gnu services shepherd)
44 #:use-module (gnu services sysctl)
45 #:use-module (gnu system pam)
46 #:use-module (gnu system shadow) ; 'user-account', etc.
47 #:use-module (gnu system uuid)
48 #:use-module (gnu system file-systems) ; 'file-system', etc.
49 #:use-module (gnu system keyboard)
50 #:use-module (gnu system mapped-devices)
51 #:use-module ((gnu system linux-initrd)
52 #:select (file-system-packages))
53 #:use-module (gnu packages admin)
54 #:use-module ((gnu packages linux)
55 #:select (alsa-utils crda eudev e2fsprogs fuse gpm kbd lvm2 rng-tools))
56 #:use-module (gnu packages bash)
57 #:use-module ((gnu packages base)
58 #:select (coreutils glibc glibc-utf8-locales))
59 #:autoload (gnu packages guile-xyz) (guile-netlink)
60 #:autoload (gnu packages hurd) (hurd)
61 #:use-module (gnu packages package-management)
62 #:use-module ((gnu packages gnupg) #:select (guile-gcrypt))
63 #:use-module (gnu packages linux)
64 #:use-module (gnu packages terminals)
65 #:use-module ((gnu build file-systems)
66 #:select (mount-flags->bit-mask
67 swap-space->flags-bit-mask))
68 #:use-module (guix gexp)
69 #:use-module (guix records)
70 #:use-module (guix modules)
71 #:use-module ((guix self) #:select (make-config.scm))
72 #:use-module (guix diagnostics)
73 #:use-module (guix i18n)
74 #:use-module (srfi srfi-1)
75 #:use-module (srfi srfi-26)
76 #:use-module (srfi srfi-34)
77 #:use-module (srfi srfi-35)
78 #:use-module (ice-9 match)
79 #:use-module (ice-9 format)
80 #:re-export (user-processes-service-type ;backwards compatibility
81 %default-substitute-urls)
82 #:export (fstab-service-type
83 root-file-system-service
84 file-system-service-type
85 swap-service
86 host-name-service
87 %default-console-font
88 console-font-service-type
89 console-font-service
90 virtual-terminal-service-type
91
92 static-networking
93 static-networking?
94 static-networking-addresses
95 static-networking-links
96 static-networking-routes
97 static-networking-requirement
98
99 network-address
100 network-address?
101 network-address-device
102 network-address-value
103 network-address-ipv6?
104
105 network-link
106 network-link?
107 network-link-name
108 network-link-type
109 network-link-arguments
110
111 network-route
112 network-route?
113 network-route-destination
114 network-route-source
115 network-route-device
116 network-route-ipv6?
117 network-route-gateway
118
119 static-networking-service
120 static-networking-service-type
121
122 %loopback-static-networking
123 %qemu-static-networking
124
125 udev-configuration
126 udev-configuration?
127 udev-configuration-rules
128 udev-service-type
129 udev-service
130 udev-rule
131 file->udev-rule
132 udev-rules-service
133
134 login-configuration
135 login-configuration?
136 login-service-type
137 login-service
138
139 agetty-configuration
140 agetty-configuration?
141 agetty-service
142 agetty-service-type
143
144 mingetty-configuration
145 mingetty-configuration-tty
146 mingetty-configuration-auto-login
147 mingetty-configuration-login-program
148 mingetty-configuration-login-pause?
149 mingetty-configuration-clear-on-logout?
150 mingetty-configuration-mingetty
151 mingetty-configuration?
152 mingetty-service
153 mingetty-service-type
154
155 %nscd-default-caches
156 %nscd-default-configuration
157
158 nscd-configuration
159 nscd-configuration?
160
161 nscd-cache
162 nscd-cache?
163
164 nscd-service-type
165 nscd-service
166
167 syslog-configuration
168 syslog-configuration?
169 syslog-service
170 syslog-service-type
171 %default-syslog.conf
172
173 %default-authorized-guix-keys
174 guix-configuration
175 guix-configuration?
176
177 guix-configuration-guix
178 guix-configuration-build-group
179 guix-configuration-build-accounts
180 guix-configuration-authorize-key?
181 guix-configuration-authorized-keys
182 guix-configuration-use-substitutes?
183 guix-configuration-substitute-urls
184 guix-configuration-extra-options
185 guix-configuration-log-file
186
187 guix-service-type
188 guix-publish-configuration
189 guix-publish-configuration?
190 guix-publish-configuration-guix
191 guix-publish-configuration-port
192 guix-publish-configuration-host
193 guix-publish-configuration-compression
194 guix-publish-configuration-compression-level ;deprecated
195 guix-publish-configuration-nar-path
196 guix-publish-configuration-cache
197 guix-publish-configuration-ttl
198 guix-publish-service-type
199
200 gpm-configuration
201 gpm-configuration?
202 gpm-service-type
203
204 urandom-seed-service-type
205
206 rngd-configuration
207 rngd-configuration?
208 rngd-service-type
209 rngd-service
210
211 kmscon-configuration
212 kmscon-configuration?
213 kmscon-service-type
214
215 pam-limits-service-type
216 pam-limits-service
217
218 references-file
219
220 %base-services))
221
222 ;;; Commentary:
223 ;;;
224 ;;; Base system services---i.e., services that 99% of the users will want to
225 ;;; use.
226 ;;;
227 ;;; Code:
228
229
230 \f
231 ;;;
232 ;;; File systems.
233 ;;;
234
235 (define (file-system->fstab-entry file-system)
236 "Return a @file{/etc/fstab} entry for @var{file-system}."
237 (string-append (match (file-system-device file-system)
238 ((? file-system-label? label)
239 (string-append "LABEL="
240 (file-system-label->string label)))
241 ((? uuid? uuid)
242 (string-append "UUID=" (uuid->string uuid)))
243 ((? string? device)
244 device))
245 "\t"
246 (file-system-mount-point file-system) "\t"
247 (file-system-type file-system) "\t"
248 (or (file-system-options file-system) "defaults") "\t"
249
250 ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
251 ;; don't have anything sensible to put in there.
252 ))
253
254 (define (file-systems->fstab file-systems)
255 "Return a @file{/etc} entry for an @file{fstab} describing
256 @var{file-systems}."
257 `(("fstab" ,(plain-file "fstab"
258 (string-append
259 "\
260 # This file was generated from your Guix configuration. Any changes
261 # will be lost upon reboot or reconfiguration.\n\n"
262 (string-join (map file-system->fstab-entry
263 file-systems)
264 "\n")
265 "\n")))))
266
267 (define fstab-service-type
268 ;; The /etc/fstab service.
269 (service-type (name 'fstab)
270 (extensions
271 (list (service-extension etc-service-type
272 file-systems->fstab)))
273 (compose concatenate)
274 (extend append)
275 (description
276 "Populate the @file{/etc/fstab} based on the given file
277 system objects.")))
278
279 (define %root-file-system-shepherd-service
280 (shepherd-service
281 (documentation "Take care of the root file system.")
282 (provision '(root-file-system))
283 (start #~(const #t))
284 (stop #~(lambda _
285 ;; Return #f if successfully stopped.
286 (sync)
287
288 (call-with-blocked-asyncs
289 (lambda ()
290 (let ((null (%make-void-port "w")))
291 ;; Close 'shepherd.log'.
292 (display "closing log\n")
293 ((@ (shepherd comm) stop-logging))
294
295 ;; Redirect the default output ports..
296 (set-current-output-port null)
297 (set-current-error-port null)
298
299 ;; Close /dev/console.
300 (for-each close-fdes '(0 1 2))
301
302 ;; At this point, there are no open files left, so the
303 ;; root file system can be re-mounted read-only.
304 (mount #f "/" #f
305 (logior MS_REMOUNT MS_RDONLY)
306 #:update-mtab? #f)
307
308 #f)))))
309 (respawn? #f)))
310
311 (define root-file-system-service-type
312 (shepherd-service-type 'root-file-system
313 (const %root-file-system-shepherd-service)
314 (description "Take care of syncing the root file
315 system and of remounting it read-only when the system shuts down.")))
316
317 (define (root-file-system-service)
318 "Return a service whose sole purpose is to re-mount read-only the root file
319 system upon shutdown (aka. cleanly \"umounting\" root.)
320
321 This service must be the root of the service dependency graph so that its
322 'stop' action is invoked when shepherd is the only process left."
323 (service root-file-system-service-type #f))
324
325 (define (file-system->shepherd-service-name file-system)
326 "Return the symbol that denotes the service mounting and unmounting
327 FILE-SYSTEM."
328 (symbol-append 'file-system-
329 (string->symbol (file-system-mount-point file-system))))
330
331 (define (mapped-device->shepherd-service-name md)
332 "Return the symbol that denotes the shepherd service of MD, a <mapped-device>."
333 (symbol-append 'device-mapping-
334 (string->symbol (string-join
335 (mapped-device-targets md) "-"))))
336
337 (define dependency->shepherd-service-name
338 (match-lambda
339 ((? mapped-device? md)
340 (mapped-device->shepherd-service-name md))
341 ((? file-system? fs)
342 (file-system->shepherd-service-name fs))))
343
344 (define (file-system-shepherd-service file-system)
345 "Return the shepherd service for @var{file-system}, or @code{#f} if
346 @var{file-system} is not auto-mounted or doesn't have its mount point created
347 upon boot."
348 (let ((target (file-system-mount-point file-system))
349 (create? (file-system-create-mount-point? file-system))
350 (mount? (file-system-mount? file-system))
351 (dependencies (file-system-dependencies file-system))
352 (packages (file-system-packages (list file-system))))
353 (and (or mount? create?)
354 (with-imported-modules (source-module-closure
355 '((gnu build file-systems)))
356 (shepherd-service
357 (provision (list (file-system->shepherd-service-name file-system)))
358 (requirement `(root-file-system
359 udev
360 ,@(map dependency->shepherd-service-name dependencies)))
361 (documentation "Check, mount, and unmount the given file system.")
362 (start #~(lambda args
363 #$(if create?
364 #~(mkdir-p #$target)
365 #t)
366
367 #$(if mount?
368 #~(let (($PATH (getenv "PATH")))
369 ;; Make sure fsck.ext2 & co. can be found.
370 (dynamic-wind
371 (lambda ()
372 ;; Don’t display the PATH settings.
373 (with-output-to-port (%make-void-port "w")
374 (lambda ()
375 (set-path-environment-variable "PATH"
376 '("bin" "sbin")
377 '#$packages))))
378 (lambda ()
379 (mount-file-system
380 (spec->file-system
381 '#$(file-system->spec file-system))
382 #:root "/"))
383 (lambda ()
384 (setenv "PATH" $PATH))))
385 #t)
386 #t))
387 (stop #~(lambda args
388 ;; Normally there are no processes left at this point, so
389 ;; TARGET can be safely unmounted.
390
391 ;; Make sure PID 1 doesn't keep TARGET busy.
392 (chdir "/")
393
394 (umount #$target)
395 #f))
396
397 ;; We need additional modules.
398 (modules `(((gnu build file-systems)
399 #:select (mount-file-system))
400 (gnu system file-systems)
401 ,@%default-modules)))))))
402
403 (define (file-system-shepherd-services file-systems)
404 "Return the list of Shepherd services for FILE-SYSTEMS."
405 (let* ((file-systems (filter (lambda (x)
406 (or (file-system-mount? x)
407 (file-system-create-mount-point? x)))
408 file-systems)))
409 (define sink
410 (shepherd-service
411 (provision '(file-systems))
412 (requirement (cons* 'root-file-system 'user-file-systems
413 (map file-system->shepherd-service-name
414 file-systems)))
415 (documentation "Target for all the initially-mounted file systems")
416 (start #~(const #t))
417 (stop #~(const #f))))
418
419 (define known-mount-points
420 (map file-system-mount-point file-systems))
421
422 (define user-unmount
423 (shepherd-service
424 (documentation "Unmount manually-mounted file systems.")
425 (provision '(user-file-systems))
426 (start #~(const #t))
427 (stop #~(lambda args
428 (define (known? mount-point)
429 (member mount-point
430 (cons* "/proc" "/sys" '#$known-mount-points)))
431
432 ;; Make sure we don't keep the user's mount points busy.
433 (chdir "/")
434
435 (for-each (lambda (mount-point)
436 (format #t "unmounting '~a'...~%" mount-point)
437 (catch 'system-error
438 (lambda ()
439 (umount mount-point))
440 (lambda args
441 (let ((errno (system-error-errno args)))
442 (format #t "failed to unmount '~a': ~a~%"
443 mount-point (strerror errno))))))
444 (filter (negate known?) (mount-points)))
445 #f))))
446
447 (cons* sink user-unmount
448 (map file-system-shepherd-service file-systems))))
449
450 (define (file-system-fstab-entries file-systems)
451 "Return the subset of @var{file-systems} that should have an entry in
452 @file{/etc/fstab}."
453 ;; /etc/fstab is about telling fsck(8), mount(8), and umount(8) about
454 ;; relevant file systems they'll have to deal with. That excludes "pseudo"
455 ;; file systems.
456 ;;
457 ;; In particular, things like GIO (part of GLib) use it to determine the set
458 ;; of mounts, which is then used by graphical file managers and desktop
459 ;; environments to display "volume" icons. Thus, we really need to exclude
460 ;; those pseudo file systems from the list.
461 (remove (lambda (file-system)
462 (or (member (file-system-type file-system)
463 %pseudo-file-system-types)
464 (memq 'bind-mount (file-system-flags file-system))))
465 file-systems))
466
467 (define file-system-service-type
468 (service-type (name 'file-systems)
469 (extensions
470 (list (service-extension shepherd-root-service-type
471 file-system-shepherd-services)
472 (service-extension fstab-service-type
473 file-system-fstab-entries)
474
475 ;; Have 'user-processes' depend on 'file-systems'.
476 (service-extension user-processes-service-type
477 (const '(file-systems)))))
478 (compose concatenate)
479 (extend append)
480 (description
481 "Provide Shepherd services to mount and unmount the given
482 file systems, as well as corresponding @file{/etc/fstab} entries.")))
483
484
485 \f
486 ;;;
487 ;;; Preserve entropy to seed /dev/urandom on boot.
488 ;;;
489
490 (define %random-seed-file
491 "/var/lib/random-seed")
492
493 (define (urandom-seed-shepherd-service _)
494 "Return a shepherd service for the /dev/urandom seed."
495 (list (shepherd-service
496 (documentation "Preserve entropy across reboots for /dev/urandom.")
497 (provision '(urandom-seed))
498
499 ;; Depend on udev so that /dev/hwrng is available.
500 (requirement '(file-systems udev))
501
502 (start #~(lambda _
503 ;; On boot, write random seed into /dev/urandom.
504 (when (file-exists? #$%random-seed-file)
505 (call-with-input-file #$%random-seed-file
506 (lambda (seed)
507 (call-with-output-file "/dev/urandom"
508 (lambda (urandom)
509 (dump-port seed urandom)
510
511 ;; Writing SEED to URANDOM isn't enough: we must
512 ;; also tell the kernel to account for these
513 ;; extra bits of entropy.
514 (let ((bits (* 8 (stat:size (stat seed)))))
515 (add-to-entropy-count urandom bits)))))))
516
517 ;; Try writing from /dev/hwrng into /dev/urandom.
518 ;; It seems that the file /dev/hwrng always exists, even
519 ;; when there is no hardware random number generator
520 ;; available. So, we handle a failed read or any other error
521 ;; reported by the operating system.
522 (let ((buf (catch 'system-error
523 (lambda ()
524 (call-with-input-file "/dev/hwrng"
525 (lambda (hwrng)
526 (get-bytevector-n hwrng 512))))
527 ;; Silence is golden...
528 (const #f))))
529 (when buf
530 (call-with-output-file "/dev/urandom"
531 (lambda (urandom)
532 (put-bytevector urandom buf)
533 (let ((bits (* 8 (bytevector-length buf))))
534 (add-to-entropy-count urandom bits))))))
535
536 ;; Immediately refresh the seed in case the system doesn't
537 ;; shut down cleanly.
538 (call-with-input-file "/dev/urandom"
539 (lambda (urandom)
540 (let ((previous-umask (umask #o077))
541 (buf (make-bytevector 512)))
542 (mkdir-p (dirname #$%random-seed-file))
543 (get-bytevector-n! urandom buf 0 512)
544 (call-with-output-file #$%random-seed-file
545 (lambda (seed)
546 (put-bytevector seed buf)))
547 (umask previous-umask))))
548 #t))
549 (stop #~(lambda _
550 ;; During shutdown, write from /dev/urandom into random seed.
551 (let ((buf (make-bytevector 512)))
552 (call-with-input-file "/dev/urandom"
553 (lambda (urandom)
554 (let ((previous-umask (umask #o077)))
555 (get-bytevector-n! urandom buf 0 512)
556 (mkdir-p (dirname #$%random-seed-file))
557 (call-with-output-file #$%random-seed-file
558 (lambda (seed)
559 (put-bytevector seed buf)))
560 (umask previous-umask))
561 #t)))))
562 (modules `((rnrs bytevectors)
563 (rnrs io ports)
564 ,@%default-modules)))))
565
566 (define urandom-seed-service-type
567 (service-type (name 'urandom-seed)
568 (extensions
569 (list (service-extension shepherd-root-service-type
570 urandom-seed-shepherd-service)
571
572 ;; Have 'user-processes' depend on 'urandom-seed'.
573 ;; This ensures that user processes and daemons don't
574 ;; start until we have seeded the PRNG.
575 (service-extension user-processes-service-type
576 (const '(urandom-seed)))))
577 (default-value #f)
578 (description
579 "Seed the @file{/dev/urandom} pseudo-random number
580 generator (RNG) with the value recorded when the system was last shut
581 down.")))
582
583
584 ;;;
585 ;;; Add hardware random number generator to entropy pool.
586 ;;;
587
588 (define-record-type* <rngd-configuration>
589 rngd-configuration make-rngd-configuration
590 rngd-configuration?
591 (rng-tools rngd-configuration-rng-tools) ;file-like
592 (device rngd-configuration-device)) ;string
593
594 (define rngd-service-type
595 (shepherd-service-type
596 'rngd
597 (lambda (config)
598 (define rng-tools (rngd-configuration-rng-tools config))
599 (define device (rngd-configuration-device config))
600
601 (define rngd-command
602 (list (file-append rng-tools "/sbin/rngd")
603 "-f" "-r" device))
604
605 (shepherd-service
606 (documentation "Add TRNG to entropy pool.")
607 (requirement '(udev))
608 (provision '(trng))
609 (start #~(make-forkexec-constructor '#$rngd-command))
610 (stop #~(make-kill-destructor))))
611 (description "Run the @command{rngd} random number generation daemon to
612 supply entropy to the kernel's pool.")))
613
614 (define* (rngd-service #:key
615 (rng-tools rng-tools)
616 (device "/dev/hwrng"))
617 "Return a service that runs the @command{rngd} program from @var{rng-tools}
618 to add @var{device} to the kernel's entropy pool. The service will fail if
619 @var{device} does not exist."
620 (service rngd-service-type
621 (rngd-configuration
622 (rng-tools rng-tools)
623 (device device))))
624
625 \f
626 ;;;
627 ;;; Console & co.
628 ;;;
629
630 (define host-name-service-type
631 (shepherd-service-type
632 'host-name
633 (lambda (name)
634 (shepherd-service
635 (documentation "Initialize the machine's host name.")
636 (provision '(host-name))
637 (start #~(lambda _
638 (sethostname #$name)))
639 (one-shot? #t)))
640 (description "Initialize the machine's host name.")))
641
642 (define (host-name-service name)
643 "Return a service that sets the host name to @var{name}."
644 (service host-name-service-type name))
645
646 (define virtual-terminal-service-type
647 ;; Ensure that virtual terminals run in UTF-8 mode. This is the case by
648 ;; default with recent Linux kernels, but this service allows us to ensure
649 ;; this. This service must start before any 'term-' service so that newly
650 ;; created terminals inherit this property. See
651 ;; <https://bugs.gnu.org/30505> for a discussion.
652 (shepherd-service-type
653 'virtual-terminal
654 (lambda (utf8?)
655 (let ((knob "/sys/module/vt/parameters/default_utf8"))
656 (shepherd-service
657 (documentation "Set virtual terminals in UTF-8 module.")
658 (provision '(virtual-terminal))
659 (requirement '(root-file-system))
660 (start #~(lambda _
661 ;; In containers /sys is read-only so don't insist on
662 ;; writing to this file.
663 (unless (= 1 (call-with-input-file #$knob read))
664 (call-with-output-file #$knob
665 (lambda (port)
666 (display 1 port))))
667 #t))
668 (stop #~(const #f)))))
669 #t ;default to UTF-8
670 (description "Ensure the Linux virtual terminals run in UTF-8 mode.")))
671
672 (define console-keymap-service-type
673 (shepherd-service-type
674 'console-keymap
675 (lambda (files)
676 (shepherd-service
677 (documentation (string-append "Load console keymap (loadkeys)."))
678 (provision '(console-keymap))
679 (start #~(lambda _
680 (zero? (system* #$(file-append kbd "/bin/loadkeys")
681 #$@files))))
682 (respawn? #f)))
683 (description "@emph{This service is deprecated in favor of the
684 @code{keyboard-layout} field of @code{operating-system}.} Load the given list
685 of console keymaps with @command{loadkeys}.")))
686
687 (define %default-console-font
688 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
689 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
690 ;; codepoints notably found in the UTF-8 manual.
691 "LatGrkCyr-8x16")
692
693 (define (console-font-shepherd-services tty+font)
694 "Return a list of Shepherd services for each pair in TTY+FONT."
695 (map (match-lambda
696 ((tty . font)
697 (let ((device (string-append "/dev/" tty)))
698 (shepherd-service
699 (documentation "Load a Unicode console font.")
700 (provision (list (symbol-append 'console-font-
701 (string->symbol tty))))
702
703 ;; Start after mingetty has been started on TTY, otherwise the settings
704 ;; are ignored.
705 (requirement (list (symbol-append 'term-
706 (string->symbol tty))))
707
708 (start #~(lambda _
709 ;; It could be that mingetty is not fully ready yet,
710 ;; which we check by calling 'ttyname'.
711 (let loop ((i 10))
712 (unless (or (zero? i)
713 (call-with-input-file #$device
714 (lambda (port)
715 (false-if-exception (ttyname port)))))
716 (usleep 500)
717 (loop (- i 1))))
718
719 ;; Assume the VT is already in UTF-8 mode, thanks to
720 ;; the 'virtual-terminal' service.
721 ;;
722 ;; 'setfont' returns EX_OSERR (71) when an
723 ;; KDFONTOP ioctl fails, for example. Like
724 ;; systemd's vconsole support, let's not treat
725 ;; this as an error.
726 (case (status:exit-val
727 (system* #$(file-append kbd "/bin/setfont")
728 "-C" #$device #$font))
729 ((0 71) #t)
730 (else #f))))
731 (stop #~(const #t))
732 (respawn? #f)))))
733 tty+font))
734
735 (define console-font-service-type
736 (service-type (name 'console-fonts)
737 (extensions
738 (list (service-extension shepherd-root-service-type
739 console-font-shepherd-services)))
740 (compose concatenate)
741 (extend append)
742 (description
743 "Install the given fonts on the specified ttys (fonts are per
744 virtual console on GNU/Linux). The value of this service is a list of
745 tty/font pairs. The font can be the name of a font provided by the @code{kbd}
746 package or any valid argument to @command{setfont}, as in this example:
747
748 @example
749 `((\"tty1\" . \"LatGrkCyr-8x16\")
750 (\"tty2\" . ,(file-append
751 font-tamzen
752 \"/share/kbd/consolefonts/TamzenForPowerline10x20.psf\"))
753 (\"tty3\" . ,(file-append
754 font-terminus
755 \"/share/consolefonts/ter-132n\"))) ; for HDPI
756 @end example\n")))
757
758 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
759 "This procedure is deprecated in favor of @code{console-font-service-type}.
760
761 Return a service that sets up Unicode support in @var{tty} and loads
762 @var{font} for that tty (fonts are per virtual console in Linux.)"
763 (simple-service (symbol-append 'console-font- (string->symbol tty))
764 console-font-service-type `((,tty . ,font))))
765
766 (define %default-motd
767 (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
768
769 (define-record-type* <login-configuration>
770 login-configuration make-login-configuration
771 login-configuration?
772 (motd login-configuration-motd ;file-like
773 (default %default-motd))
774 ;; Allow empty passwords by default so that first-time users can log in when
775 ;; the 'root' account has just been created.
776 (allow-empty-passwords? login-configuration-allow-empty-passwords?
777 (default #t))) ;Boolean
778
779 (define (login-pam-service config)
780 "Return the list of PAM service needed for CONF."
781 ;; Let 'login' be known to PAM.
782 (list (unix-pam-service "login"
783 #:login-uid? #t
784 #:allow-empty-passwords?
785 (login-configuration-allow-empty-passwords? config)
786 #:motd
787 (login-configuration-motd config))))
788
789 (define login-service-type
790 (service-type (name 'login)
791 (extensions (list (service-extension pam-root-service-type
792 login-pam-service)))
793 (default-value (login-configuration))
794 (description
795 "Provide a console log-in service as specified by its
796 configuration value, a @code{login-configuration} object.")))
797
798 (define* (login-service #:optional (config (login-configuration)))
799 "Return a service configure login according to @var{config}, which specifies
800 the message of the day, among other things."
801 (service login-service-type config))
802
803 (define-record-type* <agetty-configuration>
804 agetty-configuration make-agetty-configuration
805 agetty-configuration?
806 (agetty agetty-configuration-agetty ;file-like
807 (default util-linux))
808 (tty agetty-configuration-tty) ;string | #f
809 (term agetty-term ;string | #f
810 (default #f))
811 (baud-rate agetty-baud-rate ;string | #f
812 (default #f))
813 (auto-login agetty-auto-login ;list of strings | #f
814 (default #f))
815 (login-program agetty-login-program ;gexp
816 (default (file-append shadow "/bin/login")))
817 (login-pause? agetty-login-pause? ;Boolean
818 (default #f))
819 (eight-bits? agetty-eight-bits? ;Boolean
820 (default #f))
821 (no-reset? agetty-no-reset? ;Boolean
822 (default #f))
823 (remote? agetty-remote? ;Boolean
824 (default #f))
825 (flow-control? agetty-flow-control? ;Boolean
826 (default #f))
827 (host agetty-host ;string | #f
828 (default #f))
829 (no-issue? agetty-no-issue? ;Boolean
830 (default #f))
831 (init-string agetty-init-string ;string | #f
832 (default #f))
833 (no-clear? agetty-no-clear? ;Boolean
834 (default #f))
835 (local-line agetty-local-line ;always | never | auto
836 (default #f))
837 (extract-baud? agetty-extract-baud? ;Boolean
838 (default #f))
839 (skip-login? agetty-skip-login? ;Boolean
840 (default #f))
841 (no-newline? agetty-no-newline? ;Boolean
842 (default #f))
843 (login-options agetty-login-options ;string | #f
844 (default #f))
845 (chroot agetty-chroot ;string | #f
846 (default #f))
847 (hangup? agetty-hangup? ;Boolean
848 (default #f))
849 (keep-baud? agetty-keep-baud? ;Boolean
850 (default #f))
851 (timeout agetty-timeout ;integer | #f
852 (default #f))
853 (detect-case? agetty-detect-case? ;Boolean
854 (default #f))
855 (wait-cr? agetty-wait-cr? ;Boolean
856 (default #f))
857 (no-hints? agetty-no-hints? ;Boolean
858 (default #f))
859 (no-hostname? agetty-no hostname? ;Boolean
860 (default #f))
861 (long-hostname? agetty-long-hostname? ;Boolean
862 (default #f))
863 (erase-characters agetty-erase-characters ;string | #f
864 (default #f))
865 (kill-characters agetty-kill-characters ;string | #f
866 (default #f))
867 (chdir agetty-chdir ;string | #f
868 (default #f))
869 (delay agetty-delay ;integer | #f
870 (default #f))
871 (nice agetty-nice ;integer | #f
872 (default #f))
873 ;; "Escape hatch" for passing arbitrary command-line arguments.
874 (extra-options agetty-extra-options ;list of strings
875 (default '()))
876 ;;; XXX Unimplemented for now!
877 ;;; (issue-file agetty-issue-file ;file-like
878 ;;; (default #f))
879 )
880
881 (define (default-serial-port)
882 "Return a gexp that determines a reasonable default serial port
883 to use as the tty. This is primarily useful for headless systems."
884 (with-imported-modules (source-module-closure
885 '((gnu build linux-boot))) ;for 'find-long-options'
886 #~(begin
887 ;; console=device,options
888 ;; device: can be tty0, ttyS0, lp0, ttyUSB0 (serial).
889 ;; options: BBBBPNF. P n|o|e, N number of bits,
890 ;; F flow control (r RTS)
891 (let* ((not-comma (char-set-complement (char-set #\,)))
892 (command (linux-command-line))
893 (agetty-specs (find-long-options "agetty.tty" command))
894 (console-specs (filter (lambda (spec)
895 (and (string-prefix? "tty" spec)
896 (not (or
897 (string-prefix? "tty0" spec)
898 (string-prefix? "tty1" spec)
899 (string-prefix? "tty2" spec)
900 (string-prefix? "tty3" spec)
901 (string-prefix? "tty4" spec)
902 (string-prefix? "tty5" spec)
903 (string-prefix? "tty6" spec)
904 (string-prefix? "tty7" spec)
905 (string-prefix? "tty8" spec)
906 (string-prefix? "tty9" spec)))))
907 (find-long-options "console" command)))
908 (specs (append agetty-specs console-specs)))
909 (match specs
910 (() #f)
911 ((spec _ ...)
912 ;; Extract device name from first spec.
913 (match (string-tokenize spec not-comma)
914 ((device-name _ ...)
915 device-name))))))))
916
917 (define agetty-shepherd-service
918 (match-lambda
919 (($ <agetty-configuration> agetty tty term baud-rate auto-login
920 login-program login-pause? eight-bits? no-reset? remote? flow-control?
921 host no-issue? init-string no-clear? local-line extract-baud?
922 skip-login? no-newline? login-options chroot hangup? keep-baud? timeout
923 detect-case? wait-cr? no-hints? no-hostname? long-hostname?
924 erase-characters kill-characters chdir delay nice extra-options)
925 (list
926 (shepherd-service
927 (documentation "Run agetty on a tty.")
928 (provision (list (symbol-append 'term- (string->symbol (or tty "auto")))))
929
930 ;; Since the login prompt shows the host name, wait for the 'host-name'
931 ;; service to be done. Also wait for udev essentially so that the tty
932 ;; text is not lost in the middle of kernel messages (see also
933 ;; mingetty-shepherd-service).
934 (requirement '(user-processes host-name udev))
935
936 (modules '((ice-9 match) (gnu build linux-boot)))
937 (start
938 (with-imported-modules (source-module-closure
939 '((gnu build linux-boot)))
940 #~(lambda args
941 (let ((defaulted-tty #$(or tty (default-serial-port))))
942 (apply
943 (if defaulted-tty
944 (make-forkexec-constructor
945 (list #$(file-append util-linux "/sbin/agetty")
946 #$@extra-options
947 #$@(if eight-bits?
948 #~("--8bits")
949 #~())
950 #$@(if no-reset?
951 #~("--noreset")
952 #~())
953 #$@(if remote?
954 #~("--remote")
955 #~())
956 #$@(if flow-control?
957 #~("--flow-control")
958 #~())
959 #$@(if host
960 #~("--host" #$host)
961 #~())
962 #$@(if no-issue?
963 #~("--noissue")
964 #~())
965 #$@(if init-string
966 #~("--init-string" #$init-string)
967 #~())
968 #$@(if no-clear?
969 #~("--noclear")
970 #~())
971 ;;; FIXME This doesn't work as expected. According to agetty(8), if this option
972 ;;; is not passed, then the default is 'auto'. However, in my tests, when that
973 ;;; option is selected, agetty never presents the login prompt, and the
974 ;;; term-ttyS0 service respawns every few seconds.
975 #$@(if local-line
976 #~(#$(match local-line
977 ('auto "--local-line=auto")
978 ('always "--local-line=always")
979 ('never "-local-line=never")))
980 #~())
981 #$@(if tty
982 #~()
983 #~("--keep-baud"))
984 #$@(if extract-baud?
985 #~("--extract-baud")
986 #~())
987 #$@(if skip-login?
988 #~("--skip-login")
989 #~())
990 #$@(if no-newline?
991 #~("--nonewline")
992 #~())
993 #$@(if login-options
994 #~("--login-options" #$login-options)
995 #~())
996 #$@(if chroot
997 #~("--chroot" #$chroot)
998 #~())
999 #$@(if hangup?
1000 #~("--hangup")
1001 #~())
1002 #$@(if keep-baud?
1003 #~("--keep-baud")
1004 #~())
1005 #$@(if timeout
1006 #~("--timeout" #$(number->string timeout))
1007 #~())
1008 #$@(if detect-case?
1009 #~("--detect-case")
1010 #~())
1011 #$@(if wait-cr?
1012 #~("--wait-cr")
1013 #~())
1014 #$@(if no-hints?
1015 #~("--nohints?")
1016 #~())
1017 #$@(if no-hostname?
1018 #~("--nohostname")
1019 #~())
1020 #$@(if long-hostname?
1021 #~("--long-hostname")
1022 #~())
1023 #$@(if erase-characters
1024 #~("--erase-chars" #$erase-characters)
1025 #~())
1026 #$@(if kill-characters
1027 #~("--kill-chars" #$kill-characters)
1028 #~())
1029 #$@(if chdir
1030 #~("--chdir" #$chdir)
1031 #~())
1032 #$@(if delay
1033 #~("--delay" #$(number->string delay))
1034 #~())
1035 #$@(if nice
1036 #~("--nice" #$(number->string nice))
1037 #~())
1038 #$@(if auto-login
1039 (list "--autologin" auto-login)
1040 '())
1041 #$@(if login-program
1042 #~("--login-program" #$login-program)
1043 #~())
1044 #$@(if login-pause?
1045 #~("--login-pause")
1046 #~())
1047 defaulted-tty
1048 #$@(if baud-rate
1049 #~(#$baud-rate)
1050 #~())
1051 #$@(if term
1052 #~(#$term)
1053 #~())))
1054 (const #f)) ; never start.
1055 args)))))
1056 (stop #~(make-kill-destructor)))))))
1057
1058 (define agetty-service-type
1059 (service-type (name 'agetty)
1060 (extensions (list (service-extension shepherd-root-service-type
1061 agetty-shepherd-service)))
1062 (description
1063 "Provide console login using the @command{agetty}
1064 program.")))
1065
1066 (define* (agetty-service config)
1067 "Return a service to run agetty according to @var{config}, which specifies
1068 the tty to run, among other things."
1069 (service agetty-service-type config))
1070
1071 (define-record-type* <mingetty-configuration>
1072 mingetty-configuration make-mingetty-configuration
1073 mingetty-configuration?
1074 (mingetty mingetty-configuration-mingetty ;file-like
1075 (default mingetty))
1076 (tty mingetty-configuration-tty) ;string
1077 (auto-login mingetty-auto-login ;string | #f
1078 (default #f))
1079 (login-program mingetty-login-program ;gexp
1080 (default #f))
1081 (login-pause? mingetty-login-pause? ;Boolean
1082 (default #f))
1083 (clear-on-logout? mingetty-clear-on-logout? ;Boolean
1084 (default #t)))
1085
1086 (define mingetty-shepherd-service
1087 (match-lambda
1088 (($ <mingetty-configuration> mingetty tty auto-login login-program
1089 login-pause? clear-on-logout?)
1090 (list
1091 (shepherd-service
1092 (documentation "Run mingetty on an tty.")
1093 (provision (list (symbol-append 'term- (string->symbol tty))))
1094
1095 ;; Since the login prompt shows the host name, wait for the 'host-name'
1096 ;; service to be done. Also wait for udev essentially so that the tty
1097 ;; text is not lost in the middle of kernel messages (XXX).
1098 (requirement '(user-processes host-name udev virtual-terminal))
1099
1100 (start #~(make-forkexec-constructor
1101 (list #$(file-append mingetty "/sbin/mingetty")
1102
1103 ;; Avoiding 'vhangup' allows us to avoid 'setfont'
1104 ;; errors down the path where various ioctls get
1105 ;; EIO--see 'hung_up_tty_ioctl' in driver/tty/tty_io.c
1106 ;; in Linux.
1107 "--nohangup" #$tty
1108
1109 #$@(if clear-on-logout?
1110 #~()
1111 #~("--noclear"))
1112 #$@(if auto-login
1113 #~("--autologin" #$auto-login)
1114 #~())
1115 #$@(if login-program
1116 #~("--loginprog" #$login-program)
1117 #~())
1118 #$@(if login-pause?
1119 #~("--loginpause")
1120 #~()))))
1121 (stop #~(make-kill-destructor)))))))
1122
1123 (define mingetty-service-type
1124 (service-type (name 'mingetty)
1125 (extensions (list (service-extension shepherd-root-service-type
1126 mingetty-shepherd-service)))
1127 (description
1128 "Provide console login using the @command{mingetty}
1129 program.")))
1130
1131 (define* (mingetty-service config)
1132 "Return a service to run mingetty according to @var{config}, which specifies
1133 the tty to run, among other things."
1134 (service mingetty-service-type config))
1135
1136 (define-record-type* <nscd-configuration> nscd-configuration
1137 make-nscd-configuration
1138 nscd-configuration?
1139 (log-file nscd-configuration-log-file ;string
1140 (default "/var/log/nscd.log"))
1141 (debug-level nscd-debug-level ;integer
1142 (default 0))
1143 ;; TODO: See nscd.conf in glibc for other options to add.
1144 (caches nscd-configuration-caches ;list of <nscd-cache>
1145 (default %nscd-default-caches))
1146 (name-services nscd-configuration-name-services ;list of file-like
1147 (default '()))
1148 (glibc nscd-configuration-glibc ;file-like
1149 (default glibc)))
1150
1151 (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
1152 nscd-cache?
1153 (database nscd-cache-database) ;symbol
1154 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
1155 (negative-time-to-live nscd-cache-negative-time-to-live
1156 (default 20)) ;integer
1157 (suggested-size nscd-cache-suggested-size ;integer ("default module
1158 ;of hash table")
1159 (default 211))
1160 (check-files? nscd-cache-check-files? ;Boolean
1161 (default #t))
1162 (persistent? nscd-cache-persistent? ;Boolean
1163 (default #t))
1164 (shared? nscd-cache-shared? ;Boolean
1165 (default #t))
1166 (max-database-size nscd-cache-max-database-size ;integer
1167 (default (* 32 (expt 2 20))))
1168 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
1169 (default #t)))
1170
1171 (define %nscd-default-caches
1172 ;; Caches that we want to enable by default. Note that when providing an
1173 ;; empty nscd.conf, all caches are disabled.
1174 (list (nscd-cache (database 'hosts)
1175
1176 ;; Aggressively cache the host name cache to improve
1177 ;; privacy and resilience.
1178 (positive-time-to-live (* 3600 12))
1179 (negative-time-to-live 20)
1180 (persistent? #t))
1181
1182 (nscd-cache (database 'services)
1183
1184 ;; Services are unlikely to change, so we can be even more
1185 ;; aggressive.
1186 (positive-time-to-live (* 3600 24))
1187 (negative-time-to-live 3600)
1188 (check-files? #t) ;check /etc/services changes
1189 (persistent? #t))))
1190
1191 (define %nscd-default-configuration
1192 ;; Default nscd configuration.
1193 (nscd-configuration))
1194
1195 (define (nscd.conf-file config)
1196 "Return the @file{nscd.conf} configuration file for @var{config}, an
1197 @code{<nscd-configuration>} object."
1198 (define cache->config
1199 (match-lambda
1200 (($ <nscd-cache> (= symbol->string database)
1201 positive-ttl negative-ttl size check-files?
1202 persistent? shared? max-size propagate?)
1203 (string-append "\nenable-cache\t" database "\tyes\n"
1204
1205 "positive-time-to-live\t" database "\t"
1206 (number->string positive-ttl) "\n"
1207 "negative-time-to-live\t" database "\t"
1208 (number->string negative-ttl) "\n"
1209 "suggested-size\t" database "\t"
1210 (number->string size) "\n"
1211 "check-files\t" database "\t"
1212 (if check-files? "yes\n" "no\n")
1213 "persistent\t" database "\t"
1214 (if persistent? "yes\n" "no\n")
1215 "shared\t" database "\t"
1216 (if shared? "yes\n" "no\n")
1217 "max-db-size\t" database "\t"
1218 (number->string max-size) "\n"
1219 "auto-propagate\t" database "\t"
1220 (if propagate? "yes\n" "no\n")))))
1221
1222 (match config
1223 (($ <nscd-configuration> log-file debug-level caches)
1224 (plain-file "nscd.conf"
1225 (string-append "\
1226 # Configuration of libc's name service cache daemon (nscd).\n\n"
1227 (if log-file
1228 (string-append "logfile\t" log-file)
1229 "")
1230 "\n"
1231 (if debug-level
1232 (string-append "debug-level\t"
1233 (number->string debug-level))
1234 "")
1235 "\n"
1236 (string-concatenate
1237 (map cache->config caches)))))))
1238
1239 (define (nscd-action-procedure nscd config option)
1240 ;; XXX: This is duplicated from mcron; factorize.
1241 #~(lambda (_ . args)
1242 ;; Run 'nscd' in a pipe so we can explicitly redirect its output to
1243 ;; 'current-output-port', which at this stage is bound to the client
1244 ;; connection.
1245 (let ((pipe (apply open-pipe* OPEN_READ #$nscd
1246 "-f" #$config #$option args)))
1247 (let loop ()
1248 (match (read-line pipe 'concat)
1249 ((? eof-object?)
1250 (catch 'system-error
1251 (lambda ()
1252 (zero? (close-pipe pipe)))
1253 (lambda args
1254 ;; There's a race with the SIGCHLD handler, which could
1255 ;; call 'waitpid' before 'close-pipe' above does. If we
1256 ;; get ECHILD, that means we lost the race; in that case, we
1257 ;; cannot tell what the exit code was (FIXME).
1258 (or (= ECHILD (system-error-errno args))
1259 (apply throw args)))))
1260 (line
1261 (display line)
1262 (loop)))))))
1263
1264 (define (nscd-actions nscd config)
1265 "Return Shepherd actions for NSCD."
1266 ;; Make this functionality available as actions because that's a simple way
1267 ;; to run the right 'nscd' binary with the right config file.
1268 (list (shepherd-action
1269 (name 'statistics)
1270 (documentation "Display statistics about nscd usage.")
1271 (procedure (nscd-action-procedure nscd config "--statistics")))
1272 (shepherd-action
1273 (name 'invalidate)
1274 (documentation
1275 "Invalidate the given cache--e.g., 'hosts' for host name lookups.")
1276 (procedure (nscd-action-procedure nscd config "--invalidate")))))
1277
1278 (define (nscd-shepherd-service config)
1279 "Return a shepherd service for CONFIG, an <nscd-configuration> object."
1280 (let ((nscd (file-append (nscd-configuration-glibc config)
1281 "/sbin/nscd"))
1282 (nscd.conf (nscd.conf-file config))
1283 (name-services (nscd-configuration-name-services config)))
1284 (list (shepherd-service
1285 (documentation "Run libc's name service cache daemon (nscd).")
1286 (provision '(nscd))
1287 (requirement '(user-processes))
1288 (start #~(make-forkexec-constructor
1289 (list #$nscd "-f" #$nscd.conf "--foreground")
1290
1291 ;; Wait for the PID file. However, the PID file is
1292 ;; written before nscd is actually listening on its
1293 ;; socket (XXX).
1294 #:pid-file "/var/run/nscd/nscd.pid"
1295
1296 #:environment-variables
1297 (list (string-append "LD_LIBRARY_PATH="
1298 (string-join
1299 (map (lambda (dir)
1300 (string-append dir "/lib"))
1301 (list #$@name-services))
1302 ":")))))
1303 (stop #~(make-kill-destructor))
1304 (modules `((ice-9 popen) ;for the actions
1305 (ice-9 rdelim)
1306 (ice-9 match)
1307 ,@%default-modules))
1308 (actions (nscd-actions nscd nscd.conf))))))
1309
1310 (define nscd-activation
1311 ;; Actions to take before starting nscd.
1312 #~(begin
1313 (use-modules (guix build utils))
1314 (mkdir-p "/var/run/nscd")
1315 (mkdir-p "/var/db/nscd") ;for the persistent cache
1316
1317 ;; In libc 2.25 nscd uses inotify to watch /etc/resolv.conf, but only if
1318 ;; that file exists when it is started. Thus create it here. Note: on
1319 ;; some systems, such as when NetworkManager is used, /etc/resolv.conf
1320 ;; is a symlink, hence 'lstat'.
1321 (unless (false-if-exception (lstat "/etc/resolv.conf"))
1322 (call-with-output-file "/etc/resolv.conf"
1323 (lambda (port)
1324 (display "# This is a placeholder.\n" port))))))
1325
1326 (define nscd-service-type
1327 (service-type (name 'nscd)
1328 (extensions
1329 (list (service-extension activation-service-type
1330 (const nscd-activation))
1331 (service-extension shepherd-root-service-type
1332 nscd-shepherd-service)))
1333
1334 ;; This can be extended by providing additional name services
1335 ;; such as nss-mdns.
1336 (compose concatenate)
1337 (extend (lambda (config name-services)
1338 (nscd-configuration
1339 (inherit config)
1340 (name-services (append
1341 (nscd-configuration-name-services config)
1342 name-services)))))
1343 (default-value %nscd-default-configuration)
1344 (description
1345 "Runs libc's @dfn{name service cache daemon} (nscd) with the
1346 given configuration---an @code{<nscd-configuration>} object. @xref{Name
1347 Service Switch}, for an example.")))
1348
1349 (define* (nscd-service #:optional (config %nscd-default-configuration))
1350 "Return a service that runs libc's name service cache daemon (nscd) with the
1351 given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
1352 Service Switch}, for an example."
1353 (service nscd-service-type config))
1354
1355
1356 (define-record-type* <syslog-configuration>
1357 syslog-configuration make-syslog-configuration
1358 syslog-configuration?
1359 (syslogd syslog-configuration-syslogd
1360 (default (file-append inetutils "/libexec/syslogd")))
1361 (config-file syslog-configuration-config-file
1362 (default %default-syslog.conf)))
1363
1364 (define syslog-service-type
1365 (shepherd-service-type
1366 'syslog
1367 (lambda (config)
1368 (shepherd-service
1369 (documentation "Run the syslog daemon (syslogd).")
1370 (provision '(syslogd))
1371 (requirement '(user-processes))
1372 (start #~(let ((spawn (make-forkexec-constructor
1373 (list #$(syslog-configuration-syslogd config)
1374 "--rcfile"
1375 #$(syslog-configuration-config-file config))
1376 #:pid-file "/var/run/syslog.pid")))
1377 (lambda ()
1378 ;; Set the umask such that file permissions are #o640.
1379 (let ((mask (umask #o137))
1380 (pid (spawn)))
1381 (umask mask)
1382 pid))))
1383 (stop #~(make-kill-destructor))))
1384 (description "Run the syslog daemon, @command{syslogd}, which is
1385 responsible for logging system messages.")))
1386
1387 ;; Snippet adapted from the GNU inetutils manual.
1388 (define %default-syslog.conf
1389 (plain-file "syslog.conf" "
1390 # Log all error messages, authentication messages of
1391 # level notice or higher and anything of level err or
1392 # higher to the console.
1393 # Don't log private authentication messages!
1394 *.alert;auth.notice;authpriv.none /dev/console
1395
1396 # Log anything (except mail) of level info or higher.
1397 # Don't log private authentication messages!
1398 *.info;mail.none;authpriv.none /var/log/messages
1399
1400 # Like /var/log/messages, but also including \"debug\"-level logs.
1401 *.debug;mail.none;authpriv.none /var/log/debug
1402
1403 # Same, in a different place.
1404 *.info;mail.none;authpriv.none /dev/tty12
1405
1406 # The authpriv file has restricted access.
1407 authpriv.* /var/log/secure
1408
1409 # Log all the mail messages in one place.
1410 mail.* /var/log/maillog
1411 "))
1412
1413 (define* (syslog-service #:optional (config (syslog-configuration)))
1414 "Return a service that runs @command{syslogd} and takes
1415 @var{<syslog-configuration>} as a parameter.
1416
1417 @xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
1418 information on the configuration file syntax."
1419 (service syslog-service-type config))
1420
1421
1422 (define pam-limits-service-type
1423 (let ((security-limits
1424 ;; Create /etc/security containing the provided "limits.conf" file.
1425 (lambda (limits-file)
1426 `(("security/limits.conf"
1427 ,limits-file))))
1428 (pam-extension
1429 (lambda (pam)
1430 (let ((pam-limits (pam-entry
1431 (control "required")
1432 (module "pam_limits.so")
1433 (arguments '("conf=/etc/security/limits.conf")))))
1434 (if (member (pam-service-name pam)
1435 '("login" "su" "slim" "gdm-password" "sddm"))
1436 (pam-service
1437 (inherit pam)
1438 (session (cons pam-limits
1439 (pam-service-session pam))))
1440 pam)))))
1441 (service-type
1442 (name 'limits)
1443 (extensions
1444 (list (service-extension etc-service-type security-limits)
1445 (service-extension pam-root-service-type
1446 (lambda _ (list pam-extension)))))
1447 (description
1448 "Install the specified resource usage limits by populating
1449 @file{/etc/security/limits.conf} and using the @code{pam_limits}
1450 authentication module."))))
1451
1452 (define* (pam-limits-service #:optional (limits '()))
1453 "Return a service that makes selected programs respect the list of
1454 pam-limits-entry specified in LIMITS via pam_limits.so."
1455 (service pam-limits-service-type
1456 (plain-file "limits.conf"
1457 (string-join (map pam-limits-entry->string limits)
1458 "\n"))))
1459
1460 \f
1461 ;;;
1462 ;;; Guix services.
1463 ;;;
1464
1465 (define* (guix-build-accounts count #:key
1466 (group "guixbuild")
1467 (shadow shadow))
1468 "Return a list of COUNT user accounts for Guix build users with the given
1469 GID."
1470 (unfold (cut > <> count)
1471 (lambda (n)
1472 (user-account
1473 (name (format #f "guixbuilder~2,'0d" n))
1474 (system? #t)
1475 (group group)
1476
1477 ;; guix-daemon expects GROUP to be listed as a
1478 ;; supplementary group too:
1479 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
1480 (supplementary-groups (list group "kvm"))
1481
1482 (comment (format #f "Guix Build User ~2d" n))
1483 (home-directory "/var/empty")
1484 (shell (file-append shadow "/sbin/nologin"))))
1485 1+
1486 1))
1487
1488 (define not-config?
1489 ;; Select (guix …) and (gnu …) modules, except (guix config).
1490 (match-lambda
1491 (('guix 'config) #f)
1492 (('guix rest ...) #t)
1493 (('gnu rest ...) #t)
1494 (rest #f)))
1495
1496 (define (substitute-key-authorization keys guix)
1497 "Return a gexp with code to register KEYS, a list of files containing 'guix
1498 archive' public keys, with GUIX."
1499 (define default-acl
1500 (with-extensions (list guile-gcrypt)
1501 (with-imported-modules `(((guix config) => ,(make-config.scm))
1502 ,@(source-module-closure '((guix pki))
1503 #:select? not-config?))
1504 (computed-file "acl"
1505 #~(begin
1506 (use-modules (guix pki)
1507 (gcrypt pk-crypto)
1508 (ice-9 rdelim))
1509
1510 (define keys
1511 (map (lambda (file)
1512 (call-with-input-file file
1513 (compose string->canonical-sexp
1514 read-string)))
1515 '(#$@keys)))
1516
1517 (call-with-output-file #$output
1518 (lambda (port)
1519 (write-acl (public-keys->acl keys)
1520 port))))))))
1521
1522 (with-imported-modules '((guix build utils))
1523 #~(begin
1524 (use-modules (guix build utils))
1525
1526 ;; If the ACL already exists, move it out of the way. Create a backup
1527 ;; if it's a regular file: it's likely that the user manually updated
1528 ;; it with 'guix archive --authorize'.
1529 (if (file-exists? "/etc/guix/acl")
1530 (if (and (symbolic-link? "/etc/guix/acl")
1531 (store-file-name? (readlink "/etc/guix/acl")))
1532 (delete-file "/etc/guix/acl")
1533 (rename-file "/etc/guix/acl" "/etc/guix/acl.bak"))
1534 (mkdir-p "/etc/guix"))
1535
1536 ;; Installed the declared ACL.
1537 (symlink #+default-acl "/etc/guix/acl"))))
1538
1539 (define %default-authorized-guix-keys
1540 ;; List of authorized substitute keys.
1541 (list (file-append guix "/share/guix/berlin.guix.gnu.org.pub")
1542 (file-append guix "/share/guix/bordeaux.guix.gnu.org.pub")))
1543
1544 (define-record-type* <guix-configuration>
1545 guix-configuration make-guix-configuration
1546 guix-configuration?
1547 (guix guix-configuration-guix ;file-like
1548 (default guix))
1549 (build-group guix-configuration-build-group ;string
1550 (default "guixbuild"))
1551 (build-accounts guix-configuration-build-accounts ;integer
1552 (default 10))
1553 (authorize-key? guix-configuration-authorize-key? ;Boolean
1554 (default #t))
1555 (authorized-keys guix-configuration-authorized-keys ;list of gexps
1556 (default %default-authorized-guix-keys))
1557 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
1558 (default #t))
1559 (substitute-urls guix-configuration-substitute-urls ;list of strings
1560 (default %default-substitute-urls))
1561 (chroot-directories guix-configuration-chroot-directories ;list of file-like/strings
1562 (default '()))
1563 (max-silent-time guix-configuration-max-silent-time ;integer
1564 (default 0))
1565 (timeout guix-configuration-timeout ;integer
1566 (default 0))
1567 (log-compression guix-configuration-log-compression
1568 (default 'bzip2))
1569 (discover? guix-configuration-discover?
1570 (default #f))
1571 (extra-options guix-configuration-extra-options ;list of strings
1572 (default '()))
1573 (log-file guix-configuration-log-file ;string
1574 (default "/var/log/guix-daemon.log"))
1575 (http-proxy guix-http-proxy ;string | #f
1576 (default #f))
1577 (tmpdir guix-tmpdir ;string | #f
1578 (default #f)))
1579
1580 (define %default-guix-configuration
1581 (guix-configuration))
1582
1583 (define shepherd-set-http-proxy-action
1584 ;; Shepherd action to change the HTTP(S) proxy.
1585 (shepherd-action
1586 (name 'set-http-proxy)
1587 (documentation
1588 "Change the HTTP(S) proxy used by 'guix-daemon' and restart it.")
1589 (procedure #~(lambda* (_ #:optional proxy)
1590 (let ((environment (environ)))
1591 ;; A bit of a hack: communicate PROXY to the 'start'
1592 ;; method via environment variables.
1593 (if proxy
1594 (begin
1595 (format #t "changing HTTP/HTTPS \
1596 proxy of 'guix-daemon' to ~s...~%"
1597 proxy)
1598 (setenv "http_proxy" proxy))
1599 (begin
1600 (format #t "clearing HTTP/HTTPS \
1601 proxy of 'guix-daemon'...~%")
1602 (unsetenv "http_proxy")))
1603 (action 'guix-daemon 'restart)
1604 (environ environment)
1605 #t)))))
1606
1607 (define shepherd-discover-action
1608 ;; Shepherd action to enable or disable substitute servers discovery.
1609 (shepherd-action
1610 (name 'discover)
1611 (documentation
1612 "Enable or disable substitute servers discovery and restart the
1613 'guix-daemon'.")
1614 (procedure #~(lambda* (_ status)
1615 (let ((environment (environ)))
1616 (if (and status
1617 (string=? status "on"))
1618 (begin
1619 (format #t "enable substitute servers discovery~%")
1620 (setenv "discover" "on"))
1621 (begin
1622 (format #t "disable substitute servers discovery~%")
1623 (unsetenv "discover")))
1624 (action 'guix-daemon 'restart)
1625 (environ environment)
1626 #t)))))
1627
1628 (define (guix-shepherd-service config)
1629 "Return a <shepherd-service> for the Guix daemon service with CONFIG."
1630 (match-record config <guix-configuration>
1631 (guix build-group build-accounts authorize-key? authorized-keys
1632 use-substitutes? substitute-urls max-silent-time timeout
1633 log-compression discover? extra-options log-file
1634 http-proxy tmpdir chroot-directories)
1635 (list (shepherd-service
1636 (documentation "Run the Guix daemon.")
1637 (provision '(guix-daemon))
1638 (requirement '(user-processes))
1639 (actions (list shepherd-set-http-proxy-action
1640 shepherd-discover-action))
1641 (modules '((srfi srfi-1)
1642 (ice-9 match)
1643 (gnu build shepherd)))
1644 (start
1645 (with-imported-modules `(((guix config) => ,(make-config.scm))
1646 ,@(source-module-closure
1647 '((gnu build shepherd))
1648 #:select? not-config?))
1649 #~(lambda args
1650 (define proxy
1651 ;; HTTP/HTTPS proxy. The 'http_proxy' variable is set by
1652 ;; the 'set-http-proxy' action.
1653 (or (getenv "http_proxy") #$http-proxy))
1654
1655 (define discover?
1656 (or (getenv "discover") #$discover?))
1657
1658 ;; Start the guix-daemon from a container, when supported,
1659 ;; to solve an installation issue. See the comment below for
1660 ;; more details.
1661 (fork+exec-command/container
1662 (cons* #$(file-append guix "/bin/guix-daemon")
1663 "--build-users-group" #$build-group
1664 "--max-silent-time"
1665 #$(number->string max-silent-time)
1666 "--timeout" #$(number->string timeout)
1667 "--log-compression"
1668 #$(symbol->string log-compression)
1669 #$@(if use-substitutes?
1670 '()
1671 '("--no-substitutes"))
1672 (string-append "--discover="
1673 (if discover? "yes" "no"))
1674 "--substitute-urls" #$(string-join substitute-urls)
1675 #$@extra-options
1676
1677 ;; Add CHROOT-DIRECTORIES and all their dependencies
1678 ;; (if these are store items) to the chroot.
1679 (append-map
1680 (lambda (file)
1681 (append-map (lambda (directory)
1682 (list "--chroot-directory"
1683 directory))
1684 (call-with-input-file file
1685 read)))
1686 '#$(map references-file
1687 chroot-directories)))
1688
1689 ;; When running the installer, we need guix-daemon to
1690 ;; operate from within the same MNT namespace as the
1691 ;; installation container. In that case only, enter the
1692 ;; namespace of the process PID passed as start argument.
1693 ;; Otherwise, for symmetry purposes enter the caller
1694 ;; namespaces which is a no-op.
1695 #:pid (match args
1696 ((pid) (string->number pid))
1697 (else (getpid)))
1698
1699 #:environment-variables
1700 (append (list #$@(if tmpdir
1701 (list (string-append "TMPDIR=" tmpdir))
1702 '())
1703
1704 ;; Make sure we run in a UTF-8 locale so that
1705 ;; 'guix offload' correctly restores nars
1706 ;; that contain UTF-8 file names such as
1707 ;; 'nss-certs'. See
1708 ;; <https://bugs.gnu.org/32942>.
1709 (string-append "GUIX_LOCPATH="
1710 #$glibc-utf8-locales
1711 "/lib/locale")
1712 "LC_ALL=en_US.utf8")
1713 (if proxy
1714 (list (string-append "http_proxy=" proxy)
1715 (string-append "https_proxy=" proxy))
1716 '()))
1717
1718 #:log-file #$log-file))))
1719 (stop #~(make-kill-destructor))))))
1720
1721 (define (guix-accounts config)
1722 "Return the user accounts and user groups for CONFIG."
1723 (match config
1724 (($ <guix-configuration> _ build-group build-accounts)
1725 (cons (user-group
1726 (name build-group)
1727 (system? #t)
1728
1729 ;; Use a fixed GID so that we can create the store with the right
1730 ;; owner.
1731 (id 30000))
1732 (guix-build-accounts build-accounts
1733 #:group build-group)))))
1734
1735 (define (guix-activation config)
1736 "Return the activation gexp for CONFIG."
1737 (match-record config <guix-configuration>
1738 (guix authorize-key? authorized-keys)
1739 #~(begin
1740 ;; Assume that the store has BUILD-GROUP as its group. We could
1741 ;; otherwise call 'chown' here, but the problem is that on a COW overlayfs,
1742 ;; chown leads to an entire copy of the tree, which is a bad idea.
1743
1744 ;; Generate a key pair and optionally authorize substitute server keys.
1745 (unless (file-exists? "/etc/guix/signing-key.pub")
1746 (system* #$(file-append guix "/bin/guix") "archive"
1747 "--generate-key"))
1748
1749 #$(if authorize-key?
1750 (substitute-key-authorization authorized-keys guix)
1751 #~#f))))
1752
1753 (define* (references-file item #:optional (name "references"))
1754 "Return a file that contains the list of references of ITEM."
1755 (if (struct? item) ;lowerable object
1756 (computed-file name
1757 (with-extensions (list guile-gcrypt) ;for store-copy
1758 (with-imported-modules (source-module-closure
1759 '((guix build store-copy)))
1760 #~(begin
1761 (use-modules (guix build store-copy))
1762
1763 (call-with-output-file #$output
1764 (lambda (port)
1765 (write (map store-info-item
1766 (call-with-input-file "graph"
1767 read-reference-graph))
1768 port))))))
1769 #:options `(#:local-build? #f
1770 #:references-graphs (("graph" ,item))))
1771 (plain-file name "()")))
1772
1773 (define guix-service-type
1774 (service-type
1775 (name 'guix)
1776 (extensions
1777 (list (service-extension shepherd-root-service-type guix-shepherd-service)
1778 (service-extension account-service-type guix-accounts)
1779 (service-extension activation-service-type guix-activation)
1780 (service-extension profile-service-type
1781 (compose list guix-configuration-guix))))
1782
1783 ;; Extensions can specify extra directories to add to the build chroot.
1784 (compose concatenate)
1785 (extend (lambda (config directories)
1786 (guix-configuration
1787 (inherit config)
1788 (chroot-directories
1789 (append (guix-configuration-chroot-directories config)
1790 directories)))))
1791
1792 (default-value (guix-configuration))
1793 (description
1794 "Run the build daemon of GNU@tie{}Guix, aka. @command{guix-daemon}.")))
1795
1796
1797 (define-record-type* <guix-publish-configuration>
1798 guix-publish-configuration make-guix-publish-configuration
1799 guix-publish-configuration?
1800 (guix guix-publish-configuration-guix ;file-like
1801 (default guix))
1802 (port guix-publish-configuration-port ;number
1803 (default 80))
1804 (host guix-publish-configuration-host ;string
1805 (default "localhost"))
1806 (advertise? guix-publish-advertise? ;boolean
1807 (default #f))
1808 (compression guix-publish-configuration-compression
1809 (thunked)
1810 (default (default-compression this-record
1811 (current-source-location))))
1812 (compression-level %guix-publish-configuration-compression-level ;deprecated
1813 (default #f))
1814 (nar-path guix-publish-configuration-nar-path ;string
1815 (default "nar"))
1816 (cache guix-publish-configuration-cache ;#f | string
1817 (default #f))
1818 (cache-bypass-threshold guix-publish-configuration-cache-bypass-threshold
1819 (default (* 10 (expt 2 20)))) ;integer
1820 (workers guix-publish-configuration-workers ;#f | integer
1821 (default #f))
1822 (ttl guix-publish-configuration-ttl ;#f | integer
1823 (default #f)))
1824
1825 (define-deprecated (guix-publish-configuration-compression-level config)
1826 "Return a compression level, the old way."
1827 (match (guix-publish-configuration-compression config)
1828 (((_ level) _ ...) level)))
1829
1830 (define (default-compression config properties)
1831 "Return the default 'guix publish' compression according to CONFIG, and
1832 raise a deprecation warning if the 'compression-level' field was used."
1833 (match (%guix-publish-configuration-compression-level config)
1834 (#f
1835 ;; Default to low compression levels when there's no cache so that users
1836 ;; get good bandwidth by default.
1837 (if (guix-publish-configuration-cache config)
1838 '(("gzip" 5) ("zstd" 19))
1839 '(("gzip" 3) ("zstd" 3)))) ;zstd compresses faster
1840 (level
1841 (warn-about-deprecation 'compression-level properties
1842 #:replacement 'compression)
1843 `(("gzip" ,level)))))
1844
1845 (define (guix-publish-shepherd-service config)
1846 (define (config->compression-options config)
1847 (match (guix-publish-configuration-compression config)
1848 (() ;empty list means "no compression"
1849 '("-C0"))
1850 (lst
1851 (append-map (match-lambda
1852 ((type level)
1853 `("-C" ,(string-append type ":"
1854 (number->string level)))))
1855 lst))))
1856
1857 (match-record config <guix-publish-configuration>
1858 (guix port host nar-path cache workers ttl cache-bypass-threshold
1859 advertise?)
1860 (list (shepherd-service
1861 (provision '(guix-publish))
1862 (requirement `(user-processes
1863 guix-daemon
1864 ,@(if advertise? '(avahi-daemon) '())))
1865 (start #~(make-forkexec-constructor
1866 (list #$(file-append guix "/bin/guix")
1867 "publish" "-u" "guix-publish"
1868 "-p" #$(number->string port)
1869 #$@(config->compression-options config)
1870 (string-append "--nar-path=" #$nar-path)
1871 (string-append "--listen=" #$host)
1872 #$@(if advertise?
1873 #~("--advertise")
1874 #~())
1875 #$@(if workers
1876 #~((string-append "--workers="
1877 #$(number->string
1878 workers)))
1879 #~())
1880 #$@(if ttl
1881 #~((string-append "--ttl="
1882 #$(number->string ttl)
1883 "s"))
1884 #~())
1885 #$@(if cache
1886 #~((string-append "--cache=" #$cache)
1887 #$(string-append
1888 "--cache-bypass-threshold="
1889 (number->string
1890 cache-bypass-threshold)))
1891 #~()))
1892
1893 ;; Make sure we run in a UTF-8 locale so we can produce
1894 ;; nars for packages that contain UTF-8 file names such
1895 ;; as 'nss-certs'. See <https://bugs.gnu.org/26948>.
1896 #:environment-variables
1897 (list (string-append "GUIX_LOCPATH="
1898 #$glibc-utf8-locales "/lib/locale")
1899 "LC_ALL=en_US.utf8")
1900 #:log-file "/var/log/guix-publish.log"))
1901 (stop #~(make-kill-destructor))))))
1902
1903 (define %guix-publish-accounts
1904 (list (user-group (name "guix-publish") (system? #t))
1905 (user-account
1906 (name "guix-publish")
1907 (group "guix-publish")
1908 (system? #t)
1909 (comment "guix publish user")
1910 (home-directory "/var/empty")
1911 (shell (file-append shadow "/sbin/nologin")))))
1912
1913 (define %guix-publish-log-rotations
1914 (list (log-rotation
1915 (files (list "/var/log/guix-publish.log")))))
1916
1917 (define (guix-publish-activation config)
1918 (let ((cache (guix-publish-configuration-cache config)))
1919 (if cache
1920 (with-imported-modules '((guix build utils))
1921 #~(begin
1922 (use-modules (guix build utils))
1923
1924 (mkdir-p #$cache)
1925 (let* ((pw (getpw "guix-publish"))
1926 (uid (passwd:uid pw))
1927 (gid (passwd:gid pw)))
1928 (chown #$cache uid gid))))
1929 #t)))
1930
1931 (define guix-publish-service-type
1932 (service-type (name 'guix-publish)
1933 (extensions
1934 (list (service-extension shepherd-root-service-type
1935 guix-publish-shepherd-service)
1936 (service-extension account-service-type
1937 (const %guix-publish-accounts))
1938 (service-extension rottlog-service-type
1939 (const %guix-publish-log-rotations))
1940 (service-extension activation-service-type
1941 guix-publish-activation)))
1942 (default-value (guix-publish-configuration))
1943 (description
1944 "Add a Shepherd service running @command{guix publish}, a
1945 command that allows you to share pre-built binaries with others over HTTP.")))
1946
1947 \f
1948 ;;;
1949 ;;; Udev.
1950 ;;;
1951
1952 (define-record-type* <udev-configuration>
1953 udev-configuration make-udev-configuration
1954 udev-configuration?
1955 (udev udev-configuration-udev ;file-like
1956 (default eudev))
1957 (rules udev-configuration-rules ;list of file-like
1958 (default '())))
1959
1960 (define (udev-rules-union packages)
1961 "Return the union of the @code{lib/udev/rules.d} directories found in each
1962 item of @var{packages}."
1963 (define build
1964 (with-imported-modules '((guix build union)
1965 (guix build utils))
1966 #~(begin
1967 (use-modules (guix build union)
1968 (guix build utils)
1969 (srfi srfi-1)
1970 (srfi srfi-26))
1971
1972 (define %standard-locations
1973 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
1974
1975 (define (rules-sub-directory directory)
1976 ;; Return the sub-directory of DIRECTORY containing udev rules, or
1977 ;; #f if none was found.
1978 (find directory-exists?
1979 (map (cut string-append directory <>) %standard-locations)))
1980
1981 (mkdir-p (string-append #$output "/lib/udev"))
1982 (union-build (string-append #$output "/lib/udev/rules.d")
1983 (filter-map rules-sub-directory '#$packages)))))
1984
1985 (computed-file "udev-rules" build))
1986
1987 (define (udev-rule file-name contents)
1988 "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
1989 (computed-file file-name
1990 (with-imported-modules '((guix build utils))
1991 #~(begin
1992 (use-modules (guix build utils))
1993
1994 (define rules.d
1995 (string-append #$output "/lib/udev/rules.d"))
1996
1997 (mkdir-p rules.d)
1998 (call-with-output-file
1999 (string-append rules.d "/" #$file-name)
2000 (lambda (port)
2001 (display #$contents port)))))))
2002
2003 (define (file->udev-rule file-name file)
2004 "Return a directory with a udev rule file FILE-NAME which is a copy of FILE."
2005 (computed-file file-name
2006 (with-imported-modules '((guix build utils))
2007 #~(begin
2008 (use-modules (guix build utils))
2009
2010 (define rules.d
2011 (string-append #$output "/lib/udev/rules.d"))
2012
2013 (define file-copy-dest
2014 (string-append rules.d "/" #$file-name))
2015
2016 (mkdir-p rules.d)
2017 (copy-file #$file file-copy-dest)))))
2018
2019 (define kvm-udev-rule
2020 ;; Return a directory with a udev rule that changes the group of /dev/kvm to
2021 ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
2022 ;; but now we have to add it by ourselves.
2023
2024 ;; Build users are part of the "kvm" group, so we can fearlessly make
2025 ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
2026 (udev-rule "90-kvm.rules"
2027 "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
2028
2029 (define udev-shepherd-service
2030 ;; Return a <shepherd-service> for UDEV with RULES.
2031 (match-lambda
2032 (($ <udev-configuration> udev rules)
2033 (let* ((rules (udev-rules-union (cons* udev kvm-udev-rule rules)))
2034 (udev.conf (computed-file "udev.conf"
2035 #~(call-with-output-file #$output
2036 (lambda (port)
2037 (format port
2038 "udev_rules=\"~a/lib/udev/rules.d\"\n"
2039 #$rules))))))
2040 (list
2041 (shepherd-service
2042 (provision '(udev))
2043
2044 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
2045 ;; be added: see
2046 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
2047 (requirement '(root-file-system))
2048
2049 (documentation "Populate the /dev directory, dynamically.")
2050 (start
2051 (with-imported-modules (source-module-closure
2052 '((gnu build linux-boot)))
2053 #~(lambda ()
2054 (define udevd
2055 ;; 'udevd' from eudev.
2056 #$(file-append udev "/sbin/udevd"))
2057
2058 (define (wait-for-udevd)
2059 ;; Wait until someone's listening on udevd's control
2060 ;; socket.
2061 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
2062 (let try ()
2063 (catch 'system-error
2064 (lambda ()
2065 (connect sock PF_UNIX "/run/udev/control")
2066 (close-port sock))
2067 (lambda args
2068 (format #t "waiting for udevd...~%")
2069 (usleep 500000)
2070 (try))))))
2071
2072 ;; Allow udev to find the modules.
2073 (setenv "LINUX_MODULE_DIRECTORY"
2074 "/run/booted-system/kernel/lib/modules")
2075
2076 (let* ((kernel-release
2077 (utsname:release (uname)))
2078 (linux-module-directory
2079 (getenv "LINUX_MODULE_DIRECTORY"))
2080 (directory
2081 (string-append linux-module-directory "/"
2082 kernel-release))
2083 (old-umask (umask #o022)))
2084 ;; If we're in a container, DIRECTORY might not exist,
2085 ;; for instance because the host runs a different
2086 ;; kernel. In that case, skip it; we'll just miss a few
2087 ;; nodes like /dev/fuse.
2088 (when (file-exists? directory)
2089 (make-static-device-nodes directory))
2090 (umask old-umask))
2091
2092 (let ((pid (fork+exec-command (list udevd)
2093 #:environment-variables
2094 (cons*
2095 ;; The first one is for udev, the second one for
2096 ;; eudev.
2097 (string-append "UDEV_CONFIG_FILE=" #$udev.conf)
2098 (string-append "EUDEV_RULES_DIRECTORY="
2099 #$(file-append
2100 rules "/lib/udev/rules.d"))
2101 (string-append "LINUX_MODULE_DIRECTORY="
2102 (getenv "LINUX_MODULE_DIRECTORY"))
2103 (default-environment-variables)))))
2104 ;; Wait until udevd is up and running. This appears to
2105 ;; be needed so that the events triggered below are
2106 ;; actually handled.
2107 (wait-for-udevd)
2108
2109 ;; Trigger device node creation.
2110 (system* #$(file-append udev "/bin/udevadm")
2111 "trigger" "--action=add")
2112
2113 ;; Wait for things to settle down.
2114 (system* #$(file-append udev "/bin/udevadm")
2115 "settle")
2116 pid))))
2117 (stop #~(make-kill-destructor))
2118
2119 ;; When halting the system, 'udev' is actually killed by
2120 ;; 'user-processes', i.e., before its own 'stop' method was called.
2121 ;; Thus, make sure it is not respawned.
2122 (respawn? #f)
2123 ;; We need additional modules.
2124 (modules `((gnu build linux-boot) ;'make-static-device-nodes'
2125 ,@%default-modules))
2126
2127 (actions (list (shepherd-action
2128 (name 'rules)
2129 (documentation "Display the directory containing
2130 the udev rules in use.")
2131 (procedure #~(lambda (_)
2132 (display #$rules)
2133 (newline))))))))))))
2134
2135 (define udev-service-type
2136 (service-type (name 'udev)
2137 (extensions
2138 (list (service-extension shepherd-root-service-type
2139 udev-shepherd-service)))
2140
2141 (compose concatenate) ;concatenate the list of rules
2142 (extend (lambda (config rules)
2143 (match config
2144 (($ <udev-configuration> udev initial-rules)
2145 (udev-configuration
2146 (udev udev)
2147 (rules (append initial-rules rules)))))))
2148 (default-value (udev-configuration))
2149 (description
2150 "Run @command{udev}, which populates the @file{/dev}
2151 directory dynamically. Get extra rules from the packages listed in the
2152 @code{rules} field of its value, @code{udev-configuration} object.")))
2153
2154 (define* (udev-service #:key (udev eudev) (rules '()))
2155 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
2156 extra rules from the packages listed in @var{rules}."
2157 (service udev-service-type
2158 (udev-configuration (udev udev) (rules rules))))
2159
2160 (define* (udev-rules-service name rules #:key (groups '()))
2161 "Return a service that extends udev-service-type with RULES and
2162 account-service-type with GROUPS as system groups. This works by creating a
2163 singleton service type NAME-udev-rules, of which the returned service is an
2164 instance."
2165 (let* ((name (symbol-append name '-udev-rules))
2166 (account-extension
2167 (const (map (lambda (group)
2168 (user-group (name group) (system? #t)))
2169 groups)))
2170 (udev-extension (const (list rules)))
2171 (type (service-type
2172 (name name)
2173 (extensions (list
2174 (service-extension
2175 account-service-type account-extension)
2176 (service-extension
2177 udev-service-type udev-extension))))))
2178 (service type #f)))
2179
2180 (define (swap-space->shepherd-service-name space)
2181 (let ((target (swap-space-target space)))
2182 (symbol-append 'swap-
2183 (string->symbol
2184 (cond ((uuid? target)
2185 (uuid->string target))
2186 ((file-system-label? target)
2187 (file-system-label->string target))
2188 (else
2189 target))))))
2190
2191 ; TODO Remove after deprecation
2192 (define (swap-deprecated->shepherd-service-name sdep)
2193 (symbol-append 'swap-
2194 (string->symbol
2195 (cond ((uuid? sdep)
2196 (string-take (uuid->string sdep) 6))
2197 ((file-system-label? sdep)
2198 (file-system-label->string sdep))
2199 (else
2200 sdep)))))
2201
2202 (define swap->shepherd-service-name
2203 (match-lambda ((? swap-space? space)
2204 (swap-space->shepherd-service-name space))
2205 (sdep
2206 (swap-deprecated->shepherd-service-name sdep))))
2207
2208 (define swap-service-type
2209 (shepherd-service-type
2210 'swap
2211 (lambda (swap)
2212 (define requirements
2213 (cond ((swap-space? swap)
2214 (map dependency->shepherd-service-name
2215 (swap-space-dependencies swap)))
2216 ; TODO Remove after deprecation
2217 ((and (string? swap) (string-prefix? "/dev/mapper/" swap))
2218 (list (symbol-append 'device-mapping-
2219 (string->symbol (basename swap)))))
2220 (else
2221 '())))
2222
2223 (define device-lookup
2224 ;; The generic 'find-partition' procedures could return a partition
2225 ;; that's not swap space, but that's unlikely.
2226 (cond ((swap-space? swap)
2227 (let ((target (swap-space-target swap)))
2228 (cond ((uuid? target)
2229 #~(find-partition-by-uuid #$(uuid-bytevector target)))
2230 ((file-system-label? target)
2231 #~(find-partition-by-label
2232 #$(file-system-label->string target)))
2233 (else
2234 target))))
2235 ; TODO Remove after deprecation
2236 ((uuid? swap)
2237 #~(find-partition-by-uuid #$(uuid-bytevector swap)))
2238 ((file-system-label? swap)
2239 #~(find-partition-by-label
2240 #$(file-system-label->string swap)))
2241 (else
2242 swap)))
2243
2244 (with-imported-modules (source-module-closure '((gnu build file-systems)))
2245 (shepherd-service
2246 (provision (list (swap->shepherd-service-name swap)))
2247 (requirement `(udev ,@requirements))
2248 (documentation "Enable the given swap space.")
2249 (modules `((gnu build file-systems)
2250 ,@%default-modules))
2251 (start #~(lambda ()
2252 (let ((device #$device-lookup))
2253 (and device
2254 (begin
2255 (restart-on-EINTR (swapon device
2256 #$(if (swap-space? swap)
2257 (swap-space->flags-bit-mask
2258 swap)
2259 0)))
2260 #t)))))
2261 (stop #~(lambda _
2262 (let ((device #$device-lookup))
2263 (when device
2264 (restart-on-EINTR (swapoff device)))
2265 #f)))
2266 (respawn? #f))))
2267 (description "Turn on the virtual memory swap area.")))
2268
2269 (define (swap-service swap)
2270 "Return a service that uses @var{swap} as a swap space."
2271 (service swap-service-type swap))
2272
2273 (define %default-gpm-options
2274 ;; Default options for GPM.
2275 '("-m" "/dev/input/mice" "-t" "ps2"))
2276
2277 (define-record-type* <gpm-configuration>
2278 gpm-configuration make-gpm-configuration gpm-configuration?
2279 (gpm gpm-configuration-gpm ;file-like
2280 (default gpm))
2281 (options gpm-configuration-options ;list of strings
2282 (default %default-gpm-options)))
2283
2284 (define gpm-shepherd-service
2285 (match-lambda
2286 (($ <gpm-configuration> gpm options)
2287 (list (shepherd-service
2288 (requirement '(udev))
2289 (provision '(gpm))
2290 ;; 'gpm' runs in the background and sets a PID file.
2291 ;; Note that it requires running as "root".
2292 (start #~(make-forkexec-constructor
2293 (list #$(file-append gpm "/sbin/gpm")
2294 #$@options)
2295 #:pid-file "/var/run/gpm.pid"
2296 #:pid-file-timeout 3))
2297 (stop #~(lambda (_)
2298 ;; Return #f if successfully stopped.
2299 (not (zero? (system* #$(file-append gpm "/sbin/gpm")
2300 "-k"))))))))))
2301
2302 (define gpm-service-type
2303 (service-type (name 'gpm)
2304 (extensions
2305 (list (service-extension shepherd-root-service-type
2306 gpm-shepherd-service)))
2307 (default-value (gpm-configuration))
2308 (description
2309 "Run GPM, the general-purpose mouse daemon, with the given
2310 command-line options. GPM allows users to use the mouse in the console,
2311 notably to select, copy, and paste text. The default options use the
2312 @code{ps2} protocol, which works for both USB and PS/2 mice.")))
2313
2314
2315 (define-record-type* <kmscon-configuration>
2316 kmscon-configuration make-kmscon-configuration
2317 kmscon-configuration?
2318 (kmscon kmscon-configuration-kmscon
2319 (default kmscon))
2320 (virtual-terminal kmscon-configuration-virtual-terminal)
2321 (login-program kmscon-configuration-login-program
2322 (default (file-append shadow "/bin/login")))
2323 (login-arguments kmscon-configuration-login-arguments
2324 (default '("-p")))
2325 (auto-login kmscon-configuration-auto-login
2326 (default #f))
2327 (hardware-acceleration? kmscon-configuration-hardware-acceleration?
2328 (default #f)) ; #t causes failure
2329 (font-engine kmscon-configuration-font-engine
2330 (default "pango"))
2331 (font-size kmscon-configuration-font-size
2332 (default 12))
2333 (keyboard-layout kmscon-configuration-keyboard-layout
2334 (default #f))) ; #f | <keyboard-layout>
2335
2336 (define kmscon-service-type
2337 (shepherd-service-type
2338 'kmscon
2339 (lambda (config)
2340 (let ((kmscon (kmscon-configuration-kmscon config))
2341 (virtual-terminal (kmscon-configuration-virtual-terminal config))
2342 (login-program (kmscon-configuration-login-program config))
2343 (login-arguments (kmscon-configuration-login-arguments config))
2344 (auto-login (kmscon-configuration-auto-login config))
2345 (hardware-acceleration? (kmscon-configuration-hardware-acceleration? config))
2346 (font-engine (kmscon-configuration-font-engine config))
2347 (font-size (kmscon-configuration-font-size config))
2348 (keyboard-layout (kmscon-configuration-keyboard-layout config)))
2349
2350 (define kmscon-command
2351 #~(list
2352 #$(file-append kmscon "/bin/kmscon") "--login"
2353 "--vt" #$virtual-terminal
2354 "--no-switchvt" ;Prevent a switch to the virtual terminal.
2355 "--font-engine" #$font-engine
2356 "--font-size" #$(number->string font-size)
2357 #$@(if keyboard-layout
2358 (let* ((layout (keyboard-layout-name keyboard-layout))
2359 (variant (keyboard-layout-variant keyboard-layout))
2360 (model (keyboard-layout-model keyboard-layout))
2361 (options (keyboard-layout-options keyboard-layout)))
2362 `("--xkb-layout" ,layout
2363 ,@(if variant `("--xkb-variant" ,variant) '())
2364 ,@(if model `("--xkb-model" ,model) '())
2365 ,@(if (null? options)
2366 '()
2367 `("--xkb-options" ,(string-join options ",")))))
2368 '())
2369 #$@(if hardware-acceleration? '("--hwaccel") '())
2370 "--login" "--"
2371 #$login-program #$@login-arguments
2372 #$@(if auto-login
2373 #~(#$auto-login)
2374 #~())))
2375
2376 (shepherd-service
2377 (documentation "kmscon virtual terminal")
2378 (requirement '(user-processes udev dbus-system))
2379 (provision (list (symbol-append 'term- (string->symbol virtual-terminal))))
2380 (start #~(make-forkexec-constructor #$kmscon-command))
2381 (stop #~(make-kill-destructor)))))
2382 (description "Start the @command{kmscon} virtual terminal emulator for the
2383 Linux @dfn{kernel mode setting} (KMS).")))
2384
2385 \f
2386 ;;;
2387 ;;; Static networking.
2388 ;;;
2389
2390 (define (ipv6-address? str)
2391 "Return true if STR denotes an IPv6 address."
2392 (false-if-exception (->bool (inet-pton AF_INET6 str))))
2393
2394 (define-compile-time-procedure (assert-valid-address (address string?))
2395 "Ensure ADDRESS has a valid netmask."
2396 (unless (or (cidr->netmask address)
2397 (and=> (false-if-exception (inet-pton AF_INET address))
2398 (cut = INADDR_LOOPBACK <>))
2399 (and=> (false-if-exception (inet-pton AF_INET6 address))
2400 (cut = 1 <>)))
2401 (raise
2402 (make-compound-condition
2403 (formatted-message (G_ "address '~a' lacks a network mask")
2404 address)
2405 (condition (&error-location
2406 (location
2407 (source-properties->location procedure-call-location))))
2408 (condition (&fix-hint
2409 (hint (format #f (G_ "\
2410 Write, say, @samp{\"~a/24\"} for a 24-bit network mask.")
2411 address)))))))
2412 address)
2413
2414 (define-record-type* <static-networking>
2415 static-networking make-static-networking
2416 static-networking?
2417 (addresses static-networking-addresses) ;list of <network-address>
2418 (links static-networking-links (default '())) ;list of <network-link>
2419 (routes static-networking-routes (default '())) ;list of <network-routes>
2420 (provision static-networking-provision
2421 (default '(networking)))
2422 (requirement static-networking-requirement
2423 (default '(udev)))
2424 (name-servers static-networking-name-servers ;FIXME: doesn't belong here
2425 (default '())))
2426
2427 (define-record-type* <network-address>
2428 network-address make-network-address
2429 network-address?
2430 (device network-address-device) ;string--e.g., "en01"
2431 (value network-address-value ;string--CIDR notation
2432 (sanitize assert-valid-address))
2433 (ipv6? network-address-ipv6? ;Boolean
2434 (thunked)
2435 (default
2436 (ipv6-address? (cidr->ip (network-address-value this-record))))))
2437
2438 (define-record-type* <network-link>
2439 network-link make-network-link
2440 network-link?
2441 (name network-link-name) ;string--e.g, "v0p0"
2442 (type network-link-type) ;symbol--e.g.,'veth
2443 (arguments network-link-arguments)) ;list
2444
2445 (define-record-type* <network-route>
2446 network-route make-network-route
2447 network-route?
2448 (destination network-route-destination)
2449 (source network-route-source (default #f))
2450 (device network-route-device (default #f))
2451 (ipv6? network-route-ipv6? (thunked)
2452 (default
2453 (or (ipv6-address? (network-route-destination this-record))
2454 (and=> (network-route-gateway this-record)
2455 ipv6-address?))))
2456 (gateway network-route-gateway (default #f)))
2457
2458 (define* (cidr->netmask str #:optional (family AF_INET))
2459 "Given @var{str}, a string in CIDR notation (e.g., \"1.2.3.4/24\"), return
2460 the netmask as a string like \"255.255.255.0\"."
2461 (match (string-split str #\/)
2462 ((ip (= string->number bits))
2463 (let ((mask (ash (- (expt 2 bits) 1)
2464 (- (if (= family AF_INET6) 128 32)
2465 bits))))
2466 (inet-ntop family mask)))
2467 (_ #f)))
2468
2469 (define (cidr->ip str)
2470 "Strip the netmask bit of @var{str}, a CIDR-notation IP/netmask address."
2471 (match (string-split str #\/)
2472 ((or (ip _) (ip))
2473 ip)))
2474
2475 (define* (ip+netmask->cidr ip netmask #:optional (family AF_INET))
2476 "Return the CIDR notation (a string) for @var{ip} and @var{netmask}, two
2477 @var{family} address strings, where @var{family} is @code{AF_INET} or
2478 @code{AF_INET6}."
2479 (let* ((netmask (inet-pton family netmask))
2480 (bits (logcount netmask)))
2481 (string-append ip "/" (number->string bits))))
2482
2483 (define (static-networking->hurd-pfinet-options config)
2484 "Return command-line options for the Hurd's pfinet translator corresponding
2485 to CONFIG."
2486 (unless (null? (static-networking-links config))
2487 ;; XXX: Presumably this is not supported, or perhaps could be approximated
2488 ;; by running separate pfinet instances in some cases?
2489 (warning (G_ "network links are currently ignored on GNU/Hurd~%")))
2490
2491 (match (static-networking-addresses config)
2492 ((and addresses (first _ ...))
2493 `("--ipv6" "/servers/socket/26"
2494 "--interface" ,(network-address-device first)
2495 ,@(append-map (lambda (address)
2496 `(,(if (network-address-ipv6? address)
2497 "--address6"
2498 "--address")
2499 ,(cidr->ip (network-address-value address))
2500 ,@(match (cidr->netmask (network-address-value address)
2501 (if (network-address-ipv6? address)
2502 AF_INET6
2503 AF_INET))
2504 (#f '())
2505 (mask (list "--netmask" mask)))))
2506 addresses)
2507 ,@(append-map (lambda (route)
2508 (match route
2509 (($ <network-route> "default" #f device _ gateway)
2510 (if (network-route-ipv6? route)
2511 `("--gateway6" ,gateway)
2512 `("--gateway" ,gateway)))
2513 (($ <network-route> destination)
2514 (warning (G_ "ignoring network route for '~a'~%")
2515 destination)
2516 '())))
2517 (static-networking-routes config))))))
2518
2519 (define (network-set-up/hurd config)
2520 "Set up networking for the Hurd."
2521 ;; The Hurd implements SIOCGIFADDR and other old-style ioctls, but the only
2522 ;; way to set up IPv6 is by starting pfinet with the right options.
2523 (if (equal? (static-networking-provision config) '(loopback))
2524 (scheme-file "set-up-pflocal" #~(begin 'nothing-to-do! #t))
2525 (scheme-file "set-up-pfinet"
2526 (with-imported-modules '((guix build utils))
2527 #~(begin
2528 (use-modules (guix build utils)
2529 (ice-9 format))
2530
2531 ;; TODO: Do that without forking.
2532 (let ((options '#$(static-networking->hurd-pfinet-options
2533 config)))
2534 (format #t "starting '~a~{ ~s~}'~%"
2535 #$(file-append hurd "/hurd/pfinet")
2536 options)
2537 (apply invoke #$(file-append hurd "/bin/settrans") "-fac"
2538 "/servers/socket/2"
2539 #$(file-append hurd "/hurd/pfinet")
2540 options)))))))
2541
2542 (define (network-tear-down/hurd config)
2543 (scheme-file "tear-down-pfinet"
2544 (with-imported-modules '((guix build utils))
2545 #~(begin
2546 (use-modules (guix build utils))
2547
2548 ;; Forcefully terminate pfinet. XXX: In theory this
2549 ;; should just undo the addresses and routes of CONFIG;
2550 ;; this could be done using ioctls like SIOCDELRT, but
2551 ;; these are IPv4-only; another option would be to use
2552 ;; fsysopts but that seems to crash pfinet.
2553 (invoke #$(file-append hurd "/bin/settrans") "-fg"
2554 "/servers/socket/2")
2555 #f))))
2556
2557 (define network-set-up/linux
2558 (match-lambda
2559 (($ <static-networking> addresses links routes)
2560 (scheme-file "set-up-network"
2561 (with-extensions (list guile-netlink)
2562 #~(begin
2563 (use-modules (ip addr) (ip link) (ip route))
2564
2565 #$@(map (lambda (address)
2566 #~(begin
2567 (addr-add #$(network-address-device address)
2568 #$(network-address-value address)
2569 #:ipv6?
2570 #$(network-address-ipv6? address))
2571 ;; FIXME: loopback?
2572 (link-set #$(network-address-device address)
2573 #:up #t)))
2574 addresses)
2575 #$@(map (match-lambda
2576 (($ <network-link> name type arguments)
2577 #~(link-add #$name #$type
2578 #:type-args '#$arguments)))
2579 links)
2580 #$@(map (lambda (route)
2581 #~(route-add #$(network-route-destination route)
2582 #:device
2583 #$(network-route-device route)
2584 #:ipv6?
2585 #$(network-route-ipv6? route)
2586 #:via
2587 #$(network-route-gateway route)
2588 #:src
2589 #$(network-route-source route)))
2590 routes)
2591 #t))))))
2592
2593 (define network-tear-down/linux
2594 (match-lambda
2595 (($ <static-networking> addresses links routes)
2596 (scheme-file "tear-down-network"
2597 (with-extensions (list guile-netlink)
2598 #~(begin
2599 (use-modules (ip addr) (ip link) (ip route)
2600 (netlink error)
2601 (srfi srfi-34))
2602
2603 (define-syntax-rule (false-if-netlink-error exp)
2604 (guard (c ((netlink-error? c) #f))
2605 exp))
2606
2607 ;; Wrap calls in 'false-if-netlink-error' so this
2608 ;; script goes as far as possible undoing the effects
2609 ;; of "set-up-network".
2610
2611 #$@(map (lambda (route)
2612 #~(false-if-netlink-error
2613 (route-del #$(network-route-destination route)
2614 #:device
2615 #$(network-route-device route)
2616 #:ipv6?
2617 #$(network-route-ipv6? route)
2618 #:via
2619 #$(network-route-gateway route)
2620 #:src
2621 #$(network-route-source route))))
2622 routes)
2623 #$@(map (match-lambda
2624 (($ <network-link> name type arguments)
2625 #~(false-if-netlink-error
2626 (link-del #$name))))
2627 links)
2628 #$@(map (lambda (address)
2629 #~(false-if-netlink-error
2630 (addr-del #$(network-address-device
2631 address)
2632 #$(network-address-value address)
2633 #:ipv6?
2634 #$(network-address-ipv6? address))))
2635 addresses)
2636 #f))))))
2637
2638 (define (static-networking-shepherd-service config)
2639 (match config
2640 (($ <static-networking> addresses links routes
2641 provision requirement name-servers)
2642 (let ((loopback? (and provision (memq 'loopback provision))))
2643 (shepherd-service
2644
2645 (documentation
2646 "Bring up the networking interface using a static IP address.")
2647 (requirement requirement)
2648 (provision provision)
2649
2650 (start #~(lambda _
2651 ;; Return #t if successfully started.
2652 (load #$(let-system (system target)
2653 (if (string-contains (or target system) "-linux")
2654 (network-set-up/linux config)
2655 (network-set-up/hurd config))))))
2656 (stop #~(lambda _
2657 ;; Return #f is successfully stopped.
2658 (load #$(let-system (system target)
2659 (if (string-contains (or target system) "-linux")
2660 (network-tear-down/linux config)
2661 (network-tear-down/hurd config))))))
2662 (respawn? #f))))))
2663
2664 (define (static-networking-shepherd-services networks)
2665 (map static-networking-shepherd-service networks))
2666
2667 (define (static-networking-etc-files interfaces)
2668 "Return a /etc/resolv.conf entry for INTERFACES or the empty list."
2669 (match (delete-duplicates
2670 (append-map static-networking-name-servers
2671 interfaces))
2672 (()
2673 '())
2674 ((name-servers ...)
2675 (let ((content (string-join
2676 (map (cut string-append "nameserver " <>)
2677 name-servers)
2678 "\n" 'suffix)))
2679 `(("resolv.conf"
2680 ,(plain-file "resolv.conf"
2681 (string-append "\
2682 # Generated by 'static-networking-service'.\n"
2683 content))))))))
2684
2685 (define static-networking-service-type
2686 ;; The service type for statically-defined network interfaces.
2687 (service-type (name 'static-networking)
2688 (extensions
2689 (list
2690 (service-extension shepherd-root-service-type
2691 static-networking-shepherd-services)
2692 (service-extension etc-service-type
2693 static-networking-etc-files)))
2694 (compose concatenate)
2695 (extend append)
2696 (description
2697 "Turn up the specified network interfaces upon startup,
2698 with the given IP address, gateway, netmask, and so on. The value for
2699 services of this type is a list of @code{static-networking} objects, one per
2700 network interface.")))
2701
2702 (define-deprecated (static-networking-service interface ip
2703 #:key
2704 netmask gateway provision
2705 ;; Most interfaces require udev to be usable.
2706 (requirement '(udev))
2707 (name-servers '()))
2708 static-networking-service-type
2709 "Return a service that starts @var{interface} with address @var{ip}. If
2710 @var{netmask} is true, use it as the network mask. If @var{gateway} is true,
2711 it must be a string specifying the default network gateway.
2712
2713 This procedure can be called several times, one for each network
2714 interface of interest. Behind the scenes what it does is extend
2715 @code{static-networking-service-type} with additional network interfaces
2716 to handle."
2717 (simple-service 'static-network-interface
2718 static-networking-service-type
2719 (list (static-networking
2720 (addresses
2721 (list (network-address
2722 (device interface)
2723 (value (if netmask
2724 (ip+netmask->cidr ip netmask)
2725 ip))
2726 (ipv6? #f))))
2727 (routes
2728 (if gateway
2729 (list (network-route
2730 (destination "default")
2731 (gateway gateway)
2732 (ipv6? #f)))
2733 '()))
2734 (requirement requirement)
2735 (provision (or provision '(networking)))
2736 (name-servers name-servers)))))
2737
2738 (define %loopback-static-networking
2739 ;; The loopback device.
2740 (static-networking
2741 (addresses (list (network-address
2742 (device "lo")
2743 (value "127.0.0.1"))))
2744 (requirement '())
2745 (provision '(loopback))))
2746
2747 (define %qemu-static-networking
2748 ;; Networking configuration for QEMU's user-mode network stack (info "(QEMU)
2749 ;; Using the user mode network stack").
2750 (static-networking
2751 (addresses (list (network-address
2752 (device "eth0")
2753 (value "10.0.2.15/24"))))
2754 (routes (list (network-route
2755 (destination "default")
2756 (gateway "10.0.2.2"))))
2757 (requirement '())
2758 (provision '(networking))
2759 (name-servers '("10.0.2.3"))))
2760
2761 \f
2762 (define %base-services
2763 ;; Convenience variable holding the basic services.
2764 (list (service login-service-type)
2765
2766 (service virtual-terminal-service-type)
2767 (service console-font-service-type
2768 (map (lambda (tty)
2769 (cons tty %default-console-font))
2770 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
2771
2772 (service agetty-service-type (agetty-configuration
2773 (extra-options '("-L")) ; no carrier detect
2774 (term "vt100")
2775 (tty #f))) ; automatic
2776
2777 (service mingetty-service-type (mingetty-configuration
2778 (tty "tty1")))
2779 (service mingetty-service-type (mingetty-configuration
2780 (tty "tty2")))
2781 (service mingetty-service-type (mingetty-configuration
2782 (tty "tty3")))
2783 (service mingetty-service-type (mingetty-configuration
2784 (tty "tty4")))
2785 (service mingetty-service-type (mingetty-configuration
2786 (tty "tty5")))
2787 (service mingetty-service-type (mingetty-configuration
2788 (tty "tty6")))
2789
2790 (service static-networking-service-type
2791 (list %loopback-static-networking))
2792 (syslog-service)
2793 (service urandom-seed-service-type)
2794 (service guix-service-type)
2795 (service nscd-service-type)
2796
2797 (service rottlog-service-type)
2798
2799 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
2800 ;; used, so enable them by default. The FUSE and ALSA rules are
2801 ;; less critical, but handy.
2802 (service udev-service-type
2803 (udev-configuration
2804 (rules (list lvm2 fuse alsa-utils crda))))
2805
2806 (service sysctl-service-type)
2807
2808 (service special-files-service-type
2809 `(("/bin/sh" ,(file-append bash "/bin/sh"))
2810 ("/usr/bin/env" ,(file-append coreutils "/bin/env"))))))
2811
2812 ;;; base.scm ends here