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