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