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