services: Make 'static-networking' extensible.
[jackhill/guix/guix.git] / gnu / services.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (gnu services)
21 #:use-module (guix gexp)
22 #:use-module (guix monads)
23 #:use-module (guix store)
24 #:use-module (guix records)
25 #:use-module (guix profiles)
26 #:use-module (guix sets)
27 #:use-module (guix ui)
28 #:use-module (guix modules)
29 #:use-module (gnu packages base)
30 #:use-module (gnu packages bash)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-9)
33 #:use-module (srfi srfi-9 gnu)
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-34)
36 #:use-module (srfi srfi-35)
37 #:use-module (ice-9 vlist)
38 #:use-module (ice-9 match)
39 #:export (service-extension
40 service-extension?
41 service-extension-target
42 service-extension-compute
43
44 service-type
45 service-type?
46 service-type-name
47 service-type-extensions
48 service-type-compose
49 service-type-extend
50
51 service
52 service?
53 service-kind
54 service-parameters
55
56 simple-service
57 modify-services
58 service-back-edges
59 fold-services
60
61 service-error?
62 missing-target-service-error?
63 missing-target-service-error-service
64 missing-target-service-error-target-type
65 ambiguous-target-service-error?
66 ambiguous-target-service-error-service
67 ambiguous-target-service-error-target-type
68
69 system-service-type
70 boot-service-type
71 cleanup-service-type
72 activation-service-type
73 activation-service->script
74 %linux-bare-metal-service
75 etc-service-type
76 etc-directory
77 setuid-program-service-type
78 profile-service-type
79 firmware-service-type
80 gc-root-service-type
81
82 %boot-service
83 %activation-service
84 etc-service
85
86 file-union)) ;XXX: for lack of a better place
87
88 ;;; Comment:
89 ;;;
90 ;;; This module defines a broad notion of "service types" and "services."
91 ;;;
92 ;;; A service type describe how its instances extend instances of other
93 ;;; service types. For instance, some services extend the instance of
94 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
95 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
96 ;;; <shepherd-service>.
97 ;;;
98 ;;; When applicable, the service type defines how it can itself be extended,
99 ;;; by providing one procedure to compose extensions, and one procedure to
100 ;;; extend itself.
101 ;;;
102 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
103 ;;; instance, which is the root of the service DAG. Its value is the
104 ;;; derivation that produces the 'system' directory as returned by
105 ;;; 'operating-system-derivation'.
106 ;;;
107 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
108 ;;; "folds" by propagating extensions down the graph; it returns the root
109 ;;; service after the applying all its extensions.
110 ;;;
111 ;;; Code:
112
113 (define-record-type <service-extension>
114 (service-extension target compute)
115 service-extension?
116 (target service-extension-target) ;<service-type>
117 (compute service-extension-compute)) ;params -> params
118
119 (define-record-type* <service-type> service-type make-service-type
120 service-type?
121 (name service-type-name) ;symbol (for debugging)
122
123 ;; Things extended by services of this type.
124 (extensions service-type-extensions) ;list of <service-extensions>
125
126 ;; Given a list of extensions, "compose" them.
127 (compose service-type-compose ;list of Any -> Any
128 (default #f))
129
130 ;; Extend the services' own parameters with the extension composition.
131 (extend service-type-extend ;list of Any -> parameters
132 (default #f)))
133
134 (define (write-service-type type port)
135 (format port "#<service-type ~a ~a>"
136 (service-type-name type)
137 (number->string (object-address type) 16)))
138
139 (set-record-type-printer! <service-type> write-service-type)
140
141 ;; Services of a given type.
142 (define-record-type <service>
143 (service type parameters)
144 service?
145 (type service-kind)
146 (parameters service-parameters))
147
148 (define (simple-service name target value)
149 "Return a service that extends TARGET with VALUE. This works by creating a
150 singleton service type NAME, of which the returned service is an instance."
151 (let* ((extension (service-extension target identity))
152 (type (service-type (name name)
153 (extensions (list extension)))))
154 (service type value)))
155
156 (define-syntax %modify-service
157 (syntax-rules (=>)
158 ((_ service)
159 service)
160 ((_ svc (kind param => exp ...) clauses ...)
161 (if (eq? (service-kind svc) kind)
162 (let ((param (service-parameters svc)))
163 (service (service-kind svc)
164 (begin exp ...)))
165 (%modify-service svc clauses ...)))))
166
167 (define-syntax modify-services
168 (syntax-rules ()
169 "Modify the services listed in SERVICES according to CLAUSES and return
170 the resulting list of services. Each clause must have the form:
171
172 (TYPE VARIABLE => BODY)
173
174 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
175 identifier that is bound within BODY to the value of the service of that
176 TYPE. Consider this example:
177
178 (modify-services %base-services
179 (guix-service-type config =>
180 (guix-configuration
181 (inherit config)
182 (use-substitutes? #f)
183 (extra-options '(\"--gc-keep-derivations\"))))
184 (mingetty-service-type config =>
185 (mingetty-configuration
186 (inherit config)
187 (motd (plain-file \"motd\" \"Hi there!\")))))
188
189 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
190 all the MINGETTY-SERVICE-TYPE instances.
191
192 This is a shorthand for (map (lambda (svc) ...) %base-services)."
193 ((_ services clauses ...)
194 (map (lambda (service)
195 (%modify-service service clauses ...))
196 services))))
197
198 \f
199 ;;;
200 ;;; Core services.
201 ;;;
202
203 (define (system-derivation mentries mextensions)
204 "Return as a monadic value the derivation of the 'system' directory
205 containing the given entries."
206 (mlet %store-monad ((entries mentries)
207 (extensions (sequence %store-monad mextensions)))
208 (lower-object
209 (file-union "system"
210 (append entries (concatenate extensions))))))
211
212 (define system-service-type
213 ;; This is the ultimate service type, the root of the service DAG. The
214 ;; service of this type is extended by monadic name/item pairs. These items
215 ;; end up in the "system directory" as returned by
216 ;; 'operating-system-derivation'.
217 (service-type (name 'system)
218 (extensions '())
219 (compose identity)
220 (extend system-derivation)))
221
222 (define (compute-boot-script _ mexps)
223 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
224 (gexp->file "boot"
225 ;; Clean up and activate the system, then spawn shepherd.
226 #~(begin #$@gexps))))
227
228 (define (boot-script-entry mboot)
229 "Return, as a monadic value, an entry for the boot script in the system
230 directory."
231 (mlet %store-monad ((boot mboot))
232 (return `(("boot" ,boot)))))
233
234 (define boot-service-type
235 ;; The service of this type is extended by being passed gexps as monadic
236 ;; values. It aggregates them in a single script, as a monadic value, which
237 ;; becomes its 'parameters'. It is the only service that extends nothing.
238 (service-type (name 'boot)
239 (extensions
240 (list (service-extension system-service-type
241 boot-script-entry)))
242 (compose append)
243 (extend compute-boot-script)))
244
245 (define %boot-service
246 ;; The service that produces the boot script.
247 (service boot-service-type #t))
248
249 (define (cleanup-gexp _)
250 "Return as a monadic value a gexp to clean up /tmp and similar places upon
251 boot."
252 (with-monad %store-monad
253 (with-imported-modules '((guix build utils))
254 (return #~(begin
255 (use-modules (guix build utils))
256
257 ;; Clean out /tmp and /var/run.
258 ;;
259 ;; XXX This needs to happen before service activations, so it
260 ;; has to be here, but this also implicitly assumes that /tmp
261 ;; and /var/run are on the root partition.
262 (letrec-syntax ((fail-safe (syntax-rules ()
263 ((_ exp rest ...)
264 (begin
265 (catch 'system-error
266 (lambda () exp)
267 (const #f))
268 (fail-safe rest ...)))
269 ((_)
270 #t))))
271 ;; Ignore I/O errors so the system can boot.
272 (fail-safe
273 (delete-file-recursively "/tmp")
274 (delete-file-recursively "/var/run")
275 (mkdir "/tmp")
276 (chmod "/tmp" #o1777)
277 (mkdir "/var/run")
278 (chmod "/var/run" #o755))))))))
279
280 (define cleanup-service-type
281 ;; Service that cleans things up in /tmp and similar.
282 (service-type (name 'cleanup)
283 (extensions
284 (list (service-extension boot-service-type
285 cleanup-gexp)))))
286
287 (define* (file-union name files) ;FIXME: Factorize.
288 "Return a <computed-file> that builds a directory containing all of FILES.
289 Each item in FILES must be a list where the first element is the file name to
290 use in the new directory, and the second element is a gexp denoting the target
291 file."
292 (computed-file name
293 #~(begin
294 (mkdir #$output)
295 (chdir #$output)
296 #$@(map (match-lambda
297 ((target source)
298 #~(begin
299 ;; Stat the source to abort early if it
300 ;; does not exist.
301 (stat #$source)
302
303 (symlink #$source #$target))))
304 files))))
305
306 (define (directory-union name things)
307 "Return a directory that is the union of THINGS."
308 (match things
309 ((one)
310 ;; Only one thing; return it.
311 one)
312 (_
313 (computed-file name
314 (with-imported-modules '((guix build union))
315 #~(begin
316 (use-modules (guix build union))
317 (union-build #$output '#$things)))))))
318
319 (define* (activation-service->script service)
320 "Return as a monadic value the activation script for SERVICE, a service of
321 ACTIVATION-SCRIPT-TYPE."
322 (activation-script (service-parameters service)))
323
324 (define (activation-script gexps)
325 "Return the system's activation script, which evaluates GEXPS."
326 (define (service-activations)
327 ;; Return the activation scripts for SERVICES.
328 (mapm %store-monad
329 (cut gexp->file "activate-service" <>)
330 gexps))
331
332 (mlet* %store-monad ((actions (service-activations)))
333 (gexp->file "activate"
334 (with-imported-modules (source-module-closure
335 '((gnu build activation)))
336 #~(begin
337 (use-modules (gnu build activation))
338
339 ;; Make sure /bin/sh is valid and current.
340 (activate-/bin/sh
341 (string-append #$(canonical-package bash) "/bin/sh"))
342
343 ;; Make sure the user accounting database exists. If it
344 ;; does not exist, 'setutxent' does not create it and
345 ;; thus there is no accounting at all.
346 (close-port (open-file "/var/run/utmpx" "a0"))
347
348 ;; Same for 'wtmp', which is populated by mingetty et
349 ;; al.
350 (close-port (open-file "/var/log/wtmp" "a0"))
351
352 ;; Set up /run/current-system. Among other things this
353 ;; sets up locales, which the activation snippets
354 ;; executed below may expect.
355 (activate-current-system)
356
357 ;; Run the services' activation snippets.
358 ;; TODO: Use 'load-compiled'.
359 (for-each primitive-load '#$actions))))))
360
361 (define (gexps->activation-gexp gexps)
362 "Return a gexp that runs the activation script containing GEXPS."
363 (mlet %store-monad ((script (activation-script gexps)))
364 (return #~(primitive-load #$script))))
365
366 (define (second-argument a b) b)
367
368 (define activation-service-type
369 (service-type (name 'activate)
370 (extensions
371 (list (service-extension boot-service-type
372 gexps->activation-gexp)))
373 (compose append)
374 (extend second-argument)))
375
376 (define %activation-service
377 ;; The activation service produces the activation script from the gexps it
378 ;; receives.
379 (service activation-service-type #t))
380
381 (define %modprobe-wrapper
382 ;; Wrapper for the 'modprobe' command that knows where modules live.
383 ;;
384 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
385 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
386 ;; environment variable is not set---hence the need for this wrapper.
387 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
388 (program-file "modprobe"
389 #~(begin
390 (setenv "LINUX_MODULE_DIRECTORY"
391 "/run/booted-system/kernel/lib/modules")
392 (apply execl #$modprobe
393 (cons #$modprobe (cdr (command-line))))))))
394
395 (define %linux-kernel-activation
396 ;; Activation of the Linux kernel running on the bare metal (as opposed to
397 ;; running in a container.)
398 #~(begin
399 ;; Tell the kernel to use our 'modprobe' command.
400 (activate-modprobe #$%modprobe-wrapper)
401
402 ;; Let users debug their own processes!
403 (activate-ptrace-attach)))
404
405 (define linux-bare-metal-service-type
406 (service-type (name 'linux-bare-metal)
407 (extensions
408 (list (service-extension activation-service-type
409 (const %linux-kernel-activation))))))
410
411 (define %linux-bare-metal-service
412 ;; The service that does things that are needed on the "bare metal", but not
413 ;; necessary or impossible in a container.
414 (service linux-bare-metal-service-type #f))
415
416 (define (etc-directory service)
417 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
418 (files->etc-directory (service-parameters service)))
419
420 (define (files->etc-directory files)
421 (file-union "etc" files))
422
423 (define (etc-entry files)
424 "Return an entry for the /etc directory consisting of FILES in the system
425 directory."
426 (with-monad %store-monad
427 (return `(("etc" ,(files->etc-directory files))))))
428
429 (define etc-service-type
430 (service-type (name 'etc)
431 (extensions
432 (list
433 (service-extension activation-service-type
434 (lambda (files)
435 (let ((etc
436 (files->etc-directory files)))
437 #~(activate-etc #$etc))))
438 (service-extension system-service-type etc-entry)))
439 (compose concatenate)
440 (extend append)))
441
442 (define (etc-service files)
443 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
444 FILES must be a list of name/file-like object pairs."
445 (service etc-service-type files))
446
447 (define setuid-program-service-type
448 (service-type (name 'setuid-program)
449 (extensions
450 (list (service-extension activation-service-type
451 (lambda (programs)
452 #~(activate-setuid-programs
453 (list #$@programs))))))
454 (compose concatenate)
455 (extend append)))
456
457 (define (packages->profile-entry packages)
458 "Return a system entry for the profile containing PACKAGES."
459 (mlet %store-monad ((profile (profile-derivation
460 (packages->manifest
461 (delete-duplicates packages eq?)))))
462 (return `(("profile" ,profile)))))
463
464 (define profile-service-type
465 ;; The service that populates the system's profile---i.e.,
466 ;; /run/current-system/profile. It is extended by package lists.
467 (service-type (name 'profile)
468 (extensions
469 (list (service-extension system-service-type
470 packages->profile-entry)))
471 (compose concatenate)
472 (extend append)))
473
474 (define (firmware->activation-gexp firmware)
475 "Return a gexp to make the packages listed in FIRMWARE loadable by the
476 kernel."
477 (let ((directory (directory-union "firmware" firmware)))
478 ;; Tell the kernel where firmware is.
479 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
480
481 (define firmware-service-type
482 ;; The service that collects firmware.
483 (service-type (name 'firmware)
484 (extensions
485 (list (service-extension activation-service-type
486 firmware->activation-gexp)))
487 (compose concatenate)
488 (extend append)))
489
490 (define (gc-roots->system-entry roots)
491 "Return an entry in the system's output containing symlinks to ROOTS."
492 (mlet %store-monad ((entry (gexp->derivation
493 "gc-roots"
494 #~(let ((roots '#$roots))
495 (mkdir #$output)
496 (chdir #$output)
497 (for-each symlink
498 roots
499 (map number->string
500 (iota (length roots))))))))
501 (return (if (null? roots)
502 '()
503 `(("gc-roots" ,entry))))))
504
505 (define gc-root-service-type
506 ;; A service to associate extra garbage-collector roots to the system. This
507 ;; is a simple hack that guarantees that the system retains references to
508 ;; the given list of roots. Roots must be "lowerable" objects like
509 ;; packages, or derivations.
510 (service-type (name 'gc-roots)
511 (extensions
512 (list (service-extension system-service-type
513 gc-roots->system-entry)))
514 (compose concatenate)
515 (extend append)))
516
517 \f
518 ;;;
519 ;;; Service folding.
520 ;;;
521
522 (define-condition-type &service-error &error
523 service-error?)
524
525 (define-condition-type &missing-target-service-error &service-error
526 missing-target-service-error?
527 (service missing-target-service-error-service)
528 (target-type missing-target-service-error-target-type))
529
530 (define-condition-type &ambiguous-target-service-error &service-error
531 ambiguous-target-service-error?
532 (service ambiguous-target-service-error-service)
533 (target-type ambiguous-target-service-error-target-type))
534
535 (define (service-back-edges services)
536 "Return a procedure that, when passed a <service>, returns the list of
537 <service> objects that depend on it."
538 (define (add-edges service edges)
539 (define (add-edge extension edges)
540 (let ((target-type (service-extension-target extension)))
541 (match (filter (lambda (service)
542 (eq? (service-kind service) target-type))
543 services)
544 ((target)
545 (vhash-consq target service edges))
546 (()
547 (raise
548 (condition (&missing-target-service-error
549 (service service)
550 (target-type target-type))
551 (&message
552 (message
553 (format #f (_ "no target of type '~a' for service ~s")
554 (service-type-name target-type)
555 service))))))
556 (x
557 (raise
558 (condition (&ambiguous-target-service-error
559 (service service)
560 (target-type target-type))
561 (&message
562 (message
563 (format #f
564 (_ "more than one target service of type '~a'")
565 (service-type-name target-type))))))))))
566
567 (fold add-edge edges (service-type-extensions (service-kind service))))
568
569 (let ((edges (fold add-edges vlist-null services)))
570 (lambda (node)
571 (reverse (vhash-foldq* cons '() node edges)))))
572
573 (define* (fold-services services
574 #:key (target-type system-service-type))
575 "Fold SERVICES by propagating their extensions down to the root of type
576 TARGET-TYPE; return the root service adjusted accordingly."
577 (define dependents
578 (service-back-edges services))
579
580 (define (matching-extension target)
581 (let ((target (service-kind target)))
582 (match-lambda
583 (($ <service-extension> type)
584 (eq? type target)))))
585
586 (define (apply-extension target)
587 (lambda (service)
588 (match (find (matching-extension target)
589 (service-type-extensions (service-kind service)))
590 (($ <service-extension> _ compute)
591 (compute (service-parameters service))))))
592
593 (match (filter (lambda (service)
594 (eq? (service-kind service) target-type))
595 services)
596 ((sink)
597 (let loop ((sink sink))
598 (let* ((dependents (map loop (dependents sink)))
599 (extensions (map (apply-extension sink) dependents))
600 (extend (service-type-extend (service-kind sink)))
601 (compose (service-type-compose (service-kind sink)))
602 (params (service-parameters sink)))
603 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
604 ;; different type than the elements of EXTENSIONS.
605 (if extend
606 (service (service-kind sink)
607 (extend params (compose extensions)))
608 sink))))
609 (()
610 (raise
611 (condition (&missing-target-service-error
612 (service #f)
613 (target-type target-type))
614 (&message
615 (message (format #f (_ "service of type '~a' not found")
616 (service-type-name target-type)))))))
617 (x
618 (raise
619 (condition (&ambiguous-target-service-error
620 (service #f)
621 (target-type target-type))
622 (&message
623 (message
624 (format #f
625 (_ "more than one target service of type '~a'")
626 (service-type-name target-type)))))))))
627
628 ;;; services.scm ends here.