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