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