services: science.scm: Add missing copyright headers.
[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
3a391e68
LC
620(define (second-argument a b) b)
621
0adfe95a
LC
622(define activation-service-type
623 (service-type (name 'activate)
624 (extensions
625 (list (service-extension boot-service-type
626 gexps->activation-gexp)))
7874e9e0 627 (compose identity)
636bb2b5
LC
628 (extend second-argument)
629 (description
630 "Run @dfn{activation} code at boot time and upon
631@command{guix system reconfigure} completion.")))
0adfe95a
LC
632
633(define %activation-service
634 ;; The activation service produces the activation script from the gexps it
635 ;; receives.
636 (service activation-service-type #t))
637
a241a7ac
LC
638(define %modprobe-wrapper
639 ;; Wrapper for the 'modprobe' command that knows where modules live.
640 ;;
641 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
642 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
643 ;; environment variable is not set---hence the need for this wrapper.
644 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
645 (program-file "modprobe"
646 #~(begin
647 (setenv "LINUX_MODULE_DIRECTORY"
648 "/run/booted-system/kernel/lib/modules")
8c88e242
BW
649 ;; FIXME: Remove this crutch when the patch #40422,
650 ;; updating to kmod 27 is merged.
651 (setenv "MODPROBE_OPTIONS"
652 "-C /etc/modprobe.d")
a241a7ac
LC
653 (apply execl #$modprobe
654 (cons #$modprobe (cdr (command-line))))))))
655
656(define %linux-kernel-activation
657 ;; Activation of the Linux kernel running on the bare metal (as opposed to
658 ;; running in a container.)
659 #~(begin
660 ;; Tell the kernel to use our 'modprobe' command.
661 (activate-modprobe #$%modprobe-wrapper)
662
663 ;; Let users debug their own processes!
664 (activate-ptrace-attach)))
665
a241a7ac
LC
666(define %linux-bare-metal-service
667 ;; The service that does things that are needed on the "bare metal", but not
668 ;; necessary or impossible in a container.
6ddf4fcf
LC
669 (simple-service 'linux-bare-metal
670 activation-service-type
671 %linux-kernel-activation))
672
68d8c094
JN
673(define %hurd-rc-script
674 ;; The RC script to be started upon boot.
675 (program-file "rc"
676 (with-imported-modules (source-module-closure
677 '((guix build utils)
678 (gnu build hurd-boot)
679 (guix build syscalls)))
680 #~(begin
681 (use-modules (guix build utils)
682 (gnu build hurd-boot)
683 (guix build syscalls)
684 (ice-9 match)
685 (system repl repl)
686 (srfi srfi-1)
687 (srfi srfi-26))
688 (boot-hurd-system)))))
689
690(define (hurd-rc-entry rc)
691 "Return, as a monadic value, an entry for the RC script in the system
692directory."
693 (mlet %store-monad ((rc (lower-object rc)))
694 (return `(("rc" ,rc)))))
695
696(define hurd-startup-service-type
697 ;; The service that creates the initial SYSTEM/rc startup file.
698 (service-type (name 'startup)
699 (extensions
700 (list (service-extension system-service-type hurd-rc-entry)))
701 (default-value %hurd-rc-script)))
702
703(define %hurd-startup-service
704 ;; The service that produces the RC script.
705 (service hurd-startup-service-type %hurd-rc-script))
a241a7ac 706
387e1754
LC
707(define special-files-service-type
708 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
709 (service-type
710 (name 'special-files)
711 (extensions
712 (list (service-extension activation-service-type
713 (lambda (files)
714 #~(activate-special-files '#$files)))))
715 (compose concatenate)
636bb2b5
LC
716 (extend append)
717 (description
718 "Add special files to the root file system---e.g.,
719@file{/usr/bin/env}.")))
387e1754
LC
720
721(define (extra-special-file file target)
722 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
723 (file-append coreutils \"/bin/env\")
724and FILE could be \"/usr/bin/env\"."
725 (simple-service (string->symbol (string-append "special-file-" file))
726 special-files-service-type
727 `((,file ,target))))
728
0adfe95a
LC
729(define (etc-directory service)
730 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
efe7d19a 731 (files->etc-directory (service-value service)))
0adfe95a
LC
732
733(define (files->etc-directory files)
a322e9d1
LC
734 (define (assert-no-duplicates files)
735 (let loop ((files files)
736 (seen (set)))
737 (match files
738 (() #t)
739 (((file _) rest ...)
740 (when (set-contains? seen file)
d51bfe24
LC
741 (raise (formatted-message (G_ "duplicate '~a' entry for /etc")
742 file)))
a322e9d1
LC
743 (loop rest (set-insert file seen))))))
744
745 ;; Detect duplicates early instead of letting them through, eventually
746 ;; leading to a build failure of "etc.drv".
747 (assert-no-duplicates files)
748
0adfe95a
LC
749 (file-union "etc" files))
750
d62e201c
LC
751(define (etc-entry files)
752 "Return an entry for the /etc directory consisting of FILES in the system
753directory."
754 (with-monad %store-monad
755 (return `(("etc" ,(files->etc-directory files))))))
756
0adfe95a
LC
757(define etc-service-type
758 (service-type (name 'etc)
759 (extensions
760 (list
761 (service-extension activation-service-type
762 (lambda (files)
763 (let ((etc
764 (files->etc-directory files)))
d62e201c
LC
765 #~(activate-etc #$etc))))
766 (service-extension system-service-type etc-entry)))
0adfe95a 767 (compose concatenate)
636bb2b5
LC
768 (extend append)
769 (description "Populate the @file{/etc} directory.")))
0adfe95a
LC
770
771(define (etc-service files)
772 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
773FILES must be a list of name/file-like object pairs."
774 (service etc-service-type files))
775
776(define setuid-program-service-type
777 (service-type (name 'setuid-program)
778 (extensions
779 (list (service-extension activation-service-type
780 (lambda (programs)
781 #~(activate-setuid-programs
782 (list #$@programs))))))
783 (compose concatenate)
636bb2b5
LC
784 (extend append)
785 (description
786 "Populate @file{/run/setuid-programs} with the specified
787executables, making them setuid-root.")))
0adfe95a 788
af4c3fd5
LC
789(define (packages->profile-entry packages)
790 "Return a system entry for the profile containing PACKAGES."
07a33b1d
LC
791 ;; XXX: 'mlet' is needed here for one reason: to get the proper
792 ;; '%current-target' and '%current-target-system' bindings when
793 ;; 'packages->manifest' is called, and thus when the 'package-inputs'
794 ;; etc. procedures are called on PACKAGES. That way, conditionals in those
795 ;; inputs see the "correct" value of these two parameters. See
796 ;; <https://issues.guix.gnu.org/44952>.
797 (mlet %store-monad ((_ (current-target-system)))
45bd9133
LC
798 (return `(("profile" ,(profile
799 (content (packages->manifest
800 (delete-duplicates packages eq?)))))))))
af4c3fd5
LC
801
802(define profile-service-type
803 ;; The service that populates the system's profile---i.e.,
804 ;; /run/current-system/profile. It is extended by package lists.
805 (service-type (name 'profile)
806 (extensions
807 (list (service-extension system-service-type
808 packages->profile-entry)))
809 (compose concatenate)
636bb2b5
LC
810 (extend append)
811 (description
812 "This is the @dfn{system profile}, available as
813@file{/run/current-system/profile}. It contains packages that the sysadmin
814wants to be globally available to all the system users.")))
af4c3fd5 815
0adfe95a
LC
816(define (firmware->activation-gexp firmware)
817 "Return a gexp to make the packages listed in FIRMWARE loadable by the
818kernel."
819 (let ((directory (directory-union "firmware" firmware)))
820 ;; Tell the kernel where firmware is.
821 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
822
823(define firmware-service-type
824 ;; The service that collects firmware.
825 (service-type (name 'firmware)
826 (extensions
827 (list (service-extension activation-service-type
828 firmware->activation-gexp)))
829 (compose concatenate)
636bb2b5
LC
830 (extend append)
831 (description
832 "Make ``firmware'' files loadable by the operating system
833kernel. Firmware may then be uploaded to some of the machine's devices, such
834as Wifi cards.")))
0adfe95a 835
e0b47290
LC
836(define (gc-roots->system-entry roots)
837 "Return an entry in the system's output containing symlinks to ROOTS."
838 (mlet %store-monad ((entry (gexp->derivation
839 "gc-roots"
840 #~(let ((roots '#$roots))
841 (mkdir #$output)
842 (chdir #$output)
843 (for-each symlink
844 roots
845 (map number->string
846 (iota (length roots))))))))
847 (return (if (null? roots)
848 '()
849 `(("gc-roots" ,entry))))))
850
851(define gc-root-service-type
852 ;; A service to associate extra garbage-collector roots to the system. This
853 ;; is a simple hack that guarantees that the system retains references to
854 ;; the given list of roots. Roots must be "lowerable" objects like
855 ;; packages, or derivations.
856 (service-type (name 'gc-roots)
857 (extensions
858 (list (service-extension system-service-type
859 gc-roots->system-entry)))
860 (compose concatenate)
636bb2b5
LC
861 (extend append)
862 (description
863 "Register garbage-collector roots---i.e., store items that
dc5729a8
LC
864will not be reclaimed by the garbage collector.")
865 (default-value '())))
e0b47290 866
0adfe95a
LC
867\f
868;;;
869;;; Service folding.
870;;;
871
0adfe95a
LC
872(define-condition-type &missing-target-service-error &service-error
873 missing-target-service-error?
874 (service missing-target-service-error-service)
875 (target-type missing-target-service-error-target-type))
876
877(define-condition-type &ambiguous-target-service-error &service-error
878 ambiguous-target-service-error?
879 (service ambiguous-target-service-error-service)
880 (target-type ambiguous-target-service-error-target-type))
881
d466b1fc
LC
882(define (missing-target-error service target-type)
883 (raise
884 (condition (&missing-target-service-error
885 (service service)
886 (target-type target-type))
887 (&message
888 (message
889 (format #f (G_ "no target of type '~a' for service '~a'")
890 (service-type-name target-type)
891 (service-type-name
892 (service-kind service))))))))
893
0adfe95a
LC
894(define (service-back-edges services)
895 "Return a procedure that, when passed a <service>, returns the list of
896<service> objects that depend on it."
897 (define (add-edges service edges)
898 (define (add-edge extension edges)
899 (let ((target-type (service-extension-target extension)))
900 (match (filter (lambda (service)
901 (eq? (service-kind service) target-type))
902 services)
903 ((target)
904 (vhash-consq target service edges))
905 (()
d466b1fc 906 (missing-target-error service target-type))
0adfe95a
LC
907 (x
908 (raise
909 (condition (&ambiguous-target-service-error
910 (service service)
911 (target-type target-type))
912 (&message
913 (message
914 (format #f
69daee23 915 (G_ "more than one target service of type '~a'")
0adfe95a
LC
916 (service-type-name target-type))))))))))
917
918 (fold add-edge edges (service-type-extensions (service-kind service))))
919
920 (let ((edges (fold add-edges vlist-null services)))
921 (lambda (node)
922 (reverse (vhash-foldq* cons '() node edges)))))
923
d466b1fc
LC
924(define (instantiate-missing-services services)
925 "Return SERVICES, a list, augmented with any services targeted by extensions
926and missing from SERVICES. Only service types with a default value can be
927instantiated; other missing services lead to a
928'&missing-target-service-error'."
929 (define (adjust-service-list svc result instances)
930 (fold2 (lambda (extension result instances)
931 (define target-type
932 (service-extension-target extension))
933
934 (match (vhash-assq target-type instances)
935 (#f
936 (let ((default (service-type-default-value target-type)))
937 (if (eq? &no-default-value default)
938 (missing-target-error svc target-type)
939 (let ((new (service target-type)))
940 (values (cons new result)
941 (vhash-consq target-type new instances))))))
942 (_
943 (values result instances))))
944 result
945 instances
946 (service-type-extensions (service-kind svc))))
947
9b6c4355
LC
948 (let loop ((services services))
949 (define instances
950 (fold (lambda (service result)
951 (vhash-consq (service-kind service) service
952 result))
953 vlist-null services))
954
955 (define adjusted
956 (fold2 adjust-service-list
957 services instances
958 services))
959
960 ;; If we instantiated services, they might in turn depend on missing
961 ;; services. Loop until we've reached fixed point.
962 (if (= (length adjusted) (vlist-length instances))
963 adjusted
964 (loop adjusted))))
d466b1fc 965
d62e201c
LC
966(define* (fold-services services
967 #:key (target-type system-service-type))
0adfe95a
LC
968 "Fold SERVICES by propagating their extensions down to the root of type
969TARGET-TYPE; return the root service adjusted accordingly."
970 (define dependents
971 (service-back-edges services))
972
973 (define (matching-extension target)
974 (let ((target (service-kind target)))
975 (match-lambda
976 (($ <service-extension> type)
977 (eq? type target)))))
978
979 (define (apply-extension target)
980 (lambda (service)
981 (match (find (matching-extension target)
982 (service-type-extensions (service-kind service)))
983 (($ <service-extension> _ compute)
efe7d19a 984 (compute (service-value service))))))
0adfe95a
LC
985
986 (match (filter (lambda (service)
987 (eq? (service-kind service) target-type))
988 services)
989 ((sink)
2a4309de
LC
990 ;; Use the state monad to keep track of already-visited services in the
991 ;; graph and to memoize their value once folded.
992 (run-with-state
993 (let loop ((sink sink))
994 (mlet %state-monad ((visited (current-state)))
995 (match (vhash-assq sink visited)
996 (#f
997 (mlet* %state-monad
998 ((dependents (mapm %state-monad loop (dependents sink)))
999 (visited (current-state))
1000 (extensions -> (map (apply-extension sink) dependents))
1001 (extend -> (service-type-extend (service-kind sink)))
1002 (compose -> (service-type-compose (service-kind sink)))
1003 (params -> (service-value sink))
1004 (service
1005 ->
1006 ;; Distinguish COMPOSE and EXTEND because PARAMS typically
1007 ;; has a different type than the elements of EXTENSIONS.
1008 (if extend
1009 (service (service-kind sink)
1010 (extend params (compose extensions)))
1011 sink)))
1012 (mbegin %state-monad
1013 (set-current-state (vhash-consq sink service visited))
1014 (return service))))
1015 ((_ . service) ;SINK was already visited
1016 (return service)))))
1017 vlist-null))
0adfe95a
LC
1018 (()
1019 (raise
d51bfe24
LC
1020 (make-compound-condition
1021 (condition (&missing-target-service-error
1022 (service #f)
1023 (target-type target-type)))
1024 (formatted-message (G_ "service of type '~a' not found")
1025 (service-type-name target-type)))))
0adfe95a
LC
1026 (x
1027 (raise
1028 (condition (&ambiguous-target-service-error
1029 (service #f)
1030 (target-type target-type))
1031 (&message
1032 (message
1033 (format #f
69daee23 1034 (G_ "more than one target service of type '~a'")
0adfe95a 1035 (service-type-name target-type)))))))))
db4fdc04
LC
1036
1037;;; services.scm ends here.