Commit | Line | Data |
---|---|---|
db4fdc04 | 1 | ;;; GNU Guix --- Functional package management for GNU |
d466b1fc | 2 | ;;; Copyright © 2015, 2016, 2017, 2018 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) |
d466b1fc | 27 | #:use-module (guix combinators) |
0adfe95a LC |
28 | #:use-module (guix sets) |
29 | #:use-module (guix ui) | |
1bb895ea | 30 | #:use-module ((guix utils) #:select (source-properties->location)) |
232ccbef | 31 | #:use-module (guix modules) |
0adfe95a LC |
32 | #:use-module (gnu packages base) |
33 | #:use-module (gnu packages bash) | |
34 | #:use-module (srfi srfi-1) | |
35 | #:use-module (srfi srfi-9) | |
36 | #:use-module (srfi srfi-9 gnu) | |
37 | #:use-module (srfi srfi-26) | |
38 | #:use-module (srfi srfi-34) | |
39 | #:use-module (srfi srfi-35) | |
40 | #:use-module (ice-9 vlist) | |
41 | #:use-module (ice-9 match) | |
42 | #:export (service-extension | |
43 | service-extension? | |
7d8b5913 CB |
44 | service-extension-target |
45 | service-extension-compute | |
0adfe95a LC |
46 | |
47 | service-type | |
48 | service-type? | |
5152d13b LC |
49 | service-type-name |
50 | service-type-extensions | |
51 | service-type-compose | |
52 | service-type-extend | |
1bb895ea | 53 | service-type-default-value |
b714395a LC |
54 | service-type-description |
55 | service-type-location | |
56 | ||
0c0c1b21 LC |
57 | %service-type-path |
58 | fold-service-types | |
49483f71 | 59 | lookup-service-types |
0adfe95a | 60 | |
db4fdc04 | 61 | service |
0adfe95a LC |
62 | service? |
63 | service-kind | |
efe7d19a LC |
64 | service-value |
65 | service-parameters ;deprecated | |
0adfe95a | 66 | |
71654dfd | 67 | simple-service |
cd6f6c22 | 68 | modify-services |
5152d13b | 69 | service-back-edges |
d466b1fc | 70 | instantiate-missing-services |
0adfe95a LC |
71 | fold-services |
72 | ||
73 | service-error? | |
1bb895ea LC |
74 | missing-value-service-error? |
75 | missing-value-service-error-type | |
76 | missing-value-service-error-location | |
0adfe95a LC |
77 | missing-target-service-error? |
78 | missing-target-service-error-service | |
79 | missing-target-service-error-target-type | |
80 | ambiguous-target-service-error? | |
81 | ambiguous-target-service-error-service | |
82 | ambiguous-target-service-error-target-type | |
83 | ||
d62e201c | 84 | system-service-type |
0adfe95a | 85 | boot-service-type |
be7be9e8 | 86 | cleanup-service-type |
0adfe95a LC |
87 | activation-service-type |
88 | activation-service->script | |
a241a7ac | 89 | %linux-bare-metal-service |
387e1754 LC |
90 | special-files-service-type |
91 | extra-special-file | |
0adfe95a LC |
92 | etc-service-type |
93 | etc-directory | |
94 | setuid-program-service-type | |
af4c3fd5 | 95 | profile-service-type |
0adfe95a | 96 | firmware-service-type |
e0b47290 | 97 | gc-root-service-type |
0adfe95a LC |
98 | |
99 | %boot-service | |
100 | %activation-service | |
d298c815 | 101 | etc-service)) |
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 | ||
3943913f LC |
181 | (define (all-service-modules) |
182 | "Return the default set of service modules." | |
183 | (cons (resolve-interface '(gnu services)) | |
3c0128b0 LC |
184 | (all-modules (%service-type-path) |
185 | #:warn warn-about-load-error))) | |
3943913f | 186 | |
0c0c1b21 LC |
187 | (define* (fold-service-types proc seed |
188 | #:optional | |
3943913f | 189 | (modules (all-service-modules))) |
0c0c1b21 LC |
190 | "For each service type exported by one of MODULES, call (PROC RESULT). SEED |
191 | is used as the initial value of RESULT." | |
192 | (fold-module-public-variables (lambda (object result) | |
193 | (if (service-type? object) | |
194 | (proc object result) | |
195 | result)) | |
a3d37f3a | 196 | seed |
0c0c1b21 LC |
197 | modules)) |
198 | ||
49483f71 LC |
199 | (define lookup-service-types |
200 | (let ((table | |
201 | (delay (fold-service-types (lambda (type result) | |
202 | (vhash-consq (service-type-name type) | |
203 | type result)) | |
204 | vlist-null)))) | |
205 | (lambda (name) | |
206 | "Return the list of services with the given NAME (a symbol)." | |
207 | (vhash-foldq* cons '() name (force table))))) | |
208 | ||
0adfe95a LC |
209 | ;; Services of a given type. |
210 | (define-record-type <service> | |
1bb895ea | 211 | (make-service type value) |
db4fdc04 | 212 | service? |
0adfe95a | 213 | (type service-kind) |
efe7d19a LC |
214 | (value service-value)) |
215 | ||
1bb895ea LC |
216 | (define-syntax service |
217 | (syntax-rules () | |
218 | "Return a service instance of TYPE. The service value is VALUE or, if | |
219 | omitted, TYPE's default value." | |
220 | ((_ type value) | |
221 | (make-service type value)) | |
222 | ((_ type) | |
223 | (%service-with-default-value (current-source-location) | |
224 | type)))) | |
225 | ||
226 | (define (%service-with-default-value location type) | |
227 | "Return a instance of service type TYPE with its default value, if any. If | |
228 | TYPE does not have a default value, an error is raised." | |
229 | ;; TODO: Currently this is a run-time error but with a little bit macrology | |
230 | ;; we could turn it into an expansion-time error. | |
231 | (let ((default (service-type-default-value type))) | |
232 | (if (eq? default &no-default-value) | |
233 | (let ((location (source-properties->location location))) | |
234 | (raise | |
235 | (condition | |
236 | (&missing-value-service-error (type type) (location location)) | |
237 | (&message | |
69daee23 | 238 | (message (format #f (G_ "~a: no value specified \ |
1bb895ea LC |
239 | for service of type '~a'") |
240 | (location->string location) | |
241 | (service-type-name type))))))) | |
242 | (service type default)))) | |
243 | ||
244 | (define-condition-type &service-error &error | |
245 | service-error?) | |
246 | ||
247 | (define-condition-type &missing-value-service-error &service-error | |
248 | missing-value-service-error? | |
249 | (type missing-value-service-error-type) | |
250 | (location missing-value-service-error-location)) | |
251 | ||
252 | ||
253 | \f | |
254 | ;;; | |
255 | ;;; Helpers. | |
256 | ;;; | |
257 | ||
efe7d19a LC |
258 | (define service-parameters |
259 | ;; Deprecated alias. | |
260 | service-value) | |
0adfe95a | 261 | |
71654dfd LC |
262 | (define (simple-service name target value) |
263 | "Return a service that extends TARGET with VALUE. This works by creating a | |
264 | singleton service type NAME, of which the returned service is an instance." | |
265 | (let* ((extension (service-extension target identity)) | |
266 | (type (service-type (name name) | |
267 | (extensions (list extension))))) | |
268 | (service type value))) | |
0adfe95a | 269 | |
cd6f6c22 LC |
270 | (define-syntax %modify-service |
271 | (syntax-rules (=>) | |
272 | ((_ service) | |
273 | service) | |
274 | ((_ svc (kind param => exp ...) clauses ...) | |
275 | (if (eq? (service-kind svc) kind) | |
efe7d19a | 276 | (let ((param (service-value svc))) |
cd6f6c22 LC |
277 | (service (service-kind svc) |
278 | (begin exp ...))) | |
279 | (%modify-service svc clauses ...))))) | |
280 | ||
281 | (define-syntax modify-services | |
282 | (syntax-rules () | |
4d343a14 CM |
283 | "Modify the services listed in SERVICES according to CLAUSES and return |
284 | the resulting list of services. Each clause must have the form: | |
cd6f6c22 LC |
285 | |
286 | (TYPE VARIABLE => BODY) | |
287 | ||
288 | where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an | |
289 | identifier that is bound within BODY to the value of the service of that | |
290 | TYPE. Consider this example: | |
291 | ||
292 | (modify-services %base-services | |
293 | (guix-service-type config => | |
294 | (guix-configuration | |
295 | (inherit config) | |
296 | (use-substitutes? #f) | |
297 | (extra-options '(\"--gc-keep-derivations\")))) | |
298 | (mingetty-service-type config => | |
299 | (mingetty-configuration | |
300 | (inherit config) | |
301 | (motd (plain-file \"motd\" \"Hi there!\"))))) | |
302 | ||
303 | It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of | |
304 | all the MINGETTY-SERVICE-TYPE instances. | |
305 | ||
306 | This is a shorthand for (map (lambda (svc) ...) %base-services)." | |
307 | ((_ services clauses ...) | |
308 | (map (lambda (service) | |
309 | (%modify-service service clauses ...)) | |
310 | services)))) | |
0adfe95a LC |
311 | |
312 | \f | |
313 | ;;; | |
314 | ;;; Core services. | |
315 | ;;; | |
316 | ||
d62e201c LC |
317 | (define (system-derivation mentries mextensions) |
318 | "Return as a monadic value the derivation of the 'system' directory | |
319 | containing the given entries." | |
320 | (mlet %store-monad ((entries mentries) | |
321 | (extensions (sequence %store-monad mextensions))) | |
322 | (lower-object | |
323 | (file-union "system" | |
324 | (append entries (concatenate extensions)))))) | |
325 | ||
326 | (define system-service-type | |
327 | ;; This is the ultimate service type, the root of the service DAG. The | |
328 | ;; service of this type is extended by monadic name/item pairs. These items | |
329 | ;; end up in the "system directory" as returned by | |
330 | ;; 'operating-system-derivation'. | |
331 | (service-type (name 'system) | |
332 | (extensions '()) | |
333 | (compose identity) | |
334 | (extend system-derivation))) | |
335 | ||
3a391e68 | 336 | (define (compute-boot-script _ mexps) |
be7be9e8 | 337 | (mlet %store-monad ((gexps (sequence %store-monad mexps))) |
0adfe95a | 338 | (gexp->file "boot" |
d4053c71 | 339 | ;; Clean up and activate the system, then spawn shepherd. |
be7be9e8 | 340 | #~(begin #$@gexps)))) |
0adfe95a | 341 | |
d62e201c LC |
342 | (define (boot-script-entry mboot) |
343 | "Return, as a monadic value, an entry for the boot script in the system | |
344 | directory." | |
345 | (mlet %store-monad ((boot mboot)) | |
346 | (return `(("boot" ,boot))))) | |
347 | ||
0adfe95a LC |
348 | (define boot-service-type |
349 | ;; The service of this type is extended by being passed gexps as monadic | |
350 | ;; values. It aggregates them in a single script, as a monadic value, which | |
351 | ;; becomes its 'parameters'. It is the only service that extends nothing. | |
352 | (service-type (name 'boot) | |
d62e201c LC |
353 | (extensions |
354 | (list (service-extension system-service-type | |
355 | boot-script-entry))) | |
7874e9e0 | 356 | (compose identity) |
3a391e68 | 357 | (extend compute-boot-script))) |
0adfe95a LC |
358 | |
359 | (define %boot-service | |
d62e201c | 360 | ;; The service that produces the boot script. |
0adfe95a | 361 | (service boot-service-type #t)) |
be7be9e8 LC |
362 | |
363 | (define (cleanup-gexp _) | |
364 | "Return as a monadic value a gexp to clean up /tmp and similar places upon | |
365 | boot." | |
fd129893 LC |
366 | (with-monad %store-monad |
367 | (with-imported-modules '((guix build utils)) | |
368 | (return #~(begin | |
369 | (use-modules (guix build utils)) | |
370 | ||
371 | ;; Clean out /tmp and /var/run. | |
372 | ;; | |
373 | ;; XXX This needs to happen before service activations, so it | |
374 | ;; has to be here, but this also implicitly assumes that /tmp | |
375 | ;; and /var/run are on the root partition. | |
376 | (letrec-syntax ((fail-safe (syntax-rules () | |
377 | ((_ exp rest ...) | |
378 | (begin | |
379 | (catch 'system-error | |
380 | (lambda () exp) | |
381 | (const #f)) | |
382 | (fail-safe rest ...))) | |
383 | ((_) | |
384 | #t)))) | |
385 | ;; Ignore I/O errors so the system can boot. | |
386 | (fail-safe | |
aad8a143 LC |
387 | ;; Remove stale Shadow lock files as they would lead to |
388 | ;; failures of 'useradd' & co. | |
389 | (delete-file "/etc/group.lock") | |
390 | (delete-file "/etc/passwd.lock") | |
391 | (delete-file "/etc/.pwd.lock") ;from 'lckpwdf' | |
392 | ||
fd129893 LC |
393 | (delete-file-recursively "/tmp") |
394 | (delete-file-recursively "/var/run") | |
395 | (mkdir "/tmp") | |
396 | (chmod "/tmp" #o1777) | |
397 | (mkdir "/var/run") | |
e25ca462 DM |
398 | (chmod "/var/run" #o755) |
399 | (delete-file-recursively "/run/udev/watch.old")))))))) | |
be7be9e8 LC |
400 | |
401 | (define cleanup-service-type | |
402 | ;; Service that cleans things up in /tmp and similar. | |
403 | (service-type (name 'cleanup) | |
404 | (extensions | |
405 | (list (service-extension boot-service-type | |
406 | cleanup-gexp))))) | |
0adfe95a | 407 | |
0adfe95a LC |
408 | (define* (activation-service->script service) |
409 | "Return as a monadic value the activation script for SERVICE, a service of | |
410 | ACTIVATION-SCRIPT-TYPE." | |
efe7d19a | 411 | (activation-script (service-value service))) |
0adfe95a LC |
412 | |
413 | (define (activation-script gexps) | |
414 | "Return the system's activation script, which evaluates GEXPS." | |
0adfe95a LC |
415 | (define (service-activations) |
416 | ;; Return the activation scripts for SERVICES. | |
417 | (mapm %store-monad | |
418 | (cut gexp->file "activate-service" <>) | |
419 | gexps)) | |
420 | ||
fd129893 | 421 | (mlet* %store-monad ((actions (service-activations))) |
0adfe95a | 422 | (gexp->file "activate" |
232ccbef | 423 | (with-imported-modules (source-module-closure |
fe2b6434 CB |
424 | '((gnu build activation) |
425 | (guix build utils))) | |
fd129893 | 426 | #~(begin |
fe2b6434 CB |
427 | (use-modules (gnu build activation) |
428 | (guix build utils)) | |
0adfe95a | 429 | |
caa78166 LC |
430 | ;; Make sure the user accounting database exists. If it |
431 | ;; does not exist, 'setutxent' does not create it and | |
432 | ;; thus there is no accounting at all. | |
433 | (close-port (open-file "/var/run/utmpx" "a0")) | |
434 | ||
2986995b LC |
435 | ;; Same for 'wtmp', which is populated by mingetty et |
436 | ;; al. | |
fe2b6434 | 437 | (mkdir-p "/var/log") |
2986995b LC |
438 | (close-port (open-file "/var/log/wtmp" "a0")) |
439 | ||
97bb1ab6 CB |
440 | ;; Set up /run/current-system. Among other things this |
441 | ;; sets up locales, which the activation snippets | |
442 | ;; executed below may expect. | |
443 | (activate-current-system) | |
444 | ||
fd129893 LC |
445 | ;; Run the services' activation snippets. |
446 | ;; TODO: Use 'load-compiled'. | |
97bb1ab6 | 447 | (for-each primitive-load '#$actions)))))) |
0adfe95a LC |
448 | |
449 | (define (gexps->activation-gexp gexps) | |
450 | "Return a gexp that runs the activation script containing GEXPS." | |
451 | (mlet %store-monad ((script (activation-script gexps))) | |
452 | (return #~(primitive-load #$script)))) | |
453 | ||
3a391e68 LC |
454 | (define (second-argument a b) b) |
455 | ||
0adfe95a LC |
456 | (define activation-service-type |
457 | (service-type (name 'activate) | |
458 | (extensions | |
459 | (list (service-extension boot-service-type | |
460 | gexps->activation-gexp))) | |
7874e9e0 | 461 | (compose identity) |
0adfe95a LC |
462 | (extend second-argument))) |
463 | ||
464 | (define %activation-service | |
465 | ;; The activation service produces the activation script from the gexps it | |
466 | ;; receives. | |
467 | (service activation-service-type #t)) | |
468 | ||
a241a7ac LC |
469 | (define %modprobe-wrapper |
470 | ;; Wrapper for the 'modprobe' command that knows where modules live. | |
471 | ;; | |
472 | ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe', | |
473 | ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' | |
474 | ;; environment variable is not set---hence the need for this wrapper. | |
475 | (let ((modprobe "/run/current-system/profile/bin/modprobe")) | |
476 | (program-file "modprobe" | |
477 | #~(begin | |
478 | (setenv "LINUX_MODULE_DIRECTORY" | |
479 | "/run/booted-system/kernel/lib/modules") | |
480 | (apply execl #$modprobe | |
481 | (cons #$modprobe (cdr (command-line)))))))) | |
482 | ||
483 | (define %linux-kernel-activation | |
484 | ;; Activation of the Linux kernel running on the bare metal (as opposed to | |
485 | ;; running in a container.) | |
486 | #~(begin | |
487 | ;; Tell the kernel to use our 'modprobe' command. | |
488 | (activate-modprobe #$%modprobe-wrapper) | |
489 | ||
490 | ;; Let users debug their own processes! | |
491 | (activate-ptrace-attach))) | |
492 | ||
a241a7ac LC |
493 | (define %linux-bare-metal-service |
494 | ;; The service that does things that are needed on the "bare metal", but not | |
495 | ;; necessary or impossible in a container. | |
6ddf4fcf LC |
496 | (simple-service 'linux-bare-metal |
497 | activation-service-type | |
498 | %linux-kernel-activation)) | |
499 | ||
a241a7ac | 500 | |
387e1754 LC |
501 | (define special-files-service-type |
502 | ;; Service to install "special files" such as /bin/sh and /usr/bin/env. | |
503 | (service-type | |
504 | (name 'special-files) | |
505 | (extensions | |
506 | (list (service-extension activation-service-type | |
507 | (lambda (files) | |
508 | #~(activate-special-files '#$files))))) | |
509 | (compose concatenate) | |
510 | (extend append))) | |
511 | ||
512 | (define (extra-special-file file target) | |
513 | "Use TARGET as the \"special file\" FILE. For example, TARGET might be | |
514 | (file-append coreutils \"/bin/env\") | |
515 | and FILE could be \"/usr/bin/env\"." | |
516 | (simple-service (string->symbol (string-append "special-file-" file)) | |
517 | special-files-service-type | |
518 | `((,file ,target)))) | |
519 | ||
0adfe95a LC |
520 | (define (etc-directory service) |
521 | "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE." | |
efe7d19a | 522 | (files->etc-directory (service-value service))) |
0adfe95a LC |
523 | |
524 | (define (files->etc-directory files) | |
525 | (file-union "etc" files)) | |
526 | ||
d62e201c LC |
527 | (define (etc-entry files) |
528 | "Return an entry for the /etc directory consisting of FILES in the system | |
529 | directory." | |
530 | (with-monad %store-monad | |
531 | (return `(("etc" ,(files->etc-directory files)))))) | |
532 | ||
0adfe95a LC |
533 | (define etc-service-type |
534 | (service-type (name 'etc) | |
535 | (extensions | |
536 | (list | |
537 | (service-extension activation-service-type | |
538 | (lambda (files) | |
539 | (let ((etc | |
540 | (files->etc-directory files))) | |
d62e201c LC |
541 | #~(activate-etc #$etc)))) |
542 | (service-extension system-service-type etc-entry))) | |
0adfe95a LC |
543 | (compose concatenate) |
544 | (extend append))) | |
545 | ||
546 | (define (etc-service files) | |
547 | "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES. | |
548 | FILES must be a list of name/file-like object pairs." | |
549 | (service etc-service-type files)) | |
550 | ||
551 | (define setuid-program-service-type | |
552 | (service-type (name 'setuid-program) | |
553 | (extensions | |
554 | (list (service-extension activation-service-type | |
555 | (lambda (programs) | |
556 | #~(activate-setuid-programs | |
557 | (list #$@programs)))))) | |
558 | (compose concatenate) | |
559 | (extend append))) | |
560 | ||
af4c3fd5 LC |
561 | (define (packages->profile-entry packages) |
562 | "Return a system entry for the profile containing PACKAGES." | |
563 | (mlet %store-monad ((profile (profile-derivation | |
60a0886d SB |
564 | (packages->manifest |
565 | (delete-duplicates packages eq?))))) | |
af4c3fd5 LC |
566 | (return `(("profile" ,profile))))) |
567 | ||
568 | (define profile-service-type | |
569 | ;; The service that populates the system's profile---i.e., | |
570 | ;; /run/current-system/profile. It is extended by package lists. | |
571 | (service-type (name 'profile) | |
572 | (extensions | |
573 | (list (service-extension system-service-type | |
574 | packages->profile-entry))) | |
575 | (compose concatenate) | |
576 | (extend append))) | |
577 | ||
0adfe95a LC |
578 | (define (firmware->activation-gexp firmware) |
579 | "Return a gexp to make the packages listed in FIRMWARE loadable by the | |
580 | kernel." | |
581 | (let ((directory (directory-union "firmware" firmware))) | |
582 | ;; Tell the kernel where firmware is. | |
583 | #~(activate-firmware (string-append #$directory "/lib/firmware")))) | |
584 | ||
585 | (define firmware-service-type | |
586 | ;; The service that collects firmware. | |
587 | (service-type (name 'firmware) | |
588 | (extensions | |
589 | (list (service-extension activation-service-type | |
590 | firmware->activation-gexp))) | |
591 | (compose concatenate) | |
592 | (extend append))) | |
593 | ||
e0b47290 LC |
594 | (define (gc-roots->system-entry roots) |
595 | "Return an entry in the system's output containing symlinks to ROOTS." | |
596 | (mlet %store-monad ((entry (gexp->derivation | |
597 | "gc-roots" | |
598 | #~(let ((roots '#$roots)) | |
599 | (mkdir #$output) | |
600 | (chdir #$output) | |
601 | (for-each symlink | |
602 | roots | |
603 | (map number->string | |
604 | (iota (length roots)))))))) | |
605 | (return (if (null? roots) | |
606 | '() | |
607 | `(("gc-roots" ,entry)))))) | |
608 | ||
609 | (define gc-root-service-type | |
610 | ;; A service to associate extra garbage-collector roots to the system. This | |
611 | ;; is a simple hack that guarantees that the system retains references to | |
612 | ;; the given list of roots. Roots must be "lowerable" objects like | |
613 | ;; packages, or derivations. | |
614 | (service-type (name 'gc-roots) | |
615 | (extensions | |
616 | (list (service-extension system-service-type | |
617 | gc-roots->system-entry))) | |
618 | (compose concatenate) | |
619 | (extend append))) | |
620 | ||
0adfe95a LC |
621 | \f |
622 | ;;; | |
623 | ;;; Service folding. | |
624 | ;;; | |
625 | ||
0adfe95a LC |
626 | (define-condition-type &missing-target-service-error &service-error |
627 | missing-target-service-error? | |
628 | (service missing-target-service-error-service) | |
629 | (target-type missing-target-service-error-target-type)) | |
630 | ||
631 | (define-condition-type &ambiguous-target-service-error &service-error | |
632 | ambiguous-target-service-error? | |
633 | (service ambiguous-target-service-error-service) | |
634 | (target-type ambiguous-target-service-error-target-type)) | |
635 | ||
d466b1fc LC |
636 | (define (missing-target-error service target-type) |
637 | (raise | |
638 | (condition (&missing-target-service-error | |
639 | (service service) | |
640 | (target-type target-type)) | |
641 | (&message | |
642 | (message | |
643 | (format #f (G_ "no target of type '~a' for service '~a'") | |
644 | (service-type-name target-type) | |
645 | (service-type-name | |
646 | (service-kind service)))))))) | |
647 | ||
0adfe95a LC |
648 | (define (service-back-edges services) |
649 | "Return a procedure that, when passed a <service>, returns the list of | |
650 | <service> objects that depend on it." | |
651 | (define (add-edges service edges) | |
652 | (define (add-edge extension edges) | |
653 | (let ((target-type (service-extension-target extension))) | |
654 | (match (filter (lambda (service) | |
655 | (eq? (service-kind service) target-type)) | |
656 | services) | |
657 | ((target) | |
658 | (vhash-consq target service edges)) | |
659 | (() | |
d466b1fc | 660 | (missing-target-error service target-type)) |
0adfe95a LC |
661 | (x |
662 | (raise | |
663 | (condition (&ambiguous-target-service-error | |
664 | (service service) | |
665 | (target-type target-type)) | |
666 | (&message | |
667 | (message | |
668 | (format #f | |
69daee23 | 669 | (G_ "more than one target service of type '~a'") |
0adfe95a LC |
670 | (service-type-name target-type)))))))))) |
671 | ||
672 | (fold add-edge edges (service-type-extensions (service-kind service)))) | |
673 | ||
674 | (let ((edges (fold add-edges vlist-null services))) | |
675 | (lambda (node) | |
676 | (reverse (vhash-foldq* cons '() node edges))))) | |
677 | ||
d466b1fc LC |
678 | (define (instantiate-missing-services services) |
679 | "Return SERVICES, a list, augmented with any services targeted by extensions | |
680 | and missing from SERVICES. Only service types with a default value can be | |
681 | instantiated; other missing services lead to a | |
682 | '&missing-target-service-error'." | |
683 | (define (adjust-service-list svc result instances) | |
684 | (fold2 (lambda (extension result instances) | |
685 | (define target-type | |
686 | (service-extension-target extension)) | |
687 | ||
688 | (match (vhash-assq target-type instances) | |
689 | (#f | |
690 | (let ((default (service-type-default-value target-type))) | |
691 | (if (eq? &no-default-value default) | |
692 | (missing-target-error svc target-type) | |
693 | (let ((new (service target-type))) | |
694 | (values (cons new result) | |
695 | (vhash-consq target-type new instances)))))) | |
696 | (_ | |
697 | (values result instances)))) | |
698 | result | |
699 | instances | |
700 | (service-type-extensions (service-kind svc)))) | |
701 | ||
702 | (let ((instances (fold (lambda (service result) | |
703 | (vhash-consq (service-kind service) service | |
704 | result)) | |
705 | vlist-null services))) | |
706 | (fold2 adjust-service-list | |
707 | services instances | |
708 | services))) | |
709 | ||
d62e201c LC |
710 | (define* (fold-services services |
711 | #:key (target-type system-service-type)) | |
0adfe95a LC |
712 | "Fold SERVICES by propagating their extensions down to the root of type |
713 | TARGET-TYPE; return the root service adjusted accordingly." | |
714 | (define dependents | |
715 | (service-back-edges services)) | |
716 | ||
717 | (define (matching-extension target) | |
718 | (let ((target (service-kind target))) | |
719 | (match-lambda | |
720 | (($ <service-extension> type) | |
721 | (eq? type target))))) | |
722 | ||
723 | (define (apply-extension target) | |
724 | (lambda (service) | |
725 | (match (find (matching-extension target) | |
726 | (service-type-extensions (service-kind service))) | |
727 | (($ <service-extension> _ compute) | |
efe7d19a | 728 | (compute (service-value service)))))) |
0adfe95a LC |
729 | |
730 | (match (filter (lambda (service) | |
731 | (eq? (service-kind service) target-type)) | |
732 | services) | |
733 | ((sink) | |
734 | (let loop ((sink sink)) | |
735 | (let* ((dependents (map loop (dependents sink))) | |
736 | (extensions (map (apply-extension sink) dependents)) | |
737 | (extend (service-type-extend (service-kind sink))) | |
738 | (compose (service-type-compose (service-kind sink))) | |
efe7d19a | 739 | (params (service-value sink))) |
0adfe95a LC |
740 | ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a |
741 | ;; different type than the elements of EXTENSIONS. | |
742 | (if extend | |
743 | (service (service-kind sink) | |
744 | (extend params (compose extensions))) | |
745 | sink)))) | |
746 | (() | |
747 | (raise | |
748 | (condition (&missing-target-service-error | |
749 | (service #f) | |
750 | (target-type target-type)) | |
751 | (&message | |
69daee23 | 752 | (message (format #f (G_ "service of type '~a' not found") |
0adfe95a LC |
753 | (service-type-name target-type))))))) |
754 | (x | |
755 | (raise | |
756 | (condition (&ambiguous-target-service-error | |
757 | (service #f) | |
758 | (target-type target-type)) | |
759 | (&message | |
760 | (message | |
761 | (format #f | |
69daee23 | 762 | (G_ "more than one target service of type '~a'") |
0adfe95a | 763 | (service-type-name target-type))))))))) |
db4fdc04 LC |
764 | |
765 | ;;; services.scm ends here. |