Commit | Line | Data |
---|---|---|
db4fdc04 | 1 | ;;; GNU Guix --- Functional package management for GNU |
223ede4e | 2 | ;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
4d343a14 | 3 | ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com> |
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) |
33b7cb7a LC |
28 | #:use-module (guix channels) |
29 | #:use-module (guix describe) | |
0adfe95a LC |
30 | #:use-module (guix sets) |
31 | #:use-module (guix ui) | |
1bb895ea | 32 | #:use-module ((guix utils) #:select (source-properties->location)) |
232ccbef | 33 | #:use-module (guix modules) |
0adfe95a LC |
34 | #:use-module (gnu packages base) |
35 | #:use-module (gnu packages bash) | |
36 | #:use-module (srfi srfi-1) | |
37 | #:use-module (srfi srfi-9) | |
38 | #:use-module (srfi srfi-9 gnu) | |
39 | #:use-module (srfi srfi-26) | |
40 | #:use-module (srfi srfi-34) | |
41 | #:use-module (srfi srfi-35) | |
42 | #:use-module (ice-9 vlist) | |
43 | #:use-module (ice-9 match) | |
33b7cb7a | 44 | #:autoload (ice-9 pretty-print) (pretty-print) |
0adfe95a LC |
45 | #:export (service-extension |
46 | service-extension? | |
7d8b5913 CB |
47 | service-extension-target |
48 | service-extension-compute | |
0adfe95a LC |
49 | |
50 | service-type | |
51 | service-type? | |
5152d13b LC |
52 | service-type-name |
53 | service-type-extensions | |
54 | service-type-compose | |
55 | service-type-extend | |
1bb895ea | 56 | service-type-default-value |
b714395a LC |
57 | service-type-description |
58 | service-type-location | |
59 | ||
0c0c1b21 LC |
60 | %service-type-path |
61 | fold-service-types | |
49483f71 | 62 | lookup-service-types |
0adfe95a | 63 | |
db4fdc04 | 64 | service |
0adfe95a LC |
65 | service? |
66 | service-kind | |
efe7d19a LC |
67 | service-value |
68 | service-parameters ;deprecated | |
0adfe95a | 69 | |
71654dfd | 70 | simple-service |
cd6f6c22 | 71 | modify-services |
5152d13b | 72 | service-back-edges |
d466b1fc | 73 | instantiate-missing-services |
0adfe95a LC |
74 | fold-services |
75 | ||
76 | service-error? | |
1bb895ea LC |
77 | missing-value-service-error? |
78 | missing-value-service-error-type | |
79 | missing-value-service-error-location | |
0adfe95a LC |
80 | missing-target-service-error? |
81 | missing-target-service-error-service | |
82 | missing-target-service-error-target-type | |
83 | ambiguous-target-service-error? | |
84 | ambiguous-target-service-error-service | |
85 | ambiguous-target-service-error-target-type | |
86 | ||
d62e201c | 87 | system-service-type |
33b7cb7a | 88 | provenance-service-type |
0adfe95a | 89 | boot-service-type |
be7be9e8 | 90 | cleanup-service-type |
0adfe95a LC |
91 | activation-service-type |
92 | activation-service->script | |
a241a7ac | 93 | %linux-bare-metal-service |
387e1754 LC |
94 | special-files-service-type |
95 | extra-special-file | |
0adfe95a LC |
96 | etc-service-type |
97 | etc-directory | |
98 | setuid-program-service-type | |
af4c3fd5 | 99 | profile-service-type |
0adfe95a | 100 | firmware-service-type |
e0b47290 | 101 | gc-root-service-type |
0adfe95a LC |
102 | |
103 | %boot-service | |
104 | %activation-service | |
d298c815 | 105 | etc-service)) |
0adfe95a LC |
106 | |
107 | ;;; Comment: | |
108 | ;;; | |
109 | ;;; This module defines a broad notion of "service types" and "services." | |
db4fdc04 | 110 | ;;; |
0adfe95a LC |
111 | ;;; A service type describe how its instances extend instances of other |
112 | ;;; service types. For instance, some services extend the instance of | |
113 | ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create; | |
d4053c71 AK |
114 | ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of |
115 | ;;; <shepherd-service>. | |
0adfe95a LC |
116 | ;;; |
117 | ;;; When applicable, the service type defines how it can itself be extended, | |
118 | ;;; by providing one procedure to compose extensions, and one procedure to | |
119 | ;;; extend itself. | |
120 | ;;; | |
d62e201c LC |
121 | ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single |
122 | ;;; instance, which is the root of the service DAG. Its value is the | |
123 | ;;; derivation that produces the 'system' directory as returned by | |
124 | ;;; 'operating-system-derivation'. | |
0adfe95a LC |
125 | ;;; |
126 | ;;; The 'fold-services' procedure can be passed a list of procedures, which it | |
127 | ;;; "folds" by propagating extensions down the graph; it returns the root | |
128 | ;;; service after the applying all its extensions. | |
db4fdc04 LC |
129 | ;;; |
130 | ;;; Code: | |
131 | ||
0adfe95a LC |
132 | (define-record-type <service-extension> |
133 | (service-extension target compute) | |
134 | service-extension? | |
135 | (target service-extension-target) ;<service-type> | |
136 | (compute service-extension-compute)) ;params -> params | |
137 | ||
1bb895ea LC |
138 | (define &no-default-value |
139 | ;; Value used to denote service types that have no associated default value. | |
140 | '(no default value)) | |
141 | ||
0adfe95a LC |
142 | (define-record-type* <service-type> service-type make-service-type |
143 | service-type? | |
144 | (name service-type-name) ;symbol (for debugging) | |
145 | ||
146 | ;; Things extended by services of this type. | |
147 | (extensions service-type-extensions) ;list of <service-extensions> | |
148 | ||
149 | ;; Given a list of extensions, "compose" them. | |
150 | (compose service-type-compose ;list of Any -> Any | |
151 | (default #f)) | |
152 | ||
153 | ;; Extend the services' own parameters with the extension composition. | |
154 | (extend service-type-extend ;list of Any -> parameters | |
1bb895ea LC |
155 | (default #f)) |
156 | ||
157 | ;; Optional default value for instances of this type. | |
158 | (default-value service-type-default-value ;Any | |
b714395a LC |
159 | (default &no-default-value)) |
160 | ||
161 | ;; Meta-data. | |
162 | (description service-type-description ;string | |
163 | (default #f)) | |
164 | (location service-type-location ;<location> | |
165 | (default (and=> (current-source-location) | |
166 | source-properties->location)) | |
167 | (innate))) | |
0adfe95a LC |
168 | |
169 | (define (write-service-type type port) | |
170 | (format port "#<service-type ~a ~a>" | |
171 | (service-type-name type) | |
172 | (number->string (object-address type) 16))) | |
173 | ||
174 | (set-record-type-printer! <service-type> write-service-type) | |
175 | ||
0c0c1b21 LC |
176 | (define %distro-root-directory |
177 | ;; Absolute file name of the module hierarchy. | |
178 | (dirname (search-path %load-path "guix.scm"))) | |
179 | ||
180 | (define %service-type-path | |
181 | ;; Search path for service types. | |
182 | (make-parameter `((,%distro-root-directory . "gnu/services") | |
183 | (,%distro-root-directory . "gnu/system")))) | |
184 | ||
3943913f LC |
185 | (define (all-service-modules) |
186 | "Return the default set of service modules." | |
187 | (cons (resolve-interface '(gnu services)) | |
3c0128b0 LC |
188 | (all-modules (%service-type-path) |
189 | #:warn warn-about-load-error))) | |
3943913f | 190 | |
0c0c1b21 LC |
191 | (define* (fold-service-types proc seed |
192 | #:optional | |
3943913f | 193 | (modules (all-service-modules))) |
0c0c1b21 LC |
194 | "For each service type exported by one of MODULES, call (PROC RESULT). SEED |
195 | is used as the initial value of RESULT." | |
196 | (fold-module-public-variables (lambda (object result) | |
197 | (if (service-type? object) | |
198 | (proc object result) | |
199 | result)) | |
a3d37f3a | 200 | seed |
0c0c1b21 LC |
201 | modules)) |
202 | ||
49483f71 LC |
203 | (define lookup-service-types |
204 | (let ((table | |
205 | (delay (fold-service-types (lambda (type result) | |
206 | (vhash-consq (service-type-name type) | |
207 | type result)) | |
208 | vlist-null)))) | |
209 | (lambda (name) | |
210 | "Return the list of services with the given NAME (a symbol)." | |
211 | (vhash-foldq* cons '() name (force table))))) | |
212 | ||
0adfe95a LC |
213 | ;; Services of a given type. |
214 | (define-record-type <service> | |
1bb895ea | 215 | (make-service type value) |
db4fdc04 | 216 | service? |
0adfe95a | 217 | (type service-kind) |
efe7d19a LC |
218 | (value service-value)) |
219 | ||
1bb895ea LC |
220 | (define-syntax service |
221 | (syntax-rules () | |
222 | "Return a service instance of TYPE. The service value is VALUE or, if | |
223 | omitted, TYPE's default value." | |
224 | ((_ type value) | |
225 | (make-service type value)) | |
226 | ((_ type) | |
227 | (%service-with-default-value (current-source-location) | |
228 | type)))) | |
229 | ||
230 | (define (%service-with-default-value location type) | |
231 | "Return a instance of service type TYPE with its default value, if any. If | |
232 | TYPE does not have a default value, an error is raised." | |
233 | ;; TODO: Currently this is a run-time error but with a little bit macrology | |
234 | ;; we could turn it into an expansion-time error. | |
235 | (let ((default (service-type-default-value type))) | |
236 | (if (eq? default &no-default-value) | |
237 | (let ((location (source-properties->location location))) | |
238 | (raise | |
239 | (condition | |
240 | (&missing-value-service-error (type type) (location location)) | |
241 | (&message | |
69daee23 | 242 | (message (format #f (G_ "~a: no value specified \ |
1bb895ea LC |
243 | for service of type '~a'") |
244 | (location->string location) | |
245 | (service-type-name type))))))) | |
246 | (service type default)))) | |
247 | ||
248 | (define-condition-type &service-error &error | |
249 | service-error?) | |
250 | ||
251 | (define-condition-type &missing-value-service-error &service-error | |
252 | missing-value-service-error? | |
253 | (type missing-value-service-error-type) | |
254 | (location missing-value-service-error-location)) | |
255 | ||
256 | ||
257 | \f | |
258 | ;;; | |
259 | ;;; Helpers. | |
260 | ;;; | |
261 | ||
efe7d19a LC |
262 | (define service-parameters |
263 | ;; Deprecated alias. | |
264 | service-value) | |
0adfe95a | 265 | |
71654dfd LC |
266 | (define (simple-service name target value) |
267 | "Return a service that extends TARGET with VALUE. This works by creating a | |
268 | singleton service type NAME, of which the returned service is an instance." | |
269 | (let* ((extension (service-extension target identity)) | |
270 | (type (service-type (name name) | |
271 | (extensions (list extension))))) | |
272 | (service type value))) | |
0adfe95a | 273 | |
cd6f6c22 LC |
274 | (define-syntax %modify-service |
275 | (syntax-rules (=>) | |
276 | ((_ service) | |
277 | service) | |
278 | ((_ svc (kind param => exp ...) clauses ...) | |
279 | (if (eq? (service-kind svc) kind) | |
efe7d19a | 280 | (let ((param (service-value svc))) |
cd6f6c22 LC |
281 | (service (service-kind svc) |
282 | (begin exp ...))) | |
283 | (%modify-service svc clauses ...))))) | |
284 | ||
285 | (define-syntax modify-services | |
286 | (syntax-rules () | |
4d343a14 CM |
287 | "Modify the services listed in SERVICES according to CLAUSES and return |
288 | the resulting list of services. Each clause must have the form: | |
cd6f6c22 LC |
289 | |
290 | (TYPE VARIABLE => BODY) | |
291 | ||
292 | where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an | |
293 | identifier that is bound within BODY to the value of the service of that | |
294 | TYPE. Consider this example: | |
295 | ||
296 | (modify-services %base-services | |
297 | (guix-service-type config => | |
298 | (guix-configuration | |
299 | (inherit config) | |
300 | (use-substitutes? #f) | |
301 | (extra-options '(\"--gc-keep-derivations\")))) | |
302 | (mingetty-service-type config => | |
303 | (mingetty-configuration | |
304 | (inherit config) | |
305 | (motd (plain-file \"motd\" \"Hi there!\"))))) | |
306 | ||
307 | It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of | |
308 | all the MINGETTY-SERVICE-TYPE instances. | |
309 | ||
310 | This is a shorthand for (map (lambda (svc) ...) %base-services)." | |
311 | ((_ services clauses ...) | |
312 | (map (lambda (service) | |
313 | (%modify-service service clauses ...)) | |
314 | services)))) | |
0adfe95a LC |
315 | |
316 | \f | |
317 | ;;; | |
318 | ;;; Core services. | |
319 | ;;; | |
320 | ||
d62e201c LC |
321 | (define (system-derivation mentries mextensions) |
322 | "Return as a monadic value the derivation of the 'system' directory | |
323 | containing the given entries." | |
324 | (mlet %store-monad ((entries mentries) | |
223ede4e LC |
325 | (extensions (mapm/accumulate-builds identity |
326 | mextensions))) | |
d62e201c LC |
327 | (lower-object |
328 | (file-union "system" | |
329 | (append entries (concatenate extensions)))))) | |
330 | ||
331 | (define system-service-type | |
332 | ;; This is the ultimate service type, the root of the service DAG. The | |
333 | ;; service of this type is extended by monadic name/item pairs. These items | |
334 | ;; end up in the "system directory" as returned by | |
335 | ;; 'operating-system-derivation'. | |
336 | (service-type (name 'system) | |
337 | (extensions '()) | |
338 | (compose identity) | |
636bb2b5 LC |
339 | (extend system-derivation) |
340 | (description | |
341 | "Build the operating system top-level directory, which in | |
342 | turn refers to everything the operating system needs: its kernel, initrd, | |
343 | system profile, boot script, and so on."))) | |
d62e201c | 344 | |
378daa8c LC |
345 | (define (compute-boot-script _ gexps) |
346 | ;; Reverse GEXPS so that extensions appear in the boot script in the right | |
661c237b LC |
347 | ;; order. That is, user extensions would come first, and extensions added |
348 | ;; by 'essential-services' (e.g., running shepherd) are guaranteed to come | |
349 | ;; last. | |
378daa8c LC |
350 | (gexp->file "boot" |
351 | ;; Clean up and activate the system, then spawn shepherd. | |
352 | #~(begin #$@(reverse gexps)))) | |
0adfe95a | 353 | |
d62e201c LC |
354 | (define (boot-script-entry mboot) |
355 | "Return, as a monadic value, an entry for the boot script in the system | |
356 | directory." | |
357 | (mlet %store-monad ((boot mboot)) | |
358 | (return `(("boot" ,boot))))) | |
359 | ||
0adfe95a | 360 | (define boot-service-type |
378daa8c LC |
361 | ;; The service of this type is extended by being passed gexps. It |
362 | ;; aggregates them in a single script, as a monadic value, which becomes its | |
363 | ;; value. | |
0adfe95a | 364 | (service-type (name 'boot) |
d62e201c LC |
365 | (extensions |
366 | (list (service-extension system-service-type | |
367 | boot-script-entry))) | |
7874e9e0 | 368 | (compose identity) |
636bb2b5 LC |
369 | (extend compute-boot-script) |
370 | (description | |
371 | "Produce the operating system's boot script, which is spawned | |
372 | by the initrd once the root file system is mounted."))) | |
0adfe95a LC |
373 | |
374 | (define %boot-service | |
d62e201c | 375 | ;; The service that produces the boot script. |
0adfe95a | 376 | (service boot-service-type #t)) |
be7be9e8 | 377 | |
33b7cb7a LC |
378 | \f |
379 | ;;; | |
380 | ;;; Provenance tracking. | |
381 | ;;; | |
382 | ||
383 | (define (object->pretty-string obj) | |
384 | "Like 'object->string', but using 'pretty-print'." | |
385 | (call-with-output-string | |
386 | (lambda (port) | |
387 | (pretty-print obj port)))) | |
388 | ||
389 | (define (channel->code channel) | |
390 | "Return code to build CHANNEL, ready to be dropped in a 'channels.scm' | |
391 | file." | |
392 | `(channel (name ',(channel-name channel)) | |
393 | (url ,(channel-url channel)) | |
394 | (branch ,(channel-branch channel)) | |
395 | (commit ,(channel-commit channel)))) | |
396 | ||
397 | (define (channel->sexp channel) | |
398 | "Return an sexp describing CHANNEL. The sexp is _not_ code and is meant to | |
399 | be parsed by tools; it's potentially more future-proof than code." | |
400 | `(channel (name ,(channel-name channel)) | |
401 | (url ,(channel-url channel)) | |
402 | (branch ,(channel-branch channel)) | |
403 | (commit ,(channel-commit channel)))) | |
404 | ||
405 | (define (provenance-file channels config-file) | |
406 | "Return a 'provenance' file describing CHANNELS, a list of channels, and | |
407 | CONFIG-FILE, which can be either #f or a <local-file> containing the OS | |
408 | configuration being used." | |
409 | (scheme-file "provenance" | |
410 | #~(provenance | |
411 | (version 0) | |
412 | (channels #+@(if channels | |
413 | (map channel->sexp channels) | |
414 | '())) | |
415 | (configuration-file #+config-file)))) | |
416 | ||
417 | (define (provenance-entry config-file) | |
418 | "Return system entries describing the operating system provenance: the | |
419 | channels in use and CONFIG-FILE, if it is true." | |
420 | (define profile | |
421 | (current-profile)) | |
422 | ||
423 | (define channels | |
424 | (and=> profile profile-channels)) | |
425 | ||
426 | (mbegin %store-monad | |
427 | (let ((config-file (cond ((string? config-file) | |
428 | (local-file config-file "configuration.scm")) | |
429 | ((not config-file) | |
430 | #f) | |
431 | (else | |
432 | config-file)))) | |
433 | (return `(("provenance" ,(provenance-file channels config-file)) | |
434 | ,@(if channels | |
435 | `(("channels.scm" | |
436 | ,(plain-file "channels.scm" | |
437 | (object->pretty-string | |
438 | `(list | |
439 | ,@(map channel->code channels)))))) | |
440 | '()) | |
441 | ,@(if config-file | |
442 | `(("configuration.scm" ,config-file)) | |
443 | '())))))) | |
444 | ||
445 | (define provenance-service-type | |
446 | (service-type (name 'provenance) | |
447 | (extensions | |
448 | (list (service-extension system-service-type | |
449 | provenance-entry))) | |
450 | (default-value #f) ;the OS config file | |
451 | (description | |
452 | "Store provenance information about the system in the system | |
453 | itself: the channels used when building the system, and its configuration | |
454 | file, when available."))) | |
455 | ||
456 | \f | |
457 | ;;; | |
458 | ;;; Cleanup. | |
459 | ;;; | |
460 | ||
be7be9e8 | 461 | (define (cleanup-gexp _) |
378daa8c LC |
462 | "Return a gexp to clean up /tmp and similar places upon boot." |
463 | (with-imported-modules '((guix build utils)) | |
464 | #~(begin | |
465 | (use-modules (guix build utils)) | |
466 | ||
467 | ;; Clean out /tmp and /var/run. | |
468 | ;; | |
469 | ;; XXX This needs to happen before service activations, so it | |
470 | ;; has to be here, but this also implicitly assumes that /tmp | |
471 | ;; and /var/run are on the root partition. | |
472 | (letrec-syntax ((fail-safe (syntax-rules () | |
473 | ((_ exp rest ...) | |
474 | (begin | |
475 | (catch 'system-error | |
476 | (lambda () exp) | |
477 | (const #f)) | |
478 | (fail-safe rest ...))) | |
479 | ((_) | |
480 | #t)))) | |
481 | ;; Ignore I/O errors so the system can boot. | |
482 | (fail-safe | |
483 | ;; Remove stale Shadow lock files as they would lead to | |
484 | ;; failures of 'useradd' & co. | |
485 | (delete-file "/etc/group.lock") | |
486 | (delete-file "/etc/passwd.lock") | |
487 | (delete-file "/etc/.pwd.lock") ;from 'lckpwdf' | |
488 | ||
489 | ;; Force file names to be decoded as UTF-8. See | |
490 | ;; <https://bugs.gnu.org/26353>. | |
491 | (setenv "GUIX_LOCPATH" | |
492 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
493 | (setlocale LC_CTYPE "en_US.utf8") | |
494 | (delete-file-recursively "/tmp") | |
495 | (delete-file-recursively "/var/run") | |
496 | ||
497 | (mkdir "/tmp") | |
498 | (chmod "/tmp" #o1777) | |
499 | (mkdir "/var/run") | |
500 | (chmod "/var/run" #o755) | |
501 | (delete-file-recursively "/run/udev/watch.old")))))) | |
be7be9e8 LC |
502 | |
503 | (define cleanup-service-type | |
504 | ;; Service that cleans things up in /tmp and similar. | |
505 | (service-type (name 'cleanup) | |
506 | (extensions | |
507 | (list (service-extension boot-service-type | |
636bb2b5 LC |
508 | cleanup-gexp))) |
509 | (description | |
510 | "Delete files from @file{/tmp}, @file{/var/run}, and other | |
511 | temporary locations at boot time."))) | |
0adfe95a | 512 | |
0adfe95a LC |
513 | (define* (activation-service->script service) |
514 | "Return as a monadic value the activation script for SERVICE, a service of | |
515 | ACTIVATION-SCRIPT-TYPE." | |
efe7d19a | 516 | (activation-script (service-value service))) |
0adfe95a LC |
517 | |
518 | (define (activation-script gexps) | |
519 | "Return the system's activation script, which evaluates GEXPS." | |
378daa8c | 520 | (define actions |
03cbd94d JK |
521 | (map (cut program-file "activate-service.scm" <>) gexps)) |
522 | ||
523 | (program-file "activate.scm" | |
524 | (with-imported-modules (source-module-closure | |
525 | '((gnu build activation) | |
526 | (guix build utils))) | |
527 | #~(begin | |
528 | (use-modules (gnu build activation) | |
529 | (guix build utils)) | |
530 | ||
531 | ;; Make sure the user accounting database exists. If it | |
532 | ;; does not exist, 'setutxent' does not create it and | |
533 | ;; thus there is no accounting at all. | |
534 | (close-port (open-file "/var/run/utmpx" "a0")) | |
535 | ||
536 | ;; Same for 'wtmp', which is populated by mingetty et | |
537 | ;; al. | |
538 | (mkdir-p "/var/log") | |
539 | (close-port (open-file "/var/log/wtmp" "a0")) | |
540 | ||
541 | ;; Set up /run/current-system. Among other things this | |
542 | ;; sets up locales, which the activation snippets | |
543 | ;; executed below may expect. | |
544 | (activate-current-system) | |
545 | ||
546 | ;; Run the services' activation snippets. | |
547 | ;; TODO: Use 'load-compiled'. | |
548 | (for-each primitive-load '#$actions))))) | |
0adfe95a LC |
549 | |
550 | (define (gexps->activation-gexp gexps) | |
551 | "Return a gexp that runs the activation script containing GEXPS." | |
378daa8c | 552 | #~(primitive-load #$(activation-script gexps))) |
0adfe95a | 553 | |
3a391e68 LC |
554 | (define (second-argument a b) b) |
555 | ||
0adfe95a LC |
556 | (define activation-service-type |
557 | (service-type (name 'activate) | |
558 | (extensions | |
559 | (list (service-extension boot-service-type | |
560 | gexps->activation-gexp))) | |
7874e9e0 | 561 | (compose identity) |
636bb2b5 LC |
562 | (extend second-argument) |
563 | (description | |
564 | "Run @dfn{activation} code at boot time and upon | |
565 | @command{guix system reconfigure} completion."))) | |
0adfe95a LC |
566 | |
567 | (define %activation-service | |
568 | ;; The activation service produces the activation script from the gexps it | |
569 | ;; receives. | |
570 | (service activation-service-type #t)) | |
571 | ||
a241a7ac LC |
572 | (define %modprobe-wrapper |
573 | ;; Wrapper for the 'modprobe' command that knows where modules live. | |
574 | ;; | |
575 | ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe', | |
576 | ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' | |
577 | ;; environment variable is not set---hence the need for this wrapper. | |
578 | (let ((modprobe "/run/current-system/profile/bin/modprobe")) | |
579 | (program-file "modprobe" | |
580 | #~(begin | |
581 | (setenv "LINUX_MODULE_DIRECTORY" | |
582 | "/run/booted-system/kernel/lib/modules") | |
8c88e242 BW |
583 | ;; FIXME: Remove this crutch when the patch #40422, |
584 | ;; updating to kmod 27 is merged. | |
585 | (setenv "MODPROBE_OPTIONS" | |
586 | "-C /etc/modprobe.d") | |
a241a7ac LC |
587 | (apply execl #$modprobe |
588 | (cons #$modprobe (cdr (command-line)))))))) | |
589 | ||
590 | (define %linux-kernel-activation | |
591 | ;; Activation of the Linux kernel running on the bare metal (as opposed to | |
592 | ;; running in a container.) | |
593 | #~(begin | |
594 | ;; Tell the kernel to use our 'modprobe' command. | |
595 | (activate-modprobe #$%modprobe-wrapper) | |
596 | ||
597 | ;; Let users debug their own processes! | |
598 | (activate-ptrace-attach))) | |
599 | ||
a241a7ac LC |
600 | (define %linux-bare-metal-service |
601 | ;; The service that does things that are needed on the "bare metal", but not | |
602 | ;; necessary or impossible in a container. | |
6ddf4fcf LC |
603 | (simple-service 'linux-bare-metal |
604 | activation-service-type | |
605 | %linux-kernel-activation)) | |
606 | ||
a241a7ac | 607 | |
387e1754 LC |
608 | (define special-files-service-type |
609 | ;; Service to install "special files" such as /bin/sh and /usr/bin/env. | |
610 | (service-type | |
611 | (name 'special-files) | |
612 | (extensions | |
613 | (list (service-extension activation-service-type | |
614 | (lambda (files) | |
615 | #~(activate-special-files '#$files))))) | |
616 | (compose concatenate) | |
636bb2b5 LC |
617 | (extend append) |
618 | (description | |
619 | "Add special files to the root file system---e.g., | |
620 | @file{/usr/bin/env}."))) | |
387e1754 LC |
621 | |
622 | (define (extra-special-file file target) | |
623 | "Use TARGET as the \"special file\" FILE. For example, TARGET might be | |
624 | (file-append coreutils \"/bin/env\") | |
625 | and FILE could be \"/usr/bin/env\"." | |
626 | (simple-service (string->symbol (string-append "special-file-" file)) | |
627 | special-files-service-type | |
628 | `((,file ,target)))) | |
629 | ||
0adfe95a LC |
630 | (define (etc-directory service) |
631 | "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE." | |
efe7d19a | 632 | (files->etc-directory (service-value service))) |
0adfe95a LC |
633 | |
634 | (define (files->etc-directory files) | |
635 | (file-union "etc" files)) | |
636 | ||
d62e201c LC |
637 | (define (etc-entry files) |
638 | "Return an entry for the /etc directory consisting of FILES in the system | |
639 | directory." | |
640 | (with-monad %store-monad | |
641 | (return `(("etc" ,(files->etc-directory files)))))) | |
642 | ||
0adfe95a LC |
643 | (define etc-service-type |
644 | (service-type (name 'etc) | |
645 | (extensions | |
646 | (list | |
647 | (service-extension activation-service-type | |
648 | (lambda (files) | |
649 | (let ((etc | |
650 | (files->etc-directory files))) | |
d62e201c LC |
651 | #~(activate-etc #$etc)))) |
652 | (service-extension system-service-type etc-entry))) | |
0adfe95a | 653 | (compose concatenate) |
636bb2b5 LC |
654 | (extend append) |
655 | (description "Populate the @file{/etc} directory."))) | |
0adfe95a LC |
656 | |
657 | (define (etc-service files) | |
658 | "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES. | |
659 | FILES must be a list of name/file-like object pairs." | |
660 | (service etc-service-type files)) | |
661 | ||
662 | (define setuid-program-service-type | |
663 | (service-type (name 'setuid-program) | |
664 | (extensions | |
665 | (list (service-extension activation-service-type | |
666 | (lambda (programs) | |
667 | #~(activate-setuid-programs | |
668 | (list #$@programs)))))) | |
669 | (compose concatenate) | |
636bb2b5 LC |
670 | (extend append) |
671 | (description | |
672 | "Populate @file{/run/setuid-programs} with the specified | |
673 | executables, making them setuid-root."))) | |
0adfe95a | 674 | |
af4c3fd5 LC |
675 | (define (packages->profile-entry packages) |
676 | "Return a system entry for the profile containing PACKAGES." | |
677 | (mlet %store-monad ((profile (profile-derivation | |
60a0886d SB |
678 | (packages->manifest |
679 | (delete-duplicates packages eq?))))) | |
af4c3fd5 LC |
680 | (return `(("profile" ,profile))))) |
681 | ||
682 | (define profile-service-type | |
683 | ;; The service that populates the system's profile---i.e., | |
684 | ;; /run/current-system/profile. It is extended by package lists. | |
685 | (service-type (name 'profile) | |
686 | (extensions | |
687 | (list (service-extension system-service-type | |
688 | packages->profile-entry))) | |
689 | (compose concatenate) | |
636bb2b5 LC |
690 | (extend append) |
691 | (description | |
692 | "This is the @dfn{system profile}, available as | |
693 | @file{/run/current-system/profile}. It contains packages that the sysadmin | |
694 | wants to be globally available to all the system users."))) | |
af4c3fd5 | 695 | |
0adfe95a LC |
696 | (define (firmware->activation-gexp firmware) |
697 | "Return a gexp to make the packages listed in FIRMWARE loadable by the | |
698 | kernel." | |
699 | (let ((directory (directory-union "firmware" firmware))) | |
700 | ;; Tell the kernel where firmware is. | |
701 | #~(activate-firmware (string-append #$directory "/lib/firmware")))) | |
702 | ||
703 | (define firmware-service-type | |
704 | ;; The service that collects firmware. | |
705 | (service-type (name 'firmware) | |
706 | (extensions | |
707 | (list (service-extension activation-service-type | |
708 | firmware->activation-gexp))) | |
709 | (compose concatenate) | |
636bb2b5 LC |
710 | (extend append) |
711 | (description | |
712 | "Make ``firmware'' files loadable by the operating system | |
713 | kernel. Firmware may then be uploaded to some of the machine's devices, such | |
714 | as Wifi cards."))) | |
0adfe95a | 715 | |
e0b47290 LC |
716 | (define (gc-roots->system-entry roots) |
717 | "Return an entry in the system's output containing symlinks to ROOTS." | |
718 | (mlet %store-monad ((entry (gexp->derivation | |
719 | "gc-roots" | |
720 | #~(let ((roots '#$roots)) | |
721 | (mkdir #$output) | |
722 | (chdir #$output) | |
723 | (for-each symlink | |
724 | roots | |
725 | (map number->string | |
726 | (iota (length roots)))))))) | |
727 | (return (if (null? roots) | |
728 | '() | |
729 | `(("gc-roots" ,entry)))))) | |
730 | ||
731 | (define gc-root-service-type | |
732 | ;; A service to associate extra garbage-collector roots to the system. This | |
733 | ;; is a simple hack that guarantees that the system retains references to | |
734 | ;; the given list of roots. Roots must be "lowerable" objects like | |
735 | ;; packages, or derivations. | |
736 | (service-type (name 'gc-roots) | |
737 | (extensions | |
738 | (list (service-extension system-service-type | |
739 | gc-roots->system-entry))) | |
740 | (compose concatenate) | |
636bb2b5 LC |
741 | (extend append) |
742 | (description | |
743 | "Register garbage-collector roots---i.e., store items that | |
dc5729a8 LC |
744 | will not be reclaimed by the garbage collector.") |
745 | (default-value '()))) | |
e0b47290 | 746 | |
0adfe95a LC |
747 | \f |
748 | ;;; | |
749 | ;;; Service folding. | |
750 | ;;; | |
751 | ||
0adfe95a LC |
752 | (define-condition-type &missing-target-service-error &service-error |
753 | missing-target-service-error? | |
754 | (service missing-target-service-error-service) | |
755 | (target-type missing-target-service-error-target-type)) | |
756 | ||
757 | (define-condition-type &ambiguous-target-service-error &service-error | |
758 | ambiguous-target-service-error? | |
759 | (service ambiguous-target-service-error-service) | |
760 | (target-type ambiguous-target-service-error-target-type)) | |
761 | ||
d466b1fc LC |
762 | (define (missing-target-error service target-type) |
763 | (raise | |
764 | (condition (&missing-target-service-error | |
765 | (service service) | |
766 | (target-type target-type)) | |
767 | (&message | |
768 | (message | |
769 | (format #f (G_ "no target of type '~a' for service '~a'") | |
770 | (service-type-name target-type) | |
771 | (service-type-name | |
772 | (service-kind service)))))))) | |
773 | ||
0adfe95a LC |
774 | (define (service-back-edges services) |
775 | "Return a procedure that, when passed a <service>, returns the list of | |
776 | <service> objects that depend on it." | |
777 | (define (add-edges service edges) | |
778 | (define (add-edge extension edges) | |
779 | (let ((target-type (service-extension-target extension))) | |
780 | (match (filter (lambda (service) | |
781 | (eq? (service-kind service) target-type)) | |
782 | services) | |
783 | ((target) | |
784 | (vhash-consq target service edges)) | |
785 | (() | |
d466b1fc | 786 | (missing-target-error service target-type)) |
0adfe95a LC |
787 | (x |
788 | (raise | |
789 | (condition (&ambiguous-target-service-error | |
790 | (service service) | |
791 | (target-type target-type)) | |
792 | (&message | |
793 | (message | |
794 | (format #f | |
69daee23 | 795 | (G_ "more than one target service of type '~a'") |
0adfe95a LC |
796 | (service-type-name target-type)))))))))) |
797 | ||
798 | (fold add-edge edges (service-type-extensions (service-kind service)))) | |
799 | ||
800 | (let ((edges (fold add-edges vlist-null services))) | |
801 | (lambda (node) | |
802 | (reverse (vhash-foldq* cons '() node edges))))) | |
803 | ||
d466b1fc LC |
804 | (define (instantiate-missing-services services) |
805 | "Return SERVICES, a list, augmented with any services targeted by extensions | |
806 | and missing from SERVICES. Only service types with a default value can be | |
807 | instantiated; other missing services lead to a | |
808 | '&missing-target-service-error'." | |
809 | (define (adjust-service-list svc result instances) | |
810 | (fold2 (lambda (extension result instances) | |
811 | (define target-type | |
812 | (service-extension-target extension)) | |
813 | ||
814 | (match (vhash-assq target-type instances) | |
815 | (#f | |
816 | (let ((default (service-type-default-value target-type))) | |
817 | (if (eq? &no-default-value default) | |
818 | (missing-target-error svc target-type) | |
819 | (let ((new (service target-type))) | |
820 | (values (cons new result) | |
821 | (vhash-consq target-type new instances)))))) | |
822 | (_ | |
823 | (values result instances)))) | |
824 | result | |
825 | instances | |
826 | (service-type-extensions (service-kind svc)))) | |
827 | ||
9b6c4355 LC |
828 | (let loop ((services services)) |
829 | (define instances | |
830 | (fold (lambda (service result) | |
831 | (vhash-consq (service-kind service) service | |
832 | result)) | |
833 | vlist-null services)) | |
834 | ||
835 | (define adjusted | |
836 | (fold2 adjust-service-list | |
837 | services instances | |
838 | services)) | |
839 | ||
840 | ;; If we instantiated services, they might in turn depend on missing | |
841 | ;; services. Loop until we've reached fixed point. | |
842 | (if (= (length adjusted) (vlist-length instances)) | |
843 | adjusted | |
844 | (loop adjusted)))) | |
d466b1fc | 845 | |
d62e201c LC |
846 | (define* (fold-services services |
847 | #:key (target-type system-service-type)) | |
0adfe95a LC |
848 | "Fold SERVICES by propagating their extensions down to the root of type |
849 | TARGET-TYPE; return the root service adjusted accordingly." | |
850 | (define dependents | |
851 | (service-back-edges services)) | |
852 | ||
853 | (define (matching-extension target) | |
854 | (let ((target (service-kind target))) | |
855 | (match-lambda | |
856 | (($ <service-extension> type) | |
857 | (eq? type target))))) | |
858 | ||
859 | (define (apply-extension target) | |
860 | (lambda (service) | |
861 | (match (find (matching-extension target) | |
862 | (service-type-extensions (service-kind service))) | |
863 | (($ <service-extension> _ compute) | |
efe7d19a | 864 | (compute (service-value service)))))) |
0adfe95a LC |
865 | |
866 | (match (filter (lambda (service) | |
867 | (eq? (service-kind service) target-type)) | |
868 | services) | |
869 | ((sink) | |
2a4309de LC |
870 | ;; Use the state monad to keep track of already-visited services in the |
871 | ;; graph and to memoize their value once folded. | |
872 | (run-with-state | |
873 | (let loop ((sink sink)) | |
874 | (mlet %state-monad ((visited (current-state))) | |
875 | (match (vhash-assq sink visited) | |
876 | (#f | |
877 | (mlet* %state-monad | |
878 | ((dependents (mapm %state-monad loop (dependents sink))) | |
879 | (visited (current-state)) | |
880 | (extensions -> (map (apply-extension sink) dependents)) | |
881 | (extend -> (service-type-extend (service-kind sink))) | |
882 | (compose -> (service-type-compose (service-kind sink))) | |
883 | (params -> (service-value sink)) | |
884 | (service | |
885 | -> | |
886 | ;; Distinguish COMPOSE and EXTEND because PARAMS typically | |
887 | ;; has a different type than the elements of EXTENSIONS. | |
888 | (if extend | |
889 | (service (service-kind sink) | |
890 | (extend params (compose extensions))) | |
891 | sink))) | |
892 | (mbegin %state-monad | |
893 | (set-current-state (vhash-consq sink service visited)) | |
894 | (return service)))) | |
895 | ((_ . service) ;SINK was already visited | |
896 | (return service))))) | |
897 | vlist-null)) | |
0adfe95a LC |
898 | (() |
899 | (raise | |
900 | (condition (&missing-target-service-error | |
901 | (service #f) | |
902 | (target-type target-type)) | |
903 | (&message | |
69daee23 | 904 | (message (format #f (G_ "service of type '~a' not found") |
0adfe95a LC |
905 | (service-type-name target-type))))))) |
906 | (x | |
907 | (raise | |
908 | (condition (&ambiguous-target-service-error | |
909 | (service #f) | |
910 | (target-type target-type)) | |
911 | (&message | |
912 | (message | |
913 | (format #f | |
69daee23 | 914 | (G_ "more than one target service of type '~a'") |
0adfe95a | 915 | (service-type-name target-type))))))))) |
db4fdc04 LC |
916 | |
917 | ;;; services.scm ends here. |