gnu: linux-libre@4.1, linux-libre@4.4, linux-libre@4.9: Fix CVE-2017-6074.
[jackhill/guix/guix.git] / gnu / services / base.scm
CommitLineData
db4fdc04 1;;; GNU Guix --- Functional package management for GNU
a43aca97 2;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
34044d55 3;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
5f4a446d 4;;; Copyright © 2015, 2016 Mark H Weaver <mhw@netris.org>
e10964ef 5;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
a535e122 6;;; Copyright © 2016 Leo Famulari <leo@famulari.name>
b58cbf9a 7;;; Copyright © 2016 David Craven <david@craven.ch>
909147e4 8;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
db4fdc04
LC
9;;;
10;;; This file is part of GNU Guix.
11;;;
12;;; GNU Guix is free software; you can redistribute it and/or modify it
13;;; under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 3 of the License, or (at
15;;; your option) any later version.
16;;;
17;;; GNU Guix is distributed in the hope that it will be useful, but
18;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
23;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24
25(define-module (gnu services base)
e87f0591 26 #:use-module (guix store)
db4fdc04 27 #:use-module (gnu services)
0190c1c0 28 #:use-module (gnu services shepherd)
4a3b3b07 29 #:use-module (gnu services networking)
6e828634 30 #:use-module (gnu system pam)
db4fdc04 31 #:use-module (gnu system shadow) ; 'user-account', etc.
0adfe95a 32 #:use-module (gnu system file-systems) ; 'file-system', etc.
060d62a7 33 #:use-module (gnu system mapped-devices)
db4fdc04 34 #:use-module (gnu packages admin)
151a2c07 35 #:use-module ((gnu packages linux)
b58cbf9a 36 #:select (alsa-utils crda eudev e2fsprogs fuse gpm kbd lvm2 rng-tools))
db4fdc04 37 #:use-module ((gnu packages base)
bdb36958 38 #:select (canonical-package glibc))
387e1754 39 #:use-module (gnu packages bash)
db4fdc04 40 #:use-module (gnu packages package-management)
2d1d2dd8 41 #:use-module (gnu packages lsof)
46ec2707 42 #:use-module (gnu packages terminals)
e2f4b305 43 #:use-module ((gnu build file-systems)
2c071ce9 44 #:select (mount-flags->bit-mask))
b5f4e686 45 #:use-module (guix gexp)
6454b333 46 #:use-module (guix records)
db4fdc04
LC
47 #:use-module (srfi srfi-1)
48 #:use-module (srfi srfi-26)
6454b333 49 #:use-module (ice-9 match)
db4fdc04 50 #:use-module (ice-9 format)
e43e84ba
LC
51 #:export (fstab-service-type
52 root-file-system-service
aa1145df 53 file-system-service-type
d6e2a622 54 user-unmount-service
2a13d05e 55 swap-service
a00dd9fb 56 user-processes-service
e10964ef
SB
57 session-environment-service
58 session-environment-service-type
a00dd9fb 59 host-name-service
5eca9459 60 console-keymap-service
4a84a487
LC
61 %default-console-font
62 console-font-service-type
62ca0fdf 63 console-font-service
c797fabe
RW
64
65 udev-configuration
66 udev-configuration?
67 udev-configuration-rules
0adfe95a 68 udev-service-type
151a2c07 69 udev-service
80e6f37e 70 udev-rule
66e4f01c 71
317d3b47
DC
72 login-configuration
73 login-configuration?
74 login-service-type
75 login-service
76
66e4f01c
LC
77 mingetty-configuration
78 mingetty-configuration?
db4fdc04 79 mingetty-service
cd6f6c22 80 mingetty-service-type
6454b333
LC
81
82 %nscd-default-caches
83 %nscd-default-configuration
84
85 nscd-configuration
86 nscd-configuration?
87
88 nscd-cache
89 nscd-cache?
90
0adfe95a 91 nscd-service-type
db4fdc04 92 nscd-service
ec2e2f6c
DC
93
94 syslog-configuration
95 syslog-configuration?
db4fdc04 96 syslog-service
9009538d 97 syslog-service-type
44abcb28 98 %default-syslog.conf
0adfe95a 99
5b58c28b 100 %default-authorized-guix-keys
0adfe95a
LC
101 guix-configuration
102 guix-configuration?
70dfa4e0
MO
103
104 guix-configuration-guix
105 guix-configuration-build-group
106 guix-configuration-build-accounts
107 guix-configuration-authorize-key?
108 guix-configuration-authorized-keys
109 guix-configuration-use-substitutes?
110 guix-configuration-substitute-urls
111 guix-configuration-extra-options
112 guix-configuration-log-file
113 guix-configuration-lsof
114
8b198abe 115 guix-service
cd6f6c22 116 guix-service-type
1c52181f
LC
117 guix-publish-configuration
118 guix-publish-configuration?
119 guix-publish-service
120 guix-publish-service-type
24e96431
121
122 gpm-configuration
123 gpm-configuration?
8664cc88
LC
124 gpm-service-type
125 gpm-service
0adfe95a 126
9009538d 127 urandom-seed-service-type
a535e122 128 urandom-seed-service
24e96431
129
130 rngd-configuration
131 rngd-configuration?
b58cbf9a
DC
132 rngd-service-type
133 rngd-service
46ec2707
DC
134
135 kmscon-configuration
136 kmscon-configuration?
137 kmscon-service-type
138
909147e4
RW
139 pam-limits-service-type
140 pam-limits-service
a535e122 141
8b198abe 142 %base-services))
db4fdc04
LC
143
144;;; Commentary:
145;;;
146;;; Base system services---i.e., services that 99% of the users will want to
147;;; use.
148;;;
149;;; Code:
150
0adfe95a
LC
151\f
152;;;
153;;; File systems.
154;;;
a00dd9fb 155
e43e84ba
LC
156(define (file-system->fstab-entry file-system)
157 "Return a @file{/etc/fstab} entry for @var{file-system}."
158 (string-append (case (file-system-title file-system)
159 ((label)
160 (string-append "LABEL=" (file-system-device file-system)))
161 ((uuid)
162 (string-append
163 "UUID="
164 (uuid->string (file-system-device file-system))))
165 (else
166 (file-system-device file-system)))
167 "\t"
168 (file-system-mount-point file-system) "\t"
169 (file-system-type file-system) "\t"
170 (or (file-system-options file-system) "defaults") "\t"
171
172 ;; XXX: Omit the 'fs_freq' and 'fs_passno' fields because we
173 ;; don't have anything sensible to put in there.
174 ))
175
176(define (file-systems->fstab file-systems)
177 "Return a @file{/etc} entry for an @file{fstab} describing
178@var{file-systems}."
179 `(("fstab" ,(plain-file "fstab"
180 (string-append
181 "\
182# This file was generated from your GuixSD configuration. Any changes
183# will be lost upon reboot or reconfiguration.\n\n"
184 (string-join (map file-system->fstab-entry
185 file-systems)
186 "\n")
187 "\n")))))
188
189(define fstab-service-type
190 ;; The /etc/fstab service.
191 (service-type (name 'fstab)
192 (extensions
193 (list (service-extension etc-service-type
194 file-systems->fstab)))
aa1145df 195 (compose concatenate)
e43e84ba
LC
196 (extend append)))
197
d4053c71
AK
198(define %root-file-system-shepherd-service
199 (shepherd-service
be1c2c54
LC
200 (documentation "Take care of the root file system.")
201 (provision '(root-file-system))
202 (start #~(const #t))
203 (stop #~(lambda _
204 ;; Return #f if successfully stopped.
205 (sync)
206
207 (call-with-blocked-asyncs
208 (lambda ()
209 (let ((null (%make-void-port "w")))
34044d55 210 ;; Close 'shepherd.log'.
be1c2c54 211 (display "closing log\n")
34044d55 212 ((@ (shepherd comm) stop-logging))
be1c2c54
LC
213
214 ;; Redirect the default output ports..
215 (set-current-output-port null)
216 (set-current-error-port null)
217
218 ;; Close /dev/console.
219 (for-each close-fdes '(0 1 2))
220
221 ;; At this point, there are no open files left, so the
222 ;; root file system can be re-mounted read-only.
223 (mount #f "/" #f
224 (logior MS_REMOUNT MS_RDONLY)
225 #:update-mtab? #f)
226
227 #f)))))
228 (respawn? #f)))
a00dd9fb 229
0adfe95a 230(define root-file-system-service-type
d4053c71
AK
231 (shepherd-service-type 'root-file-system
232 (const %root-file-system-shepherd-service)))
0adfe95a
LC
233
234(define (root-file-system-service)
235 "Return a service whose sole purpose is to re-mount read-only the root file
236system upon shutdown (aka. cleanly \"umounting\" root.)
237
238This service must be the root of the service dependency graph so that its
d4053c71 239'stop' action is invoked when shepherd is the only process left."
0adfe95a
LC
240 (service root-file-system-service-type #f))
241
d4053c71 242(define (file-system->shepherd-service-name file-system)
0adfe95a
LC
243 "Return the symbol that denotes the service mounting and unmounting
244FILE-SYSTEM."
245 (symbol-append 'file-system-
246 (string->symbol (file-system-mount-point file-system))))
247
d4053c71
AK
248(define (mapped-device->shepherd-service-name md)
249 "Return the symbol that denotes the shepherd service of MD, a <mapped-device>."
e502bf89
LC
250 (symbol-append 'device-mapping-
251 (string->symbol (mapped-device-target md))))
252
d4053c71 253(define dependency->shepherd-service-name
e502bf89
LC
254 (match-lambda
255 ((? mapped-device? md)
d4053c71 256 (mapped-device->shepherd-service-name md))
e502bf89 257 ((? file-system? fs)
d4053c71 258 (file-system->shepherd-service-name fs))))
e502bf89 259
d4053c71 260(define (file-system-shepherd-service file-system)
aa1145df
LC
261 "Return the shepherd service for @var{file-system}, or @code{#f} if
262@var{file-system} is not auto-mounted upon boot."
e43e84ba
LC
263 (let ((target (file-system-mount-point file-system))
264 (device (file-system-device file-system))
265 (type (file-system-type file-system))
266 (title (file-system-title file-system))
bf7ef1bb
JD
267 (flags (file-system-flags file-system))
268 (options (file-system-options file-system))
e43e84ba
LC
269 (check? (file-system-check? file-system))
270 (create? (file-system-create-mount-point? file-system))
271 (dependencies (file-system-dependencies file-system)))
aa1145df
LC
272 (and (file-system-mount? file-system)
273 (with-imported-modules '((gnu build file-systems)
274 (guix build bournish))
a91c3fc7
LC
275 (shepherd-service
276 (provision (list (file-system->shepherd-service-name file-system)))
277 (requirement `(root-file-system
278 ,@(map dependency->shepherd-service-name dependencies)))
279 (documentation "Check, mount, and unmount the given file system.")
280 (start #~(lambda args
bf7ef1bb
JD
281 #$(if create?
282 #~(mkdir-p #$target)
283 #t)
9328eafb
LC
284
285 (let (($PATH (getenv "PATH")))
286 ;; Make sure fsck.ext2 & co. can be found.
287 (dynamic-wind
288 (lambda ()
289 (setenv "PATH"
290 (string-append
291 #$e2fsprogs "/sbin:"
292 "/run/current-system/profile/sbin:"
293 $PATH)))
294 (lambda ()
295 (mount-file-system
296 `(#$device #$title #$target #$type #$flags
297 #$options #$check?)
298 #:root "/"))
299 (lambda ()
300 (setenv "PATH" $PATH)))
301 #t)))
a91c3fc7
LC
302 (stop #~(lambda args
303 ;; Normally there are no processes left at this point, so
304 ;; TARGET can be safely unmounted.
305
306 ;; Make sure PID 1 doesn't keep TARGET busy.
307 (chdir "/")
308
309 (umount #$target)
310 #f))
311
312 ;; We need an additional module.
313 (modules `(((gnu build file-systems)
bf7ef1bb 314 #:select (mount-file-system))
aa1145df 315 ,@%default-modules)))))))
e43e84ba 316
a43aca97
LC
317(define (file-system-shepherd-services file-systems)
318 "Return the list of Shepherd services for FILE-SYSTEMS."
319 (let* ((file-systems (filter file-system-mount? file-systems)))
320 (define sink
321 (shepherd-service
322 (provision '(file-systems))
323 (requirement (cons* 'root-file-system 'user-file-systems
324 (map file-system->shepherd-service-name
325 file-systems)))
326 (documentation "Target for all the initially-mounted file systems")
327 (start #~(const #t))
328 (stop #~(const #f))))
329
330 (cons sink (map file-system-shepherd-service file-systems))))
331
0adfe95a 332(define file-system-service-type
aa1145df 333 (service-type (name 'file-systems)
e43e84ba 334 (extensions
d4053c71 335 (list (service-extension shepherd-root-service-type
a43aca97 336 file-system-shepherd-services)
e43e84ba 337 (service-extension fstab-service-type
aa1145df
LC
338 identity)))
339 (compose concatenate)
340 (extend append)))
0adfe95a
LC
341
342(define user-unmount-service-type
d4053c71 343 (shepherd-service-type
5f44ee4f 344 'user-file-systems
0adfe95a 345 (lambda (known-mount-points)
d4053c71 346 (shepherd-service
0adfe95a 347 (documentation "Unmount manually-mounted file systems.")
5f44ee4f 348 (provision '(user-file-systems))
0adfe95a
LC
349 (start #~(const #t))
350 (stop #~(lambda args
351 (define (known? mount-point)
352 (member mount-point
353 (cons* "/proc" "/sys" '#$known-mount-points)))
354
355 ;; Make sure we don't keep the user's mount points busy.
356 (chdir "/")
357
358 (for-each (lambda (mount-point)
359 (format #t "unmounting '~a'...~%" mount-point)
360 (catch 'system-error
361 (lambda ()
362 (umount mount-point))
363 (lambda args
364 (let ((errno (system-error-errno args)))
365 (format #t "failed to unmount '~a': ~a~%"
366 mount-point (strerror errno))))))
367 (filter (negate known?) (mount-points)))
368 #f))))))
023f391c 369
d6e2a622
LC
370(define (user-unmount-service known-mount-points)
371 "Return a service whose sole purpose is to unmount file systems not listed
372in KNOWN-MOUNT-POINTS when it is stopped."
0adfe95a 373 (service user-unmount-service-type known-mount-points))
d6e2a622 374
7d57cfd3
LC
375(define %do-not-kill-file
376 ;; Name of the file listing PIDs of processes that must survive when halting
377 ;; the system. Typical example is user-space file systems.
b8c02c18 378 "/etc/shepherd/do-not-kill")
7d57cfd3 379
0adfe95a 380(define user-processes-service-type
d4053c71 381 (shepherd-service-type
00184239 382 'user-processes
a43aca97
LC
383 (lambda (grace-delay)
384 (shepherd-service
385 (documentation "When stopped, terminate all user processes.")
386 (provision '(user-processes))
387 (requirement '(file-systems))
388 (start #~(const #t))
389 (stop #~(lambda _
390 (define (kill-except omit signal)
391 ;; Kill all the processes with SIGNAL except those listed
392 ;; in OMIT and the current process.
393 (let ((omit (cons (getpid) omit)))
394 (for-each (lambda (pid)
395 (unless (memv pid omit)
396 (false-if-exception
397 (kill pid signal))))
398 (processes))))
399
400 (define omitted-pids
401 ;; List of PIDs that must not be killed.
402 (if (file-exists? #$%do-not-kill-file)
403 (map string->number
404 (call-with-input-file #$%do-not-kill-file
405 (compose string-tokenize
406 (@ (ice-9 rdelim) read-string))))
407 '()))
408
409 (define (now)
410 (car (gettimeofday)))
411
412 (define (sleep* n)
413 ;; Really sleep N seconds.
414 ;; Work around <http://bugs.gnu.org/19581>.
415 (define start (now))
416 (let loop ((elapsed 0))
417 (when (> n elapsed)
418 (sleep (- n elapsed))
419 (loop (- (now) start)))))
420
421 (define lset= (@ (srfi srfi-1) lset=))
422
423 (display "sending all processes the TERM signal\n")
424
425 (if (null? omitted-pids)
426 (begin
427 ;; Easy: terminate all of them.
428 (kill -1 SIGTERM)
429 (sleep* #$grace-delay)
430 (kill -1 SIGKILL))
431 (begin
432 ;; Kill them all except OMITTED-PIDS. XXX: We would
433 ;; like to (kill -1 SIGSTOP) to get a fixed list of
434 ;; processes, like 'killall5' does, but that seems
435 ;; unreliable.
436 (kill-except omitted-pids SIGTERM)
437 (sleep* #$grace-delay)
438 (kill-except omitted-pids SIGKILL)
439 (delete-file #$%do-not-kill-file)))
440
441 (let wait ()
442 (let ((pids (processes)))
443 (unless (lset= = pids (cons 1 omitted-pids))
444 (format #t "waiting for process termination\
0adfe95a 445 (processes left: ~s)~%"
a43aca97
LC
446 pids)
447 (sleep* 2)
448 (wait))))
0adfe95a 449
a43aca97
LC
450 (display "all processes have been terminated\n")
451 #f))
452 (respawn? #f)))))
0adfe95a 453
a43aca97 454(define* (user-processes-service #:key (grace-delay 4))
a00dd9fb
LC
455 "Return the service that is responsible for terminating all the processes so
456that the root file system can be re-mounted read-only, just before
457rebooting/halting. Processes still running GRACE-DELAY seconds after SIGTERM
458has been sent are terminated with SIGKILL.
459
a43aca97
LC
460The returned service will depend on 'file-systems', meaning that it is
461considered started after all the auto-mount file systems have been mounted.
023f391c 462
a00dd9fb
LC
463All the services that spawn processes must depend on this one so that they are
464stopped before 'kill' is called."
a43aca97 465 (service user-processes-service-type grace-delay))
d656c14e 466
0adfe95a 467\f
a535e122
LF
468;;;
469;;; Preserve entropy to seed /dev/urandom on boot.
470;;;
471
472(define %random-seed-file
473 "/var/lib/random-seed")
474
a535e122
LF
475(define (urandom-seed-shepherd-service _)
476 "Return a shepherd service for the /dev/urandom seed."
477 (list (shepherd-service
478 (documentation "Preserve entropy across reboots for /dev/urandom.")
479 (provision '(urandom-seed))
480 (requirement '(user-processes))
481 (start #~(lambda _
482 ;; On boot, write random seed into /dev/urandom.
483 (when (file-exists? #$%random-seed-file)
484 (call-with-input-file #$%random-seed-file
485 (lambda (seed)
486 (call-with-output-file "/dev/urandom"
487 (lambda (urandom)
488 (dump-port seed urandom))))))
71cb237a
LF
489 ;; Immediately refresh the seed in case the system doesn't
490 ;; shut down cleanly.
491 (call-with-input-file "/dev/urandom"
492 (lambda (urandom)
493 (let ((previous-umask (umask #o077))
494 (buf (make-bytevector 512)))
495 (mkdir-p (dirname #$%random-seed-file))
496 (get-bytevector-n! urandom buf 0 512)
497 (call-with-output-file #$%random-seed-file
498 (lambda (seed)
499 (put-bytevector seed buf)))
500 (umask previous-umask))))
a535e122
LF
501 #t))
502 (stop #~(lambda _
503 ;; During shutdown, write from /dev/urandom into random seed.
504 (let ((buf (make-bytevector 512)))
505 (call-with-input-file "/dev/urandom"
506 (lambda (urandom)
8fe5d95e
LF
507 (let ((previous-umask (umask #o077)))
508 (get-bytevector-n! urandom buf 0 512)
71cb237a 509 (mkdir-p (dirname #$%random-seed-file))
8fe5d95e
LF
510 (call-with-output-file #$%random-seed-file
511 (lambda (seed)
512 (put-bytevector seed buf)))
513 (umask previous-umask))
a535e122
LF
514 #t)))))
515 (modules `((rnrs bytevectors)
516 (rnrs io ports)
517 ,@%default-modules)))))
518
519(define urandom-seed-service-type
520 (service-type (name 'urandom-seed)
521 (extensions
522 (list (service-extension shepherd-root-service-type
71cb237a 523 urandom-seed-shepherd-service)))))
a535e122
LF
524
525(define (urandom-seed-service)
526 (service urandom-seed-service-type #f))
527
b58cbf9a
DC
528
529;;;
530;;; Add hardware random number generator to entropy pool.
531;;;
532
533(define-record-type* <rngd-configuration>
534 rngd-configuration make-rngd-configuration
535 rngd-configuration?
536 (rng-tools rngd-configuration-rng-tools) ;package
537 (device rngd-configuration-device)) ;string
538
539(define rngd-service-type
540 (shepherd-service-type
541 'rngd
542 (lambda (config)
543 (define rng-tools (rngd-configuration-rng-tools config))
544 (define device (rngd-configuration-device config))
545
546 (define rngd-command
9e41130b 547 (list (file-append rng-tools "/sbin/rngd")
b58cbf9a
DC
548 "-f" "-r" device))
549
550 (shepherd-service
551 (documentation "Add TRNG to entropy pool.")
552 (requirement '(udev))
553 (provision '(trng))
554 (start #~(make-forkexec-constructor #$@rngd-command))
555 (stop #~(make-kill-destructor))))))
556
557(define* (rngd-service #:key
558 (rng-tools rng-tools)
559 (device "/dev/hwrng"))
560 "Return a service that runs the @command{rngd} program from @var{rng-tools}
561to add @var{device} to the kernel's entropy pool. The service will fail if
562@var{device} does not exist."
563 (service rngd-service-type
564 (rngd-configuration
565 (rng-tools rng-tools)
566 (device device))))
567
568
e10964ef
SB
569;;;
570;;; System-wide environment variables.
571;;;
572
573(define (environment-variables->environment-file vars)
574 "Return a file for pam_env(8) that contains environment variables VARS."
575 (apply mixed-text-file "environment"
576 (append-map (match-lambda
577 ((key . value)
578 (list key "=" value "\n")))
579 vars)))
580
581(define session-environment-service-type
582 (service-type
583 (name 'session-environment)
584 (extensions
585 (list (service-extension
586 etc-service-type
587 (lambda (vars)
588 (list `("environment"
589 ,(environment-variables->environment-file vars)))))))
590 (compose concatenate)
591 (extend append)))
592
593(define (session-environment-service vars)
594 "Return a service that builds the @file{/etc/environment}, which can be read
595by PAM-aware applications to set environment variables for sessions.
596
597VARS should be an association list in which both the keys and the values are
598strings or string-valued gexps."
599 (service session-environment-service-type vars))
600
601\f
0adfe95a
LC
602;;;
603;;; Console & co.
604;;;
605
606(define host-name-service-type
d4053c71 607 (shepherd-service-type
00184239 608 'host-name
0adfe95a 609 (lambda (name)
d4053c71 610 (shepherd-service
0adfe95a
LC
611 (documentation "Initialize the machine's host name.")
612 (provision '(host-name))
613 (start #~(lambda _
614 (sethostname #$name)))
615 (respawn? #f)))))
a00dd9fb 616
db4fdc04 617(define (host-name-service name)
51da7ca0 618 "Return a service that sets the host name to @var{name}."
0adfe95a 619 (service host-name-service-type name))
db4fdc04 620
62ca0fdf
LC
621(define (unicode-start tty)
622 "Return a gexp to start Unicode support on @var{tty}."
623
624 ;; We have to run 'unicode_start' in a pipe so that when it invokes the
625 ;; 'tty' command, that command returns TTY.
626 #~(begin
627 (let ((pid (primitive-fork)))
628 (case pid
629 ((0)
630 (close-fdes 0)
631 (dup2 (open-fdes #$tty O_RDONLY) 0)
632 (close-fdes 1)
633 (dup2 (open-fdes #$tty O_WRONLY) 1)
9fc037fe 634 (execl #$(file-append kbd "/bin/unicode_start")
62ca0fdf
LC
635 "unicode_start"))
636 (else
637 (zero? (cdr (waitpid pid))))))))
638
0adfe95a 639(define console-keymap-service-type
d4053c71 640 (shepherd-service-type
00184239 641 'console-keymap
b3d05f48 642 (lambda (files)
d4053c71 643 (shepherd-service
0adfe95a
LC
644 (documentation (string-append "Load console keymap (loadkeys)."))
645 (provision '(console-keymap))
646 (start #~(lambda _
9fc037fe 647 (zero? (system* #$(file-append kbd "/bin/loadkeys")
b3d05f48 648 #$@files))))
0adfe95a
LC
649 (respawn? #f)))))
650
b3d05f48
AK
651(define (console-keymap-service . files)
652 "Return a service to load console keymaps from @var{files}."
653 (service console-keymap-service-type files))
0adfe95a 654
4a84a487
LC
655(define %default-console-font
656 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
657 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
658 ;; codepoints notably found in the UTF-8 manual.
659 "LatGrkCyr-8x16")
660
661(define (console-font-shepherd-services tty+font)
662 "Return a list of Shepherd services for each pair in TTY+FONT."
663 (map (match-lambda
664 ((tty . font)
665 (let ((device (string-append "/dev/" tty)))
666 (shepherd-service
667 (documentation "Load a Unicode console font.")
668 (provision (list (symbol-append 'console-font-
669 (string->symbol tty))))
670
671 ;; Start after mingetty has been started on TTY, otherwise the settings
672 ;; are ignored.
673 (requirement (list (symbol-append 'term-
674 (string->symbol tty))))
675
676 (start #~(lambda _
677 (and #$(unicode-start device)
678 (zero?
9fc037fe 679 (system* #$(file-append kbd "/bin/setfont")
4a84a487
LC
680 "-C" #$device #$font)))))
681 (stop #~(const #t))
682 (respawn? #f)))))
683 tty+font))
0adfe95a 684
4a84a487
LC
685(define console-font-service-type
686 (service-type (name 'console-fonts)
687 (extensions
688 (list (service-extension shepherd-root-service-type
689 console-font-shepherd-services)))
690 (compose concatenate)
691 (extend append)))
5eca9459 692
62ca0fdf 693(define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
4a84a487
LC
694 "This procedure is deprecated in favor of @code{console-font-service-type}.
695
696Return a service that sets up Unicode support in @var{tty} and loads
62ca0fdf 697@var{font} for that tty (fonts are per virtual console in Linux.)"
4a84a487
LC
698 (simple-service (symbol-append 'console-font- (string->symbol tty))
699 console-font-service-type `((,tty . ,font))))
62ca0fdf 700
317d3b47
DC
701(define %default-motd
702 (plain-file "motd" "This is the GNU operating system, welcome!\n\n"))
703
704(define-record-type* <login-configuration>
705 login-configuration make-login-configuration
706 login-configuration?
707 (motd login-configuration-motd ;file-like
708 (default %default-motd))
709 ;; Allow empty passwords by default so that first-time users can log in when
710 ;; the 'root' account has just been created.
711 (allow-empty-passwords? login-configuration-allow-empty-passwords?
712 (default #t))) ;Boolean
713
714(define (login-pam-service config)
715 "Return the list of PAM service needed for CONF."
716 ;; Let 'login' be known to PAM.
717 (list (unix-pam-service "login"
718 #:allow-empty-passwords?
719 (login-configuration-allow-empty-passwords? config)
720 #:motd
721 (login-configuration-motd config))))
722
723(define login-service-type
724 (service-type (name 'login)
725 (extensions (list (service-extension pam-root-service-type
726 login-pam-service)))))
727
728(define* (login-service #:optional (config (login-configuration)))
729 "Return a service configure login according to @var{config}, which specifies
730the message of the day, among other things."
731 (service login-service-type config))
732
66e4f01c
LC
733(define-record-type* <mingetty-configuration>
734 mingetty-configuration make-mingetty-configuration
735 mingetty-configuration?
736 (mingetty mingetty-configuration-mingetty ;<package>
737 (default mingetty))
738 (tty mingetty-configuration-tty) ;string
66e4f01c
LC
739 (auto-login mingetty-auto-login ;string | #f
740 (default #f))
741 (login-program mingetty-login-program ;gexp
742 (default #f))
743 (login-pause? mingetty-login-pause? ;Boolean
317d3b47 744 (default #f)))
0adfe95a 745
d4053c71 746(define mingetty-shepherd-service
0adfe95a 747 (match-lambda
317d3b47
DC
748 (($ <mingetty-configuration> mingetty tty auto-login login-program
749 login-pause?)
0adfe95a 750 (list
d4053c71 751 (shepherd-service
0adfe95a
LC
752 (documentation "Run mingetty on an tty.")
753 (provision (list (symbol-append 'term- (string->symbol tty))))
754
755 ;; Since the login prompt shows the host name, wait for the 'host-name'
756 ;; service to be done. Also wait for udev essentially so that the tty
757 ;; text is not lost in the middle of kernel messages (XXX).
758 (requirement '(user-processes host-name udev))
759
760 (start #~(make-forkexec-constructor
9fc037fe 761 (list #$(file-append mingetty "/sbin/mingetty")
0adfe95a
LC
762 "--noclear" #$tty
763 #$@(if auto-login
764 #~("--autologin" #$auto-login)
765 #~())
766 #$@(if login-program
767 #~("--loginprog" #$login-program)
768 #~())
769 #$@(if login-pause?
770 #~("--loginpause")
771 #~()))))
772 (stop #~(make-kill-destructor)))))))
773
774(define mingetty-service-type
775 (service-type (name 'mingetty)
d4053c71 776 (extensions (list (service-extension shepherd-root-service-type
317d3b47 777 mingetty-shepherd-service)))))
0adfe95a
LC
778
779(define* (mingetty-service config)
780 "Return a service to run mingetty according to @var{config}, which specifies
781the tty to run, among other things."
782 (service mingetty-service-type config))
db4fdc04 783
6454b333
LC
784(define-record-type* <nscd-configuration> nscd-configuration
785 make-nscd-configuration
786 nscd-configuration?
787 (log-file nscd-configuration-log-file ;string
788 (default "/var/log/nscd.log"))
789 (debug-level nscd-debug-level ;integer
790 (default 0))
791 ;; TODO: See nscd.conf in glibc for other options to add.
792 (caches nscd-configuration-caches ;list of <nscd-cache>
b893f1ae
LC
793 (default %nscd-default-caches))
794 (name-services nscd-configuration-name-services ;list of <packages>
795 (default '()))
796 (glibc nscd-configuration-glibc ;<package>
797 (default (canonical-package glibc))))
6454b333
LC
798
799(define-record-type* <nscd-cache> nscd-cache make-nscd-cache
800 nscd-cache?
801 (database nscd-cache-database) ;symbol
802 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
803 (negative-time-to-live nscd-cache-negative-time-to-live
804 (default 20)) ;integer
805 (suggested-size nscd-cache-suggested-size ;integer ("default module
806 ;of hash table")
807 (default 211))
808 (check-files? nscd-cache-check-files? ;Boolean
809 (default #t))
810 (persistent? nscd-cache-persistent? ;Boolean
811 (default #t))
812 (shared? nscd-cache-shared? ;Boolean
813 (default #t))
814 (max-database-size nscd-cache-max-database-size ;integer
815 (default (* 32 (expt 2 20))))
816 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
817 (default #t)))
818
819(define %nscd-default-caches
820 ;; Caches that we want to enable by default. Note that when providing an
821 ;; empty nscd.conf, all caches are disabled.
822 (list (nscd-cache (database 'hosts)
823
824 ;; Aggressively cache the host name cache to improve
825 ;; privacy and resilience.
826 (positive-time-to-live (* 3600 12))
827 (negative-time-to-live 20)
828 (persistent? #t))
829
830 (nscd-cache (database 'services)
831
832 ;; Services are unlikely to change, so we can be even more
833 ;; aggressive.
834 (positive-time-to-live (* 3600 24))
835 (negative-time-to-live 3600)
836 (check-files? #t) ;check /etc/services changes
837 (persistent? #t))))
838
839(define %nscd-default-configuration
840 ;; Default nscd configuration.
841 (nscd-configuration))
842
843(define (nscd.conf-file config)
844 "Return the @file{nscd.conf} configuration file for @var{config}, an
845@code{<nscd-configuration>} object."
846 (define cache->config
847 (match-lambda
be1c2c54
LC
848 (($ <nscd-cache> (= symbol->string database)
849 positive-ttl negative-ttl size check-files?
850 persistent? shared? max-size propagate?)
851 (string-append "\nenable-cache\t" database "\tyes\n"
852
853 "positive-time-to-live\t" database "\t"
854 (number->string positive-ttl) "\n"
855 "negative-time-to-live\t" database "\t"
856 (number->string negative-ttl) "\n"
857 "suggested-size\t" database "\t"
858 (number->string size) "\n"
859 "check-files\t" database "\t"
860 (if check-files? "yes\n" "no\n")
861 "persistent\t" database "\t"
862 (if persistent? "yes\n" "no\n")
863 "shared\t" database "\t"
864 (if shared? "yes\n" "no\n")
865 "max-db-size\t" database "\t"
866 (number->string max-size) "\n"
867 "auto-propagate\t" database "\t"
868 (if propagate? "yes\n" "no\n")))))
6454b333
LC
869
870 (match config
871 (($ <nscd-configuration> log-file debug-level caches)
be1c2c54
LC
872 (plain-file "nscd.conf"
873 (string-append "\
6454b333 874# Configuration of libc's name service cache daemon (nscd).\n\n"
be1c2c54
LC
875 (if log-file
876 (string-append "logfile\t" log-file)
877 "")
878 "\n"
879 (if debug-level
880 (string-append "debug-level\t"
881 (number->string debug-level))
882 "")
883 "\n"
884 (string-concatenate
885 (map cache->config caches)))))))
6454b333 886
d4053c71
AK
887(define (nscd-shepherd-service config)
888 "Return a shepherd service for CONFIG, an <nscd-configuration> object."
0adfe95a
LC
889 (let ((nscd.conf (nscd.conf-file config))
890 (name-services (nscd-configuration-name-services config)))
d4053c71 891 (list (shepherd-service
0adfe95a
LC
892 (documentation "Run libc's name service cache daemon (nscd).")
893 (provision '(nscd))
894 (requirement '(user-processes))
895 (start #~(make-forkexec-constructor
9fc037fe 896 (list #$(file-append (nscd-configuration-glibc config)
0adfe95a
LC
897 "/sbin/nscd")
898 "-f" #$nscd.conf "--foreground")
899
04101d99
LC
900 ;; Wait for the PID file. However, the PID file is
901 ;; written before nscd is actually listening on its
902 ;; socket (XXX).
903 #:pid-file "/var/run/nscd/nscd.pid"
904
0adfe95a
LC
905 #:environment-variables
906 (list (string-append "LD_LIBRARY_PATH="
907 (string-join
908 (map (lambda (dir)
909 (string-append dir "/lib"))
910 (list #$@name-services))
911 ":")))))
cc7234ae 912 (stop #~(make-kill-destructor))))))
0adfe95a
LC
913
914(define nscd-activation
915 ;; Actions to take before starting nscd.
916 #~(begin
917 (use-modules (guix build utils))
918 (mkdir-p "/var/run/nscd")
919 (mkdir-p "/var/db/nscd"))) ;for the persistent cache
920
921(define nscd-service-type
922 (service-type (name 'nscd)
923 (extensions
924 (list (service-extension activation-service-type
925 (const nscd-activation))
d4053c71
AK
926 (service-extension shepherd-root-service-type
927 nscd-shepherd-service)))
0adfe95a
LC
928
929 ;; This can be extended by providing additional name services
930 ;; such as nss-mdns.
931 (compose concatenate)
932 (extend (lambda (config name-services)
933 (nscd-configuration
934 (inherit config)
935 (name-services (append
936 (nscd-configuration-name-services config)
937 name-services)))))))
938
b893f1ae 939(define* (nscd-service #:optional (config %nscd-default-configuration))
6454b333 940 "Return a service that runs libc's name service cache daemon (nscd) with the
b893f1ae
LC
941given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
942Service Switch}, for an example."
0adfe95a
LC
943 (service nscd-service-type config))
944
ec2e2f6c
DC
945
946(define-record-type* <syslog-configuration>
947 syslog-configuration make-syslog-configuration
948 syslog-configuration?
949 (syslogd syslog-configuration-syslogd
9e41130b 950 (default (file-append inetutils "/libexec/syslogd")))
ec2e2f6c
DC
951 (config-file syslog-configuration-config-file
952 (default %default-syslog.conf)))
953
0adfe95a 954(define syslog-service-type
d4053c71 955 (shepherd-service-type
00184239 956 'syslog
ec2e2f6c 957 (lambda (config)
d4053c71 958 (shepherd-service
0adfe95a
LC
959 (documentation "Run the syslog daemon (syslogd).")
960 (provision '(syslogd))
961 (requirement '(user-processes))
962 (start #~(make-forkexec-constructor
ec2e2f6c 963 (list #$(syslog-configuration-syslogd config)
afa54a38
LC
964 "--rcfile" #$(syslog-configuration-config-file config))
965 #:pid-file "/var/run/syslog.pid"))
0adfe95a 966 (stop #~(make-kill-destructor))))))
be1c2c54
LC
967
968;; Snippet adapted from the GNU inetutils manual.
969(define %default-syslog.conf
970 (plain-file "syslog.conf" "
1f3fc60d 971 # Log all error messages, authentication messages of
db4fdc04
LC
972 # level notice or higher and anything of level err or
973 # higher to the console.
974 # Don't log private authentication messages!
6a191274 975 *.alert;auth.notice;authpriv.none /dev/console
db4fdc04
LC
976
977 # Log anything (except mail) of level info or higher.
978 # Don't log private authentication messages!
979 *.info;mail.none;authpriv.none /var/log/messages
980
981 # Same, in a different place.
982 *.info;mail.none;authpriv.none /dev/tty12
983
984 # The authpriv file has restricted access.
985 authpriv.* /var/log/secure
986
987 # Log all the mail messages in one place.
988 mail.* /var/log/maillog
be1c2c54 989"))
0adfe95a 990
ec2e2f6c
DC
991(define* (syslog-service #:optional (config (syslog-configuration)))
992 "Return a service that runs @command{syslogd} and takes
993@var{<syslog-configuration>} as a parameter.
44abcb28
LC
994
995@xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
996information on the configuration file syntax."
ec2e2f6c
DC
997 (service syslog-service-type config))
998
db4fdc04 999
909147e4
RW
1000(define pam-limits-service-type
1001 (let ((security-limits
1002 ;; Create /etc/security containing the provided "limits.conf" file.
1003 (lambda (limits-file)
1004 `(("security"
1005 ,(computed-file
1006 "security"
1007 #~(begin
1008 (mkdir #$output)
1009 (stat #$limits-file)
1010 (symlink #$limits-file
1011 (string-append #$output "/limits.conf"))))))))
1012 (pam-extension
1013 (lambda (pam)
1014 (let ((pam-limits (pam-entry
1015 (control "required")
1016 (module "pam_limits.so")
1017 (arguments '("conf=/etc/security/limits.conf")))))
1018 (if (member (pam-service-name pam)
1019 '("login" "su" "slim"))
1020 (pam-service
1021 (inherit pam)
1022 (session (cons pam-limits
1023 (pam-service-session pam))))
1024 pam)))))
1025 (service-type
1026 (name 'limits)
1027 (extensions
1028 (list (service-extension etc-service-type security-limits)
1029 (service-extension pam-root-service-type
1030 (lambda _ (list pam-extension))))))))
1031
1032(define* (pam-limits-service #:optional (limits '()))
1033 "Return a service that makes selected programs respect the list of
1034pam-limits-entry specified in LIMITS via pam_limits.so."
1035 (service pam-limits-service-type
1036 (plain-file "limits.conf"
1037 (string-join (map pam-limits-entry->string limits)
1038 "\n"))))
1039
1c52181f
LC
1040\f
1041;;;
1042;;; Guix services.
1043;;;
1044
db4fdc04 1045(define* (guix-build-accounts count #:key
ab6a279a 1046 (group "guixbuild")
db4fdc04 1047 (first-uid 30001)
db4fdc04
LC
1048 (shadow shadow))
1049 "Return a list of COUNT user accounts for Guix build users, with UIDs
1050starting at FIRST-UID, and under GID."
5250a4f2
LC
1051 (unfold (cut > <> count)
1052 (lambda (n)
1053 (user-account
1054 (name (format #f "guixbuilder~2,'0d" n))
1055 (system? #t)
1056 (uid (+ first-uid n -1))
1057 (group group)
1058
1059 ;; guix-daemon expects GROUP to be listed as a
1060 ;; supplementary group too:
1061 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
1062 (supplementary-groups (list group "kvm"))
1063
1064 (comment (format #f "Guix Build User ~2d" n))
1065 (home-directory "/var/empty")
9e41130b 1066 (shell (file-append shadow "/sbin/nologin"))))
5250a4f2
LC
1067 1+
1068 1))
db4fdc04 1069
5b58c28b
LC
1070(define (hydra-key-authorization key guix)
1071 "Return a gexp with code to register KEY, a file containing a 'guix archive'
1072public key, with GUIX."
2c5c696c
LC
1073 #~(unless (file-exists? "/etc/guix/acl")
1074 (let ((pid (primitive-fork)))
1075 (case pid
1076 ((0)
5b58c28b 1077 (let* ((key #$key)
2c5c696c
LC
1078 (port (open-file key "r0b")))
1079 (format #t "registering public key '~a'...~%" key)
1080 (close-port (current-input-port))
2c5c696c 1081 (dup port 0)
9fc037fe 1082 (execl #$(file-append guix "/bin/guix")
2c5c696c
LC
1083 "guix" "archive" "--authorize")
1084 (exit 1)))
1085 (else
1086 (let ((status (cdr (waitpid pid))))
1087 (unless (zero? status)
1088 (format (current-error-port) "warning: \
1089failed to register hydra.gnu.org public key: ~a~%" status))))))))
1090
5b58c28b
LC
1091(define %default-authorized-guix-keys
1092 ;; List of authorized substitute keys.
9e41130b 1093 (list (file-append guix "/share/guix/hydra.gnu.org.pub")))
5b58c28b 1094
0adfe95a
LC
1095(define-record-type* <guix-configuration>
1096 guix-configuration make-guix-configuration
1097 guix-configuration?
1098 (guix guix-configuration-guix ;<package>
1099 (default guix))
1100 (build-group guix-configuration-build-group ;string
1101 (default "guixbuild"))
1102 (build-accounts guix-configuration-build-accounts ;integer
1103 (default 10))
1104 (authorize-key? guix-configuration-authorize-key? ;Boolean
1105 (default #t))
5b58c28b
LC
1106 (authorized-keys guix-configuration-authorized-keys ;list of gexps
1107 (default %default-authorized-guix-keys))
0adfe95a
LC
1108 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
1109 (default #t))
b0b9f6e0
LC
1110 (substitute-urls guix-configuration-substitute-urls ;list of strings
1111 (default %default-substitute-urls))
0adfe95a
LC
1112 (extra-options guix-configuration-extra-options ;list of strings
1113 (default '()))
dc0ef095
LC
1114 (log-file guix-configuration-log-file ;string
1115 (default "/var/log/guix-daemon.log"))
0adfe95a 1116 (lsof guix-configuration-lsof ;<package>
f78903f3 1117 (default lsof)))
0adfe95a
LC
1118
1119(define %default-guix-configuration
1120 (guix-configuration))
1121
d4053c71
AK
1122(define (guix-shepherd-service config)
1123 "Return a <shepherd-service> for the Guix daemon service with CONFIG."
0adfe95a 1124 (match config
5b58c28b
LC
1125 (($ <guix-configuration> guix build-group build-accounts
1126 authorize-key? keys
b0b9f6e0 1127 use-substitutes? substitute-urls extra-options
dc0ef095 1128 log-file lsof)
d4053c71 1129 (list (shepherd-service
0adfe95a
LC
1130 (documentation "Run the Guix daemon.")
1131 (provision '(guix-daemon))
1132 (requirement '(user-processes))
1133 (start
1134 #~(make-forkexec-constructor
9fc037fe 1135 (list #$(file-append guix "/bin/guix-daemon")
0adfe95a
LC
1136 "--build-users-group" #$build-group
1137 #$@(if use-substitutes?
1138 '()
1139 '("--no-substitutes"))
b0b9f6e0 1140 "--substitute-urls" #$(string-join substitute-urls)
0adfe95a
LC
1141 #$@extra-options)
1142
f78903f3 1143 ;; Add 'lsof' (for the GC) to the daemon's $PATH.
0adfe95a 1144 #:environment-variables
dc0ef095
LC
1145 (list (string-append "PATH=" #$lsof "/bin"))
1146
1147 #:log-file #$log-file))
0adfe95a
LC
1148 (stop #~(make-kill-destructor)))))))
1149
1150(define (guix-accounts config)
1151 "Return the user accounts and user groups for CONFIG."
1152 (match config
1153 (($ <guix-configuration> _ build-group build-accounts)
1154 (cons (user-group
1155 (name build-group)
1156 (system? #t)
1157
1158 ;; Use a fixed GID so that we can create the store with the right
1159 ;; owner.
1160 (id 30000))
1161 (guix-build-accounts build-accounts
1162 #:group build-group)))))
1163
1164(define (guix-activation config)
1165 "Return the activation gexp for CONFIG."
1166 (match config
5b58c28b 1167 (($ <guix-configuration> guix build-group build-accounts authorize-key? keys)
0adfe95a
LC
1168 ;; Assume that the store has BUILD-GROUP as its group. We could
1169 ;; otherwise call 'chown' here, but the problem is that on a COW unionfs,
1170 ;; chown leads to an entire copy of the tree, which is a bad idea.
1171
1172 ;; Optionally authorize hydra.gnu.org's key.
5f4a446d 1173 (if authorize-key?
5b58c28b
LC
1174 #~(begin
1175 #$@(map (cut hydra-key-authorization <> guix) keys))
5f4a446d 1176 #~#f))))
0adfe95a
LC
1177
1178(define guix-service-type
1179 (service-type
1180 (name 'guix)
1181 (extensions
d4053c71 1182 (list (service-extension shepherd-root-service-type guix-shepherd-service)
0adfe95a 1183 (service-extension account-service-type guix-accounts)
9a8b9eb8
LC
1184 (service-extension activation-service-type guix-activation)
1185 (service-extension profile-service-type
1186 (compose list guix-configuration-guix))))))
0adfe95a
LC
1187
1188(define* (guix-service #:optional (config %default-guix-configuration))
1189 "Return a service that runs the Guix build daemon according to
1190@var{config}."
1191 (service guix-service-type config))
1192
1c52181f
LC
1193
1194(define-record-type* <guix-publish-configuration>
1195 guix-publish-configuration make-guix-publish-configuration
1196 guix-publish-configuration?
1197 (guix guix-publish-configuration-guix ;package
1198 (default guix))
1199 (port guix-publish-configuration-port ;number
1200 (default 80))
1201 (host guix-publish-configuration-host ;string
1202 (default "localhost")))
1203
d4053c71 1204(define guix-publish-shepherd-service
1c52181f
LC
1205 (match-lambda
1206 (($ <guix-publish-configuration> guix port host)
d4053c71 1207 (list (shepherd-service
1c52181f
LC
1208 (provision '(guix-publish))
1209 (requirement '(guix-daemon))
1210 (start #~(make-forkexec-constructor
9fc037fe 1211 (list #$(file-append guix "/bin/guix")
1c52181f
LC
1212 "publish" "-u" "guix-publish"
1213 "-p" #$(number->string port)
1214 (string-append "--listen=" #$host))))
1215 (stop #~(make-kill-destructor)))))))
1216
1217(define %guix-publish-accounts
1218 (list (user-group (name "guix-publish") (system? #t))
1219 (user-account
1220 (name "guix-publish")
1221 (group "guix-publish")
1222 (system? #t)
1223 (comment "guix publish user")
1224 (home-directory "/var/empty")
9e41130b 1225 (shell (file-append shadow "/sbin/nologin")))))
1c52181f
LC
1226
1227(define guix-publish-service-type
1228 (service-type (name 'guix-publish)
1229 (extensions
d4053c71
AK
1230 (list (service-extension shepherd-root-service-type
1231 guix-publish-shepherd-service)
1c52181f
LC
1232 (service-extension account-service-type
1233 (const %guix-publish-accounts))))))
1234
1235(define* (guix-publish-service #:key (guix guix) (port 80) (host "localhost"))
1236 "Return a service that runs @command{guix publish} listening on @var{host}
1237and @var{port} (@pxref{Invoking guix publish}).
1238
1239This assumes that @file{/etc/guix} already contains a signing key pair as
1240created by @command{guix archive --generate-key} (@pxref{Invoking guix
1241archive}). If that is not the case, the service will fail to start."
1242 (service guix-publish-service-type
1243 (guix-publish-configuration (guix guix) (port port) (host host))))
1244
0adfe95a
LC
1245\f
1246;;;
1247;;; Udev.
1248;;;
1249
1250(define-record-type* <udev-configuration>
1251 udev-configuration make-udev-configuration
1252 udev-configuration?
1253 (udev udev-configuration-udev ;<package>
1254 (default udev))
1255 (rules udev-configuration-rules ;list of <package>
1256 (default '())))
db4fdc04 1257
ecd06ca9
LC
1258(define (udev-rules-union packages)
1259 "Return the union of the @code{lib/udev/rules.d} directories found in each
1260item of @var{packages}."
1261 (define build
4ee96a79
LC
1262 (with-imported-modules '((guix build union)
1263 (guix build utils))
1264 #~(begin
1265 (use-modules (guix build union)
1266 (guix build utils)
1267 (srfi srfi-1)
1268 (srfi srfi-26))
ecd06ca9 1269
4ee96a79
LC
1270 (define %standard-locations
1271 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
ecd06ca9 1272
4ee96a79
LC
1273 (define (rules-sub-directory directory)
1274 ;; Return the sub-directory of DIRECTORY containing udev rules, or
1275 ;; #f if none was found.
1276 (find directory-exists?
1277 (map (cut string-append directory <>) %standard-locations)))
ecd06ca9 1278
4ee96a79
LC
1279 (mkdir-p (string-append #$output "/lib/udev"))
1280 (union-build (string-append #$output "/lib/udev/rules.d")
1281 (filter-map rules-sub-directory '#$packages)))))
ecd06ca9 1282
4ee96a79 1283 (computed-file "udev-rules" build))
ecd06ca9 1284
80e6f37e
RW
1285(define (udev-rule file-name contents)
1286 "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
1287 (computed-file file-name
4ee96a79
LC
1288 (with-imported-modules '((guix build utils))
1289 #~(begin
1290 (use-modules (guix build utils))
1291
1292 (define rules.d
1293 (string-append #$output "/lib/udev/rules.d"))
1294
1295 (mkdir-p rules.d)
1296 (call-with-output-file
1297 (string-append rules.d "/" #$file-name)
1298 (lambda (port)
1299 (display #$contents port)))))))
7f28bf9a 1300
80e6f37e
RW
1301(define kvm-udev-rule
1302 ;; Return a directory with a udev rule that changes the group of /dev/kvm to
1303 ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
1304 ;; but now we have to add it by ourselves.
1305
1306 ;; Build users are part of the "kvm" group, so we can fearlessly make
1307 ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
1308 (udev-rule "90-kvm.rules"
1309 "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
1310
d4053c71
AK
1311(define udev-shepherd-service
1312 ;; Return a <shepherd-service> for UDEV with RULES.
0adfe95a
LC
1313 (match-lambda
1314 (($ <udev-configuration> udev rules)
80e6f37e 1315 (let* ((rules (udev-rules-union (cons* udev kvm-udev-rule rules)))
0adfe95a
LC
1316 (udev.conf (computed-file "udev.conf"
1317 #~(call-with-output-file #$output
1318 (lambda (port)
1319 (format port
1320 "udev_rules=\"~a/lib/udev/rules.d\"\n"
1321 #$rules))))))
1322 (list
d4053c71 1323 (shepherd-service
0adfe95a
LC
1324 (provision '(udev))
1325
1326 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
1327 ;; be added: see
1328 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
1329 (requirement '(root-file-system))
1330
1331 (documentation "Populate the /dev directory, dynamically.")
1332 (start #~(lambda ()
1333 (define find
1334 (@ (srfi srfi-1) find))
1335
1336 (define udevd
1337 ;; Choose the right 'udevd'.
1338 (find file-exists?
1339 (map (lambda (suffix)
1340 (string-append #$udev suffix))
1341 '("/libexec/udev/udevd" ;udev
1342 "/sbin/udevd")))) ;eudev
1343
1344 (define (wait-for-udevd)
1345 ;; Wait until someone's listening on udevd's control
1346 ;; socket.
1347 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
1348 (let try ()
1349 (catch 'system-error
1350 (lambda ()
1351 (connect sock PF_UNIX "/run/udev/control")
1352 (close-port sock))
1353 (lambda args
1354 (format #t "waiting for udevd...~%")
1355 (usleep 500000)
1356 (try))))))
1357
1358 ;; Allow udev to find the modules.
1359 (setenv "LINUX_MODULE_DIRECTORY"
1360 "/run/booted-system/kernel/lib/modules")
1361
1362 ;; The first one is for udev, the second one for eudev.
1363 (setenv "UDEV_CONFIG_FILE" #$udev.conf)
1364 (setenv "EUDEV_RULES_DIRECTORY"
9fc037fe 1365 #$(file-append rules "/lib/udev/rules.d"))
0adfe95a
LC
1366
1367 (let ((pid (primitive-fork)))
1368 (case pid
1369 ((0)
1370 (exec-command (list udevd)))
1371 (else
1372 ;; Wait until udevd is up and running. This
1373 ;; appears to be needed so that the events
1374 ;; triggered below are actually handled.
1375 (wait-for-udevd)
1376
1377 ;; Trigger device node creation.
9fc037fe 1378 (system* #$(file-append udev "/bin/udevadm")
0adfe95a
LC
1379 "trigger" "--action=add")
1380
1381 ;; Wait for things to settle down.
9fc037fe 1382 (system* #$(file-append udev "/bin/udevadm")
0adfe95a
LC
1383 "settle")
1384 pid)))))
1385 (stop #~(make-kill-destructor))
1386
1387 ;; When halting the system, 'udev' is actually killed by
1388 ;; 'user-processes', i.e., before its own 'stop' method was called.
1389 ;; Thus, make sure it is not respawned.
1390 (respawn? #f)))))))
1391
1392(define udev-service-type
1393 (service-type (name 'udev)
1394 (extensions
d4053c71
AK
1395 (list (service-extension shepherd-root-service-type
1396 udev-shepherd-service)))
0adfe95a
LC
1397
1398 (compose concatenate) ;concatenate the list of rules
1399 (extend (lambda (config rules)
1400 (match config
1401 (($ <udev-configuration> udev initial-rules)
1402 (udev-configuration
1403 (udev udev)
1404 (rules (append initial-rules rules)))))))))
1405
255f7308 1406(define* (udev-service #:key (udev eudev) (rules '()))
ecd06ca9
LC
1407 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
1408extra rules from the packages listed in @var{rules}."
0adfe95a
LC
1409 (service udev-service-type
1410 (udev-configuration (udev udev) (rules rules))))
1411
0adfe95a 1412(define swap-service-type
d4053c71 1413 (shepherd-service-type
00184239 1414 'swap
0adfe95a
LC
1415 (lambda (device)
1416 (define requirement
1417 (if (string-prefix? "/dev/mapper/" device)
1418 (list (symbol-append 'device-mapping-
1419 (string->symbol (basename device))))
1420 '()))
1421
d4053c71 1422 (shepherd-service
0adfe95a
LC
1423 (provision (list (symbol-append 'swap- (string->symbol device))))
1424 (requirement `(udev ,@requirement))
1425 (documentation "Enable the given swap device.")
1426 (start #~(lambda ()
1427 (restart-on-EINTR (swapon #$device))
1428 #t))
1429 (stop #~(lambda _
1430 (restart-on-EINTR (swapoff #$device))
1431 #f))
1432 (respawn? #f)))))
5dae0186 1433
2a13d05e
LC
1434(define (swap-service device)
1435 "Return a service that uses @var{device} as a swap device."
0adfe95a 1436 (service swap-service-type device))
2a13d05e 1437
8664cc88
LC
1438(define-record-type* <gpm-configuration>
1439 gpm-configuration make-gpm-configuration gpm-configuration?
1440 (gpm gpm-configuration-gpm) ;package
1441 (options gpm-configuration-options)) ;list of strings
1442
d4053c71 1443(define gpm-shepherd-service
8664cc88 1444 (match-lambda
a907d997 1445 (($ <gpm-configuration> gpm options)
d4053c71 1446 (list (shepherd-service
8664cc88
LC
1447 (requirement '(udev))
1448 (provision '(gpm))
1449 (start #~(lambda ()
1450 ;; 'gpm' runs in the background and sets a PID file.
1451 ;; Note that it requires running as "root".
1452 (false-if-exception (delete-file "/var/run/gpm.pid"))
9fc037fe 1453 (fork+exec-command (list #$(file-append gpm "/sbin/gpm")
8664cc88
LC
1454 #$@options))
1455
1456 ;; Wait for the PID file to appear; declare failure if
1457 ;; it doesn't show up.
1458 (let loop ((i 3))
1459 (or (file-exists? "/var/run/gpm.pid")
1460 (if (zero? i)
1461 #f
1462 (begin
1463 (sleep 1)
1464 (loop (1- i))))))))
1465
1466 (stop #~(lambda (_)
1467 ;; Return #f if successfully stopped.
9fc037fe 1468 (not (zero? (system* #$(file-append gpm "/sbin/gpm")
8664cc88
LC
1469 "-k"))))))))))
1470
1471(define gpm-service-type
1472 (service-type (name 'gpm)
1473 (extensions
d4053c71
AK
1474 (list (service-extension shepherd-root-service-type
1475 gpm-shepherd-service)))))
8664cc88
LC
1476
1477(define* (gpm-service #:key (gpm gpm)
1478 (options '("-m" "/dev/input/mice" "-t" "ps2")))
1479 "Run @var{gpm}, the general-purpose mouse daemon, with the given
1480command-line @var{options}. GPM allows users to use the mouse in the console,
1481notably to select, copy, and paste text. The default value of @var{options}
1482uses the @code{ps2} protocol, which works for both USB and PS/2 mice.
1483
1484This service is not part of @var{%base-services}."
1485 ;; To test in QEMU, use "-usbdevice mouse" and then, in the monitor, use
1486 ;; "info mice" and "mouse_set X" to use the right mouse.
1487 (service gpm-service-type
1488 (gpm-configuration (gpm gpm) (options options))))
1489
46ec2707
DC
1490(define-record-type* <kmscon-configuration>
1491 kmscon-configuration make-kmscon-configuration
1492 kmscon-configuration?
1493 (kmscon kmscon-configuration-kmscon
1494 (default kmscon))
1495 (virtual-terminal kmscon-configuration-virtual-terminal)
1496 (login-program kmscon-configuration-login-program
9fc037fe 1497 (default (file-append shadow "/bin/login")))
46ec2707
DC
1498 (login-arguments kmscon-configuration-login-arguments
1499 (default '("-p")))
1500 (hardware-acceleration? kmscon-configuration-hardware-acceleration?
1501 (default #f))) ; #t causes failure
1502
1503(define kmscon-service-type
1504 (shepherd-service-type
1505 'kmscon
1506 (lambda (config)
1507 (let ((kmscon (kmscon-configuration-kmscon config))
1508 (virtual-terminal (kmscon-configuration-virtual-terminal config))
1509 (login-program (kmscon-configuration-login-program config))
1510 (login-arguments (kmscon-configuration-login-arguments config))
1511 (hardware-acceleration? (kmscon-configuration-hardware-acceleration? config)))
1512
1513 (define kmscon-command
1514 #~(list
9fc037fe 1515 #$(file-append kmscon "/bin/kmscon") "--login"
46ec2707
DC
1516 "--vt" #$virtual-terminal
1517 #$@(if hardware-acceleration? '("--hwaccel") '())
1518 "--" #$login-program #$@login-arguments))
1519
1520 (shepherd-service
1521 (documentation "kmscon virtual terminal")
1522 (requirement '(user-processes udev dbus-system))
1523 (provision (list (symbol-append 'term- (string->symbol virtual-terminal))))
1524 (start #~(make-forkexec-constructor #$kmscon-command))
1525 (stop #~(make-kill-destructor)))))))
1526
8664cc88 1527\f
8b198abe
LC
1528(define %base-services
1529 ;; Convenience variable holding the basic services.
317d3b47
DC
1530 (list (login-service)
1531
4a84a487
LC
1532 (service console-font-service-type
1533 (map (lambda (tty)
1534 (cons tty %default-console-font))
1535 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
317d3b47
DC
1536
1537 (mingetty-service (mingetty-configuration
1538 (tty "tty1")))
1539 (mingetty-service (mingetty-configuration
1540 (tty "tty2")))
1541 (mingetty-service (mingetty-configuration
1542 (tty "tty3")))
1543 (mingetty-service (mingetty-configuration
1544 (tty "tty4")))
1545 (mingetty-service (mingetty-configuration
1546 (tty "tty5")))
1547 (mingetty-service (mingetty-configuration
1548 (tty "tty6")))
1549
8de3e4b3
LC
1550 (service static-networking-service-type
1551 (list (static-networking (interface "lo")
1552 (ip "127.0.0.1")
1553 (provision '(loopback)))))
317d3b47
DC
1554 (syslog-service)
1555 (urandom-seed-service)
1556 (guix-service)
1557 (nscd-service)
1558
1559 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
1560 ;; used, so enable them by default. The FUSE and ALSA rules are
1561 ;; less critical, but handy.
387e1754
LC
1562 (udev-service #:rules (list lvm2 fuse alsa-utils crda))
1563
1564 (service special-files-service-type
1565 `(("/bin/sh" ,(file-append (canonical-package bash)
1566 "/bin/sh"))))))
8b198abe 1567
db4fdc04 1568;;; base.scm ends here