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