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 | |
aad8a143 LC |
371 | ;; Remove stale Shadow lock files as they would lead to |
372 | ;; failures of 'useradd' & co. | |
373 | (delete-file "/etc/group.lock") | |
374 | (delete-file "/etc/passwd.lock") | |
375 | (delete-file "/etc/.pwd.lock") ;from 'lckpwdf' | |
376 | ||
fd129893 LC |
377 | (delete-file-recursively "/tmp") |
378 | (delete-file-recursively "/var/run") | |
379 | (mkdir "/tmp") | |
380 | (chmod "/tmp" #o1777) | |
381 | (mkdir "/var/run") | |
382 | (chmod "/var/run" #o755)))))))) | |
be7be9e8 LC |
383 | |
384 | (define cleanup-service-type | |
385 | ;; Service that cleans things up in /tmp and similar. | |
386 | (service-type (name 'cleanup) | |
387 | (extensions | |
388 | (list (service-extension boot-service-type | |
389 | cleanup-gexp))))) | |
0adfe95a LC |
390 | |
391 | (define* (file-union name files) ;FIXME: Factorize. | |
392 | "Return a <computed-file> that builds a directory containing all of FILES. | |
393 | Each item in FILES must be a list where the first element is the file name to | |
394 | use in the new directory, and the second element is a gexp denoting the target | |
395 | file." | |
396 | (computed-file name | |
397 | #~(begin | |
398 | (mkdir #$output) | |
399 | (chdir #$output) | |
400 | #$@(map (match-lambda | |
401 | ((target source) | |
37dd1e6a LC |
402 | #~(begin |
403 | ;; Stat the source to abort early if it | |
404 | ;; does not exist. | |
405 | (stat #$source) | |
406 | ||
407 | (symlink #$source #$target)))) | |
0adfe95a LC |
408 | files)))) |
409 | ||
410 | (define (directory-union name things) | |
411 | "Return a directory that is the union of THINGS." | |
412 | (match things | |
413 | ((one) | |
414 | ;; Only one thing; return it. | |
415 | one) | |
416 | (_ | |
417 | (computed-file name | |
4ee96a79 LC |
418 | (with-imported-modules '((guix build union)) |
419 | #~(begin | |
420 | (use-modules (guix build union)) | |
421 | (union-build #$output '#$things))))))) | |
0adfe95a | 422 | |
0adfe95a LC |
423 | (define* (activation-service->script service) |
424 | "Return as a monadic value the activation script for SERVICE, a service of | |
425 | ACTIVATION-SCRIPT-TYPE." | |
efe7d19a | 426 | (activation-script (service-value service))) |
0adfe95a LC |
427 | |
428 | (define (activation-script gexps) | |
429 | "Return the system's activation script, which evaluates GEXPS." | |
0adfe95a LC |
430 | (define (service-activations) |
431 | ;; Return the activation scripts for SERVICES. | |
432 | (mapm %store-monad | |
433 | (cut gexp->file "activate-service" <>) | |
434 | gexps)) | |
435 | ||
fd129893 | 436 | (mlet* %store-monad ((actions (service-activations))) |
0adfe95a | 437 | (gexp->file "activate" |
232ccbef | 438 | (with-imported-modules (source-module-closure |
fe2b6434 CB |
439 | '((gnu build activation) |
440 | (guix build utils))) | |
fd129893 | 441 | #~(begin |
fe2b6434 CB |
442 | (use-modules (gnu build activation) |
443 | (guix build utils)) | |
0adfe95a | 444 | |
caa78166 LC |
445 | ;; Make sure the user accounting database exists. If it |
446 | ;; does not exist, 'setutxent' does not create it and | |
447 | ;; thus there is no accounting at all. | |
448 | (close-port (open-file "/var/run/utmpx" "a0")) | |
449 | ||
2986995b LC |
450 | ;; Same for 'wtmp', which is populated by mingetty et |
451 | ;; al. | |
fe2b6434 | 452 | (mkdir-p "/var/log") |
2986995b LC |
453 | (close-port (open-file "/var/log/wtmp" "a0")) |
454 | ||
97bb1ab6 CB |
455 | ;; Set up /run/current-system. Among other things this |
456 | ;; sets up locales, which the activation snippets | |
457 | ;; executed below may expect. | |
458 | (activate-current-system) | |
459 | ||
fd129893 LC |
460 | ;; Run the services' activation snippets. |
461 | ;; TODO: Use 'load-compiled'. | |
97bb1ab6 | 462 | (for-each primitive-load '#$actions)))))) |
0adfe95a LC |
463 | |
464 | (define (gexps->activation-gexp gexps) | |
465 | "Return a gexp that runs the activation script containing GEXPS." | |
466 | (mlet %store-monad ((script (activation-script gexps))) | |
467 | (return #~(primitive-load #$script)))) | |
468 | ||
3a391e68 LC |
469 | (define (second-argument a b) b) |
470 | ||
0adfe95a LC |
471 | (define activation-service-type |
472 | (service-type (name 'activate) | |
473 | (extensions | |
474 | (list (service-extension boot-service-type | |
475 | gexps->activation-gexp))) | |
476 | (compose append) | |
477 | (extend second-argument))) | |
478 | ||
479 | (define %activation-service | |
480 | ;; The activation service produces the activation script from the gexps it | |
481 | ;; receives. | |
482 | (service activation-service-type #t)) | |
483 | ||
a241a7ac LC |
484 | (define %modprobe-wrapper |
485 | ;; Wrapper for the 'modprobe' command that knows where modules live. | |
486 | ;; | |
487 | ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe', | |
488 | ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' | |
489 | ;; environment variable is not set---hence the need for this wrapper. | |
490 | (let ((modprobe "/run/current-system/profile/bin/modprobe")) | |
491 | (program-file "modprobe" | |
492 | #~(begin | |
493 | (setenv "LINUX_MODULE_DIRECTORY" | |
494 | "/run/booted-system/kernel/lib/modules") | |
495 | (apply execl #$modprobe | |
496 | (cons #$modprobe (cdr (command-line)))))))) | |
497 | ||
498 | (define %linux-kernel-activation | |
499 | ;; Activation of the Linux kernel running on the bare metal (as opposed to | |
500 | ;; running in a container.) | |
501 | #~(begin | |
502 | ;; Tell the kernel to use our 'modprobe' command. | |
503 | (activate-modprobe #$%modprobe-wrapper) | |
504 | ||
505 | ;; Let users debug their own processes! | |
506 | (activate-ptrace-attach))) | |
507 | ||
a241a7ac LC |
508 | (define %linux-bare-metal-service |
509 | ;; The service that does things that are needed on the "bare metal", but not | |
510 | ;; necessary or impossible in a container. | |
6ddf4fcf LC |
511 | (simple-service 'linux-bare-metal |
512 | activation-service-type | |
513 | %linux-kernel-activation)) | |
514 | ||
a241a7ac | 515 | |
387e1754 LC |
516 | (define special-files-service-type |
517 | ;; Service to install "special files" such as /bin/sh and /usr/bin/env. | |
518 | (service-type | |
519 | (name 'special-files) | |
520 | (extensions | |
521 | (list (service-extension activation-service-type | |
522 | (lambda (files) | |
523 | #~(activate-special-files '#$files))))) | |
524 | (compose concatenate) | |
525 | (extend append))) | |
526 | ||
527 | (define (extra-special-file file target) | |
528 | "Use TARGET as the \"special file\" FILE. For example, TARGET might be | |
529 | (file-append coreutils \"/bin/env\") | |
530 | and FILE could be \"/usr/bin/env\"." | |
531 | (simple-service (string->symbol (string-append "special-file-" file)) | |
532 | special-files-service-type | |
533 | `((,file ,target)))) | |
534 | ||
0adfe95a LC |
535 | (define (etc-directory service) |
536 | "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE." | |
efe7d19a | 537 | (files->etc-directory (service-value service))) |
0adfe95a LC |
538 | |
539 | (define (files->etc-directory files) | |
540 | (file-union "etc" files)) | |
541 | ||
d62e201c LC |
542 | (define (etc-entry files) |
543 | "Return an entry for the /etc directory consisting of FILES in the system | |
544 | directory." | |
545 | (with-monad %store-monad | |
546 | (return `(("etc" ,(files->etc-directory files)))))) | |
547 | ||
0adfe95a LC |
548 | (define etc-service-type |
549 | (service-type (name 'etc) | |
550 | (extensions | |
551 | (list | |
552 | (service-extension activation-service-type | |
553 | (lambda (files) | |
554 | (let ((etc | |
555 | (files->etc-directory files))) | |
d62e201c LC |
556 | #~(activate-etc #$etc)))) |
557 | (service-extension system-service-type etc-entry))) | |
0adfe95a LC |
558 | (compose concatenate) |
559 | (extend append))) | |
560 | ||
561 | (define (etc-service files) | |
562 | "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES. | |
563 | FILES must be a list of name/file-like object pairs." | |
564 | (service etc-service-type files)) | |
565 | ||
566 | (define setuid-program-service-type | |
567 | (service-type (name 'setuid-program) | |
568 | (extensions | |
569 | (list (service-extension activation-service-type | |
570 | (lambda (programs) | |
571 | #~(activate-setuid-programs | |
572 | (list #$@programs)))))) | |
573 | (compose concatenate) | |
574 | (extend append))) | |
575 | ||
af4c3fd5 LC |
576 | (define (packages->profile-entry packages) |
577 | "Return a system entry for the profile containing PACKAGES." | |
578 | (mlet %store-monad ((profile (profile-derivation | |
60a0886d SB |
579 | (packages->manifest |
580 | (delete-duplicates packages eq?))))) | |
af4c3fd5 LC |
581 | (return `(("profile" ,profile))))) |
582 | ||
583 | (define profile-service-type | |
584 | ;; The service that populates the system's profile---i.e., | |
585 | ;; /run/current-system/profile. It is extended by package lists. | |
586 | (service-type (name 'profile) | |
587 | (extensions | |
588 | (list (service-extension system-service-type | |
589 | packages->profile-entry))) | |
590 | (compose concatenate) | |
591 | (extend append))) | |
592 | ||
0adfe95a LC |
593 | (define (firmware->activation-gexp firmware) |
594 | "Return a gexp to make the packages listed in FIRMWARE loadable by the | |
595 | kernel." | |
596 | (let ((directory (directory-union "firmware" firmware))) | |
597 | ;; Tell the kernel where firmware is. | |
598 | #~(activate-firmware (string-append #$directory "/lib/firmware")))) | |
599 | ||
600 | (define firmware-service-type | |
601 | ;; The service that collects firmware. | |
602 | (service-type (name 'firmware) | |
603 | (extensions | |
604 | (list (service-extension activation-service-type | |
605 | firmware->activation-gexp))) | |
606 | (compose concatenate) | |
607 | (extend append))) | |
608 | ||
e0b47290 LC |
609 | (define (gc-roots->system-entry roots) |
610 | "Return an entry in the system's output containing symlinks to ROOTS." | |
611 | (mlet %store-monad ((entry (gexp->derivation | |
612 | "gc-roots" | |
613 | #~(let ((roots '#$roots)) | |
614 | (mkdir #$output) | |
615 | (chdir #$output) | |
616 | (for-each symlink | |
617 | roots | |
618 | (map number->string | |
619 | (iota (length roots)))))))) | |
620 | (return (if (null? roots) | |
621 | '() | |
622 | `(("gc-roots" ,entry)))))) | |
623 | ||
624 | (define gc-root-service-type | |
625 | ;; A service to associate extra garbage-collector roots to the system. This | |
626 | ;; is a simple hack that guarantees that the system retains references to | |
627 | ;; the given list of roots. Roots must be "lowerable" objects like | |
628 | ;; packages, or derivations. | |
629 | (service-type (name 'gc-roots) | |
630 | (extensions | |
631 | (list (service-extension system-service-type | |
632 | gc-roots->system-entry))) | |
633 | (compose concatenate) | |
634 | (extend append))) | |
635 | ||
0adfe95a LC |
636 | \f |
637 | ;;; | |
638 | ;;; Service folding. | |
639 | ;;; | |
640 | ||
0adfe95a LC |
641 | (define-condition-type &missing-target-service-error &service-error |
642 | missing-target-service-error? | |
643 | (service missing-target-service-error-service) | |
644 | (target-type missing-target-service-error-target-type)) | |
645 | ||
646 | (define-condition-type &ambiguous-target-service-error &service-error | |
647 | ambiguous-target-service-error? | |
648 | (service ambiguous-target-service-error-service) | |
649 | (target-type ambiguous-target-service-error-target-type)) | |
650 | ||
651 | (define (service-back-edges services) | |
652 | "Return a procedure that, when passed a <service>, returns the list of | |
653 | <service> objects that depend on it." | |
654 | (define (add-edges service edges) | |
655 | (define (add-edge extension edges) | |
656 | (let ((target-type (service-extension-target extension))) | |
657 | (match (filter (lambda (service) | |
658 | (eq? (service-kind service) target-type)) | |
659 | services) | |
660 | ((target) | |
661 | (vhash-consq target service edges)) | |
662 | (() | |
663 | (raise | |
664 | (condition (&missing-target-service-error | |
665 | (service service) | |
666 | (target-type target-type)) | |
667 | (&message | |
668 | (message | |
638e9dea | 669 | (format #f (G_ "no target of type '~a' for service '~a'") |
0adfe95a | 670 | (service-type-name target-type) |
638e9dea LC |
671 | (service-type-name |
672 | (service-kind service)))))))) | |
0adfe95a LC |
673 | (x |
674 | (raise | |
675 | (condition (&ambiguous-target-service-error | |
676 | (service service) | |
677 | (target-type target-type)) | |
678 | (&message | |
679 | (message | |
680 | (format #f | |
69daee23 | 681 | (G_ "more than one target service of type '~a'") |
0adfe95a LC |
682 | (service-type-name target-type)))))))))) |
683 | ||
684 | (fold add-edge edges (service-type-extensions (service-kind service)))) | |
685 | ||
686 | (let ((edges (fold add-edges vlist-null services))) | |
687 | (lambda (node) | |
688 | (reverse (vhash-foldq* cons '() node edges))))) | |
689 | ||
d62e201c LC |
690 | (define* (fold-services services |
691 | #:key (target-type system-service-type)) | |
0adfe95a LC |
692 | "Fold SERVICES by propagating their extensions down to the root of type |
693 | TARGET-TYPE; return the root service adjusted accordingly." | |
694 | (define dependents | |
695 | (service-back-edges services)) | |
696 | ||
697 | (define (matching-extension target) | |
698 | (let ((target (service-kind target))) | |
699 | (match-lambda | |
700 | (($ <service-extension> type) | |
701 | (eq? type target))))) | |
702 | ||
703 | (define (apply-extension target) | |
704 | (lambda (service) | |
705 | (match (find (matching-extension target) | |
706 | (service-type-extensions (service-kind service))) | |
707 | (($ <service-extension> _ compute) | |
efe7d19a | 708 | (compute (service-value service)))))) |
0adfe95a LC |
709 | |
710 | (match (filter (lambda (service) | |
711 | (eq? (service-kind service) target-type)) | |
712 | services) | |
713 | ((sink) | |
714 | (let loop ((sink sink)) | |
715 | (let* ((dependents (map loop (dependents sink))) | |
716 | (extensions (map (apply-extension sink) dependents)) | |
717 | (extend (service-type-extend (service-kind sink))) | |
718 | (compose (service-type-compose (service-kind sink))) | |
efe7d19a | 719 | (params (service-value sink))) |
0adfe95a LC |
720 | ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a |
721 | ;; different type than the elements of EXTENSIONS. | |
722 | (if extend | |
723 | (service (service-kind sink) | |
724 | (extend params (compose extensions))) | |
725 | sink)))) | |
726 | (() | |
727 | (raise | |
728 | (condition (&missing-target-service-error | |
729 | (service #f) | |
730 | (target-type target-type)) | |
731 | (&message | |
69daee23 | 732 | (message (format #f (G_ "service of type '~a' not found") |
0adfe95a LC |
733 | (service-type-name target-type))))))) |
734 | (x | |
735 | (raise | |
736 | (condition (&ambiguous-target-service-error | |
737 | (service #f) | |
738 | (target-type target-type)) | |
739 | (&message | |
740 | (message | |
741 | (format #f | |
69daee23 | 742 | (G_ "more than one target service of type '~a'") |
0adfe95a | 743 | (service-type-name target-type))))))))) |
db4fdc04 LC |
744 | |
745 | ;;; services.scm ends here. |