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