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