gnu: libxml2: Update replacement to 2.9.4 [security fixes].
[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
434(define %urandom-seed-activation
435 ;; Activation gexp for the urandom seed
436 #~(begin
437 (use-modules (guix build utils))
438
439 (mkdir-p (dirname #$%random-seed-file))
440 (close-port (open-file #$%random-seed-file "a0b"))
441 (chmod #$%random-seed-file #o600)))
442
443(define (urandom-seed-shepherd-service _)
444 "Return a shepherd service for the /dev/urandom seed."
445 (list (shepherd-service
446 (documentation "Preserve entropy across reboots for /dev/urandom.")
447 (provision '(urandom-seed))
448 (requirement '(user-processes))
449 (start #~(lambda _
450 ;; On boot, write random seed into /dev/urandom.
451 (when (file-exists? #$%random-seed-file)
452 (call-with-input-file #$%random-seed-file
453 (lambda (seed)
454 (call-with-output-file "/dev/urandom"
455 (lambda (urandom)
456 (dump-port seed urandom))))))
457 #t))
458 (stop #~(lambda _
459 ;; During shutdown, write from /dev/urandom into random seed.
460 (let ((buf (make-bytevector 512)))
461 (call-with-input-file "/dev/urandom"
462 (lambda (urandom)
463 (get-bytevector-n! urandom buf 0 512)
464 (call-with-output-file #$%random-seed-file
465 (lambda (seed)
466 (put-bytevector seed buf)))
467 #t)))))
468 (modules `((rnrs bytevectors)
469 (rnrs io ports)
470 ,@%default-modules)))))
471
472(define urandom-seed-service-type
473 (service-type (name 'urandom-seed)
474 (extensions
475 (list (service-extension shepherd-root-service-type
476 urandom-seed-shepherd-service)
477 (service-extension activation-service-type
478 (const %urandom-seed-activation))))))
479
480(define (urandom-seed-service)
481 (service urandom-seed-service-type #f))
482
483\f
e10964ef
SB
484;;;
485;;; System-wide environment variables.
486;;;
487
488(define (environment-variables->environment-file vars)
489 "Return a file for pam_env(8) that contains environment variables VARS."
490 (apply mixed-text-file "environment"
491 (append-map (match-lambda
492 ((key . value)
493 (list key "=" value "\n")))
494 vars)))
495
496(define session-environment-service-type
497 (service-type
498 (name 'session-environment)
499 (extensions
500 (list (service-extension
501 etc-service-type
502 (lambda (vars)
503 (list `("environment"
504 ,(environment-variables->environment-file vars)))))))
505 (compose concatenate)
506 (extend append)))
507
508(define (session-environment-service vars)
509 "Return a service that builds the @file{/etc/environment}, which can be read
510by PAM-aware applications to set environment variables for sessions.
511
512VARS should be an association list in which both the keys and the values are
513strings or string-valued gexps."
514 (service session-environment-service-type vars))
515
516\f
0adfe95a
LC
517;;;
518;;; Console & co.
519;;;
520
521(define host-name-service-type
d4053c71 522 (shepherd-service-type
00184239 523 'host-name
0adfe95a 524 (lambda (name)
d4053c71 525 (shepherd-service
0adfe95a
LC
526 (documentation "Initialize the machine's host name.")
527 (provision '(host-name))
528 (start #~(lambda _
529 (sethostname #$name)))
530 (respawn? #f)))))
a00dd9fb 531
db4fdc04 532(define (host-name-service name)
51da7ca0 533 "Return a service that sets the host name to @var{name}."
0adfe95a 534 (service host-name-service-type name))
db4fdc04 535
62ca0fdf
LC
536(define (unicode-start tty)
537 "Return a gexp to start Unicode support on @var{tty}."
538
539 ;; We have to run 'unicode_start' in a pipe so that when it invokes the
540 ;; 'tty' command, that command returns TTY.
541 #~(begin
542 (let ((pid (primitive-fork)))
543 (case pid
544 ((0)
545 (close-fdes 0)
546 (dup2 (open-fdes #$tty O_RDONLY) 0)
547 (close-fdes 1)
548 (dup2 (open-fdes #$tty O_WRONLY) 1)
549 (execl (string-append #$kbd "/bin/unicode_start")
550 "unicode_start"))
551 (else
552 (zero? (cdr (waitpid pid))))))))
553
0adfe95a 554(define console-keymap-service-type
d4053c71 555 (shepherd-service-type
00184239 556 'console-keymap
b3d05f48 557 (lambda (files)
d4053c71 558 (shepherd-service
0adfe95a
LC
559 (documentation (string-append "Load console keymap (loadkeys)."))
560 (provision '(console-keymap))
561 (start #~(lambda _
562 (zero? (system* (string-append #$kbd "/bin/loadkeys")
b3d05f48 563 #$@files))))
0adfe95a
LC
564 (respawn? #f)))))
565
b3d05f48
AK
566(define (console-keymap-service . files)
567 "Return a service to load console keymaps from @var{files}."
568 (service console-keymap-service-type files))
0adfe95a
LC
569
570(define console-font-service-type
d4053c71 571 (shepherd-service-type
00184239 572 'console-font
0adfe95a
LC
573 (match-lambda
574 ((tty font)
575 (let ((device (string-append "/dev/" tty)))
d4053c71 576 (shepherd-service
0adfe95a
LC
577 (documentation "Load a Unicode console font.")
578 (provision (list (symbol-append 'console-font-
579 (string->symbol tty))))
580
581 ;; Start after mingetty has been started on TTY, otherwise the settings
582 ;; are ignored.
583 (requirement (list (symbol-append 'term-
584 (string->symbol tty))))
585
586 (start #~(lambda _
587 (and #$(unicode-start device)
588 (zero?
589 (system* (string-append #$kbd "/bin/setfont")
590 "-C" #$device #$font)))))
591 (stop #~(const #t))
592 (respawn? #f)))))))
5eca9459 593
62ca0fdf
LC
594(define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
595 "Return a service that sets up Unicode support in @var{tty} and loads
596@var{font} for that tty (fonts are per virtual console in Linux.)"
597 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
598 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
599 ;; codepoints notably found in the UTF-8 manual.
0adfe95a 600 (service console-font-service-type (list tty font)))
62ca0fdf 601
66e4f01c
LC
602(define-record-type* <mingetty-configuration>
603 mingetty-configuration make-mingetty-configuration
604 mingetty-configuration?
605 (mingetty mingetty-configuration-mingetty ;<package>
606 (default mingetty))
607 (tty mingetty-configuration-tty) ;string
608 (motd mingetty-configuration-motd ;file-like
609 (default (plain-file "motd" "Welcome.\n")))
610 (auto-login mingetty-auto-login ;string | #f
611 (default #f))
612 (login-program mingetty-login-program ;gexp
613 (default #f))
614 (login-pause? mingetty-login-pause? ;Boolean
615 (default #f))
616
617 ;; Allow empty passwords by default so that first-time users can log in when
618 ;; the 'root' account has just been created.
619 (allow-empty-passwords? mingetty-configuration-allow-empty-passwords?
620 (default #t))) ;Boolean
621
0adfe95a
LC
622(define (mingetty-pam-service conf)
623 "Return the list of PAM service needed for CONF."
624 ;; Let 'login' be known to PAM. All the mingetty services will have that
625 ;; PAM service, but that's fine because they're all identical and duplicates
626 ;; are removed.
627 (list (unix-pam-service "login"
628 #:allow-empty-passwords?
629 (mingetty-configuration-allow-empty-passwords? conf)
630 #:motd
631 (mingetty-configuration-motd conf))))
632
d4053c71 633(define mingetty-shepherd-service
0adfe95a 634 (match-lambda
66e4f01c
LC
635 (($ <mingetty-configuration> mingetty tty motd auto-login login-program
636 login-pause? allow-empty-passwords?)
0adfe95a 637 (list
d4053c71 638 (shepherd-service
0adfe95a
LC
639 (documentation "Run mingetty on an tty.")
640 (provision (list (symbol-append 'term- (string->symbol tty))))
641
642 ;; Since the login prompt shows the host name, wait for the 'host-name'
643 ;; service to be done. Also wait for udev essentially so that the tty
644 ;; text is not lost in the middle of kernel messages (XXX).
645 (requirement '(user-processes host-name udev))
646
647 (start #~(make-forkexec-constructor
648 (list (string-append #$mingetty "/sbin/mingetty")
649 "--noclear" #$tty
650 #$@(if auto-login
651 #~("--autologin" #$auto-login)
652 #~())
653 #$@(if login-program
654 #~("--loginprog" #$login-program)
655 #~())
656 #$@(if login-pause?
657 #~("--loginpause")
658 #~()))))
659 (stop #~(make-kill-destructor)))))))
660
661(define mingetty-service-type
662 (service-type (name 'mingetty)
d4053c71
AK
663 (extensions (list (service-extension shepherd-root-service-type
664 mingetty-shepherd-service)
0adfe95a
LC
665 (service-extension pam-root-service-type
666 mingetty-pam-service)))))
667
668(define* (mingetty-service config)
669 "Return a service to run mingetty according to @var{config}, which specifies
670the tty to run, among other things."
671 (service mingetty-service-type config))
db4fdc04 672
6454b333
LC
673(define-record-type* <nscd-configuration> nscd-configuration
674 make-nscd-configuration
675 nscd-configuration?
676 (log-file nscd-configuration-log-file ;string
677 (default "/var/log/nscd.log"))
678 (debug-level nscd-debug-level ;integer
679 (default 0))
680 ;; TODO: See nscd.conf in glibc for other options to add.
681 (caches nscd-configuration-caches ;list of <nscd-cache>
b893f1ae
LC
682 (default %nscd-default-caches))
683 (name-services nscd-configuration-name-services ;list of <packages>
684 (default '()))
685 (glibc nscd-configuration-glibc ;<package>
686 (default (canonical-package glibc))))
6454b333
LC
687
688(define-record-type* <nscd-cache> nscd-cache make-nscd-cache
689 nscd-cache?
690 (database nscd-cache-database) ;symbol
691 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
692 (negative-time-to-live nscd-cache-negative-time-to-live
693 (default 20)) ;integer
694 (suggested-size nscd-cache-suggested-size ;integer ("default module
695 ;of hash table")
696 (default 211))
697 (check-files? nscd-cache-check-files? ;Boolean
698 (default #t))
699 (persistent? nscd-cache-persistent? ;Boolean
700 (default #t))
701 (shared? nscd-cache-shared? ;Boolean
702 (default #t))
703 (max-database-size nscd-cache-max-database-size ;integer
704 (default (* 32 (expt 2 20))))
705 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
706 (default #t)))
707
708(define %nscd-default-caches
709 ;; Caches that we want to enable by default. Note that when providing an
710 ;; empty nscd.conf, all caches are disabled.
711 (list (nscd-cache (database 'hosts)
712
713 ;; Aggressively cache the host name cache to improve
714 ;; privacy and resilience.
715 (positive-time-to-live (* 3600 12))
716 (negative-time-to-live 20)
717 (persistent? #t))
718
719 (nscd-cache (database 'services)
720
721 ;; Services are unlikely to change, so we can be even more
722 ;; aggressive.
723 (positive-time-to-live (* 3600 24))
724 (negative-time-to-live 3600)
725 (check-files? #t) ;check /etc/services changes
726 (persistent? #t))))
727
728(define %nscd-default-configuration
729 ;; Default nscd configuration.
730 (nscd-configuration))
731
732(define (nscd.conf-file config)
733 "Return the @file{nscd.conf} configuration file for @var{config}, an
734@code{<nscd-configuration>} object."
735 (define cache->config
736 (match-lambda
be1c2c54
LC
737 (($ <nscd-cache> (= symbol->string database)
738 positive-ttl negative-ttl size check-files?
739 persistent? shared? max-size propagate?)
740 (string-append "\nenable-cache\t" database "\tyes\n"
741
742 "positive-time-to-live\t" database "\t"
743 (number->string positive-ttl) "\n"
744 "negative-time-to-live\t" database "\t"
745 (number->string negative-ttl) "\n"
746 "suggested-size\t" database "\t"
747 (number->string size) "\n"
748 "check-files\t" database "\t"
749 (if check-files? "yes\n" "no\n")
750 "persistent\t" database "\t"
751 (if persistent? "yes\n" "no\n")
752 "shared\t" database "\t"
753 (if shared? "yes\n" "no\n")
754 "max-db-size\t" database "\t"
755 (number->string max-size) "\n"
756 "auto-propagate\t" database "\t"
757 (if propagate? "yes\n" "no\n")))))
6454b333
LC
758
759 (match config
760 (($ <nscd-configuration> log-file debug-level caches)
be1c2c54
LC
761 (plain-file "nscd.conf"
762 (string-append "\
6454b333 763# Configuration of libc's name service cache daemon (nscd).\n\n"
be1c2c54
LC
764 (if log-file
765 (string-append "logfile\t" log-file)
766 "")
767 "\n"
768 (if debug-level
769 (string-append "debug-level\t"
770 (number->string debug-level))
771 "")
772 "\n"
773 (string-concatenate
774 (map cache->config caches)))))))
6454b333 775
d4053c71
AK
776(define (nscd-shepherd-service config)
777 "Return a shepherd service for CONFIG, an <nscd-configuration> object."
0adfe95a
LC
778 (let ((nscd.conf (nscd.conf-file config))
779 (name-services (nscd-configuration-name-services config)))
d4053c71 780 (list (shepherd-service
0adfe95a
LC
781 (documentation "Run libc's name service cache daemon (nscd).")
782 (provision '(nscd))
783 (requirement '(user-processes))
784 (start #~(make-forkexec-constructor
785 (list (string-append #$(nscd-configuration-glibc config)
786 "/sbin/nscd")
787 "-f" #$nscd.conf "--foreground")
788
789 #:environment-variables
790 (list (string-append "LD_LIBRARY_PATH="
791 (string-join
792 (map (lambda (dir)
793 (string-append dir "/lib"))
794 (list #$@name-services))
795 ":")))))
cc7234ae 796 (stop #~(make-kill-destructor))))))
0adfe95a
LC
797
798(define nscd-activation
799 ;; Actions to take before starting nscd.
800 #~(begin
801 (use-modules (guix build utils))
802 (mkdir-p "/var/run/nscd")
803 (mkdir-p "/var/db/nscd"))) ;for the persistent cache
804
805(define nscd-service-type
806 (service-type (name 'nscd)
807 (extensions
808 (list (service-extension activation-service-type
809 (const nscd-activation))
d4053c71
AK
810 (service-extension shepherd-root-service-type
811 nscd-shepherd-service)))
0adfe95a
LC
812
813 ;; This can be extended by providing additional name services
814 ;; such as nss-mdns.
815 (compose concatenate)
816 (extend (lambda (config name-services)
817 (nscd-configuration
818 (inherit config)
819 (name-services (append
820 (nscd-configuration-name-services config)
821 name-services)))))))
822
b893f1ae 823(define* (nscd-service #:optional (config %nscd-default-configuration))
6454b333 824 "Return a service that runs libc's name service cache daemon (nscd) with the
b893f1ae
LC
825given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
826Service Switch}, for an example."
0adfe95a
LC
827 (service nscd-service-type config))
828
829(define syslog-service-type
d4053c71 830 (shepherd-service-type
00184239 831 'syslog
0adfe95a 832 (lambda (config-file)
d4053c71 833 (shepherd-service
0adfe95a
LC
834 (documentation "Run the syslog daemon (syslogd).")
835 (provision '(syslogd))
836 (requirement '(user-processes))
837 (start #~(make-forkexec-constructor
838 (list (string-append #$inetutils "/libexec/syslogd")
839 "--no-detach" "--rcfile" #$config-file)))
840 (stop #~(make-kill-destructor))))))
be1c2c54
LC
841
842;; Snippet adapted from the GNU inetutils manual.
843(define %default-syslog.conf
844 (plain-file "syslog.conf" "
1f3fc60d 845 # Log all error messages, authentication messages of
db4fdc04
LC
846 # level notice or higher and anything of level err or
847 # higher to the console.
848 # Don't log private authentication messages!
6a191274 849 *.alert;auth.notice;authpriv.none /dev/console
db4fdc04
LC
850
851 # Log anything (except mail) of level info or higher.
852 # Don't log private authentication messages!
853 *.info;mail.none;authpriv.none /var/log/messages
854
855 # Same, in a different place.
856 *.info;mail.none;authpriv.none /dev/tty12
857
858 # The authpriv file has restricted access.
859 authpriv.* /var/log/secure
860
861 # Log all the mail messages in one place.
862 mail.* /var/log/maillog
be1c2c54 863"))
0adfe95a 864
be1c2c54 865(define* (syslog-service #:key (config-file %default-syslog.conf))
44abcb28
LC
866 "Return a service that runs @command{syslogd}. If configuration file
867name @var{config-file} is not specified, use some reasonable default
868settings.
869
870@xref{syslogd invocation,,, inetutils, GNU Inetutils}, for more
871information on the configuration file syntax."
0adfe95a 872 (service syslog-service-type config-file))
db4fdc04 873
1c52181f
LC
874\f
875;;;
876;;; Guix services.
877;;;
878
db4fdc04 879(define* (guix-build-accounts count #:key
ab6a279a 880 (group "guixbuild")
db4fdc04 881 (first-uid 30001)
db4fdc04
LC
882 (shadow shadow))
883 "Return a list of COUNT user accounts for Guix build users, with UIDs
884starting at FIRST-UID, and under GID."
5250a4f2
LC
885 (unfold (cut > <> count)
886 (lambda (n)
887 (user-account
888 (name (format #f "guixbuilder~2,'0d" n))
889 (system? #t)
890 (uid (+ first-uid n -1))
891 (group group)
892
893 ;; guix-daemon expects GROUP to be listed as a
894 ;; supplementary group too:
895 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
896 (supplementary-groups (list group "kvm"))
897
898 (comment (format #f "Guix Build User ~2d" n))
899 (home-directory "/var/empty")
900 (shell #~(string-append #$shadow "/sbin/nologin"))))
901 1+
902 1))
db4fdc04 903
2c5c696c
LC
904(define (hydra-key-authorization guix)
905 "Return a gexp with code to register the hydra.gnu.org public key with
906GUIX."
907 #~(unless (file-exists? "/etc/guix/acl")
908 (let ((pid (primitive-fork)))
909 (case pid
910 ((0)
911 (let* ((key (string-append #$guix
912 "/share/guix/hydra.gnu.org.pub"))
913 (port (open-file key "r0b")))
914 (format #t "registering public key '~a'...~%" key)
915 (close-port (current-input-port))
2c5c696c
LC
916 (dup port 0)
917 (execl (string-append #$guix "/bin/guix")
918 "guix" "archive" "--authorize")
919 (exit 1)))
920 (else
921 (let ((status (cdr (waitpid pid))))
922 (unless (zero? status)
923 (format (current-error-port) "warning: \
924failed to register hydra.gnu.org public key: ~a~%" status))))))))
925
0adfe95a
LC
926(define-record-type* <guix-configuration>
927 guix-configuration make-guix-configuration
928 guix-configuration?
929 (guix guix-configuration-guix ;<package>
930 (default guix))
931 (build-group guix-configuration-build-group ;string
932 (default "guixbuild"))
933 (build-accounts guix-configuration-build-accounts ;integer
934 (default 10))
935 (authorize-key? guix-configuration-authorize-key? ;Boolean
936 (default #t))
937 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
938 (default #t))
b0b9f6e0
LC
939 (substitute-urls guix-configuration-substitute-urls ;list of strings
940 (default %default-substitute-urls))
0adfe95a
LC
941 (extra-options guix-configuration-extra-options ;list of strings
942 (default '()))
943 (lsof guix-configuration-lsof ;<package>
944 (default lsof))
945 (lsh guix-configuration-lsh ;<package>
946 (default lsh)))
947
948(define %default-guix-configuration
949 (guix-configuration))
950
d4053c71
AK
951(define (guix-shepherd-service config)
952 "Return a <shepherd-service> for the Guix daemon service with CONFIG."
0adfe95a
LC
953 (match config
954 (($ <guix-configuration> guix build-group build-accounts authorize-key?
b0b9f6e0
LC
955 use-substitutes? substitute-urls extra-options
956 lsof lsh)
d4053c71 957 (list (shepherd-service
0adfe95a
LC
958 (documentation "Run the Guix daemon.")
959 (provision '(guix-daemon))
960 (requirement '(user-processes))
961 (start
962 #~(make-forkexec-constructor
963 (list (string-append #$guix "/bin/guix-daemon")
964 "--build-users-group" #$build-group
965 #$@(if use-substitutes?
966 '()
967 '("--no-substitutes"))
b0b9f6e0 968 "--substitute-urls" #$(string-join substitute-urls)
0adfe95a
LC
969 #$@extra-options)
970
971 ;; Add 'lsof' (for the GC) and 'lsh' (for offloading) to the
972 ;; daemon's $PATH.
973 #:environment-variables
974 (list (string-append "PATH=" #$lsof "/bin:" #$lsh "/bin"))))
975 (stop #~(make-kill-destructor)))))))
976
977(define (guix-accounts config)
978 "Return the user accounts and user groups for CONFIG."
979 (match config
980 (($ <guix-configuration> _ build-group build-accounts)
981 (cons (user-group
982 (name build-group)
983 (system? #t)
984
985 ;; Use a fixed GID so that we can create the store with the right
986 ;; owner.
987 (id 30000))
988 (guix-build-accounts build-accounts
989 #:group build-group)))))
990
991(define (guix-activation config)
992 "Return the activation gexp for CONFIG."
993 (match config
994 (($ <guix-configuration> guix build-group build-accounts authorize-key?)
995 ;; Assume that the store has BUILD-GROUP as its group. We could
996 ;; otherwise call 'chown' here, but the problem is that on a COW unionfs,
997 ;; chown leads to an entire copy of the tree, which is a bad idea.
998
999 ;; Optionally authorize hydra.gnu.org's key.
1000 (and authorize-key?
1001 (hydra-key-authorization guix)))))
1002
1003(define guix-service-type
1004 (service-type
1005 (name 'guix)
1006 (extensions
d4053c71 1007 (list (service-extension shepherd-root-service-type guix-shepherd-service)
0adfe95a 1008 (service-extension account-service-type guix-accounts)
9a8b9eb8
LC
1009 (service-extension activation-service-type guix-activation)
1010 (service-extension profile-service-type
1011 (compose list guix-configuration-guix))))))
0adfe95a
LC
1012
1013(define* (guix-service #:optional (config %default-guix-configuration))
1014 "Return a service that runs the Guix build daemon according to
1015@var{config}."
1016 (service guix-service-type config))
1017
1c52181f
LC
1018
1019(define-record-type* <guix-publish-configuration>
1020 guix-publish-configuration make-guix-publish-configuration
1021 guix-publish-configuration?
1022 (guix guix-publish-configuration-guix ;package
1023 (default guix))
1024 (port guix-publish-configuration-port ;number
1025 (default 80))
1026 (host guix-publish-configuration-host ;string
1027 (default "localhost")))
1028
d4053c71 1029(define guix-publish-shepherd-service
1c52181f
LC
1030 (match-lambda
1031 (($ <guix-publish-configuration> guix port host)
d4053c71 1032 (list (shepherd-service
1c52181f
LC
1033 (provision '(guix-publish))
1034 (requirement '(guix-daemon))
1035 (start #~(make-forkexec-constructor
1036 (list (string-append #$guix "/bin/guix")
1037 "publish" "-u" "guix-publish"
1038 "-p" #$(number->string port)
1039 (string-append "--listen=" #$host))))
1040 (stop #~(make-kill-destructor)))))))
1041
1042(define %guix-publish-accounts
1043 (list (user-group (name "guix-publish") (system? #t))
1044 (user-account
1045 (name "guix-publish")
1046 (group "guix-publish")
1047 (system? #t)
1048 (comment "guix publish user")
1049 (home-directory "/var/empty")
1050 (shell #~(string-append #$shadow "/sbin/nologin")))))
1051
1052(define guix-publish-service-type
1053 (service-type (name 'guix-publish)
1054 (extensions
d4053c71
AK
1055 (list (service-extension shepherd-root-service-type
1056 guix-publish-shepherd-service)
1c52181f
LC
1057 (service-extension account-service-type
1058 (const %guix-publish-accounts))))))
1059
1060(define* (guix-publish-service #:key (guix guix) (port 80) (host "localhost"))
1061 "Return a service that runs @command{guix publish} listening on @var{host}
1062and @var{port} (@pxref{Invoking guix publish}).
1063
1064This assumes that @file{/etc/guix} already contains a signing key pair as
1065created by @command{guix archive --generate-key} (@pxref{Invoking guix
1066archive}). If that is not the case, the service will fail to start."
1067 (service guix-publish-service-type
1068 (guix-publish-configuration (guix guix) (port port) (host host))))
1069
0adfe95a
LC
1070\f
1071;;;
1072;;; Udev.
1073;;;
1074
1075(define-record-type* <udev-configuration>
1076 udev-configuration make-udev-configuration
1077 udev-configuration?
1078 (udev udev-configuration-udev ;<package>
1079 (default udev))
1080 (rules udev-configuration-rules ;list of <package>
1081 (default '())))
db4fdc04 1082
ecd06ca9
LC
1083(define (udev-rules-union packages)
1084 "Return the union of the @code{lib/udev/rules.d} directories found in each
1085item of @var{packages}."
1086 (define build
1087 #~(begin
1088 (use-modules (guix build union)
1089 (guix build utils)
1090 (srfi srfi-1)
1091 (srfi srfi-26))
1092
1093 (define %standard-locations
1094 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
1095
1096 (define (rules-sub-directory directory)
1097 ;; Return the sub-directory of DIRECTORY containing udev rules, or
1098 ;; #f if none was found.
1099 (find directory-exists?
1100 (map (cut string-append directory <>) %standard-locations)))
1101
1102 (mkdir-p (string-append #$output "/lib/udev"))
1103 (union-build (string-append #$output "/lib/udev/rules.d")
1104 (filter-map rules-sub-directory '#$packages))))
1105
be1c2c54
LC
1106 (computed-file "udev-rules" build
1107 #:modules '((guix build union)
1108 (guix build utils))))
ecd06ca9 1109
80e6f37e
RW
1110(define (udev-rule file-name contents)
1111 "Return a directory with a udev rule file FILE-NAME containing CONTENTS."
1112 (computed-file file-name
be1c2c54
LC
1113 #~(begin
1114 (use-modules (guix build utils))
1115
1116 (define rules.d
1117 (string-append #$output "/lib/udev/rules.d"))
1118
1119 (mkdir-p rules.d)
1120 (call-with-output-file
80e6f37e 1121 (string-append rules.d "/" #$file-name)
be1c2c54 1122 (lambda (port)
80e6f37e 1123 (display #$contents port))))
be1c2c54 1124 #:modules '((guix build utils))))
7f28bf9a 1125
80e6f37e
RW
1126(define kvm-udev-rule
1127 ;; Return a directory with a udev rule that changes the group of /dev/kvm to
1128 ;; "kvm" and makes it #o660. Apparently QEMU-KVM used to ship this rule,
1129 ;; but now we have to add it by ourselves.
1130
1131 ;; Build users are part of the "kvm" group, so we can fearlessly make
1132 ;; /dev/kvm 660 (see <http://bugs.gnu.org/18994>, for background.)
1133 (udev-rule "90-kvm.rules"
1134 "KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n"))
1135
d4053c71
AK
1136(define udev-shepherd-service
1137 ;; Return a <shepherd-service> for UDEV with RULES.
0adfe95a
LC
1138 (match-lambda
1139 (($ <udev-configuration> udev rules)
80e6f37e 1140 (let* ((rules (udev-rules-union (cons* udev kvm-udev-rule rules)))
0adfe95a
LC
1141 (udev.conf (computed-file "udev.conf"
1142 #~(call-with-output-file #$output
1143 (lambda (port)
1144 (format port
1145 "udev_rules=\"~a/lib/udev/rules.d\"\n"
1146 #$rules))))))
1147 (list
d4053c71 1148 (shepherd-service
0adfe95a
LC
1149 (provision '(udev))
1150
1151 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
1152 ;; be added: see
1153 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
1154 (requirement '(root-file-system))
1155
1156 (documentation "Populate the /dev directory, dynamically.")
1157 (start #~(lambda ()
1158 (define find
1159 (@ (srfi srfi-1) find))
1160
1161 (define udevd
1162 ;; Choose the right 'udevd'.
1163 (find file-exists?
1164 (map (lambda (suffix)
1165 (string-append #$udev suffix))
1166 '("/libexec/udev/udevd" ;udev
1167 "/sbin/udevd")))) ;eudev
1168
1169 (define (wait-for-udevd)
1170 ;; Wait until someone's listening on udevd's control
1171 ;; socket.
1172 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
1173 (let try ()
1174 (catch 'system-error
1175 (lambda ()
1176 (connect sock PF_UNIX "/run/udev/control")
1177 (close-port sock))
1178 (lambda args
1179 (format #t "waiting for udevd...~%")
1180 (usleep 500000)
1181 (try))))))
1182
1183 ;; Allow udev to find the modules.
1184 (setenv "LINUX_MODULE_DIRECTORY"
1185 "/run/booted-system/kernel/lib/modules")
1186
1187 ;; The first one is for udev, the second one for eudev.
1188 (setenv "UDEV_CONFIG_FILE" #$udev.conf)
1189 (setenv "EUDEV_RULES_DIRECTORY"
1190 (string-append #$rules "/lib/udev/rules.d"))
1191
1192 (let ((pid (primitive-fork)))
1193 (case pid
1194 ((0)
1195 (exec-command (list udevd)))
1196 (else
1197 ;; Wait until udevd is up and running. This
1198 ;; appears to be needed so that the events
1199 ;; triggered below are actually handled.
1200 (wait-for-udevd)
1201
1202 ;; Trigger device node creation.
1203 (system* (string-append #$udev "/bin/udevadm")
1204 "trigger" "--action=add")
1205
1206 ;; Wait for things to settle down.
1207 (system* (string-append #$udev "/bin/udevadm")
1208 "settle")
1209 pid)))))
1210 (stop #~(make-kill-destructor))
1211
1212 ;; When halting the system, 'udev' is actually killed by
1213 ;; 'user-processes', i.e., before its own 'stop' method was called.
1214 ;; Thus, make sure it is not respawned.
1215 (respawn? #f)))))))
1216
1217(define udev-service-type
1218 (service-type (name 'udev)
1219 (extensions
d4053c71
AK
1220 (list (service-extension shepherd-root-service-type
1221 udev-shepherd-service)))
0adfe95a
LC
1222
1223 (compose concatenate) ;concatenate the list of rules
1224 (extend (lambda (config rules)
1225 (match config
1226 (($ <udev-configuration> udev initial-rules)
1227 (udev-configuration
1228 (udev udev)
1229 (rules (append initial-rules rules)))))))))
1230
255f7308 1231(define* (udev-service #:key (udev eudev) (rules '()))
ecd06ca9
LC
1232 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
1233extra rules from the packages listed in @var{rules}."
0adfe95a
LC
1234 (service udev-service-type
1235 (udev-configuration (udev udev) (rules rules))))
1236
0adfe95a 1237(define swap-service-type
d4053c71 1238 (shepherd-service-type
00184239 1239 'swap
0adfe95a
LC
1240 (lambda (device)
1241 (define requirement
1242 (if (string-prefix? "/dev/mapper/" device)
1243 (list (symbol-append 'device-mapping-
1244 (string->symbol (basename device))))
1245 '()))
1246
d4053c71 1247 (shepherd-service
0adfe95a
LC
1248 (provision (list (symbol-append 'swap- (string->symbol device))))
1249 (requirement `(udev ,@requirement))
1250 (documentation "Enable the given swap device.")
1251 (start #~(lambda ()
1252 (restart-on-EINTR (swapon #$device))
1253 #t))
1254 (stop #~(lambda _
1255 (restart-on-EINTR (swapoff #$device))
1256 #f))
1257 (respawn? #f)))))
5dae0186 1258
2a13d05e
LC
1259(define (swap-service device)
1260 "Return a service that uses @var{device} as a swap device."
0adfe95a 1261 (service swap-service-type device))
2a13d05e 1262
8664cc88
LC
1263(define-record-type* <gpm-configuration>
1264 gpm-configuration make-gpm-configuration gpm-configuration?
1265 (gpm gpm-configuration-gpm) ;package
1266 (options gpm-configuration-options)) ;list of strings
1267
d4053c71 1268(define gpm-shepherd-service
8664cc88 1269 (match-lambda
a907d997 1270 (($ <gpm-configuration> gpm options)
d4053c71 1271 (list (shepherd-service
8664cc88
LC
1272 (requirement '(udev))
1273 (provision '(gpm))
1274 (start #~(lambda ()
1275 ;; 'gpm' runs in the background and sets a PID file.
1276 ;; Note that it requires running as "root".
1277 (false-if-exception (delete-file "/var/run/gpm.pid"))
1278 (fork+exec-command (list (string-append #$gpm "/sbin/gpm")
1279 #$@options))
1280
1281 ;; Wait for the PID file to appear; declare failure if
1282 ;; it doesn't show up.
1283 (let loop ((i 3))
1284 (or (file-exists? "/var/run/gpm.pid")
1285 (if (zero? i)
1286 #f
1287 (begin
1288 (sleep 1)
1289 (loop (1- i))))))))
1290
1291 (stop #~(lambda (_)
1292 ;; Return #f if successfully stopped.
1293 (not (zero? (system* (string-append #$gpm "/sbin/gpm")
1294 "-k"))))))))))
1295
1296(define gpm-service-type
1297 (service-type (name 'gpm)
1298 (extensions
d4053c71
AK
1299 (list (service-extension shepherd-root-service-type
1300 gpm-shepherd-service)))))
8664cc88
LC
1301
1302(define* (gpm-service #:key (gpm gpm)
1303 (options '("-m" "/dev/input/mice" "-t" "ps2")))
1304 "Run @var{gpm}, the general-purpose mouse daemon, with the given
1305command-line @var{options}. GPM allows users to use the mouse in the console,
1306notably to select, copy, and paste text. The default value of @var{options}
1307uses the @code{ps2} protocol, which works for both USB and PS/2 mice.
1308
1309This service is not part of @var{%base-services}."
1310 ;; To test in QEMU, use "-usbdevice mouse" and then, in the monitor, use
1311 ;; "info mice" and "mouse_set X" to use the right mouse.
1312 (service gpm-service-type
1313 (gpm-configuration (gpm gpm) (options options))))
1314
1315\f
8b198abe
LC
1316(define %base-services
1317 ;; Convenience variable holding the basic services.
ce8a6dfc 1318 (let ((motd (plain-file "motd" "
8b198abe 1319This is the GNU operating system, welcome!\n\n")))
62ca0fdf
LC
1320 (list (console-font-service "tty1")
1321 (console-font-service "tty2")
1322 (console-font-service "tty3")
1323 (console-font-service "tty4")
1324 (console-font-service "tty5")
1325 (console-font-service "tty6")
1326
66e4f01c
LC
1327 (mingetty-service (mingetty-configuration
1328 (tty "tty1") (motd motd)))
1329 (mingetty-service (mingetty-configuration
1330 (tty "tty2") (motd motd)))
1331 (mingetty-service (mingetty-configuration
1332 (tty "tty3") (motd motd)))
1333 (mingetty-service (mingetty-configuration
1334 (tty "tty4") (motd motd)))
1335 (mingetty-service (mingetty-configuration
1336 (tty "tty5") (motd motd)))
1337 (mingetty-service (mingetty-configuration
1338 (tty "tty6") (motd motd)))
1339
4a3b3b07
LC
1340 (static-networking-service "lo" "127.0.0.1"
1341 #:provision '(loopback))
8b198abe 1342 (syslog-service)
a535e122 1343 (urandom-seed-service)
8b198abe 1344 (guix-service)
151a2c07 1345 (nscd-service)
ecd06ca9 1346
52bd5734
LC
1347 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
1348 ;; used, so enable them by default. The FUSE and ALSA rules are
1349 ;; less critical, but handy.
68ac258b 1350 (udev-service #:rules (list lvm2 fuse alsa-utils crda)))))
8b198abe 1351
db4fdc04 1352;;; base.scm ends here