gnu: Use 'gexp->file' in conjunction with 'with-imported-modules'.
[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 (with-monad %store-monad
242 (with-imported-modules '((guix build utils))
243 (return #~(begin
244 (use-modules (guix build utils))
245
246 ;; Clean out /tmp and /var/run.
247 ;;
248 ;; XXX This needs to happen before service activations, so it
249 ;; has to be here, but this also implicitly assumes that /tmp
250 ;; and /var/run are on the root partition.
251 (letrec-syntax ((fail-safe (syntax-rules ()
252 ((_ exp rest ...)
253 (begin
254 (catch 'system-error
255 (lambda () exp)
256 (const #f))
257 (fail-safe rest ...)))
258 ((_)
259 #t))))
260 ;; Ignore I/O errors so the system can boot.
261 (fail-safe
262 (delete-file-recursively "/tmp")
263 (delete-file-recursively "/var/run")
264 (mkdir "/tmp")
265 (chmod "/tmp" #o1777)
266 (mkdir "/var/run")
267 (chmod "/var/run" #o755))))))))
268
269 (define cleanup-service-type
270 ;; Service that cleans things up in /tmp and similar.
271 (service-type (name 'cleanup)
272 (extensions
273 (list (service-extension boot-service-type
274 cleanup-gexp)))))
275
276 (define* (file-union name files) ;FIXME: Factorize.
277 "Return a <computed-file> that builds a directory containing all of FILES.
278 Each item in FILES must be a list where the first element is the file name to
279 use in the new directory, and the second element is a gexp denoting the target
280 file."
281 (computed-file name
282 #~(begin
283 (mkdir #$output)
284 (chdir #$output)
285 #$@(map (match-lambda
286 ((target source)
287 #~(begin
288 ;; Stat the source to abort early if it
289 ;; does not exist.
290 (stat #$source)
291
292 (symlink #$source #$target))))
293 files))))
294
295 (define (directory-union name things)
296 "Return a directory that is the union of THINGS."
297 (match things
298 ((one)
299 ;; Only one thing; return it.
300 one)
301 (_
302 (computed-file name
303 (with-imported-modules '((guix build union))
304 #~(begin
305 (use-modules (guix build union))
306 (union-build #$output '#$things)))))))
307
308 (define* (activation-service->script service)
309 "Return as a monadic value the activation script for SERVICE, a service of
310 ACTIVATION-SCRIPT-TYPE."
311 (activation-script (service-parameters service)))
312
313 (define (activation-script gexps)
314 "Return the system's activation script, which evaluates GEXPS."
315 (define %modules
316 '((gnu build activation)
317 (gnu build linux-boot)
318 (gnu build linux-modules)
319 (gnu build file-systems)
320 (guix build utils)
321 (guix build syscalls)
322 (guix build bournish)
323 (guix elf)))
324
325 (define (service-activations)
326 ;; Return the activation scripts for SERVICES.
327 (mapm %store-monad
328 (cut gexp->file "activate-service" <>)
329 gexps))
330
331 (mlet* %store-monad ((actions (service-activations)))
332 (gexp->file "activate"
333 (with-imported-modules %modules
334 #~(begin
335 (use-modules (gnu build activation))
336
337 ;; Make sure /bin/sh is valid and current.
338 (activate-/bin/sh
339 (string-append #$(canonical-package bash) "/bin/sh"))
340
341 ;; Run the services' activation snippets.
342 ;; TODO: Use 'load-compiled'.
343 (for-each primitive-load '#$actions)
344
345 ;; Set up /run/current-system.
346 (activate-current-system))))))
347
348 (define (gexps->activation-gexp gexps)
349 "Return a gexp that runs the activation script containing GEXPS."
350 (mlet %store-monad ((script (activation-script gexps)))
351 (return #~(primitive-load #$script))))
352
353 (define (second-argument a b) b)
354
355 (define activation-service-type
356 (service-type (name 'activate)
357 (extensions
358 (list (service-extension boot-service-type
359 gexps->activation-gexp)))
360 (compose append)
361 (extend second-argument)))
362
363 (define %activation-service
364 ;; The activation service produces the activation script from the gexps it
365 ;; receives.
366 (service activation-service-type #t))
367
368 (define %modprobe-wrapper
369 ;; Wrapper for the 'modprobe' command that knows where modules live.
370 ;;
371 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
372 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
373 ;; environment variable is not set---hence the need for this wrapper.
374 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
375 (program-file "modprobe"
376 #~(begin
377 (setenv "LINUX_MODULE_DIRECTORY"
378 "/run/booted-system/kernel/lib/modules")
379 (apply execl #$modprobe
380 (cons #$modprobe (cdr (command-line))))))))
381
382 (define %linux-kernel-activation
383 ;; Activation of the Linux kernel running on the bare metal (as opposed to
384 ;; running in a container.)
385 #~(begin
386 ;; Tell the kernel to use our 'modprobe' command.
387 (activate-modprobe #$%modprobe-wrapper)
388
389 ;; Let users debug their own processes!
390 (activate-ptrace-attach)))
391
392 (define linux-bare-metal-service-type
393 (service-type (name 'linux-bare-metal)
394 (extensions
395 (list (service-extension activation-service-type
396 (const %linux-kernel-activation))))))
397
398 (define %linux-bare-metal-service
399 ;; The service that does things that are needed on the "bare metal", but not
400 ;; necessary or impossible in a container.
401 (service linux-bare-metal-service-type #f))
402
403 (define (etc-directory service)
404 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
405 (files->etc-directory (service-parameters service)))
406
407 (define (files->etc-directory files)
408 (file-union "etc" files))
409
410 (define (etc-entry files)
411 "Return an entry for the /etc directory consisting of FILES in the system
412 directory."
413 (with-monad %store-monad
414 (return `(("etc" ,(files->etc-directory files))))))
415
416 (define etc-service-type
417 (service-type (name 'etc)
418 (extensions
419 (list
420 (service-extension activation-service-type
421 (lambda (files)
422 (let ((etc
423 (files->etc-directory files)))
424 #~(activate-etc #$etc))))
425 (service-extension system-service-type etc-entry)))
426 (compose concatenate)
427 (extend append)))
428
429 (define (etc-service files)
430 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
431 FILES must be a list of name/file-like object pairs."
432 (service etc-service-type files))
433
434 (define setuid-program-service-type
435 (service-type (name 'setuid-program)
436 (extensions
437 (list (service-extension activation-service-type
438 (lambda (programs)
439 #~(activate-setuid-programs
440 (list #$@programs))))))
441 (compose concatenate)
442 (extend append)))
443
444 (define (packages->profile-entry packages)
445 "Return a system entry for the profile containing PACKAGES."
446 (mlet %store-monad ((profile (profile-derivation
447 (packages->manifest
448 (delete-duplicates packages eq?)))))
449 (return `(("profile" ,profile)))))
450
451 (define profile-service-type
452 ;; The service that populates the system's profile---i.e.,
453 ;; /run/current-system/profile. It is extended by package lists.
454 (service-type (name 'profile)
455 (extensions
456 (list (service-extension system-service-type
457 packages->profile-entry)))
458 (compose concatenate)
459 (extend append)))
460
461 (define (firmware->activation-gexp firmware)
462 "Return a gexp to make the packages listed in FIRMWARE loadable by the
463 kernel."
464 (let ((directory (directory-union "firmware" firmware)))
465 ;; Tell the kernel where firmware is.
466 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
467
468 (define firmware-service-type
469 ;; The service that collects firmware.
470 (service-type (name 'firmware)
471 (extensions
472 (list (service-extension activation-service-type
473 firmware->activation-gexp)))
474 (compose concatenate)
475 (extend append)))
476
477 (define (gc-roots->system-entry roots)
478 "Return an entry in the system's output containing symlinks to ROOTS."
479 (mlet %store-monad ((entry (gexp->derivation
480 "gc-roots"
481 #~(let ((roots '#$roots))
482 (mkdir #$output)
483 (chdir #$output)
484 (for-each symlink
485 roots
486 (map number->string
487 (iota (length roots))))))))
488 (return (if (null? roots)
489 '()
490 `(("gc-roots" ,entry))))))
491
492 (define gc-root-service-type
493 ;; A service to associate extra garbage-collector roots to the system. This
494 ;; is a simple hack that guarantees that the system retains references to
495 ;; the given list of roots. Roots must be "lowerable" objects like
496 ;; packages, or derivations.
497 (service-type (name 'gc-roots)
498 (extensions
499 (list (service-extension system-service-type
500 gc-roots->system-entry)))
501 (compose concatenate)
502 (extend append)))
503
504 \f
505 ;;;
506 ;;; Service folding.
507 ;;;
508
509 (define-condition-type &service-error &error
510 service-error?)
511
512 (define-condition-type &missing-target-service-error &service-error
513 missing-target-service-error?
514 (service missing-target-service-error-service)
515 (target-type missing-target-service-error-target-type))
516
517 (define-condition-type &ambiguous-target-service-error &service-error
518 ambiguous-target-service-error?
519 (service ambiguous-target-service-error-service)
520 (target-type ambiguous-target-service-error-target-type))
521
522 (define (service-back-edges services)
523 "Return a procedure that, when passed a <service>, returns the list of
524 <service> objects that depend on it."
525 (define (add-edges service edges)
526 (define (add-edge extension edges)
527 (let ((target-type (service-extension-target extension)))
528 (match (filter (lambda (service)
529 (eq? (service-kind service) target-type))
530 services)
531 ((target)
532 (vhash-consq target service edges))
533 (()
534 (raise
535 (condition (&missing-target-service-error
536 (service service)
537 (target-type target-type))
538 (&message
539 (message
540 (format #f (_ "no target of type '~a' for service ~s")
541 (service-type-name target-type)
542 service))))))
543 (x
544 (raise
545 (condition (&ambiguous-target-service-error
546 (service service)
547 (target-type target-type))
548 (&message
549 (message
550 (format #f
551 (_ "more than one target service of type '~a'")
552 (service-type-name target-type))))))))))
553
554 (fold add-edge edges (service-type-extensions (service-kind service))))
555
556 (let ((edges (fold add-edges vlist-null services)))
557 (lambda (node)
558 (reverse (vhash-foldq* cons '() node edges)))))
559
560 (define* (fold-services services
561 #:key (target-type system-service-type))
562 "Fold SERVICES by propagating their extensions down to the root of type
563 TARGET-TYPE; return the root service adjusted accordingly."
564 (define dependents
565 (service-back-edges services))
566
567 (define (matching-extension target)
568 (let ((target (service-kind target)))
569 (match-lambda
570 (($ <service-extension> type)
571 (eq? type target)))))
572
573 (define (apply-extension target)
574 (lambda (service)
575 (match (find (matching-extension target)
576 (service-type-extensions (service-kind service)))
577 (($ <service-extension> _ compute)
578 (compute (service-parameters service))))))
579
580 (match (filter (lambda (service)
581 (eq? (service-kind service) target-type))
582 services)
583 ((sink)
584 (let loop ((sink sink))
585 (let* ((dependents (map loop (dependents sink)))
586 (extensions (map (apply-extension sink) dependents))
587 (extend (service-type-extend (service-kind sink)))
588 (compose (service-type-compose (service-kind sink)))
589 (params (service-parameters sink)))
590 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
591 ;; different type than the elements of EXTENSIONS.
592 (if extend
593 (service (service-kind sink)
594 (extend params (compose extensions)))
595 sink))))
596 (()
597 (raise
598 (condition (&missing-target-service-error
599 (service #f)
600 (target-type target-type))
601 (&message
602 (message (format #f (_ "service of type '~a' not found")
603 (service-type-name target-type)))))))
604 (x
605 (raise
606 (condition (&ambiguous-target-service-error
607 (service #f)
608 (target-type target-type))
609 (&message
610 (message
611 (format #f
612 (_ "more than one target service of type '~a'")
613 (service-type-name target-type)))))))))
614
615 ;;; services.scm ends here.