Commit | Line | Data |
---|---|---|
db4fdc04 | 1 | ;;; GNU Guix --- Functional package management for GNU |
caa78166 | 2 | ;;; Copyright © 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org> |
4d343a14 | 3 | ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com> |
db4fdc04 LC |
4 | ;;; |
5 | ;;; This file is part of GNU Guix. | |
6 | ;;; | |
7 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
8 | ;;; under the terms of the GNU General Public License as published by | |
9 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
10 | ;;; your option) any later version. | |
11 | ;;; | |
12 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
13 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;;; GNU General Public License for more details. | |
16 | ;;; | |
17 | ;;; You should have received a copy of the GNU General Public License | |
18 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
19 | ||
20 | (define-module (gnu services) | |
bebc8681 | 21 | #:use-module (guix gexp) |
0adfe95a LC |
22 | #:use-module (guix monads) |
23 | #:use-module (guix store) | |
db4fdc04 | 24 | #:use-module (guix records) |
af4c3fd5 | 25 | #:use-module (guix profiles) |
0c0c1b21 | 26 | #:use-module (guix discovery) |
0adfe95a LC |
27 | #:use-module (guix sets) |
28 | #:use-module (guix ui) | |
1bb895ea | 29 | #:use-module ((guix utils) #:select (source-properties->location)) |
232ccbef | 30 | #:use-module (guix modules) |
0adfe95a LC |
31 | #:use-module (gnu packages base) |
32 | #:use-module (gnu packages bash) | |
33 | #:use-module (srfi srfi-1) | |
34 | #:use-module (srfi srfi-9) | |
35 | #:use-module (srfi srfi-9 gnu) | |
36 | #:use-module (srfi srfi-26) | |
37 | #:use-module (srfi srfi-34) | |
38 | #:use-module (srfi srfi-35) | |
39 | #:use-module (ice-9 vlist) | |
40 | #:use-module (ice-9 match) | |
41 | #:export (service-extension | |
42 | service-extension? | |
7d8b5913 CB |
43 | service-extension-target |
44 | service-extension-compute | |
0adfe95a LC |
45 | |
46 | service-type | |
47 | service-type? | |
5152d13b LC |
48 | service-type-name |
49 | service-type-extensions | |
50 | service-type-compose | |
51 | service-type-extend | |
1bb895ea | 52 | service-type-default-value |
b714395a LC |
53 | service-type-description |
54 | service-type-location | |
55 | ||
0c0c1b21 LC |
56 | %service-type-path |
57 | fold-service-types | |
0adfe95a | 58 | |
db4fdc04 | 59 | service |
0adfe95a LC |
60 | service? |
61 | service-kind | |
efe7d19a LC |
62 | service-value |
63 | service-parameters ;deprecated | |
0adfe95a | 64 | |
71654dfd | 65 | simple-service |
cd6f6c22 | 66 | modify-services |
5152d13b | 67 | service-back-edges |
0adfe95a LC |
68 | fold-services |
69 | ||
70 | service-error? | |
1bb895ea LC |
71 | missing-value-service-error? |
72 | missing-value-service-error-type | |
73 | missing-value-service-error-location | |
0adfe95a LC |
74 | missing-target-service-error? |
75 | missing-target-service-error-service | |
76 | missing-target-service-error-target-type | |
77 | ambiguous-target-service-error? | |
78 | ambiguous-target-service-error-service | |
79 | ambiguous-target-service-error-target-type | |
80 | ||
d62e201c | 81 | system-service-type |
0adfe95a | 82 | boot-service-type |
be7be9e8 | 83 | cleanup-service-type |
0adfe95a LC |
84 | activation-service-type |
85 | activation-service->script | |
a241a7ac | 86 | %linux-bare-metal-service |
387e1754 LC |
87 | special-files-service-type |
88 | extra-special-file | |
0adfe95a LC |
89 | etc-service-type |
90 | etc-directory | |
91 | setuid-program-service-type | |
af4c3fd5 | 92 | profile-service-type |
0adfe95a | 93 | firmware-service-type |
e0b47290 | 94 | gc-root-service-type |
0adfe95a LC |
95 | |
96 | %boot-service | |
97 | %activation-service | |
98 | etc-service | |
99 | ||
94d2a250 LC |
100 | file-union ;XXX: for lack of a better place |
101 | directory-union)) | |
0adfe95a LC |
102 | |
103 | ;;; Comment: | |
104 | ;;; | |
105 | ;;; This module defines a broad notion of "service types" and "services." | |
db4fdc04 | 106 | ;;; |
0adfe95a LC |
107 | ;;; A service type describe how its instances extend instances of other |
108 | ;;; service types. For instance, some services extend the instance of | |
109 | ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create; | |
d4053c71 AK |
110 | ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of |
111 | ;;; <shepherd-service>. | |
0adfe95a LC |
112 | ;;; |
113 | ;;; When applicable, the service type defines how it can itself be extended, | |
114 | ;;; by providing one procedure to compose extensions, and one procedure to | |
115 | ;;; extend itself. | |
116 | ;;; | |
d62e201c LC |
117 | ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single |
118 | ;;; instance, which is the root of the service DAG. Its value is the | |
119 | ;;; derivation that produces the 'system' directory as returned by | |
120 | ;;; 'operating-system-derivation'. | |
0adfe95a LC |
121 | ;;; |
122 | ;;; The 'fold-services' procedure can be passed a list of procedures, which it | |
123 | ;;; "folds" by propagating extensions down the graph; it returns the root | |
124 | ;;; service after the applying all its extensions. | |
db4fdc04 LC |
125 | ;;; |
126 | ;;; Code: | |
127 | ||
0adfe95a LC |
128 | (define-record-type <service-extension> |
129 | (service-extension target compute) | |
130 | service-extension? | |
131 | (target service-extension-target) ;<service-type> | |
132 | (compute service-extension-compute)) ;params -> params | |
133 | ||
1bb895ea LC |
134 | (define &no-default-value |
135 | ;; Value used to denote service types that have no associated default value. | |
136 | '(no default value)) | |
137 | ||
0adfe95a LC |
138 | (define-record-type* <service-type> service-type make-service-type |
139 | service-type? | |
140 | (name service-type-name) ;symbol (for debugging) | |
141 | ||
142 | ;; Things extended by services of this type. | |
143 | (extensions service-type-extensions) ;list of <service-extensions> | |
144 | ||
145 | ;; Given a list of extensions, "compose" them. | |
146 | (compose service-type-compose ;list of Any -> Any | |
147 | (default #f)) | |
148 | ||
149 | ;; Extend the services' own parameters with the extension composition. | |
150 | (extend service-type-extend ;list of Any -> parameters | |
1bb895ea LC |
151 | (default #f)) |
152 | ||
153 | ;; Optional default value for instances of this type. | |
154 | (default-value service-type-default-value ;Any | |
b714395a LC |
155 | (default &no-default-value)) |
156 | ||
157 | ;; Meta-data. | |
158 | (description service-type-description ;string | |
159 | (default #f)) | |
160 | (location service-type-location ;<location> | |
161 | (default (and=> (current-source-location) | |
162 | source-properties->location)) | |
163 | (innate))) | |
0adfe95a LC |
164 | |
165 | (define (write-service-type type port) | |
166 | (format port "#<service-type ~a ~a>" | |
167 | (service-type-name type) | |
168 | (number->string (object-address type) 16))) | |
169 | ||
170 | (set-record-type-printer! <service-type> write-service-type) | |
171 | ||
0c0c1b21 LC |
172 | (define %distro-root-directory |
173 | ;; Absolute file name of the module hierarchy. | |
174 | (dirname (search-path %load-path "guix.scm"))) | |
175 | ||
176 | (define %service-type-path | |
177 | ;; Search path for service types. | |
178 | (make-parameter `((,%distro-root-directory . "gnu/services") | |
179 | (,%distro-root-directory . "gnu/system")))) | |
180 | ||
181 | (define* (fold-service-types proc seed | |
182 | #:optional | |
183 | (modules (all-modules (%service-type-path)))) | |
184 | "For each service type exported by one of MODULES, call (PROC RESULT). SEED | |
185 | is used as the initial value of RESULT." | |
186 | (fold-module-public-variables (lambda (object result) | |
187 | (if (service-type? object) | |
188 | (proc object result) | |
189 | result)) | |
190 | '() | |
191 | modules)) | |
192 | ||
0adfe95a LC |
193 | ;; Services of a given type. |
194 | (define-record-type <service> | |
1bb895ea | 195 | (make-service type value) |
db4fdc04 | 196 | service? |
0adfe95a | 197 | (type service-kind) |
efe7d19a LC |
198 | (value service-value)) |
199 | ||
1bb895ea LC |
200 | (define-syntax service |
201 | (syntax-rules () | |
202 | "Return a service instance of TYPE. The service value is VALUE or, if | |
203 | omitted, TYPE's default value." | |
204 | ((_ type value) | |
205 | (make-service type value)) | |
206 | ((_ type) | |
207 | (%service-with-default-value (current-source-location) | |
208 | type)))) | |
209 | ||
210 | (define (%service-with-default-value location type) | |
211 | "Return a instance of service type TYPE with its default value, if any. If | |
212 | TYPE does not have a default value, an error is raised." | |
213 | ;; TODO: Currently this is a run-time error but with a little bit macrology | |
214 | ;; we could turn it into an expansion-time error. | |
215 | (let ((default (service-type-default-value type))) | |
216 | (if (eq? default &no-default-value) | |
217 | (let ((location (source-properties->location location))) | |
218 | (raise | |
219 | (condition | |
220 | (&missing-value-service-error (type type) (location location)) | |
221 | (&message | |
69daee23 | 222 | (message (format #f (G_ "~a: no value specified \ |
1bb895ea LC |
223 | for service of type '~a'") |
224 | (location->string location) | |
225 | (service-type-name type))))))) | |
226 | (service type default)))) | |
227 | ||
228 | (define-condition-type &service-error &error | |
229 | service-error?) | |
230 | ||
231 | (define-condition-type &missing-value-service-error &service-error | |
232 | missing-value-service-error? | |
233 | (type missing-value-service-error-type) | |
234 | (location missing-value-service-error-location)) | |
235 | ||
236 | ||
237 | \f | |
238 | ;;; | |
239 | ;;; Helpers. | |
240 | ;;; | |
241 | ||
efe7d19a LC |
242 | (define service-parameters |
243 | ;; Deprecated alias. | |
244 | service-value) | |
0adfe95a | 245 | |
71654dfd LC |
246 | (define (simple-service name target value) |
247 | "Return a service that extends TARGET with VALUE. This works by creating a | |
248 | singleton service type NAME, of which the returned service is an instance." | |
249 | (let* ((extension (service-extension target identity)) | |
250 | (type (service-type (name name) | |
251 | (extensions (list extension))))) | |
252 | (service type value))) | |
0adfe95a | 253 | |
cd6f6c22 LC |
254 | (define-syntax %modify-service |
255 | (syntax-rules (=>) | |
256 | ((_ service) | |
257 | service) | |
258 | ((_ svc (kind param => exp ...) clauses ...) | |
259 | (if (eq? (service-kind svc) kind) | |
efe7d19a | 260 | (let ((param (service-value svc))) |
cd6f6c22 LC |
261 | (service (service-kind svc) |
262 | (begin exp ...))) | |
263 | (%modify-service svc clauses ...))))) | |
264 | ||
265 | (define-syntax modify-services | |
266 | (syntax-rules () | |
4d343a14 CM |
267 | "Modify the services listed in SERVICES according to CLAUSES and return |
268 | the resulting list of services. Each clause must have the form: | |
cd6f6c22 LC |
269 | |
270 | (TYPE VARIABLE => BODY) | |
271 | ||
272 | where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an | |
273 | identifier that is bound within BODY to the value of the service of that | |
274 | TYPE. Consider this example: | |
275 | ||
276 | (modify-services %base-services | |
277 | (guix-service-type config => | |
278 | (guix-configuration | |
279 | (inherit config) | |
280 | (use-substitutes? #f) | |
281 | (extra-options '(\"--gc-keep-derivations\")))) | |
282 | (mingetty-service-type config => | |
283 | (mingetty-configuration | |
284 | (inherit config) | |
285 | (motd (plain-file \"motd\" \"Hi there!\"))))) | |
286 | ||
287 | It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of | |
288 | all the MINGETTY-SERVICE-TYPE instances. | |
289 | ||
290 | This is a shorthand for (map (lambda (svc) ...) %base-services)." | |
291 | ((_ services clauses ...) | |
292 | (map (lambda (service) | |
293 | (%modify-service service clauses ...)) | |
294 | services)))) | |
0adfe95a LC |
295 | |
296 | \f | |
297 | ;;; | |
298 | ;;; Core services. | |
299 | ;;; | |
300 | ||
d62e201c LC |
301 | (define (system-derivation mentries mextensions) |
302 | "Return as a monadic value the derivation of the 'system' directory | |
303 | containing the given entries." | |
304 | (mlet %store-monad ((entries mentries) | |
305 | (extensions (sequence %store-monad mextensions))) | |
306 | (lower-object | |
307 | (file-union "system" | |
308 | (append entries (concatenate extensions)))))) | |
309 | ||
310 | (define system-service-type | |
311 | ;; This is the ultimate service type, the root of the service DAG. The | |
312 | ;; service of this type is extended by monadic name/item pairs. These items | |
313 | ;; end up in the "system directory" as returned by | |
314 | ;; 'operating-system-derivation'. | |
315 | (service-type (name 'system) | |
316 | (extensions '()) | |
317 | (compose identity) | |
318 | (extend system-derivation))) | |
319 | ||
3a391e68 | 320 | (define (compute-boot-script _ mexps) |
be7be9e8 | 321 | (mlet %store-monad ((gexps (sequence %store-monad mexps))) |
0adfe95a | 322 | (gexp->file "boot" |
d4053c71 | 323 | ;; Clean up and activate the system, then spawn shepherd. |
be7be9e8 | 324 | #~(begin #$@gexps)))) |
0adfe95a | 325 | |
d62e201c LC |
326 | (define (boot-script-entry mboot) |
327 | "Return, as a monadic value, an entry for the boot script in the system | |
328 | directory." | |
329 | (mlet %store-monad ((boot mboot)) | |
330 | (return `(("boot" ,boot))))) | |
331 | ||
0adfe95a LC |
332 | (define boot-service-type |
333 | ;; The service of this type is extended by being passed gexps as monadic | |
334 | ;; values. It aggregates them in a single script, as a monadic value, which | |
335 | ;; becomes its 'parameters'. It is the only service that extends nothing. | |
336 | (service-type (name 'boot) | |
d62e201c LC |
337 | (extensions |
338 | (list (service-extension system-service-type | |
339 | boot-script-entry))) | |
3a391e68 LC |
340 | (compose append) |
341 | (extend compute-boot-script))) | |
0adfe95a LC |
342 | |
343 | (define %boot-service | |
d62e201c | 344 | ;; The service that produces the boot script. |
0adfe95a | 345 | (service boot-service-type #t)) |
be7be9e8 LC |
346 | |
347 | (define (cleanup-gexp _) | |
348 | "Return as a monadic value a gexp to clean up /tmp and similar places upon | |
349 | boot." | |
fd129893 LC |
350 | (with-monad %store-monad |
351 | (with-imported-modules '((guix build utils)) | |
352 | (return #~(begin | |
353 | (use-modules (guix build utils)) | |
354 | ||
355 | ;; Clean out /tmp and /var/run. | |
356 | ;; | |
357 | ;; XXX This needs to happen before service activations, so it | |
358 | ;; has to be here, but this also implicitly assumes that /tmp | |
359 | ;; and /var/run are on the root partition. | |
360 | (letrec-syntax ((fail-safe (syntax-rules () | |
361 | ((_ exp rest ...) | |
362 | (begin | |
363 | (catch 'system-error | |
364 | (lambda () exp) | |
365 | (const #f)) | |
366 | (fail-safe rest ...))) | |
367 | ((_) | |
368 | #t)))) | |
369 | ;; Ignore I/O errors so the system can boot. | |
370 | (fail-safe | |
371 | (delete-file-recursively "/tmp") | |
372 | (delete-file-recursively "/var/run") | |
373 | (mkdir "/tmp") | |
374 | (chmod "/tmp" #o1777) | |
375 | (mkdir "/var/run") | |
376 | (chmod "/var/run" #o755)))))))) | |
be7be9e8 LC |
377 | |
378 | (define cleanup-service-type | |
379 | ;; Service that cleans things up in /tmp and similar. | |
380 | (service-type (name 'cleanup) | |
381 | (extensions | |
382 | (list (service-extension boot-service-type | |
383 | cleanup-gexp))))) | |
0adfe95a LC |
384 | |
385 | (define* (file-union name files) ;FIXME: Factorize. | |
386 | "Return a <computed-file> that builds a directory containing all of FILES. | |
387 | Each item in FILES must be a list where the first element is the file name to | |
388 | use in the new directory, and the second element is a gexp denoting the target | |
389 | file." | |
390 | (computed-file name | |
391 | #~(begin | |
392 | (mkdir #$output) | |
393 | (chdir #$output) | |
394 | #$@(map (match-lambda | |
395 | ((target source) | |
37dd1e6a LC |
396 | #~(begin |
397 | ;; Stat the source to abort early if it | |
398 | ;; does not exist. | |
399 | (stat #$source) | |
400 | ||
401 | (symlink #$source #$target)))) | |
0adfe95a LC |
402 | files)))) |
403 | ||
404 | (define (directory-union name things) | |
405 | "Return a directory that is the union of THINGS." | |
406 | (match things | |
407 | ((one) | |
408 | ;; Only one thing; return it. | |
409 | one) | |
410 | (_ | |
411 | (computed-file name | |
4ee96a79 LC |
412 | (with-imported-modules '((guix build union)) |
413 | #~(begin | |
414 | (use-modules (guix build union)) | |
415 | (union-build #$output '#$things))))))) | |
0adfe95a | 416 | |
0adfe95a LC |
417 | (define* (activation-service->script service) |
418 | "Return as a monadic value the activation script for SERVICE, a service of | |
419 | ACTIVATION-SCRIPT-TYPE." | |
efe7d19a | 420 | (activation-script (service-value service))) |
0adfe95a LC |
421 | |
422 | (define (activation-script gexps) | |
423 | "Return the system's activation script, which evaluates GEXPS." | |
0adfe95a LC |
424 | (define (service-activations) |
425 | ;; Return the activation scripts for SERVICES. | |
426 | (mapm %store-monad | |
427 | (cut gexp->file "activate-service" <>) | |
428 | gexps)) | |
429 | ||
fd129893 | 430 | (mlet* %store-monad ((actions (service-activations))) |
0adfe95a | 431 | (gexp->file "activate" |
232ccbef | 432 | (with-imported-modules (source-module-closure |
fe2b6434 CB |
433 | '((gnu build activation) |
434 | (guix build utils))) | |
fd129893 | 435 | #~(begin |
fe2b6434 CB |
436 | (use-modules (gnu build activation) |
437 | (guix build utils)) | |
0adfe95a | 438 | |
caa78166 LC |
439 | ;; Make sure the user accounting database exists. If it |
440 | ;; does not exist, 'setutxent' does not create it and | |
441 | ;; thus there is no accounting at all. | |
442 | (close-port (open-file "/var/run/utmpx" "a0")) | |
443 | ||
2986995b LC |
444 | ;; Same for 'wtmp', which is populated by mingetty et |
445 | ;; al. | |
fe2b6434 | 446 | (mkdir-p "/var/log") |
2986995b LC |
447 | (close-port (open-file "/var/log/wtmp" "a0")) |
448 | ||
97bb1ab6 CB |
449 | ;; Set up /run/current-system. Among other things this |
450 | ;; sets up locales, which the activation snippets | |
451 | ;; executed below may expect. | |
452 | (activate-current-system) | |
453 | ||
fd129893 LC |
454 | ;; Run the services' activation snippets. |
455 | ;; TODO: Use 'load-compiled'. | |
97bb1ab6 | 456 | (for-each primitive-load '#$actions)))))) |
0adfe95a LC |
457 | |
458 | (define (gexps->activation-gexp gexps) | |
459 | "Return a gexp that runs the activation script containing GEXPS." | |
460 | (mlet %store-monad ((script (activation-script gexps))) | |
461 | (return #~(primitive-load #$script)))) | |
462 | ||
3a391e68 LC |
463 | (define (second-argument a b) b) |
464 | ||
0adfe95a LC |
465 | (define activation-service-type |
466 | (service-type (name 'activate) | |
467 | (extensions | |
468 | (list (service-extension boot-service-type | |
469 | gexps->activation-gexp))) | |
470 | (compose append) | |
471 | (extend second-argument))) | |
472 | ||
473 | (define %activation-service | |
474 | ;; The activation service produces the activation script from the gexps it | |
475 | ;; receives. | |
476 | (service activation-service-type #t)) | |
477 | ||
a241a7ac LC |
478 | (define %modprobe-wrapper |
479 | ;; Wrapper for the 'modprobe' command that knows where modules live. | |
480 | ;; | |
481 | ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe', | |
482 | ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' | |
483 | ;; environment variable is not set---hence the need for this wrapper. | |
484 | (let ((modprobe "/run/current-system/profile/bin/modprobe")) | |
485 | (program-file "modprobe" | |
486 | #~(begin | |
487 | (setenv "LINUX_MODULE_DIRECTORY" | |
488 | "/run/booted-system/kernel/lib/modules") | |
489 | (apply execl #$modprobe | |
490 | (cons #$modprobe (cdr (command-line)))))))) | |
491 | ||
492 | (define %linux-kernel-activation | |
493 | ;; Activation of the Linux kernel running on the bare metal (as opposed to | |
494 | ;; running in a container.) | |
495 | #~(begin | |
496 | ;; Tell the kernel to use our 'modprobe' command. | |
497 | (activate-modprobe #$%modprobe-wrapper) | |
498 | ||
499 | ;; Let users debug their own processes! | |
500 | (activate-ptrace-attach))) | |
501 | ||
a241a7ac LC |
502 | (define %linux-bare-metal-service |
503 | ;; The service that does things that are needed on the "bare metal", but not | |
504 | ;; necessary or impossible in a container. | |
6ddf4fcf LC |
505 | (simple-service 'linux-bare-metal |
506 | activation-service-type | |
507 | %linux-kernel-activation)) | |
508 | ||
a241a7ac | 509 | |
387e1754 LC |
510 | (define special-files-service-type |
511 | ;; Service to install "special files" such as /bin/sh and /usr/bin/env. | |
512 | (service-type | |
513 | (name 'special-files) | |
514 | (extensions | |
515 | (list (service-extension activation-service-type | |
516 | (lambda (files) | |
517 | #~(activate-special-files '#$files))))) | |
518 | (compose concatenate) | |
519 | (extend append))) | |
520 | ||
521 | (define (extra-special-file file target) | |
522 | "Use TARGET as the \"special file\" FILE. For example, TARGET might be | |
523 | (file-append coreutils \"/bin/env\") | |
524 | and FILE could be \"/usr/bin/env\"." | |
525 | (simple-service (string->symbol (string-append "special-file-" file)) | |
526 | special-files-service-type | |
527 | `((,file ,target)))) | |
528 | ||
0adfe95a LC |
529 | (define (etc-directory service) |
530 | "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE." | |
efe7d19a | 531 | (files->etc-directory (service-value service))) |
0adfe95a LC |
532 | |
533 | (define (files->etc-directory files) | |
534 | (file-union "etc" files)) | |
535 | ||
d62e201c LC |
536 | (define (etc-entry files) |
537 | "Return an entry for the /etc directory consisting of FILES in the system | |
538 | directory." | |
539 | (with-monad %store-monad | |
540 | (return `(("etc" ,(files->etc-directory files)))))) | |
541 | ||
0adfe95a LC |
542 | (define etc-service-type |
543 | (service-type (name 'etc) | |
544 | (extensions | |
545 | (list | |
546 | (service-extension activation-service-type | |
547 | (lambda (files) | |
548 | (let ((etc | |
549 | (files->etc-directory files))) | |
d62e201c LC |
550 | #~(activate-etc #$etc)))) |
551 | (service-extension system-service-type etc-entry))) | |
0adfe95a LC |
552 | (compose concatenate) |
553 | (extend append))) | |
554 | ||
555 | (define (etc-service files) | |
556 | "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES. | |
557 | FILES must be a list of name/file-like object pairs." | |
558 | (service etc-service-type files)) | |
559 | ||
560 | (define setuid-program-service-type | |
561 | (service-type (name 'setuid-program) | |
562 | (extensions | |
563 | (list (service-extension activation-service-type | |
564 | (lambda (programs) | |
565 | #~(activate-setuid-programs | |
566 | (list #$@programs)))))) | |
567 | (compose concatenate) | |
568 | (extend append))) | |
569 | ||
af4c3fd5 LC |
570 | (define (packages->profile-entry packages) |
571 | "Return a system entry for the profile containing PACKAGES." | |
572 | (mlet %store-monad ((profile (profile-derivation | |
60a0886d SB |
573 | (packages->manifest |
574 | (delete-duplicates packages eq?))))) | |
af4c3fd5 LC |
575 | (return `(("profile" ,profile))))) |
576 | ||
577 | (define profile-service-type | |
578 | ;; The service that populates the system's profile---i.e., | |
579 | ;; /run/current-system/profile. It is extended by package lists. | |
580 | (service-type (name 'profile) | |
581 | (extensions | |
582 | (list (service-extension system-service-type | |
583 | packages->profile-entry))) | |
584 | (compose concatenate) | |
585 | (extend append))) | |
586 | ||
0adfe95a LC |
587 | (define (firmware->activation-gexp firmware) |
588 | "Return a gexp to make the packages listed in FIRMWARE loadable by the | |
589 | kernel." | |
590 | (let ((directory (directory-union "firmware" firmware))) | |
591 | ;; Tell the kernel where firmware is. | |
592 | #~(activate-firmware (string-append #$directory "/lib/firmware")))) | |
593 | ||
594 | (define firmware-service-type | |
595 | ;; The service that collects firmware. | |
596 | (service-type (name 'firmware) | |
597 | (extensions | |
598 | (list (service-extension activation-service-type | |
599 | firmware->activation-gexp))) | |
600 | (compose concatenate) | |
601 | (extend append))) | |
602 | ||
e0b47290 LC |
603 | (define (gc-roots->system-entry roots) |
604 | "Return an entry in the system's output containing symlinks to ROOTS." | |
605 | (mlet %store-monad ((entry (gexp->derivation | |
606 | "gc-roots" | |
607 | #~(let ((roots '#$roots)) | |
608 | (mkdir #$output) | |
609 | (chdir #$output) | |
610 | (for-each symlink | |
611 | roots | |
612 | (map number->string | |
613 | (iota (length roots)))))))) | |
614 | (return (if (null? roots) | |
615 | '() | |
616 | `(("gc-roots" ,entry)))))) | |
617 | ||
618 | (define gc-root-service-type | |
619 | ;; A service to associate extra garbage-collector roots to the system. This | |
620 | ;; is a simple hack that guarantees that the system retains references to | |
621 | ;; the given list of roots. Roots must be "lowerable" objects like | |
622 | ;; packages, or derivations. | |
623 | (service-type (name 'gc-roots) | |
624 | (extensions | |
625 | (list (service-extension system-service-type | |
626 | gc-roots->system-entry))) | |
627 | (compose concatenate) | |
628 | (extend append))) | |
629 | ||
0adfe95a LC |
630 | \f |
631 | ;;; | |
632 | ;;; Service folding. | |
633 | ;;; | |
634 | ||
0adfe95a LC |
635 | (define-condition-type &missing-target-service-error &service-error |
636 | missing-target-service-error? | |
637 | (service missing-target-service-error-service) | |
638 | (target-type missing-target-service-error-target-type)) | |
639 | ||
640 | (define-condition-type &ambiguous-target-service-error &service-error | |
641 | ambiguous-target-service-error? | |
642 | (service ambiguous-target-service-error-service) | |
643 | (target-type ambiguous-target-service-error-target-type)) | |
644 | ||
645 | (define (service-back-edges services) | |
646 | "Return a procedure that, when passed a <service>, returns the list of | |
647 | <service> objects that depend on it." | |
648 | (define (add-edges service edges) | |
649 | (define (add-edge extension edges) | |
650 | (let ((target-type (service-extension-target extension))) | |
651 | (match (filter (lambda (service) | |
652 | (eq? (service-kind service) target-type)) | |
653 | services) | |
654 | ((target) | |
655 | (vhash-consq target service edges)) | |
656 | (() | |
657 | (raise | |
658 | (condition (&missing-target-service-error | |
659 | (service service) | |
660 | (target-type target-type)) | |
661 | (&message | |
662 | (message | |
638e9dea | 663 | (format #f (G_ "no target of type '~a' for service '~a'") |
0adfe95a | 664 | (service-type-name target-type) |
638e9dea LC |
665 | (service-type-name |
666 | (service-kind service)))))))) | |
0adfe95a LC |
667 | (x |
668 | (raise | |
669 | (condition (&ambiguous-target-service-error | |
670 | (service service) | |
671 | (target-type target-type)) | |
672 | (&message | |
673 | (message | |
674 | (format #f | |
69daee23 | 675 | (G_ "more than one target service of type '~a'") |
0adfe95a LC |
676 | (service-type-name target-type)))))))))) |
677 | ||
678 | (fold add-edge edges (service-type-extensions (service-kind service)))) | |
679 | ||
680 | (let ((edges (fold add-edges vlist-null services))) | |
681 | (lambda (node) | |
682 | (reverse (vhash-foldq* cons '() node edges))))) | |
683 | ||
d62e201c LC |
684 | (define* (fold-services services |
685 | #:key (target-type system-service-type)) | |
0adfe95a LC |
686 | "Fold SERVICES by propagating their extensions down to the root of type |
687 | TARGET-TYPE; return the root service adjusted accordingly." | |
688 | (define dependents | |
689 | (service-back-edges services)) | |
690 | ||
691 | (define (matching-extension target) | |
692 | (let ((target (service-kind target))) | |
693 | (match-lambda | |
694 | (($ <service-extension> type) | |
695 | (eq? type target))))) | |
696 | ||
697 | (define (apply-extension target) | |
698 | (lambda (service) | |
699 | (match (find (matching-extension target) | |
700 | (service-type-extensions (service-kind service))) | |
701 | (($ <service-extension> _ compute) | |
efe7d19a | 702 | (compute (service-value service)))))) |
0adfe95a LC |
703 | |
704 | (match (filter (lambda (service) | |
705 | (eq? (service-kind service) target-type)) | |
706 | services) | |
707 | ((sink) | |
708 | (let loop ((sink sink)) | |
709 | (let* ((dependents (map loop (dependents sink))) | |
710 | (extensions (map (apply-extension sink) dependents)) | |
711 | (extend (service-type-extend (service-kind sink))) | |
712 | (compose (service-type-compose (service-kind sink))) | |
efe7d19a | 713 | (params (service-value sink))) |
0adfe95a LC |
714 | ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a |
715 | ;; different type than the elements of EXTENSIONS. | |
716 | (if extend | |
717 | (service (service-kind sink) | |
718 | (extend params (compose extensions))) | |
719 | sink)))) | |
720 | (() | |
721 | (raise | |
722 | (condition (&missing-target-service-error | |
723 | (service #f) | |
724 | (target-type target-type)) | |
725 | (&message | |
69daee23 | 726 | (message (format #f (G_ "service of type '~a' not found") |
0adfe95a LC |
727 | (service-type-name target-type))))))) |
728 | (x | |
729 | (raise | |
730 | (condition (&ambiguous-target-service-error | |
731 | (service #f) | |
732 | (target-type target-type)) | |
733 | (&message | |
734 | (message | |
735 | (format #f | |
69daee23 | 736 | (G_ "more than one target service of type '~a'") |
0adfe95a | 737 | (service-type-name target-type))))))))) |
db4fdc04 LC |
738 | |
739 | ;;; services.scm ends here. |