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