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