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