git: Work around wrong default argument of 'clone'.
[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")
395 (chmod "/var/run" #o755))))))))
be7be9e8
LC
396
397(define cleanup-service-type
398 ;; Service that cleans things up in /tmp and similar.
399 (service-type (name 'cleanup)
400 (extensions
401 (list (service-extension boot-service-type
402 cleanup-gexp)))))
0adfe95a 403
0adfe95a
LC
404(define* (activation-service->script service)
405 "Return as a monadic value the activation script for SERVICE, a service of
406ACTIVATION-SCRIPT-TYPE."
efe7d19a 407 (activation-script (service-value service)))
0adfe95a
LC
408
409(define (activation-script gexps)
410 "Return the system's activation script, which evaluates GEXPS."
0adfe95a
LC
411 (define (service-activations)
412 ;; Return the activation scripts for SERVICES.
413 (mapm %store-monad
414 (cut gexp->file "activate-service" <>)
415 gexps))
416
fd129893 417 (mlet* %store-monad ((actions (service-activations)))
0adfe95a 418 (gexp->file "activate"
232ccbef 419 (with-imported-modules (source-module-closure
fe2b6434
CB
420 '((gnu build activation)
421 (guix build utils)))
fd129893 422 #~(begin
fe2b6434
CB
423 (use-modules (gnu build activation)
424 (guix build utils))
0adfe95a 425
caa78166
LC
426 ;; Make sure the user accounting database exists. If it
427 ;; does not exist, 'setutxent' does not create it and
428 ;; thus there is no accounting at all.
429 (close-port (open-file "/var/run/utmpx" "a0"))
430
2986995b
LC
431 ;; Same for 'wtmp', which is populated by mingetty et
432 ;; al.
fe2b6434 433 (mkdir-p "/var/log")
2986995b
LC
434 (close-port (open-file "/var/log/wtmp" "a0"))
435
97bb1ab6
CB
436 ;; Set up /run/current-system. Among other things this
437 ;; sets up locales, which the activation snippets
438 ;; executed below may expect.
439 (activate-current-system)
440
fd129893
LC
441 ;; Run the services' activation snippets.
442 ;; TODO: Use 'load-compiled'.
97bb1ab6 443 (for-each primitive-load '#$actions))))))
0adfe95a
LC
444
445(define (gexps->activation-gexp gexps)
446 "Return a gexp that runs the activation script containing GEXPS."
447 (mlet %store-monad ((script (activation-script gexps)))
448 (return #~(primitive-load #$script))))
449
3a391e68
LC
450(define (second-argument a b) b)
451
0adfe95a
LC
452(define activation-service-type
453 (service-type (name 'activate)
454 (extensions
455 (list (service-extension boot-service-type
456 gexps->activation-gexp)))
457 (compose append)
458 (extend second-argument)))
459
460(define %activation-service
461 ;; The activation service produces the activation script from the gexps it
462 ;; receives.
463 (service activation-service-type #t))
464
a241a7ac
LC
465(define %modprobe-wrapper
466 ;; Wrapper for the 'modprobe' command that knows where modules live.
467 ;;
468 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
469 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
470 ;; environment variable is not set---hence the need for this wrapper.
471 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
472 (program-file "modprobe"
473 #~(begin
474 (setenv "LINUX_MODULE_DIRECTORY"
475 "/run/booted-system/kernel/lib/modules")
476 (apply execl #$modprobe
477 (cons #$modprobe (cdr (command-line))))))))
478
479(define %linux-kernel-activation
480 ;; Activation of the Linux kernel running on the bare metal (as opposed to
481 ;; running in a container.)
482 #~(begin
483 ;; Tell the kernel to use our 'modprobe' command.
484 (activate-modprobe #$%modprobe-wrapper)
485
486 ;; Let users debug their own processes!
487 (activate-ptrace-attach)))
488
a241a7ac
LC
489(define %linux-bare-metal-service
490 ;; The service that does things that are needed on the "bare metal", but not
491 ;; necessary or impossible in a container.
6ddf4fcf
LC
492 (simple-service 'linux-bare-metal
493 activation-service-type
494 %linux-kernel-activation))
495
a241a7ac 496
387e1754
LC
497(define special-files-service-type
498 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
499 (service-type
500 (name 'special-files)
501 (extensions
502 (list (service-extension activation-service-type
503 (lambda (files)
504 #~(activate-special-files '#$files)))))
505 (compose concatenate)
506 (extend append)))
507
508(define (extra-special-file file target)
509 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
510 (file-append coreutils \"/bin/env\")
511and FILE could be \"/usr/bin/env\"."
512 (simple-service (string->symbol (string-append "special-file-" file))
513 special-files-service-type
514 `((,file ,target))))
515
0adfe95a
LC
516(define (etc-directory service)
517 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
efe7d19a 518 (files->etc-directory (service-value service)))
0adfe95a
LC
519
520(define (files->etc-directory files)
521 (file-union "etc" files))
522
d62e201c
LC
523(define (etc-entry files)
524 "Return an entry for the /etc directory consisting of FILES in the system
525directory."
526 (with-monad %store-monad
527 (return `(("etc" ,(files->etc-directory files))))))
528
0adfe95a
LC
529(define etc-service-type
530 (service-type (name 'etc)
531 (extensions
532 (list
533 (service-extension activation-service-type
534 (lambda (files)
535 (let ((etc
536 (files->etc-directory files)))
d62e201c
LC
537 #~(activate-etc #$etc))))
538 (service-extension system-service-type etc-entry)))
0adfe95a
LC
539 (compose concatenate)
540 (extend append)))
541
542(define (etc-service files)
543 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
544FILES must be a list of name/file-like object pairs."
545 (service etc-service-type files))
546
547(define setuid-program-service-type
548 (service-type (name 'setuid-program)
549 (extensions
550 (list (service-extension activation-service-type
551 (lambda (programs)
552 #~(activate-setuid-programs
553 (list #$@programs))))))
554 (compose concatenate)
555 (extend append)))
556
af4c3fd5
LC
557(define (packages->profile-entry packages)
558 "Return a system entry for the profile containing PACKAGES."
559 (mlet %store-monad ((profile (profile-derivation
60a0886d
SB
560 (packages->manifest
561 (delete-duplicates packages eq?)))))
af4c3fd5
LC
562 (return `(("profile" ,profile)))))
563
564(define profile-service-type
565 ;; The service that populates the system's profile---i.e.,
566 ;; /run/current-system/profile. It is extended by package lists.
567 (service-type (name 'profile)
568 (extensions
569 (list (service-extension system-service-type
570 packages->profile-entry)))
571 (compose concatenate)
572 (extend append)))
573
0adfe95a
LC
574(define (firmware->activation-gexp firmware)
575 "Return a gexp to make the packages listed in FIRMWARE loadable by the
576kernel."
577 (let ((directory (directory-union "firmware" firmware)))
578 ;; Tell the kernel where firmware is.
579 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
580
581(define firmware-service-type
582 ;; The service that collects firmware.
583 (service-type (name 'firmware)
584 (extensions
585 (list (service-extension activation-service-type
586 firmware->activation-gexp)))
587 (compose concatenate)
588 (extend append)))
589
e0b47290
LC
590(define (gc-roots->system-entry roots)
591 "Return an entry in the system's output containing symlinks to ROOTS."
592 (mlet %store-monad ((entry (gexp->derivation
593 "gc-roots"
594 #~(let ((roots '#$roots))
595 (mkdir #$output)
596 (chdir #$output)
597 (for-each symlink
598 roots
599 (map number->string
600 (iota (length roots))))))))
601 (return (if (null? roots)
602 '()
603 `(("gc-roots" ,entry))))))
604
605(define gc-root-service-type
606 ;; A service to associate extra garbage-collector roots to the system. This
607 ;; is a simple hack that guarantees that the system retains references to
608 ;; the given list of roots. Roots must be "lowerable" objects like
609 ;; packages, or derivations.
610 (service-type (name 'gc-roots)
611 (extensions
612 (list (service-extension system-service-type
613 gc-roots->system-entry)))
614 (compose concatenate)
615 (extend append)))
616
0adfe95a
LC
617\f
618;;;
619;;; Service folding.
620;;;
621
0adfe95a
LC
622(define-condition-type &missing-target-service-error &service-error
623 missing-target-service-error?
624 (service missing-target-service-error-service)
625 (target-type missing-target-service-error-target-type))
626
627(define-condition-type &ambiguous-target-service-error &service-error
628 ambiguous-target-service-error?
629 (service ambiguous-target-service-error-service)
630 (target-type ambiguous-target-service-error-target-type))
631
632(define (service-back-edges services)
633 "Return a procedure that, when passed a <service>, returns the list of
634<service> objects that depend on it."
635 (define (add-edges service edges)
636 (define (add-edge extension edges)
637 (let ((target-type (service-extension-target extension)))
638 (match (filter (lambda (service)
639 (eq? (service-kind service) target-type))
640 services)
641 ((target)
642 (vhash-consq target service edges))
643 (()
644 (raise
645 (condition (&missing-target-service-error
646 (service service)
647 (target-type target-type))
648 (&message
649 (message
638e9dea 650 (format #f (G_ "no target of type '~a' for service '~a'")
0adfe95a 651 (service-type-name target-type)
638e9dea
LC
652 (service-type-name
653 (service-kind service))))))))
0adfe95a
LC
654 (x
655 (raise
656 (condition (&ambiguous-target-service-error
657 (service service)
658 (target-type target-type))
659 (&message
660 (message
661 (format #f
69daee23 662 (G_ "more than one target service of type '~a'")
0adfe95a
LC
663 (service-type-name target-type))))))))))
664
665 (fold add-edge edges (service-type-extensions (service-kind service))))
666
667 (let ((edges (fold add-edges vlist-null services)))
668 (lambda (node)
669 (reverse (vhash-foldq* cons '() node edges)))))
670
d62e201c
LC
671(define* (fold-services services
672 #:key (target-type system-service-type))
0adfe95a
LC
673 "Fold SERVICES by propagating their extensions down to the root of type
674TARGET-TYPE; return the root service adjusted accordingly."
675 (define dependents
676 (service-back-edges services))
677
678 (define (matching-extension target)
679 (let ((target (service-kind target)))
680 (match-lambda
681 (($ <service-extension> type)
682 (eq? type target)))))
683
684 (define (apply-extension target)
685 (lambda (service)
686 (match (find (matching-extension target)
687 (service-type-extensions (service-kind service)))
688 (($ <service-extension> _ compute)
efe7d19a 689 (compute (service-value service))))))
0adfe95a
LC
690
691 (match (filter (lambda (service)
692 (eq? (service-kind service) target-type))
693 services)
694 ((sink)
695 (let loop ((sink sink))
696 (let* ((dependents (map loop (dependents sink)))
697 (extensions (map (apply-extension sink) dependents))
698 (extend (service-type-extend (service-kind sink)))
699 (compose (service-type-compose (service-kind sink)))
efe7d19a 700 (params (service-value sink)))
0adfe95a
LC
701 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
702 ;; different type than the elements of EXTENSIONS.
703 (if extend
704 (service (service-kind sink)
705 (extend params (compose extensions)))
706 sink))))
707 (()
708 (raise
709 (condition (&missing-target-service-error
710 (service #f)
711 (target-type target-type))
712 (&message
69daee23 713 (message (format #f (G_ "service of type '~a' not found")
0adfe95a
LC
714 (service-type-name target-type)))))))
715 (x
716 (raise
717 (condition (&ambiguous-target-service-error
718 (service #f)
719 (target-type target-type))
720 (&message
721 (message
722 (format #f
69daee23 723 (G_ "more than one target service of type '~a'")
0adfe95a 724 (service-type-name target-type)))))))))
db4fdc04
LC
725
726;;; services.scm ends here.