gnu: zathura-pdf-mupdf: Update to 0.3.2.
[jackhill/guix/guix.git] / gnu / services.scm
CommitLineData
db4fdc04 1;;; GNU Guix --- Functional package management for GNU
caa78166 2;;; Copyright © 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
4d343a14 3;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
db4fdc04
LC
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)
bebc8681 21 #:use-module (guix gexp)
0adfe95a
LC
22 #:use-module (guix monads)
23 #:use-module (guix store)
db4fdc04 24 #:use-module (guix records)
af4c3fd5 25 #:use-module (guix profiles)
0c0c1b21 26 #:use-module (guix discovery)
0adfe95a
LC
27 #:use-module (guix sets)
28 #:use-module (guix ui)
1bb895ea 29 #:use-module ((guix utils) #:select (source-properties->location))
232ccbef 30 #:use-module (guix modules)
0adfe95a
LC
31 #:use-module (gnu packages base)
32 #:use-module (gnu packages bash)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-9)
35 #:use-module (srfi srfi-9 gnu)
36 #:use-module (srfi srfi-26)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
39 #:use-module (ice-9 vlist)
40 #:use-module (ice-9 match)
41 #:export (service-extension
42 service-extension?
7d8b5913
CB
43 service-extension-target
44 service-extension-compute
0adfe95a
LC
45
46 service-type
47 service-type?
5152d13b
LC
48 service-type-name
49 service-type-extensions
50 service-type-compose
51 service-type-extend
1bb895ea 52 service-type-default-value
b714395a
LC
53 service-type-description
54 service-type-location
55
0c0c1b21
LC
56 %service-type-path
57 fold-service-types
49483f71 58 lookup-service-types
0adfe95a 59
db4fdc04 60 service
0adfe95a
LC
61 service?
62 service-kind
efe7d19a
LC
63 service-value
64 service-parameters ;deprecated
0adfe95a 65
71654dfd 66 simple-service
cd6f6c22 67 modify-services
5152d13b 68 service-back-edges
0adfe95a
LC
69 fold-services
70
71 service-error?
1bb895ea
LC
72 missing-value-service-error?
73 missing-value-service-error-type
74 missing-value-service-error-location
0adfe95a
LC
75 missing-target-service-error?
76 missing-target-service-error-service
77 missing-target-service-error-target-type
78 ambiguous-target-service-error?
79 ambiguous-target-service-error-service
80 ambiguous-target-service-error-target-type
81
d62e201c 82 system-service-type
0adfe95a 83 boot-service-type
be7be9e8 84 cleanup-service-type
0adfe95a
LC
85 activation-service-type
86 activation-service->script
a241a7ac 87 %linux-bare-metal-service
387e1754
LC
88 special-files-service-type
89 extra-special-file
0adfe95a
LC
90 etc-service-type
91 etc-directory
92 setuid-program-service-type
af4c3fd5 93 profile-service-type
0adfe95a 94 firmware-service-type
e0b47290 95 gc-root-service-type
0adfe95a
LC
96
97 %boot-service
98 %activation-service
d298c815 99 etc-service))
0adfe95a
LC
100
101;;; Comment:
102;;;
103;;; This module defines a broad notion of "service types" and "services."
db4fdc04 104;;;
0adfe95a
LC
105;;; A service type describe how its instances extend instances of other
106;;; service types. For instance, some services extend the instance of
107;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
d4053c71
AK
108;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
109;;; <shepherd-service>.
0adfe95a
LC
110;;;
111;;; When applicable, the service type defines how it can itself be extended,
112;;; by providing one procedure to compose extensions, and one procedure to
113;;; extend itself.
114;;;
d62e201c
LC
115;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
116;;; instance, which is the root of the service DAG. Its value is the
117;;; derivation that produces the 'system' directory as returned by
118;;; 'operating-system-derivation'.
0adfe95a
LC
119;;;
120;;; The 'fold-services' procedure can be passed a list of procedures, which it
121;;; "folds" by propagating extensions down the graph; it returns the root
122;;; service after the applying all its extensions.
db4fdc04
LC
123;;;
124;;; Code:
125
0adfe95a
LC
126(define-record-type <service-extension>
127 (service-extension target compute)
128 service-extension?
129 (target service-extension-target) ;<service-type>
130 (compute service-extension-compute)) ;params -> params
131
1bb895ea
LC
132(define &no-default-value
133 ;; Value used to denote service types that have no associated default value.
134 '(no default value))
135
0adfe95a
LC
136(define-record-type* <service-type> service-type make-service-type
137 service-type?
138 (name service-type-name) ;symbol (for debugging)
139
140 ;; Things extended by services of this type.
141 (extensions service-type-extensions) ;list of <service-extensions>
142
143 ;; Given a list of extensions, "compose" them.
144 (compose service-type-compose ;list of Any -> Any
145 (default #f))
146
147 ;; Extend the services' own parameters with the extension composition.
148 (extend service-type-extend ;list of Any -> parameters
1bb895ea
LC
149 (default #f))
150
151 ;; Optional default value for instances of this type.
152 (default-value service-type-default-value ;Any
b714395a
LC
153 (default &no-default-value))
154
155 ;; Meta-data.
156 (description service-type-description ;string
157 (default #f))
158 (location service-type-location ;<location>
159 (default (and=> (current-source-location)
160 source-properties->location))
161 (innate)))
0adfe95a
LC
162
163(define (write-service-type type port)
164 (format port "#<service-type ~a ~a>"
165 (service-type-name type)
166 (number->string (object-address type) 16)))
167
168(set-record-type-printer! <service-type> write-service-type)
169
0c0c1b21
LC
170(define %distro-root-directory
171 ;; Absolute file name of the module hierarchy.
172 (dirname (search-path %load-path "guix.scm")))
173
174(define %service-type-path
175 ;; Search path for service types.
176 (make-parameter `((,%distro-root-directory . "gnu/services")
177 (,%distro-root-directory . "gnu/system"))))
178
3943913f
LC
179(define (all-service-modules)
180 "Return the default set of service modules."
181 (cons (resolve-interface '(gnu services))
182 (all-modules (%service-type-path))))
183
0c0c1b21
LC
184(define* (fold-service-types proc seed
185 #:optional
3943913f 186 (modules (all-service-modules)))
0c0c1b21
LC
187 "For each service type exported by one of MODULES, call (PROC RESULT). SEED
188is used as the initial value of RESULT."
189 (fold-module-public-variables (lambda (object result)
190 (if (service-type? object)
191 (proc object result)
192 result))
a3d37f3a 193 seed
0c0c1b21
LC
194 modules))
195
49483f71
LC
196(define lookup-service-types
197 (let ((table
198 (delay (fold-service-types (lambda (type result)
199 (vhash-consq (service-type-name type)
200 type result))
201 vlist-null))))
202 (lambda (name)
203 "Return the list of services with the given NAME (a symbol)."
204 (vhash-foldq* cons '() name (force table)))))
205
0adfe95a
LC
206;; Services of a given type.
207(define-record-type <service>
1bb895ea 208 (make-service type value)
db4fdc04 209 service?
0adfe95a 210 (type service-kind)
efe7d19a
LC
211 (value service-value))
212
1bb895ea
LC
213(define-syntax service
214 (syntax-rules ()
215 "Return a service instance of TYPE. The service value is VALUE or, if
216omitted, TYPE's default value."
217 ((_ type value)
218 (make-service type value))
219 ((_ type)
220 (%service-with-default-value (current-source-location)
221 type))))
222
223(define (%service-with-default-value location type)
224 "Return a instance of service type TYPE with its default value, if any. If
225TYPE does not have a default value, an error is raised."
226 ;; TODO: Currently this is a run-time error but with a little bit macrology
227 ;; we could turn it into an expansion-time error.
228 (let ((default (service-type-default-value type)))
229 (if (eq? default &no-default-value)
230 (let ((location (source-properties->location location)))
231 (raise
232 (condition
233 (&missing-value-service-error (type type) (location location))
234 (&message
69daee23 235 (message (format #f (G_ "~a: no value specified \
1bb895ea
LC
236for service of type '~a'")
237 (location->string location)
238 (service-type-name type)))))))
239 (service type default))))
240
241(define-condition-type &service-error &error
242 service-error?)
243
244(define-condition-type &missing-value-service-error &service-error
245 missing-value-service-error?
246 (type missing-value-service-error-type)
247 (location missing-value-service-error-location))
248
249
250\f
251;;;
252;;; Helpers.
253;;;
254
efe7d19a
LC
255(define service-parameters
256 ;; Deprecated alias.
257 service-value)
0adfe95a 258
71654dfd
LC
259(define (simple-service name target value)
260 "Return a service that extends TARGET with VALUE. This works by creating a
261singleton service type NAME, of which the returned service is an instance."
262 (let* ((extension (service-extension target identity))
263 (type (service-type (name name)
264 (extensions (list extension)))))
265 (service type value)))
0adfe95a 266
cd6f6c22
LC
267(define-syntax %modify-service
268 (syntax-rules (=>)
269 ((_ service)
270 service)
271 ((_ svc (kind param => exp ...) clauses ...)
272 (if (eq? (service-kind svc) kind)
efe7d19a 273 (let ((param (service-value svc)))
cd6f6c22
LC
274 (service (service-kind svc)
275 (begin exp ...)))
276 (%modify-service svc clauses ...)))))
277
278(define-syntax modify-services
279 (syntax-rules ()
4d343a14
CM
280 "Modify the services listed in SERVICES according to CLAUSES and return
281the resulting list of services. Each clause must have the form:
cd6f6c22
LC
282
283 (TYPE VARIABLE => BODY)
284
285where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
286identifier that is bound within BODY to the value of the service of that
287TYPE. Consider this example:
288
289 (modify-services %base-services
290 (guix-service-type config =>
291 (guix-configuration
292 (inherit config)
293 (use-substitutes? #f)
294 (extra-options '(\"--gc-keep-derivations\"))))
295 (mingetty-service-type config =>
296 (mingetty-configuration
297 (inherit config)
298 (motd (plain-file \"motd\" \"Hi there!\")))))
299
300It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
301all the MINGETTY-SERVICE-TYPE instances.
302
303This is a shorthand for (map (lambda (svc) ...) %base-services)."
304 ((_ services clauses ...)
305 (map (lambda (service)
306 (%modify-service service clauses ...))
307 services))))
0adfe95a
LC
308
309\f
310;;;
311;;; Core services.
312;;;
313
d62e201c
LC
314(define (system-derivation mentries mextensions)
315 "Return as a monadic value the derivation of the 'system' directory
316containing the given entries."
317 (mlet %store-monad ((entries mentries)
318 (extensions (sequence %store-monad mextensions)))
319 (lower-object
320 (file-union "system"
321 (append entries (concatenate extensions))))))
322
323(define system-service-type
324 ;; This is the ultimate service type, the root of the service DAG. The
325 ;; service of this type is extended by monadic name/item pairs. These items
326 ;; end up in the "system directory" as returned by
327 ;; 'operating-system-derivation'.
328 (service-type (name 'system)
329 (extensions '())
330 (compose identity)
331 (extend system-derivation)))
332
3a391e68 333(define (compute-boot-script _ mexps)
be7be9e8 334 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
0adfe95a 335 (gexp->file "boot"
d4053c71 336 ;; Clean up and activate the system, then spawn shepherd.
be7be9e8 337 #~(begin #$@gexps))))
0adfe95a 338
d62e201c
LC
339(define (boot-script-entry mboot)
340 "Return, as a monadic value, an entry for the boot script in the system
341directory."
342 (mlet %store-monad ((boot mboot))
343 (return `(("boot" ,boot)))))
344
0adfe95a
LC
345(define boot-service-type
346 ;; The service of this type is extended by being passed gexps as monadic
347 ;; values. It aggregates them in a single script, as a monadic value, which
348 ;; becomes its 'parameters'. It is the only service that extends nothing.
349 (service-type (name 'boot)
d62e201c
LC
350 (extensions
351 (list (service-extension system-service-type
352 boot-script-entry)))
3a391e68
LC
353 (compose append)
354 (extend compute-boot-script)))
0adfe95a
LC
355
356(define %boot-service
d62e201c 357 ;; The service that produces the boot script.
0adfe95a 358 (service boot-service-type #t))
be7be9e8
LC
359
360(define (cleanup-gexp _)
361 "Return as a monadic value a gexp to clean up /tmp and similar places upon
362boot."
fd129893
LC
363 (with-monad %store-monad
364 (with-imported-modules '((guix build utils))
365 (return #~(begin
366 (use-modules (guix build utils))
367
368 ;; Clean out /tmp and /var/run.
369 ;;
370 ;; XXX This needs to happen before service activations, so it
371 ;; has to be here, but this also implicitly assumes that /tmp
372 ;; and /var/run are on the root partition.
373 (letrec-syntax ((fail-safe (syntax-rules ()
374 ((_ exp rest ...)
375 (begin
376 (catch 'system-error
377 (lambda () exp)
378 (const #f))
379 (fail-safe rest ...)))
380 ((_)
381 #t))))
382 ;; Ignore I/O errors so the system can boot.
383 (fail-safe
aad8a143
LC
384 ;; Remove stale Shadow lock files as they would lead to
385 ;; failures of 'useradd' & co.
386 (delete-file "/etc/group.lock")
387 (delete-file "/etc/passwd.lock")
388 (delete-file "/etc/.pwd.lock") ;from 'lckpwdf'
389
fd129893
LC
390 (delete-file-recursively "/tmp")
391 (delete-file-recursively "/var/run")
392 (mkdir "/tmp")
393 (chmod "/tmp" #o1777)
394 (mkdir "/var/run")
e25ca462
DM
395 (chmod "/var/run" #o755)
396 (delete-file-recursively "/run/udev/watch.old"))))))))
be7be9e8
LC
397
398(define cleanup-service-type
399 ;; Service that cleans things up in /tmp and similar.
400 (service-type (name 'cleanup)
401 (extensions
402 (list (service-extension boot-service-type
403 cleanup-gexp)))))
0adfe95a 404
0adfe95a
LC
405(define* (activation-service->script service)
406 "Return as a monadic value the activation script for SERVICE, a service of
407ACTIVATION-SCRIPT-TYPE."
efe7d19a 408 (activation-script (service-value service)))
0adfe95a
LC
409
410(define (activation-script gexps)
411 "Return the system's activation script, which evaluates GEXPS."
0adfe95a
LC
412 (define (service-activations)
413 ;; Return the activation scripts for SERVICES.
414 (mapm %store-monad
415 (cut gexp->file "activate-service" <>)
416 gexps))
417
fd129893 418 (mlet* %store-monad ((actions (service-activations)))
0adfe95a 419 (gexp->file "activate"
232ccbef 420 (with-imported-modules (source-module-closure
fe2b6434
CB
421 '((gnu build activation)
422 (guix build utils)))
fd129893 423 #~(begin
fe2b6434
CB
424 (use-modules (gnu build activation)
425 (guix build utils))
0adfe95a 426
caa78166
LC
427 ;; Make sure the user accounting database exists. If it
428 ;; does not exist, 'setutxent' does not create it and
429 ;; thus there is no accounting at all.
430 (close-port (open-file "/var/run/utmpx" "a0"))
431
2986995b
LC
432 ;; Same for 'wtmp', which is populated by mingetty et
433 ;; al.
fe2b6434 434 (mkdir-p "/var/log")
2986995b
LC
435 (close-port (open-file "/var/log/wtmp" "a0"))
436
97bb1ab6
CB
437 ;; Set up /run/current-system. Among other things this
438 ;; sets up locales, which the activation snippets
439 ;; executed below may expect.
440 (activate-current-system)
441
fd129893
LC
442 ;; Run the services' activation snippets.
443 ;; TODO: Use 'load-compiled'.
97bb1ab6 444 (for-each primitive-load '#$actions))))))
0adfe95a
LC
445
446(define (gexps->activation-gexp gexps)
447 "Return a gexp that runs the activation script containing GEXPS."
448 (mlet %store-monad ((script (activation-script gexps)))
449 (return #~(primitive-load #$script))))
450
3a391e68
LC
451(define (second-argument a b) b)
452
0adfe95a
LC
453(define activation-service-type
454 (service-type (name 'activate)
455 (extensions
456 (list (service-extension boot-service-type
457 gexps->activation-gexp)))
458 (compose append)
459 (extend second-argument)))
460
461(define %activation-service
462 ;; The activation service produces the activation script from the gexps it
463 ;; receives.
464 (service activation-service-type #t))
465
a241a7ac
LC
466(define %modprobe-wrapper
467 ;; Wrapper for the 'modprobe' command that knows where modules live.
468 ;;
469 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
470 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
471 ;; environment variable is not set---hence the need for this wrapper.
472 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
473 (program-file "modprobe"
474 #~(begin
475 (setenv "LINUX_MODULE_DIRECTORY"
476 "/run/booted-system/kernel/lib/modules")
477 (apply execl #$modprobe
478 (cons #$modprobe (cdr (command-line))))))))
479
480(define %linux-kernel-activation
481 ;; Activation of the Linux kernel running on the bare metal (as opposed to
482 ;; running in a container.)
483 #~(begin
484 ;; Tell the kernel to use our 'modprobe' command.
485 (activate-modprobe #$%modprobe-wrapper)
486
487 ;; Let users debug their own processes!
488 (activate-ptrace-attach)))
489
a241a7ac
LC
490(define %linux-bare-metal-service
491 ;; The service that does things that are needed on the "bare metal", but not
492 ;; necessary or impossible in a container.
6ddf4fcf
LC
493 (simple-service 'linux-bare-metal
494 activation-service-type
495 %linux-kernel-activation))
496
a241a7ac 497
387e1754
LC
498(define special-files-service-type
499 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
500 (service-type
501 (name 'special-files)
502 (extensions
503 (list (service-extension activation-service-type
504 (lambda (files)
505 #~(activate-special-files '#$files)))))
506 (compose concatenate)
507 (extend append)))
508
509(define (extra-special-file file target)
510 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
511 (file-append coreutils \"/bin/env\")
512and FILE could be \"/usr/bin/env\"."
513 (simple-service (string->symbol (string-append "special-file-" file))
514 special-files-service-type
515 `((,file ,target))))
516
0adfe95a
LC
517(define (etc-directory service)
518 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
efe7d19a 519 (files->etc-directory (service-value service)))
0adfe95a
LC
520
521(define (files->etc-directory files)
522 (file-union "etc" files))
523
d62e201c
LC
524(define (etc-entry files)
525 "Return an entry for the /etc directory consisting of FILES in the system
526directory."
527 (with-monad %store-monad
528 (return `(("etc" ,(files->etc-directory files))))))
529
0adfe95a
LC
530(define etc-service-type
531 (service-type (name 'etc)
532 (extensions
533 (list
534 (service-extension activation-service-type
535 (lambda (files)
536 (let ((etc
537 (files->etc-directory files)))
d62e201c
LC
538 #~(activate-etc #$etc))))
539 (service-extension system-service-type etc-entry)))
0adfe95a
LC
540 (compose concatenate)
541 (extend append)))
542
543(define (etc-service files)
544 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
545FILES must be a list of name/file-like object pairs."
546 (service etc-service-type files))
547
548(define setuid-program-service-type
549 (service-type (name 'setuid-program)
550 (extensions
551 (list (service-extension activation-service-type
552 (lambda (programs)
553 #~(activate-setuid-programs
554 (list #$@programs))))))
555 (compose concatenate)
556 (extend append)))
557
af4c3fd5
LC
558(define (packages->profile-entry packages)
559 "Return a system entry for the profile containing PACKAGES."
560 (mlet %store-monad ((profile (profile-derivation
60a0886d
SB
561 (packages->manifest
562 (delete-duplicates packages eq?)))))
af4c3fd5
LC
563 (return `(("profile" ,profile)))))
564
565(define profile-service-type
566 ;; The service that populates the system's profile---i.e.,
567 ;; /run/current-system/profile. It is extended by package lists.
568 (service-type (name 'profile)
569 (extensions
570 (list (service-extension system-service-type
571 packages->profile-entry)))
572 (compose concatenate)
573 (extend append)))
574
0adfe95a
LC
575(define (firmware->activation-gexp firmware)
576 "Return a gexp to make the packages listed in FIRMWARE loadable by the
577kernel."
578 (let ((directory (directory-union "firmware" firmware)))
579 ;; Tell the kernel where firmware is.
580 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
581
582(define firmware-service-type
583 ;; The service that collects firmware.
584 (service-type (name 'firmware)
585 (extensions
586 (list (service-extension activation-service-type
587 firmware->activation-gexp)))
588 (compose concatenate)
589 (extend append)))
590
e0b47290
LC
591(define (gc-roots->system-entry roots)
592 "Return an entry in the system's output containing symlinks to ROOTS."
593 (mlet %store-monad ((entry (gexp->derivation
594 "gc-roots"
595 #~(let ((roots '#$roots))
596 (mkdir #$output)
597 (chdir #$output)
598 (for-each symlink
599 roots
600 (map number->string
601 (iota (length roots))))))))
602 (return (if (null? roots)
603 '()
604 `(("gc-roots" ,entry))))))
605
606(define gc-root-service-type
607 ;; A service to associate extra garbage-collector roots to the system. This
608 ;; is a simple hack that guarantees that the system retains references to
609 ;; the given list of roots. Roots must be "lowerable" objects like
610 ;; packages, or derivations.
611 (service-type (name 'gc-roots)
612 (extensions
613 (list (service-extension system-service-type
614 gc-roots->system-entry)))
615 (compose concatenate)
616 (extend append)))
617
0adfe95a
LC
618\f
619;;;
620;;; Service folding.
621;;;
622
0adfe95a
LC
623(define-condition-type &missing-target-service-error &service-error
624 missing-target-service-error?
625 (service missing-target-service-error-service)
626 (target-type missing-target-service-error-target-type))
627
628(define-condition-type &ambiguous-target-service-error &service-error
629 ambiguous-target-service-error?
630 (service ambiguous-target-service-error-service)
631 (target-type ambiguous-target-service-error-target-type))
632
633(define (service-back-edges services)
634 "Return a procedure that, when passed a <service>, returns the list of
635<service> objects that depend on it."
636 (define (add-edges service edges)
637 (define (add-edge extension edges)
638 (let ((target-type (service-extension-target extension)))
639 (match (filter (lambda (service)
640 (eq? (service-kind service) target-type))
641 services)
642 ((target)
643 (vhash-consq target service edges))
644 (()
645 (raise
646 (condition (&missing-target-service-error
647 (service service)
648 (target-type target-type))
649 (&message
650 (message
638e9dea 651 (format #f (G_ "no target of type '~a' for service '~a'")
0adfe95a 652 (service-type-name target-type)
638e9dea
LC
653 (service-type-name
654 (service-kind service))))))))
0adfe95a
LC
655 (x
656 (raise
657 (condition (&ambiguous-target-service-error
658 (service service)
659 (target-type target-type))
660 (&message
661 (message
662 (format #f
69daee23 663 (G_ "more than one target service of type '~a'")
0adfe95a
LC
664 (service-type-name target-type))))))))))
665
666 (fold add-edge edges (service-type-extensions (service-kind service))))
667
668 (let ((edges (fold add-edges vlist-null services)))
669 (lambda (node)
670 (reverse (vhash-foldq* cons '() node edges)))))
671
d62e201c
LC
672(define* (fold-services services
673 #:key (target-type system-service-type))
0adfe95a
LC
674 "Fold SERVICES by propagating their extensions down to the root of type
675TARGET-TYPE; return the root service adjusted accordingly."
676 (define dependents
677 (service-back-edges services))
678
679 (define (matching-extension target)
680 (let ((target (service-kind target)))
681 (match-lambda
682 (($ <service-extension> type)
683 (eq? type target)))))
684
685 (define (apply-extension target)
686 (lambda (service)
687 (match (find (matching-extension target)
688 (service-type-extensions (service-kind service)))
689 (($ <service-extension> _ compute)
efe7d19a 690 (compute (service-value service))))))
0adfe95a
LC
691
692 (match (filter (lambda (service)
693 (eq? (service-kind service) target-type))
694 services)
695 ((sink)
696 (let loop ((sink sink))
697 (let* ((dependents (map loop (dependents sink)))
698 (extensions (map (apply-extension sink) dependents))
699 (extend (service-type-extend (service-kind sink)))
700 (compose (service-type-compose (service-kind sink)))
efe7d19a 701 (params (service-value sink)))
0adfe95a
LC
702 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
703 ;; different type than the elements of EXTENSIONS.
704 (if extend
705 (service (service-kind sink)
706 (extend params (compose extensions)))
707 sink))))
708 (()
709 (raise
710 (condition (&missing-target-service-error
711 (service #f)
712 (target-type target-type))
713 (&message
69daee23 714 (message (format #f (G_ "service of type '~a' not found")
0adfe95a
LC
715 (service-type-name target-type)))))))
716 (x
717 (raise
718 (condition (&ambiguous-target-service-error
719 (service #f)
720 (target-type target-type))
721 (&message
722 (message
723 (format #f
69daee23 724 (G_ "more than one target service of type '~a'")
0adfe95a 725 (service-type-name target-type)))))))))
db4fdc04
LC
726
727;;; services.scm ends here.