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>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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 diagnostics)
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
50 service-extension-target
51 service-extension-compute
56 service-type-extensions
59 service-type-default-value
60 service-type-description
71 service-parameters ;deprecated
76 instantiate-missing-services
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
91 provenance-service-type
92 sexp->system-provenance
96 activation-service-type
97 activation-service->script
98 %linux-bare-metal-service
100 %hurd-startup-service
101 special-files-service-type
105 setuid-program-service-type
107 firmware-service-type
116 ;;; This module defines a broad notion of "service types" and "services."
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;
121 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
122 ;;; <shepherd-service>.
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
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'.
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.
139 (define-record-type <service-extension>
140 (service-extension target compute)
142 (target service-extension-target) ;<service-type>
143 (compute service-extension-compute)) ;params -> params
145 (define &no-default-value
146 ;; Value used to denote service types that have no associated default value.
149 (define-record-type* <service-type> service-type make-service-type
151 (name service-type-name) ;symbol (for debugging)
153 ;; Things extended by services of this type.
154 (extensions service-type-extensions) ;list of <service-extensions>
156 ;; Given a list of extensions, "compose" them.
157 (compose service-type-compose ;list of Any -> Any
160 ;; Extend the services' own parameters with the extension composition.
161 (extend service-type-extend ;list of Any -> parameters
164 ;; Optional default value for instances of this type.
165 (default-value service-type-default-value ;Any
166 (default &no-default-value))
169 (description service-type-description ;string
171 (location service-type-location ;<location>
172 (default (and=> (current-source-location)
173 source-properties->location))
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)))
181 (set-record-type-printer! <service-type> write-service-type)
183 (define %distro-root-directory
184 ;; Absolute file name of the module hierarchy.
185 (dirname (search-path %load-path "guix.scm")))
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"))))
192 (define (all-service-modules)
193 "Return the default set of service modules."
194 (cons (resolve-interface '(gnu services))
195 (all-modules (%service-type-path)
196 #:warn warn-about-load-error)))
198 (define* (fold-service-types proc seed
200 (modules (all-service-modules)))
201 "For each service type exported by one of MODULES, call (PROC RESULT). SEED
202 is used as the initial value of RESULT."
203 (fold-module-public-variables (lambda (object result)
204 (if (service-type? object)
210 (define lookup-service-types
212 (delay (fold-service-types (lambda (type result)
213 (vhash-consq (service-type-name type)
217 "Return the list of services with the given NAME (a symbol)."
218 (vhash-foldq* cons '() name (force table)))))
220 ;; Services of a given type.
221 (define-record-type <service>
222 (make-service type value)
225 (value service-value))
227 (define-syntax service
229 "Return a service instance of TYPE. The service value is VALUE or, if
230 omitted, TYPE's default value."
232 (make-service type value))
234 (%service-with-default-value (current-source-location)
237 (define (%service-with-default-value location type)
238 "Return a instance of service type TYPE with its default value, if any. If
239 TYPE 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)))
246 (make-compound-condition
248 (&missing-value-service-error (type type) (location location)))
249 (formatted-message (G_ "~a: no value specified \
250 for service of type '~a'")
251 (location->string location)
252 (service-type-name type)))))
253 (service type default))))
255 (define-condition-type &service-error &error
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))
269 (define service-parameters
273 (define (simple-service name target value)
274 "Return a service that extends TARGET with VALUE. This works by creating a
275 singleton 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)))
281 (define-syntax %modify-service
285 ((_ svc (kind param => exp ...) clauses ...)
286 (if (eq? (service-kind svc) kind)
287 (let ((param (service-value svc)))
288 (service (service-kind svc)
290 (%modify-service svc clauses ...)))))
292 (define-syntax modify-services
294 "Modify the services listed in SERVICES according to CLAUSES and return
295 the resulting list of services. Each clause must have the form:
297 (TYPE VARIABLE => BODY)
299 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
300 identifier that is bound within BODY to the value of the service of that
301 TYPE. Consider this example:
303 (modify-services %base-services
304 (guix-service-type config =>
307 (use-substitutes? #f)
308 (extra-options '(\"--gc-keep-derivations\"))))
309 (mingetty-service-type config =>
310 (mingetty-configuration
312 (motd (plain-file \"motd\" \"Hi there!\")))))
314 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
315 all the MINGETTY-SERVICE-TYPE instances.
317 This is a shorthand for (map (lambda (svc) ...) %base-services)."
318 ((_ services clauses ...)
319 (map (lambda (service)
320 (%modify-service service clauses ...))
328 (define (system-derivation entries mextensions)
329 "Return as a monadic value the derivation of the 'system' directory
330 containing the given entries."
331 (mlet %store-monad ((extensions (mapm/accumulate-builds identity
335 (append entries (concatenate extensions))))))
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)
345 (extend system-derivation)
347 "Build the operating system top-level directory, which in
348 turn refers to everything the operating system needs: its kernel, initrd,
349 system profile, boot script, and so on.")))
351 (define (compute-boot-script _ gexps)
352 ;; Reverse GEXPS so that extensions appear in the boot script in the right
353 ;; order. That is, user extensions would come first, and extensions added
354 ;; by 'essential-services' (e.g., running shepherd) are guaranteed to come
357 ;; Clean up and activate the system, then spawn shepherd.
358 #~(begin #$@(reverse gexps))))
360 (define (boot-script-entry mboot)
361 "Return, as a monadic value, an entry for the boot script in the system
363 (mlet %store-monad ((boot mboot))
364 (return `(("boot" ,boot)))))
366 (define boot-service-type
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
370 (service-type (name 'boot)
372 (list (service-extension system-service-type
375 (extend compute-boot-script)
377 "Produce the operating system's boot script, which is spawned
378 by the initrd once the root file system is mounted.")))
380 (define %boot-service
381 ;; The service that produces the boot script.
382 (service boot-service-type #t))
386 ;;; Provenance tracking.
389 (define (object->pretty-string obj)
390 "Like 'object->string', but using 'pretty-print'."
391 (call-with-output-string
393 (pretty-print obj port))))
395 (define (channel->code channel)
396 "Return code to build CHANNEL, ready to be dropped in a 'channels.scm'
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))
409 (make-channel-introduction
410 ,(channel-introduction-first-signed-commit intro)
412 ,(openpgp-format-fingerprint
413 (channel-introduction-first-commit-signer
417 (define (channel->sexp channel)
418 "Return an sexp describing CHANNEL. The sexp is _not_ code and is meant to
419 be parsed by tools; it's potentially more future-proof than code."
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.
423 `(channel (name ,(channel-name channel))
424 (url ,(channel-url channel))
425 (branch ,(channel-branch channel))
426 (commit ,(channel-commit channel))))
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'."
432 (('channel ('name name)
437 ;; XXX: In the future REST may include a channel introduction.
438 (channel (name name) (url url)
439 (branch branch) (commit commit)))))
441 (define (provenance-file channels config-file)
442 "Return a 'provenance' file describing CHANNELS, a list of channels, and
443 CONFIG-FILE, which can be either #f or a <local-file> containing the OS
444 configuration being used."
445 (scheme-file "provenance"
448 (channels #+@(if channels
449 (map channel->sexp channels)
451 (configuration-file #+config-file))))
453 (define (provenance-entry config-file)
454 "Return system entries describing the operating system provenance: the
455 channels in use and CONFIG-FILE, if it is true."
460 (and=> profile profile-channels))
463 (let ((config-file (cond ((string? config-file)
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"))
474 (return `(("provenance" ,(provenance-file channels config-file))
477 ,(plain-file "channels.scm"
478 (object->pretty-string
480 ,@(map channel->code channels))))))
483 `(("configuration.scm" ,config-file))
486 (define provenance-service-type
487 (service-type (name 'provenance)
489 (list (service-extension system-service-type
491 (default-value #f) ;the OS config file
493 "Store provenance information about the system in the system
494 itself: the channels used when building the system, and its configuration
495 file, when available.")))
497 (define (sexp->system-provenance sexp)
498 "Parse SEXP, an s-expression read from /run/current-system/provenance or
499 similar, and return two values: the list of channels listed therein, and the
500 OS configuration file or #f."
502 (('provenance ('version 0)
503 ('channels channels ...)
504 ('configuration-file config-file))
505 (values (map sexp->channel channels)
510 (define (system-provenance system)
511 "Given SYSTEM, the file name of a system generation, return two values: the
512 list of channels SYSTEM is built from, and its configuration file. If that
513 information is missing, return the empty list (for channels) and possibly
514 #false (for the configuration file)."
517 (sexp->system-provenance
518 (call-with-input-file (string-append system "/provenance")
527 (define (cleanup-gexp _)
528 "Return a gexp to clean up /tmp and similar places upon boot."
529 (with-imported-modules '((guix build utils))
531 (use-modules (guix build utils))
533 ;; Clean out /tmp and /var/run.
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 ()
544 (fail-safe rest ...)))
547 ;; Ignore I/O errors so the system can boot.
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'
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")
564 (chmod "/tmp" #o1777)
566 (chmod "/var/run" #o755)
567 (delete-file-recursively "/run/udev/watch.old"))))))
569 (define cleanup-service-type
570 ;; Service that cleans things up in /tmp and similar.
571 (service-type (name 'cleanup)
573 (list (service-extension boot-service-type
576 "Delete files from @file{/tmp}, @file{/var/run}, and other
577 temporary locations at boot time.")))
579 (define* (activation-service->script service)
580 "Return as a monadic value the activation script for SERVICE, a service of
581 ACTIVATION-SCRIPT-TYPE."
582 (activation-script (service-value service)))
584 (define (activation-script gexps)
585 "Return the system's activation script, which evaluates GEXPS."
587 (map (cut program-file "activate-service.scm" <>) gexps))
589 (program-file "activate.scm"
590 (with-imported-modules (source-module-closure
591 '((gnu build activation)
594 (use-modules (gnu build activation)
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"))
602 ;; Same for 'wtmp', which is populated by mingetty et
605 (close-port (open-file "/var/log/wtmp" "a0"))
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)
612 ;; Run the services' activation snippets.
613 ;; TODO: Use 'load-compiled'.
614 (for-each primitive-load '#$actions)))))
616 (define (gexps->activation-gexp gexps)
617 "Return a gexp that runs the activation script containing GEXPS."
618 #~(primitive-load #$(activation-script gexps)))
620 (define (activation-profile-entry gexps)
621 "Return, as a monadic value, an entry for the activation script in the
623 (mlet %store-monad ((activate (lower-object (activation-script gexps))))
624 (return `(("activate" ,activate)))))
626 (define (second-argument a b) b)
628 (define activation-service-type
629 (service-type (name 'activate)
631 (list (service-extension boot-service-type
632 gexps->activation-gexp)
633 (service-extension system-service-type
634 activation-profile-entry)))
636 (extend second-argument)
638 "Run @dfn{activation} code at boot time and upon
639 @command{guix system reconfigure} completion.")))
641 (define %activation-service
642 ;; The activation service produces the activation script from the gexps it
644 (service activation-service-type #t))
646 (define %modprobe-wrapper
647 ;; Wrapper for the 'modprobe' command that knows where modules live.
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"
655 (setenv "LINUX_MODULE_DIRECTORY"
656 "/run/booted-system/kernel/lib/modules")
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")
661 (apply execl #$modprobe
662 (cons #$modprobe (cdr (command-line))))))))
664 (define %linux-kernel-activation
665 ;; Activation of the Linux kernel running on the bare metal (as opposed to
666 ;; running in a container.)
668 ;; Tell the kernel to use our 'modprobe' command.
669 (activate-modprobe #$%modprobe-wrapper)
671 ;; Let users debug their own processes!
672 (activate-ptrace-attach)))
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.
677 (simple-service 'linux-bare-metal
678 activation-service-type
679 %linux-kernel-activation))
681 (define %hurd-rc-script
682 ;; The RC script to be started upon boot.
684 (with-imported-modules (source-module-closure
686 (gnu build hurd-boot)
687 (guix build syscalls)))
689 (use-modules (guix build utils)
690 (gnu build hurd-boot)
691 (guix build syscalls)
696 (boot-hurd-system)))))
698 (define (hurd-rc-entry rc)
699 "Return, as a monadic value, an entry for the RC script in the system
701 (mlet %store-monad ((rc (lower-object rc)))
702 (return `(("rc" ,rc)))))
704 (define hurd-startup-service-type
705 ;; The service that creates the initial SYSTEM/rc startup file.
706 (service-type (name 'startup)
708 (list (service-extension system-service-type hurd-rc-entry)))
709 (default-value %hurd-rc-script)))
711 (define %hurd-startup-service
712 ;; The service that produces the RC script.
713 (service hurd-startup-service-type %hurd-rc-script))
715 (define special-files-service-type
716 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
718 (name 'special-files)
720 (list (service-extension activation-service-type
722 #~(activate-special-files '#$files)))))
723 (compose concatenate)
726 "Add special files to the root file system---e.g.,
727 @file{/usr/bin/env}.")))
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\")
732 and FILE could be \"/usr/bin/env\"."
733 (simple-service (string->symbol (string-append "special-file-" file))
734 special-files-service-type
737 (define (etc-directory service)
738 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
739 (files->etc-directory (service-value service)))
741 (define (files->etc-directory files)
742 (define (assert-no-duplicates files)
743 (let loop ((files files)
748 (when (set-contains? seen file)
749 (raise (formatted-message (G_ "duplicate '~a' entry for /etc")
751 (loop rest (set-insert file seen))))))
753 ;; Detect duplicates early instead of letting them through, eventually
754 ;; leading to a build failure of "etc.drv".
755 (assert-no-duplicates files)
757 (file-union "etc" files))
759 (define (etc-entry files)
760 "Return an entry for the /etc directory consisting of FILES in the system
762 (with-monad %store-monad
763 (return `(("etc" ,(files->etc-directory files))))))
765 (define etc-service-type
766 (service-type (name 'etc)
769 (service-extension activation-service-type
772 (files->etc-directory files)))
773 #~(activate-etc #$etc))))
774 (service-extension system-service-type etc-entry)))
775 (compose concatenate)
777 (description "Populate the @file{/etc} directory.")))
779 (define (etc-service files)
780 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
781 FILES must be a list of name/file-like object pairs."
782 (service etc-service-type files))
784 (define setuid-program-service-type
785 (service-type (name 'setuid-program)
787 (list (service-extension activation-service-type
789 #~(activate-setuid-programs
790 (list #$@programs))))))
791 (compose concatenate)
794 "Populate @file{/run/setuid-programs} with the specified
795 executables, making them setuid-root.")))
797 (define (packages->profile-entry packages)
798 "Return a system entry for the profile containing PACKAGES."
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)))
806 (return `(("profile" ,(profile
807 (content (packages->manifest
808 (delete-duplicates packages eq?)))))))))
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)
815 (list (service-extension system-service-type
816 packages->profile-entry)))
817 (compose concatenate)
820 "This is the @dfn{system profile}, available as
821 @file{/run/current-system/profile}. It contains packages that the sysadmin
822 wants to be globally available to all the system users.")))
824 (define (firmware->activation-gexp firmware)
825 "Return a gexp to make the packages listed in FIRMWARE loadable by the
827 (let ((directory (directory-union "firmware" firmware)))
828 ;; Tell the kernel where firmware is.
829 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
831 (define firmware-service-type
832 ;; The service that collects firmware.
833 (service-type (name 'firmware)
835 (list (service-extension activation-service-type
836 firmware->activation-gexp)))
837 (compose concatenate)
840 "Make ``firmware'' files loadable by the operating system
841 kernel. Firmware may then be uploaded to some of the machine's devices, such
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
848 #~(let ((roots '#$roots))
854 (iota (length roots))))))))
855 (return (if (null? roots)
857 `(("gc-roots" ,entry))))))
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)
866 (list (service-extension system-service-type
867 gc-roots->system-entry)))
868 (compose concatenate)
871 "Register garbage-collector roots---i.e., store items that
872 will not be reclaimed by the garbage collector.")
873 (default-value '())))
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))
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))
890 (define (missing-target-error service target-type)
892 (condition (&missing-target-service-error
894 (target-type target-type))
897 (format #f (G_ "no target of type '~a' for service '~a'")
898 (service-type-name target-type)
900 (service-kind service))))))))
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))
912 (vhash-consq target service edges))
914 (missing-target-error service target-type))
917 (condition (&ambiguous-target-service-error
919 (target-type target-type))
923 (G_ "more than one target service of type '~a'")
924 (service-type-name target-type))))))))))
926 (fold add-edge edges (service-type-extensions (service-kind service))))
928 (let ((edges (fold add-edges vlist-null services)))
930 (reverse (vhash-foldq* cons '() node edges)))))
932 (define (instantiate-missing-services services)
933 "Return SERVICES, a list, augmented with any services targeted by extensions
934 and missing from SERVICES. Only service types with a default value can be
935 instantiated; 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)
940 (service-extension-target extension))
942 (match (vhash-assq target-type instances)
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))))))
951 (values result instances))))
954 (service-type-extensions (service-kind svc))))
956 (let loop ((services services))
958 (fold (lambda (service result)
959 (vhash-consq (service-kind service) service
961 vlist-null services))
964 (fold2 adjust-service-list
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))
974 (define* (fold-services services
975 #:key (target-type system-service-type))
976 "Fold SERVICES by propagating their extensions down to the root of type
977 TARGET-TYPE; return the root service adjusted accordingly."
979 (service-back-edges services))
981 (define (matching-extension target)
982 (let ((target (service-kind target)))
984 (($ <service-extension> type)
985 (eq? type target)))))
987 (define (apply-extension target)
989 (match (find (matching-extension target)
990 (service-type-extensions (service-kind service)))
991 (($ <service-extension> _ compute)
992 (compute (service-value service))))))
994 (match (filter (lambda (service)
995 (eq? (service-kind service) target-type))
998 ;; Use the state monad to keep track of already-visited services in the
999 ;; graph and to memoize their value once folded.
1001 (let loop ((sink sink))
1002 (mlet %state-monad ((visited (current-state)))
1003 (match (vhash-assq sink visited)
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))
1014 ;; Distinguish COMPOSE and EXTEND because PARAMS typically
1015 ;; has a different type than the elements of EXTENSIONS.
1017 (service (service-kind sink)
1018 (extend params (compose extensions)))
1020 (mbegin %state-monad
1021 (set-current-state (vhash-consq sink service visited))
1023 ((_ . service) ;SINK was already visited
1024 (return service)))))
1028 (make-compound-condition
1029 (condition (&missing-target-service-error
1031 (target-type target-type)))
1032 (formatted-message (G_ "service of type '~a' not found")
1033 (service-type-name target-type)))))
1036 (condition (&ambiguous-target-service-error
1038 (target-type target-type))
1042 (G_ "more than one target service of type '~a'")
1043 (service-type-name target-type)))))))))
1045 ;;; services.scm ends here.