Commit | Line | Data |
---|---|---|
db4fdc04 | 1 | ;;; GNU Guix --- Functional package management for GNU |
223ede4e | 2 | ;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
4d343a14 | 3 | ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com> |
59bcffa3 | 4 | ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org> |
a247f5c7 | 5 | ;;; Copyright © 2020, 2021 Ricardo Wurmus <rekado@elephly.net> |
a3df3825 | 6 | ;;; Copyright © 2021 raid5atemyhomework <raid5atemyhomework@protonmail.com> |
db4fdc04 LC |
7 | ;;; |
8 | ;;; This file is part of GNU Guix. | |
9 | ;;; | |
10 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
11 | ;;; under the terms of the GNU General Public License as published by | |
12 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
13 | ;;; your option) any later version. | |
14 | ;;; | |
15 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
16 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;;; GNU General Public License for more details. | |
19 | ;;; | |
20 | ;;; You should have received a copy of the GNU General Public License | |
21 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
22 | ||
23 | (define-module (gnu services) | |
bebc8681 | 24 | #:use-module (guix gexp) |
0adfe95a LC |
25 | #:use-module (guix monads) |
26 | #:use-module (guix store) | |
db4fdc04 | 27 | #:use-module (guix records) |
af4c3fd5 | 28 | #:use-module (guix profiles) |
0c0c1b21 | 29 | #:use-module (guix discovery) |
d466b1fc | 30 | #:use-module (guix combinators) |
33b7cb7a LC |
31 | #:use-module (guix channels) |
32 | #:use-module (guix describe) | |
0adfe95a LC |
33 | #:use-module (guix sets) |
34 | #:use-module (guix ui) | |
d51bfe24 | 35 | #:use-module (guix diagnostics) |
eb5cf39e | 36 | #:autoload (guix openpgp) (openpgp-format-fingerprint) |
232ccbef | 37 | #:use-module (guix modules) |
a3df3825 | 38 | #:use-module (guix packages) |
39 | #:use-module (guix utils) | |
0adfe95a LC |
40 | #:use-module (gnu packages base) |
41 | #:use-module (gnu packages bash) | |
59bcffa3 | 42 | #:use-module (gnu packages hurd) |
0adfe95a LC |
43 | #:use-module (srfi srfi-1) |
44 | #:use-module (srfi srfi-9) | |
45 | #:use-module (srfi srfi-9 gnu) | |
46 | #:use-module (srfi srfi-26) | |
47 | #:use-module (srfi srfi-34) | |
48 | #:use-module (srfi srfi-35) | |
49 | #:use-module (ice-9 vlist) | |
50 | #:use-module (ice-9 match) | |
33b7cb7a | 51 | #:autoload (ice-9 pretty-print) (pretty-print) |
0adfe95a LC |
52 | #:export (service-extension |
53 | service-extension? | |
7d8b5913 CB |
54 | service-extension-target |
55 | service-extension-compute | |
0adfe95a LC |
56 | |
57 | service-type | |
58 | service-type? | |
5152d13b LC |
59 | service-type-name |
60 | service-type-extensions | |
61 | service-type-compose | |
62 | service-type-extend | |
1bb895ea | 63 | service-type-default-value |
b714395a LC |
64 | service-type-description |
65 | service-type-location | |
66 | ||
0c0c1b21 LC |
67 | %service-type-path |
68 | fold-service-types | |
49483f71 | 69 | lookup-service-types |
0adfe95a | 70 | |
db4fdc04 | 71 | service |
0adfe95a LC |
72 | service? |
73 | service-kind | |
efe7d19a LC |
74 | service-value |
75 | service-parameters ;deprecated | |
0adfe95a | 76 | |
71654dfd | 77 | simple-service |
cd6f6c22 | 78 | modify-services |
5152d13b | 79 | service-back-edges |
d466b1fc | 80 | instantiate-missing-services |
0adfe95a LC |
81 | fold-services |
82 | ||
83 | service-error? | |
1bb895ea LC |
84 | missing-value-service-error? |
85 | missing-value-service-error-type | |
86 | missing-value-service-error-location | |
0adfe95a LC |
87 | missing-target-service-error? |
88 | missing-target-service-error-service | |
89 | missing-target-service-error-target-type | |
90 | ambiguous-target-service-error? | |
91 | ambiguous-target-service-error-service | |
92 | ambiguous-target-service-error-target-type | |
93 | ||
d62e201c | 94 | system-service-type |
33b7cb7a | 95 | provenance-service-type |
a396dd01 | 96 | sexp->system-provenance |
b91a73a6 | 97 | system-provenance |
0adfe95a | 98 | boot-service-type |
be7be9e8 | 99 | cleanup-service-type |
0adfe95a LC |
100 | activation-service-type |
101 | activation-service->script | |
a241a7ac | 102 | %linux-bare-metal-service |
68d8c094 JN |
103 | %hurd-rc-script |
104 | %hurd-startup-service | |
387e1754 LC |
105 | special-files-service-type |
106 | extra-special-file | |
0adfe95a LC |
107 | etc-service-type |
108 | etc-directory | |
109 | setuid-program-service-type | |
af4c3fd5 | 110 | profile-service-type |
0adfe95a | 111 | firmware-service-type |
e0b47290 | 112 | gc-root-service-type |
a3df3825 | 113 | linux-builder-service-type |
114 | linux-builder-configuration | |
115 | linux-builder-configuration? | |
116 | linux-builder-configuration-kernel | |
117 | linux-builder-configuration-modules | |
118 | linux-loadable-module-service-type | |
0adfe95a LC |
119 | |
120 | %boot-service | |
121 | %activation-service | |
a247f5c7 RW |
122 | etc-service) |
123 | #:re-export (;; Note: Re-export 'delete' to allow for proper syntax matching | |
124 | ;; in 'modify-services' forms. See | |
125 | ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=26805#16>. | |
126 | delete)) | |
0adfe95a LC |
127 | |
128 | ;;; Comment: | |
129 | ;;; | |
130 | ;;; This module defines a broad notion of "service types" and "services." | |
db4fdc04 | 131 | ;;; |
0adfe95a LC |
132 | ;;; A service type describe how its instances extend instances of other |
133 | ;;; service types. For instance, some services extend the instance of | |
134 | ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create; | |
d4053c71 AK |
135 | ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of |
136 | ;;; <shepherd-service>. | |
0adfe95a LC |
137 | ;;; |
138 | ;;; When applicable, the service type defines how it can itself be extended, | |
139 | ;;; by providing one procedure to compose extensions, and one procedure to | |
140 | ;;; extend itself. | |
141 | ;;; | |
d62e201c LC |
142 | ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single |
143 | ;;; instance, which is the root of the service DAG. Its value is the | |
144 | ;;; derivation that produces the 'system' directory as returned by | |
145 | ;;; 'operating-system-derivation'. | |
0adfe95a LC |
146 | ;;; |
147 | ;;; The 'fold-services' procedure can be passed a list of procedures, which it | |
148 | ;;; "folds" by propagating extensions down the graph; it returns the root | |
149 | ;;; service after the applying all its extensions. | |
db4fdc04 LC |
150 | ;;; |
151 | ;;; Code: | |
152 | ||
0adfe95a LC |
153 | (define-record-type <service-extension> |
154 | (service-extension target compute) | |
155 | service-extension? | |
156 | (target service-extension-target) ;<service-type> | |
157 | (compute service-extension-compute)) ;params -> params | |
158 | ||
1bb895ea LC |
159 | (define &no-default-value |
160 | ;; Value used to denote service types that have no associated default value. | |
161 | '(no default value)) | |
162 | ||
0adfe95a LC |
163 | (define-record-type* <service-type> service-type make-service-type |
164 | service-type? | |
165 | (name service-type-name) ;symbol (for debugging) | |
166 | ||
167 | ;; Things extended by services of this type. | |
168 | (extensions service-type-extensions) ;list of <service-extensions> | |
169 | ||
170 | ;; Given a list of extensions, "compose" them. | |
171 | (compose service-type-compose ;list of Any -> Any | |
172 | (default #f)) | |
173 | ||
174 | ;; Extend the services' own parameters with the extension composition. | |
175 | (extend service-type-extend ;list of Any -> parameters | |
1bb895ea LC |
176 | (default #f)) |
177 | ||
178 | ;; Optional default value for instances of this type. | |
179 | (default-value service-type-default-value ;Any | |
b714395a LC |
180 | (default &no-default-value)) |
181 | ||
182 | ;; Meta-data. | |
183 | (description service-type-description ;string | |
184 | (default #f)) | |
185 | (location service-type-location ;<location> | |
186 | (default (and=> (current-source-location) | |
187 | source-properties->location)) | |
188 | (innate))) | |
0adfe95a LC |
189 | |
190 | (define (write-service-type type port) | |
191 | (format port "#<service-type ~a ~a>" | |
192 | (service-type-name type) | |
193 | (number->string (object-address type) 16))) | |
194 | ||
195 | (set-record-type-printer! <service-type> write-service-type) | |
196 | ||
0c0c1b21 LC |
197 | (define %distro-root-directory |
198 | ;; Absolute file name of the module hierarchy. | |
199 | (dirname (search-path %load-path "guix.scm"))) | |
200 | ||
201 | (define %service-type-path | |
202 | ;; Search path for service types. | |
203 | (make-parameter `((,%distro-root-directory . "gnu/services") | |
204 | (,%distro-root-directory . "gnu/system")))) | |
205 | ||
3943913f LC |
206 | (define (all-service-modules) |
207 | "Return the default set of service modules." | |
208 | (cons (resolve-interface '(gnu services)) | |
3c0128b0 LC |
209 | (all-modules (%service-type-path) |
210 | #:warn warn-about-load-error))) | |
3943913f | 211 | |
0c0c1b21 LC |
212 | (define* (fold-service-types proc seed |
213 | #:optional | |
3943913f | 214 | (modules (all-service-modules))) |
0c0c1b21 LC |
215 | "For each service type exported by one of MODULES, call (PROC RESULT). SEED |
216 | is used as the initial value of RESULT." | |
217 | (fold-module-public-variables (lambda (object result) | |
218 | (if (service-type? object) | |
219 | (proc object result) | |
220 | result)) | |
a3d37f3a | 221 | seed |
0c0c1b21 LC |
222 | modules)) |
223 | ||
49483f71 LC |
224 | (define lookup-service-types |
225 | (let ((table | |
226 | (delay (fold-service-types (lambda (type result) | |
227 | (vhash-consq (service-type-name type) | |
228 | type result)) | |
229 | vlist-null)))) | |
230 | (lambda (name) | |
231 | "Return the list of services with the given NAME (a symbol)." | |
232 | (vhash-foldq* cons '() name (force table))))) | |
233 | ||
0adfe95a LC |
234 | ;; Services of a given type. |
235 | (define-record-type <service> | |
1bb895ea | 236 | (make-service type value) |
db4fdc04 | 237 | service? |
0adfe95a | 238 | (type service-kind) |
efe7d19a LC |
239 | (value service-value)) |
240 | ||
1bb895ea LC |
241 | (define-syntax service |
242 | (syntax-rules () | |
243 | "Return a service instance of TYPE. The service value is VALUE or, if | |
244 | omitted, TYPE's default value." | |
245 | ((_ type value) | |
246 | (make-service type value)) | |
247 | ((_ type) | |
248 | (%service-with-default-value (current-source-location) | |
249 | type)))) | |
250 | ||
251 | (define (%service-with-default-value location type) | |
252 | "Return a instance of service type TYPE with its default value, if any. If | |
253 | TYPE does not have a default value, an error is raised." | |
254 | ;; TODO: Currently this is a run-time error but with a little bit macrology | |
255 | ;; we could turn it into an expansion-time error. | |
256 | (let ((default (service-type-default-value type))) | |
257 | (if (eq? default &no-default-value) | |
258 | (let ((location (source-properties->location location))) | |
259 | (raise | |
d51bfe24 LC |
260 | (make-compound-condition |
261 | (condition | |
262 | (&missing-value-service-error (type type) (location location))) | |
263 | (formatted-message (G_ "~a: no value specified \ | |
1bb895ea | 264 | for service of type '~a'") |
d51bfe24 LC |
265 | (location->string location) |
266 | (service-type-name type))))) | |
1bb895ea LC |
267 | (service type default)))) |
268 | ||
269 | (define-condition-type &service-error &error | |
270 | service-error?) | |
271 | ||
272 | (define-condition-type &missing-value-service-error &service-error | |
273 | missing-value-service-error? | |
274 | (type missing-value-service-error-type) | |
275 | (location missing-value-service-error-location)) | |
276 | ||
277 | ||
278 | \f | |
279 | ;;; | |
280 | ;;; Helpers. | |
281 | ;;; | |
282 | ||
efe7d19a LC |
283 | (define service-parameters |
284 | ;; Deprecated alias. | |
285 | service-value) | |
0adfe95a | 286 | |
71654dfd LC |
287 | (define (simple-service name target value) |
288 | "Return a service that extends TARGET with VALUE. This works by creating a | |
289 | singleton service type NAME, of which the returned service is an instance." | |
290 | (let* ((extension (service-extension target identity)) | |
291 | (type (service-type (name name) | |
292 | (extensions (list extension))))) | |
293 | (service type value))) | |
0adfe95a | 294 | |
cd6f6c22 | 295 | (define-syntax %modify-service |
a247f5c7 RW |
296 | (syntax-rules (=> delete) |
297 | ((_ svc (delete kind) clauses ...) | |
298 | (if (eq? (service-kind svc) kind) | |
299 | #f | |
300 | (%modify-service svc clauses ...))) | |
cd6f6c22 LC |
301 | ((_ service) |
302 | service) | |
303 | ((_ svc (kind param => exp ...) clauses ...) | |
304 | (if (eq? (service-kind svc) kind) | |
efe7d19a | 305 | (let ((param (service-value svc))) |
cd6f6c22 LC |
306 | (service (service-kind svc) |
307 | (begin exp ...))) | |
308 | (%modify-service svc clauses ...))))) | |
309 | ||
310 | (define-syntax modify-services | |
311 | (syntax-rules () | |
4d343a14 CM |
312 | "Modify the services listed in SERVICES according to CLAUSES and return |
313 | the resulting list of services. Each clause must have the form: | |
cd6f6c22 LC |
314 | |
315 | (TYPE VARIABLE => BODY) | |
316 | ||
317 | where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an | |
318 | identifier that is bound within BODY to the value of the service of that | |
319 | TYPE. Consider this example: | |
320 | ||
321 | (modify-services %base-services | |
322 | (guix-service-type config => | |
323 | (guix-configuration | |
324 | (inherit config) | |
325 | (use-substitutes? #f) | |
326 | (extra-options '(\"--gc-keep-derivations\")))) | |
327 | (mingetty-service-type config => | |
328 | (mingetty-configuration | |
329 | (inherit config) | |
a247f5c7 RW |
330 | (motd (plain-file \"motd\" \"Hi there!\")))) |
331 | (delete udev-service-type)) | |
cd6f6c22 LC |
332 | |
333 | It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of | |
a247f5c7 RW |
334 | all the MINGETTY-SERVICE-TYPE instances, and it deletes instances of the |
335 | UDEV-SERVICE-TYPE. | |
cd6f6c22 | 336 | |
a247f5c7 | 337 | This is a shorthand for (filter-map (lambda (svc) ...) %base-services)." |
cd6f6c22 | 338 | ((_ services clauses ...) |
a247f5c7 RW |
339 | (filter-map (lambda (service) |
340 | (%modify-service service clauses ...)) | |
341 | services)))) | |
0adfe95a LC |
342 | |
343 | \f | |
344 | ;;; | |
345 | ;;; Core services. | |
346 | ;;; | |
347 | ||
0e5c2d5e | 348 | (define (system-derivation entries mextensions) |
d62e201c LC |
349 | "Return as a monadic value the derivation of the 'system' directory |
350 | containing the given entries." | |
0e5c2d5e | 351 | (mlet %store-monad ((extensions (mapm/accumulate-builds identity |
223ede4e | 352 | mextensions))) |
d62e201c LC |
353 | (lower-object |
354 | (file-union "system" | |
355 | (append entries (concatenate extensions)))))) | |
356 | ||
357 | (define system-service-type | |
358 | ;; This is the ultimate service type, the root of the service DAG. The | |
359 | ;; service of this type is extended by monadic name/item pairs. These items | |
360 | ;; end up in the "system directory" as returned by | |
361 | ;; 'operating-system-derivation'. | |
362 | (service-type (name 'system) | |
363 | (extensions '()) | |
364 | (compose identity) | |
636bb2b5 LC |
365 | (extend system-derivation) |
366 | (description | |
367 | "Build the operating system top-level directory, which in | |
368 | turn refers to everything the operating system needs: its kernel, initrd, | |
369 | system profile, boot script, and so on."))) | |
d62e201c | 370 | |
378daa8c LC |
371 | (define (compute-boot-script _ gexps) |
372 | ;; Reverse GEXPS so that extensions appear in the boot script in the right | |
661c237b LC |
373 | ;; order. That is, user extensions would come first, and extensions added |
374 | ;; by 'essential-services' (e.g., running shepherd) are guaranteed to come | |
375 | ;; last. | |
378daa8c LC |
376 | (gexp->file "boot" |
377 | ;; Clean up and activate the system, then spawn shepherd. | |
378 | #~(begin #$@(reverse gexps)))) | |
0adfe95a | 379 | |
d62e201c LC |
380 | (define (boot-script-entry mboot) |
381 | "Return, as a monadic value, an entry for the boot script in the system | |
382 | directory." | |
383 | (mlet %store-monad ((boot mboot)) | |
384 | (return `(("boot" ,boot))))) | |
385 | ||
0adfe95a | 386 | (define boot-service-type |
378daa8c LC |
387 | ;; The service of this type is extended by being passed gexps. It |
388 | ;; aggregates them in a single script, as a monadic value, which becomes its | |
389 | ;; value. | |
0adfe95a | 390 | (service-type (name 'boot) |
d62e201c LC |
391 | (extensions |
392 | (list (service-extension system-service-type | |
393 | boot-script-entry))) | |
7874e9e0 | 394 | (compose identity) |
636bb2b5 LC |
395 | (extend compute-boot-script) |
396 | (description | |
397 | "Produce the operating system's boot script, which is spawned | |
398 | by the initrd once the root file system is mounted."))) | |
0adfe95a LC |
399 | |
400 | (define %boot-service | |
d62e201c | 401 | ;; The service that produces the boot script. |
0adfe95a | 402 | (service boot-service-type #t)) |
be7be9e8 | 403 | |
33b7cb7a LC |
404 | \f |
405 | ;;; | |
406 | ;;; Provenance tracking. | |
407 | ;;; | |
408 | ||
409 | (define (object->pretty-string obj) | |
410 | "Like 'object->string', but using 'pretty-print'." | |
411 | (call-with-output-string | |
412 | (lambda (port) | |
413 | (pretty-print obj port)))) | |
414 | ||
415 | (define (channel->code channel) | |
416 | "Return code to build CHANNEL, ready to be dropped in a 'channels.scm' | |
417 | file." | |
eb5cf39e LC |
418 | ;; Since the 'introduction' field is backward-incompatible, and since it's |
419 | ;; optional when using the "official" 'guix channel, include it if and only | |
420 | ;; if we're referring to a different channel. | |
421 | (let ((intro (and (not (equal? (list channel) %default-channels)) | |
422 | (channel-introduction channel)))) | |
423 | `(channel (name ',(channel-name channel)) | |
424 | (url ,(channel-url channel)) | |
425 | (branch ,(channel-branch channel)) | |
426 | (commit ,(channel-commit channel)) | |
427 | ,@(if intro | |
428 | `((introduction | |
429 | (make-channel-introduction | |
430 | ,(channel-introduction-first-signed-commit intro) | |
431 | (openpgp-fingerprint | |
432 | ,(openpgp-format-fingerprint | |
433 | (channel-introduction-first-commit-signer | |
434 | intro)))))) | |
435 | '())))) | |
33b7cb7a LC |
436 | |
437 | (define (channel->sexp channel) | |
438 | "Return an sexp describing CHANNEL. The sexp is _not_ code and is meant to | |
439 | be parsed by tools; it's potentially more future-proof than code." | |
eb5cf39e LC |
440 | ;; TODO: Add CHANNEL's introduction. Currently we can't do that because |
441 | ;; older 'guix system describe' expect exactly name/url/branch/commit | |
442 | ;; without any additional fields. | |
33b7cb7a LC |
443 | `(channel (name ,(channel-name channel)) |
444 | (url ,(channel-url channel)) | |
445 | (branch ,(channel-branch channel)) | |
446 | (commit ,(channel-commit channel)))) | |
447 | ||
b91a73a6 LC |
448 | (define (sexp->channel sexp) |
449 | "Return the channel corresponding to SEXP, an sexp as found in the | |
450 | \"provenance\" file produced by 'provenance-service-type'." | |
451 | (match sexp | |
452 | (('channel ('name name) | |
453 | ('url url) | |
454 | ('branch branch) | |
455 | ('commit commit) | |
456 | rest ...) | |
457 | ;; XXX: In the future REST may include a channel introduction. | |
458 | (channel (name name) (url url) | |
459 | (branch branch) (commit commit))))) | |
460 | ||
33b7cb7a LC |
461 | (define (provenance-file channels config-file) |
462 | "Return a 'provenance' file describing CHANNELS, a list of channels, and | |
463 | CONFIG-FILE, which can be either #f or a <local-file> containing the OS | |
464 | configuration being used." | |
465 | (scheme-file "provenance" | |
466 | #~(provenance | |
467 | (version 0) | |
468 | (channels #+@(if channels | |
469 | (map channel->sexp channels) | |
470 | '())) | |
471 | (configuration-file #+config-file)))) | |
472 | ||
473 | (define (provenance-entry config-file) | |
474 | "Return system entries describing the operating system provenance: the | |
475 | channels in use and CONFIG-FILE, if it is true." | |
476 | (define profile | |
477 | (current-profile)) | |
478 | ||
479 | (define channels | |
480 | (and=> profile profile-channels)) | |
481 | ||
482 | (mbegin %store-monad | |
483 | (let ((config-file (cond ((string? config-file) | |
5dbfdf8b LC |
484 | ;; CONFIG-FILE has been passed typically via |
485 | ;; 'guix system reconfigure CONFIG-FILE' so we | |
486 | ;; can assume it's valid: tell 'local-file' to | |
487 | ;; not emit a warning. | |
488 | (local-file (assume-valid-file-name config-file) | |
489 | "configuration.scm")) | |
33b7cb7a LC |
490 | ((not config-file) |
491 | #f) | |
492 | (else | |
493 | config-file)))) | |
494 | (return `(("provenance" ,(provenance-file channels config-file)) | |
495 | ,@(if channels | |
496 | `(("channels.scm" | |
497 | ,(plain-file "channels.scm" | |
498 | (object->pretty-string | |
499 | `(list | |
500 | ,@(map channel->code channels)))))) | |
501 | '()) | |
502 | ,@(if config-file | |
503 | `(("configuration.scm" ,config-file)) | |
504 | '())))))) | |
505 | ||
506 | (define provenance-service-type | |
507 | (service-type (name 'provenance) | |
508 | (extensions | |
509 | (list (service-extension system-service-type | |
510 | provenance-entry))) | |
511 | (default-value #f) ;the OS config file | |
512 | (description | |
513 | "Store provenance information about the system in the system | |
514 | itself: the channels used when building the system, and its configuration | |
515 | file, when available."))) | |
516 | ||
a396dd01 LC |
517 | (define (sexp->system-provenance sexp) |
518 | "Parse SEXP, an s-expression read from /run/current-system/provenance or | |
519 | similar, and return two values: the list of channels listed therein, and the | |
520 | OS configuration file or #f." | |
521 | (match sexp | |
522 | (('provenance ('version 0) | |
523 | ('channels channels ...) | |
524 | ('configuration-file config-file)) | |
525 | (values (map sexp->channel channels) | |
526 | config-file)) | |
527 | (_ | |
528 | (values '() #f)))) | |
529 | ||
b91a73a6 LC |
530 | (define (system-provenance system) |
531 | "Given SYSTEM, the file name of a system generation, return two values: the | |
532 | list of channels SYSTEM is built from, and its configuration file. If that | |
533 | information is missing, return the empty list (for channels) and possibly | |
534 | #false (for the configuration file)." | |
535 | (catch 'system-error | |
536 | (lambda () | |
a396dd01 LC |
537 | (sexp->system-provenance |
538 | (call-with-input-file (string-append system "/provenance") | |
539 | read))) | |
b91a73a6 LC |
540 | (lambda _ |
541 | (values '() #f)))) | |
33b7cb7a LC |
542 | \f |
543 | ;;; | |
544 | ;;; Cleanup. | |
545 | ;;; | |
546 | ||
be7be9e8 | 547 | (define (cleanup-gexp _) |
378daa8c LC |
548 | "Return a gexp to clean up /tmp and similar places upon boot." |
549 | (with-imported-modules '((guix build utils)) | |
550 | #~(begin | |
551 | (use-modules (guix build utils)) | |
552 | ||
553 | ;; Clean out /tmp and /var/run. | |
554 | ;; | |
555 | ;; XXX This needs to happen before service activations, so it | |
556 | ;; has to be here, but this also implicitly assumes that /tmp | |
557 | ;; and /var/run are on the root partition. | |
558 | (letrec-syntax ((fail-safe (syntax-rules () | |
559 | ((_ exp rest ...) | |
560 | (begin | |
561 | (catch 'system-error | |
562 | (lambda () exp) | |
563 | (const #f)) | |
564 | (fail-safe rest ...))) | |
565 | ((_) | |
566 | #t)))) | |
567 | ;; Ignore I/O errors so the system can boot. | |
568 | (fail-safe | |
569 | ;; Remove stale Shadow lock files as they would lead to | |
570 | ;; failures of 'useradd' & co. | |
571 | (delete-file "/etc/group.lock") | |
572 | (delete-file "/etc/passwd.lock") | |
573 | (delete-file "/etc/.pwd.lock") ;from 'lckpwdf' | |
574 | ||
575 | ;; Force file names to be decoded as UTF-8. See | |
576 | ;; <https://bugs.gnu.org/26353>. | |
577 | (setenv "GUIX_LOCPATH" | |
578 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
579 | (setlocale LC_CTYPE "en_US.utf8") | |
580 | (delete-file-recursively "/tmp") | |
581 | (delete-file-recursively "/var/run") | |
582 | ||
583 | (mkdir "/tmp") | |
584 | (chmod "/tmp" #o1777) | |
585 | (mkdir "/var/run") | |
586 | (chmod "/var/run" #o755) | |
587 | (delete-file-recursively "/run/udev/watch.old")))))) | |
be7be9e8 LC |
588 | |
589 | (define cleanup-service-type | |
590 | ;; Service that cleans things up in /tmp and similar. | |
591 | (service-type (name 'cleanup) | |
592 | (extensions | |
593 | (list (service-extension boot-service-type | |
636bb2b5 LC |
594 | cleanup-gexp))) |
595 | (description | |
596 | "Delete files from @file{/tmp}, @file{/var/run}, and other | |
597 | temporary locations at boot time."))) | |
0adfe95a | 598 | |
0adfe95a LC |
599 | (define* (activation-service->script service) |
600 | "Return as a monadic value the activation script for SERVICE, a service of | |
601 | ACTIVATION-SCRIPT-TYPE." | |
efe7d19a | 602 | (activation-script (service-value service))) |
0adfe95a LC |
603 | |
604 | (define (activation-script gexps) | |
605 | "Return the system's activation script, which evaluates GEXPS." | |
378daa8c | 606 | (define actions |
03cbd94d JK |
607 | (map (cut program-file "activate-service.scm" <>) gexps)) |
608 | ||
609 | (program-file "activate.scm" | |
610 | (with-imported-modules (source-module-closure | |
611 | '((gnu build activation) | |
612 | (guix build utils))) | |
dac7dd1b MO |
613 | #~(begin |
614 | (use-modules (gnu build activation) | |
615 | (guix build utils)) | |
616 | ||
617 | ;; Make sure the user accounting database exists. If it | |
618 | ;; does not exist, 'setutxent' does not create it and | |
619 | ;; thus there is no accounting at all. | |
620 | (close-port (open-file "/var/run/utmpx" "a0")) | |
621 | ||
622 | ;; Same for 'wtmp', which is populated by mingetty et | |
623 | ;; al. | |
624 | (mkdir-p "/var/log") | |
625 | (close-port (open-file "/var/log/wtmp" "a0")) | |
626 | ||
627 | ;; Set up /run/current-system. Among other things this | |
628 | ;; sets up locales, which the activation snippets | |
629 | ;; executed below may expect. | |
630 | (activate-current-system) | |
631 | ||
632 | ;; Run the services' activation snippets. | |
633 | ;; TODO: Use 'load-compiled'. | |
634 | (for-each primitive-load '#$actions))))) | |
0adfe95a LC |
635 | |
636 | (define (gexps->activation-gexp gexps) | |
637 | "Return a gexp that runs the activation script containing GEXPS." | |
378daa8c | 638 | #~(primitive-load #$(activation-script gexps))) |
0adfe95a | 639 | |
df138dc2 BW |
640 | (define (activation-profile-entry gexps) |
641 | "Return, as a monadic value, an entry for the activation script in the | |
642 | system directory." | |
643 | (mlet %store-monad ((activate (lower-object (activation-script gexps)))) | |
644 | (return `(("activate" ,activate))))) | |
645 | ||
3a391e68 LC |
646 | (define (second-argument a b) b) |
647 | ||
0adfe95a LC |
648 | (define activation-service-type |
649 | (service-type (name 'activate) | |
650 | (extensions | |
651 | (list (service-extension boot-service-type | |
df138dc2 BW |
652 | gexps->activation-gexp) |
653 | (service-extension system-service-type | |
654 | activation-profile-entry))) | |
7874e9e0 | 655 | (compose identity) |
636bb2b5 LC |
656 | (extend second-argument) |
657 | (description | |
658 | "Run @dfn{activation} code at boot time and upon | |
659 | @command{guix system reconfigure} completion."))) | |
0adfe95a LC |
660 | |
661 | (define %activation-service | |
662 | ;; The activation service produces the activation script from the gexps it | |
663 | ;; receives. | |
664 | (service activation-service-type #t)) | |
665 | ||
a241a7ac LC |
666 | (define %modprobe-wrapper |
667 | ;; Wrapper for the 'modprobe' command that knows where modules live. | |
668 | ;; | |
669 | ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe', | |
670 | ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' | |
671 | ;; environment variable is not set---hence the need for this wrapper. | |
672 | (let ((modprobe "/run/current-system/profile/bin/modprobe")) | |
673 | (program-file "modprobe" | |
674 | #~(begin | |
675 | (setenv "LINUX_MODULE_DIRECTORY" | |
676 | "/run/booted-system/kernel/lib/modules") | |
8c88e242 BW |
677 | ;; FIXME: Remove this crutch when the patch #40422, |
678 | ;; updating to kmod 27 is merged. | |
679 | (setenv "MODPROBE_OPTIONS" | |
680 | "-C /etc/modprobe.d") | |
a241a7ac LC |
681 | (apply execl #$modprobe |
682 | (cons #$modprobe (cdr (command-line)))))))) | |
683 | ||
684 | (define %linux-kernel-activation | |
685 | ;; Activation of the Linux kernel running on the bare metal (as opposed to | |
686 | ;; running in a container.) | |
687 | #~(begin | |
688 | ;; Tell the kernel to use our 'modprobe' command. | |
689 | (activate-modprobe #$%modprobe-wrapper) | |
690 | ||
691 | ;; Let users debug their own processes! | |
692 | (activate-ptrace-attach))) | |
693 | ||
a241a7ac LC |
694 | (define %linux-bare-metal-service |
695 | ;; The service that does things that are needed on the "bare metal", but not | |
696 | ;; necessary or impossible in a container. | |
6ddf4fcf LC |
697 | (simple-service 'linux-bare-metal |
698 | activation-service-type | |
699 | %linux-kernel-activation)) | |
700 | ||
68d8c094 JN |
701 | (define %hurd-rc-script |
702 | ;; The RC script to be started upon boot. | |
703 | (program-file "rc" | |
704 | (with-imported-modules (source-module-closure | |
705 | '((guix build utils) | |
706 | (gnu build hurd-boot) | |
707 | (guix build syscalls))) | |
708 | #~(begin | |
709 | (use-modules (guix build utils) | |
710 | (gnu build hurd-boot) | |
711 | (guix build syscalls) | |
712 | (ice-9 match) | |
713 | (system repl repl) | |
714 | (srfi srfi-1) | |
715 | (srfi srfi-26)) | |
716 | (boot-hurd-system))))) | |
717 | ||
718 | (define (hurd-rc-entry rc) | |
719 | "Return, as a monadic value, an entry for the RC script in the system | |
720 | directory." | |
721 | (mlet %store-monad ((rc (lower-object rc))) | |
722 | (return `(("rc" ,rc))))) | |
723 | ||
724 | (define hurd-startup-service-type | |
725 | ;; The service that creates the initial SYSTEM/rc startup file. | |
726 | (service-type (name 'startup) | |
727 | (extensions | |
728 | (list (service-extension system-service-type hurd-rc-entry))) | |
729 | (default-value %hurd-rc-script))) | |
730 | ||
731 | (define %hurd-startup-service | |
732 | ;; The service that produces the RC script. | |
733 | (service hurd-startup-service-type %hurd-rc-script)) | |
a241a7ac | 734 | |
387e1754 LC |
735 | (define special-files-service-type |
736 | ;; Service to install "special files" such as /bin/sh and /usr/bin/env. | |
737 | (service-type | |
738 | (name 'special-files) | |
739 | (extensions | |
740 | (list (service-extension activation-service-type | |
741 | (lambda (files) | |
742 | #~(activate-special-files '#$files))))) | |
743 | (compose concatenate) | |
636bb2b5 LC |
744 | (extend append) |
745 | (description | |
746 | "Add special files to the root file system---e.g., | |
747 | @file{/usr/bin/env}."))) | |
387e1754 LC |
748 | |
749 | (define (extra-special-file file target) | |
750 | "Use TARGET as the \"special file\" FILE. For example, TARGET might be | |
751 | (file-append coreutils \"/bin/env\") | |
752 | and FILE could be \"/usr/bin/env\"." | |
753 | (simple-service (string->symbol (string-append "special-file-" file)) | |
754 | special-files-service-type | |
755 | `((,file ,target)))) | |
756 | ||
0adfe95a LC |
757 | (define (etc-directory service) |
758 | "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE." | |
efe7d19a | 759 | (files->etc-directory (service-value service))) |
0adfe95a LC |
760 | |
761 | (define (files->etc-directory files) | |
a322e9d1 LC |
762 | (define (assert-no-duplicates files) |
763 | (let loop ((files files) | |
764 | (seen (set))) | |
765 | (match files | |
766 | (() #t) | |
767 | (((file _) rest ...) | |
768 | (when (set-contains? seen file) | |
d51bfe24 LC |
769 | (raise (formatted-message (G_ "duplicate '~a' entry for /etc") |
770 | file))) | |
a322e9d1 LC |
771 | (loop rest (set-insert file seen)))))) |
772 | ||
773 | ;; Detect duplicates early instead of letting them through, eventually | |
774 | ;; leading to a build failure of "etc.drv". | |
775 | (assert-no-duplicates files) | |
776 | ||
0adfe95a LC |
777 | (file-union "etc" files)) |
778 | ||
d62e201c LC |
779 | (define (etc-entry files) |
780 | "Return an entry for the /etc directory consisting of FILES in the system | |
781 | directory." | |
782 | (with-monad %store-monad | |
783 | (return `(("etc" ,(files->etc-directory files)))))) | |
784 | ||
0adfe95a LC |
785 | (define etc-service-type |
786 | (service-type (name 'etc) | |
787 | (extensions | |
788 | (list | |
789 | (service-extension activation-service-type | |
790 | (lambda (files) | |
791 | (let ((etc | |
792 | (files->etc-directory files))) | |
d62e201c LC |
793 | #~(activate-etc #$etc)))) |
794 | (service-extension system-service-type etc-entry))) | |
0adfe95a | 795 | (compose concatenate) |
636bb2b5 LC |
796 | (extend append) |
797 | (description "Populate the @file{/etc} directory."))) | |
0adfe95a LC |
798 | |
799 | (define (etc-service files) | |
800 | "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES. | |
801 | FILES must be a list of name/file-like object pairs." | |
802 | (service etc-service-type files)) | |
803 | ||
804 | (define setuid-program-service-type | |
805 | (service-type (name 'setuid-program) | |
806 | (extensions | |
807 | (list (service-extension activation-service-type | |
808 | (lambda (programs) | |
809 | #~(activate-setuid-programs | |
810 | (list #$@programs)))))) | |
811 | (compose concatenate) | |
636bb2b5 LC |
812 | (extend append) |
813 | (description | |
814 | "Populate @file{/run/setuid-programs} with the specified | |
815 | executables, making them setuid-root."))) | |
0adfe95a | 816 | |
af4c3fd5 LC |
817 | (define (packages->profile-entry packages) |
818 | "Return a system entry for the profile containing PACKAGES." | |
07a33b1d LC |
819 | ;; XXX: 'mlet' is needed here for one reason: to get the proper |
820 | ;; '%current-target' and '%current-target-system' bindings when | |
821 | ;; 'packages->manifest' is called, and thus when the 'package-inputs' | |
822 | ;; etc. procedures are called on PACKAGES. That way, conditionals in those | |
823 | ;; inputs see the "correct" value of these two parameters. See | |
824 | ;; <https://issues.guix.gnu.org/44952>. | |
825 | (mlet %store-monad ((_ (current-target-system))) | |
45bd9133 LC |
826 | (return `(("profile" ,(profile |
827 | (content (packages->manifest | |
828 | (delete-duplicates packages eq?))))))))) | |
af4c3fd5 LC |
829 | |
830 | (define profile-service-type | |
831 | ;; The service that populates the system's profile---i.e., | |
832 | ;; /run/current-system/profile. It is extended by package lists. | |
833 | (service-type (name 'profile) | |
834 | (extensions | |
835 | (list (service-extension system-service-type | |
836 | packages->profile-entry))) | |
837 | (compose concatenate) | |
636bb2b5 LC |
838 | (extend append) |
839 | (description | |
840 | "This is the @dfn{system profile}, available as | |
841 | @file{/run/current-system/profile}. It contains packages that the sysadmin | |
842 | wants to be globally available to all the system users."))) | |
af4c3fd5 | 843 | |
0adfe95a LC |
844 | (define (firmware->activation-gexp firmware) |
845 | "Return a gexp to make the packages listed in FIRMWARE loadable by the | |
846 | kernel." | |
847 | (let ((directory (directory-union "firmware" firmware))) | |
848 | ;; Tell the kernel where firmware is. | |
849 | #~(activate-firmware (string-append #$directory "/lib/firmware")))) | |
850 | ||
851 | (define firmware-service-type | |
852 | ;; The service that collects firmware. | |
853 | (service-type (name 'firmware) | |
854 | (extensions | |
855 | (list (service-extension activation-service-type | |
856 | firmware->activation-gexp))) | |
857 | (compose concatenate) | |
636bb2b5 LC |
858 | (extend append) |
859 | (description | |
860 | "Make ``firmware'' files loadable by the operating system | |
861 | kernel. Firmware may then be uploaded to some of the machine's devices, such | |
862 | as Wifi cards."))) | |
0adfe95a | 863 | |
e0b47290 LC |
864 | (define (gc-roots->system-entry roots) |
865 | "Return an entry in the system's output containing symlinks to ROOTS." | |
866 | (mlet %store-monad ((entry (gexp->derivation | |
867 | "gc-roots" | |
868 | #~(let ((roots '#$roots)) | |
869 | (mkdir #$output) | |
870 | (chdir #$output) | |
871 | (for-each symlink | |
872 | roots | |
873 | (map number->string | |
874 | (iota (length roots)))))))) | |
875 | (return (if (null? roots) | |
876 | '() | |
877 | `(("gc-roots" ,entry)))))) | |
878 | ||
879 | (define gc-root-service-type | |
880 | ;; A service to associate extra garbage-collector roots to the system. This | |
881 | ;; is a simple hack that guarantees that the system retains references to | |
882 | ;; the given list of roots. Roots must be "lowerable" objects like | |
883 | ;; packages, or derivations. | |
884 | (service-type (name 'gc-roots) | |
885 | (extensions | |
886 | (list (service-extension system-service-type | |
887 | gc-roots->system-entry))) | |
888 | (compose concatenate) | |
636bb2b5 LC |
889 | (extend append) |
890 | (description | |
891 | "Register garbage-collector roots---i.e., store items that | |
dc5729a8 LC |
892 | will not be reclaimed by the garbage collector.") |
893 | (default-value '()))) | |
e0b47290 | 894 | |
a3df3825 | 895 | ;; Configuration for the Linux kernel builder. |
896 | (define-record-type* <linux-builder-configuration> | |
897 | linux-builder-configuration | |
898 | make-linux-builder-configuration | |
899 | linux-builder-configuration? | |
900 | this-linux-builder-configuration | |
901 | ||
902 | (kernel linux-builder-configuration-kernel) ; package | |
903 | (modules linux-builder-configuration-modules (default '()))) ; list of packages | |
904 | ||
905 | (define (package-for-kernel target-kernel module-package) | |
906 | "Return a package like MODULE-PACKAGE, adapted for TARGET-KERNEL, if | |
907 | possible (that is if there's a LINUX keyword argument in the build system)." | |
908 | (package | |
909 | (inherit module-package) | |
910 | (arguments | |
911 | (substitute-keyword-arguments (package-arguments module-package) | |
912 | ((#:linux kernel #f) | |
913 | target-kernel))))) | |
914 | ||
915 | (define (linux-builder-configuration->system-entry config) | |
916 | "Return the kernel entry of the 'system' directory." | |
917 | (let* ((kernel (linux-builder-configuration-kernel config)) | |
918 | (modules (linux-builder-configuration-modules config)) | |
919 | (kernel (profile | |
920 | (content (packages->manifest | |
921 | (cons kernel | |
922 | (map (lambda (module) | |
923 | (cond | |
924 | ((package? module) | |
925 | (package-for-kernel kernel module)) | |
926 | ;; support (,package "kernel-module-output") | |
927 | ((and (list? module) (package? (car module))) | |
928 | (cons (package-for-kernel kernel | |
929 | (car module)) | |
930 | (cdr module))) | |
931 | (else | |
932 | module))) | |
933 | modules)))) | |
934 | (hooks (list linux-module-database))))) | |
935 | (with-monad %store-monad | |
936 | (return `(("kernel" ,kernel)))))) | |
937 | ||
938 | (define linux-builder-service-type | |
939 | (service-type (name 'linux-builder) | |
940 | (extensions | |
941 | (list (service-extension system-service-type | |
942 | linux-builder-configuration->system-entry))) | |
943 | (default-value '()) | |
944 | (compose identity) | |
945 | (extend (lambda (config modifiers) | |
946 | (if (null? modifiers) | |
947 | config | |
948 | ((apply compose modifiers) config)))) | |
949 | (description "Builds the linux-libre kernel profile, containing | |
950 | the kernel itself and any linux-loadable kernel modules. This can be extended | |
951 | with a function that accepts the current configuration and returns a new | |
952 | configuration."))) | |
953 | ||
954 | (define (linux-loadable-module-builder-modifier modules) | |
955 | "Extends linux-builder-service-type by appending the given MODULES to the | |
956 | configuration of linux-builder-service-type." | |
957 | (lambda (config) | |
958 | (linux-builder-configuration | |
959 | (inherit config) | |
960 | (modules (append (linux-builder-configuration-modules config) | |
961 | modules))))) | |
962 | ||
963 | (define linux-loadable-module-service-type | |
964 | (service-type (name 'linux-loadable-modules) | |
965 | (extensions | |
966 | (list (service-extension linux-builder-service-type | |
967 | linux-loadable-module-builder-modifier))) | |
968 | (default-value '()) | |
969 | (compose concatenate) | |
970 | (extend append) | |
971 | (description "Adds packages and package outputs as modules | |
972 | included in the booted linux-libre profile. Other services can extend this | |
973 | service type to add particular modules to the set of linux-loadable modules."))) | |
974 | ||
975 | ||
0adfe95a LC |
976 | \f |
977 | ;;; | |
978 | ;;; Service folding. | |
979 | ;;; | |
980 | ||
0adfe95a LC |
981 | (define-condition-type &missing-target-service-error &service-error |
982 | missing-target-service-error? | |
983 | (service missing-target-service-error-service) | |
984 | (target-type missing-target-service-error-target-type)) | |
985 | ||
986 | (define-condition-type &ambiguous-target-service-error &service-error | |
987 | ambiguous-target-service-error? | |
988 | (service ambiguous-target-service-error-service) | |
989 | (target-type ambiguous-target-service-error-target-type)) | |
990 | ||
d466b1fc LC |
991 | (define (missing-target-error service target-type) |
992 | (raise | |
993 | (condition (&missing-target-service-error | |
994 | (service service) | |
995 | (target-type target-type)) | |
996 | (&message | |
997 | (message | |
998 | (format #f (G_ "no target of type '~a' for service '~a'") | |
999 | (service-type-name target-type) | |
1000 | (service-type-name | |
1001 | (service-kind service)))))))) | |
1002 | ||
0adfe95a LC |
1003 | (define (service-back-edges services) |
1004 | "Return a procedure that, when passed a <service>, returns the list of | |
1005 | <service> objects that depend on it." | |
1006 | (define (add-edges service edges) | |
1007 | (define (add-edge extension edges) | |
1008 | (let ((target-type (service-extension-target extension))) | |
1009 | (match (filter (lambda (service) | |
1010 | (eq? (service-kind service) target-type)) | |
1011 | services) | |
1012 | ((target) | |
1013 | (vhash-consq target service edges)) | |
1014 | (() | |
d466b1fc | 1015 | (missing-target-error service target-type)) |
0adfe95a LC |
1016 | (x |
1017 | (raise | |
1018 | (condition (&ambiguous-target-service-error | |
1019 | (service service) | |
1020 | (target-type target-type)) | |
1021 | (&message | |
1022 | (message | |
1023 | (format #f | |
69daee23 | 1024 | (G_ "more than one target service of type '~a'") |
0adfe95a LC |
1025 | (service-type-name target-type)))))))))) |
1026 | ||
1027 | (fold add-edge edges (service-type-extensions (service-kind service)))) | |
1028 | ||
1029 | (let ((edges (fold add-edges vlist-null services))) | |
1030 | (lambda (node) | |
1031 | (reverse (vhash-foldq* cons '() node edges))))) | |
1032 | ||
d466b1fc LC |
1033 | (define (instantiate-missing-services services) |
1034 | "Return SERVICES, a list, augmented with any services targeted by extensions | |
1035 | and missing from SERVICES. Only service types with a default value can be | |
1036 | instantiated; other missing services lead to a | |
1037 | '&missing-target-service-error'." | |
1038 | (define (adjust-service-list svc result instances) | |
1039 | (fold2 (lambda (extension result instances) | |
1040 | (define target-type | |
1041 | (service-extension-target extension)) | |
1042 | ||
1043 | (match (vhash-assq target-type instances) | |
1044 | (#f | |
1045 | (let ((default (service-type-default-value target-type))) | |
1046 | (if (eq? &no-default-value default) | |
1047 | (missing-target-error svc target-type) | |
1048 | (let ((new (service target-type))) | |
1049 | (values (cons new result) | |
1050 | (vhash-consq target-type new instances)))))) | |
1051 | (_ | |
1052 | (values result instances)))) | |
1053 | result | |
1054 | instances | |
1055 | (service-type-extensions (service-kind svc)))) | |
1056 | ||
9b6c4355 LC |
1057 | (let loop ((services services)) |
1058 | (define instances | |
1059 | (fold (lambda (service result) | |
1060 | (vhash-consq (service-kind service) service | |
1061 | result)) | |
1062 | vlist-null services)) | |
1063 | ||
1064 | (define adjusted | |
1065 | (fold2 adjust-service-list | |
1066 | services instances | |
1067 | services)) | |
1068 | ||
1069 | ;; If we instantiated services, they might in turn depend on missing | |
1070 | ;; services. Loop until we've reached fixed point. | |
1071 | (if (= (length adjusted) (vlist-length instances)) | |
1072 | adjusted | |
1073 | (loop adjusted)))) | |
d466b1fc | 1074 | |
d62e201c LC |
1075 | (define* (fold-services services |
1076 | #:key (target-type system-service-type)) | |
0adfe95a LC |
1077 | "Fold SERVICES by propagating their extensions down to the root of type |
1078 | TARGET-TYPE; return the root service adjusted accordingly." | |
1079 | (define dependents | |
1080 | (service-back-edges services)) | |
1081 | ||
1082 | (define (matching-extension target) | |
1083 | (let ((target (service-kind target))) | |
1084 | (match-lambda | |
1085 | (($ <service-extension> type) | |
1086 | (eq? type target))))) | |
1087 | ||
1088 | (define (apply-extension target) | |
1089 | (lambda (service) | |
1090 | (match (find (matching-extension target) | |
1091 | (service-type-extensions (service-kind service))) | |
1092 | (($ <service-extension> _ compute) | |
efe7d19a | 1093 | (compute (service-value service)))))) |
0adfe95a LC |
1094 | |
1095 | (match (filter (lambda (service) | |
1096 | (eq? (service-kind service) target-type)) | |
1097 | services) | |
1098 | ((sink) | |
2a4309de LC |
1099 | ;; Use the state monad to keep track of already-visited services in the |
1100 | ;; graph and to memoize their value once folded. | |
1101 | (run-with-state | |
1102 | (let loop ((sink sink)) | |
1103 | (mlet %state-monad ((visited (current-state))) | |
1104 | (match (vhash-assq sink visited) | |
1105 | (#f | |
1106 | (mlet* %state-monad | |
1107 | ((dependents (mapm %state-monad loop (dependents sink))) | |
1108 | (visited (current-state)) | |
1109 | (extensions -> (map (apply-extension sink) dependents)) | |
1110 | (extend -> (service-type-extend (service-kind sink))) | |
1111 | (compose -> (service-type-compose (service-kind sink))) | |
1112 | (params -> (service-value sink)) | |
1113 | (service | |
1114 | -> | |
1115 | ;; Distinguish COMPOSE and EXTEND because PARAMS typically | |
1116 | ;; has a different type than the elements of EXTENSIONS. | |
1117 | (if extend | |
1118 | (service (service-kind sink) | |
1119 | (extend params (compose extensions))) | |
1120 | sink))) | |
1121 | (mbegin %state-monad | |
1122 | (set-current-state (vhash-consq sink service visited)) | |
1123 | (return service)))) | |
1124 | ((_ . service) ;SINK was already visited | |
1125 | (return service))))) | |
1126 | vlist-null)) | |
0adfe95a LC |
1127 | (() |
1128 | (raise | |
d51bfe24 LC |
1129 | (make-compound-condition |
1130 | (condition (&missing-target-service-error | |
1131 | (service #f) | |
1132 | (target-type target-type))) | |
1133 | (formatted-message (G_ "service of type '~a' not found") | |
1134 | (service-type-name target-type))))) | |
0adfe95a LC |
1135 | (x |
1136 | (raise | |
1137 | (condition (&ambiguous-target-service-error | |
1138 | (service #f) | |
1139 | (target-type target-type)) | |
1140 | (&message | |
1141 | (message | |
1142 | (format #f | |
69daee23 | 1143 | (G_ "more than one target service of type '~a'") |
0adfe95a | 1144 | (service-type-name target-type))))))))) |
db4fdc04 LC |
1145 | |
1146 | ;;; services.scm ends here. |