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