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