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