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