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