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 utils) #:select (source-properties->location))
34 #:use-module (guix modules)
35 #:use-module (gnu packages base)
36 #:use-module (gnu packages bash)
37 #:use-module (gnu packages hurd)
38 #:use-module (srfi srfi-1)
39 #:use-module (srfi srfi-9)
40 #:use-module (srfi srfi-9 gnu)
41 #:use-module (srfi srfi-26)
42 #:use-module (srfi srfi-34)
43 #:use-module (srfi srfi-35)
44 #:use-module (ice-9 vlist)
45 #:use-module (ice-9 match)
46 #:autoload (ice-9 pretty-print) (pretty-print)
47 #:export (service-extension
49 service-extension-target
50 service-extension-compute
55 service-type-extensions
58 service-type-default-value
59 service-type-description
70 service-parameters ;deprecated
75 instantiate-missing-services
79 missing-value-service-error?
80 missing-value-service-error-type
81 missing-value-service-error-location
82 missing-target-service-error?
83 missing-target-service-error-service
84 missing-target-service-error-target-type
85 ambiguous-target-service-error?
86 ambiguous-target-service-error-service
87 ambiguous-target-service-error-target-type
90 provenance-service-type
93 activation-service-type
94 activation-service->script
95 %linux-bare-metal-service
98 special-files-service-type
102 setuid-program-service-type
104 firmware-service-type
113 ;;; This module defines a broad notion of "service types" and "services."
115 ;;; A service type describe how its instances extend instances of other
116 ;;; service types. For instance, some services extend the instance of
117 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
118 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
119 ;;; <shepherd-service>.
121 ;;; When applicable, the service type defines how it can itself be extended,
122 ;;; by providing one procedure to compose extensions, and one procedure to
125 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
126 ;;; instance, which is the root of the service DAG. Its value is the
127 ;;; derivation that produces the 'system' directory as returned by
128 ;;; 'operating-system-derivation'.
130 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
131 ;;; "folds" by propagating extensions down the graph; it returns the root
132 ;;; service after the applying all its extensions.
136 (define-record-type <service-extension>
137 (service-extension target compute)
139 (target service-extension-target) ;<service-type>
140 (compute service-extension-compute)) ;params -> params
142 (define &no-default-value
143 ;; Value used to denote service types that have no associated default value.
146 (define-record-type* <service-type> service-type make-service-type
148 (name service-type-name) ;symbol (for debugging)
150 ;; Things extended by services of this type.
151 (extensions service-type-extensions) ;list of <service-extensions>
153 ;; Given a list of extensions, "compose" them.
154 (compose service-type-compose ;list of Any -> Any
157 ;; Extend the services' own parameters with the extension composition.
158 (extend service-type-extend ;list of Any -> parameters
161 ;; Optional default value for instances of this type.
162 (default-value service-type-default-value ;Any
163 (default &no-default-value))
166 (description service-type-description ;string
168 (location service-type-location ;<location>
169 (default (and=> (current-source-location)
170 source-properties->location))
173 (define (write-service-type type port)
174 (format port "#<service-type ~a ~a>"
175 (service-type-name type)
176 (number->string (object-address type) 16)))
178 (set-record-type-printer! <service-type> write-service-type)
180 (define %distro-root-directory
181 ;; Absolute file name of the module hierarchy.
182 (dirname (search-path %load-path "guix.scm")))
184 (define %service-type-path
185 ;; Search path for service types.
186 (make-parameter `((,%distro-root-directory . "gnu/services")
187 (,%distro-root-directory . "gnu/system"))))
189 (define (all-service-modules)
190 "Return the default set of service modules."
191 (cons (resolve-interface '(gnu services))
192 (all-modules (%service-type-path)
193 #:warn warn-about-load-error)))
195 (define* (fold-service-types proc seed
197 (modules (all-service-modules)))
198 "For each service type exported by one of MODULES, call (PROC RESULT). SEED
199 is used as the initial value of RESULT."
200 (fold-module-public-variables (lambda (object result)
201 (if (service-type? object)
207 (define lookup-service-types
209 (delay (fold-service-types (lambda (type result)
210 (vhash-consq (service-type-name type)
214 "Return the list of services with the given NAME (a symbol)."
215 (vhash-foldq* cons '() name (force table)))))
217 ;; Services of a given type.
218 (define-record-type <service>
219 (make-service type value)
222 (value service-value))
224 (define-syntax service
226 "Return a service instance of TYPE. The service value is VALUE or, if
227 omitted, TYPE's default value."
229 (make-service type value))
231 (%service-with-default-value (current-source-location)
234 (define (%service-with-default-value location type)
235 "Return a instance of service type TYPE with its default value, if any. If
236 TYPE does not have a default value, an error is raised."
237 ;; TODO: Currently this is a run-time error but with a little bit macrology
238 ;; we could turn it into an expansion-time error.
239 (let ((default (service-type-default-value type)))
240 (if (eq? default &no-default-value)
241 (let ((location (source-properties->location location)))
244 (&missing-value-service-error (type type) (location location))
246 (message (format #f (G_ "~a: no value specified \
247 for service of type '~a'")
248 (location->string location)
249 (service-type-name type)))))))
250 (service type default))))
252 (define-condition-type &service-error &error
255 (define-condition-type &missing-value-service-error &service-error
256 missing-value-service-error?
257 (type missing-value-service-error-type)
258 (location missing-value-service-error-location))
266 (define service-parameters
270 (define (simple-service name target value)
271 "Return a service that extends TARGET with VALUE. This works by creating a
272 singleton service type NAME, of which the returned service is an instance."
273 (let* ((extension (service-extension target identity))
274 (type (service-type (name name)
275 (extensions (list extension)))))
276 (service type value)))
278 (define-syntax %modify-service
282 ((_ svc (kind param => exp ...) clauses ...)
283 (if (eq? (service-kind svc) kind)
284 (let ((param (service-value svc)))
285 (service (service-kind svc)
287 (%modify-service svc clauses ...)))))
289 (define-syntax modify-services
291 "Modify the services listed in SERVICES according to CLAUSES and return
292 the resulting list of services. Each clause must have the form:
294 (TYPE VARIABLE => BODY)
296 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
297 identifier that is bound within BODY to the value of the service of that
298 TYPE. Consider this example:
300 (modify-services %base-services
301 (guix-service-type config =>
304 (use-substitutes? #f)
305 (extra-options '(\"--gc-keep-derivations\"))))
306 (mingetty-service-type config =>
307 (mingetty-configuration
309 (motd (plain-file \"motd\" \"Hi there!\")))))
311 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
312 all the MINGETTY-SERVICE-TYPE instances.
314 This is a shorthand for (map (lambda (svc) ...) %base-services)."
315 ((_ services clauses ...)
316 (map (lambda (service)
317 (%modify-service service clauses ...))
325 (define (system-derivation entries mextensions)
326 "Return as a monadic value the derivation of the 'system' directory
327 containing the given entries."
328 (mlet %store-monad ((extensions (mapm/accumulate-builds identity
332 (append entries (concatenate extensions))))))
334 (define system-service-type
335 ;; This is the ultimate service type, the root of the service DAG. The
336 ;; service of this type is extended by monadic name/item pairs. These items
337 ;; end up in the "system directory" as returned by
338 ;; 'operating-system-derivation'.
339 (service-type (name 'system)
342 (extend system-derivation)
344 "Build the operating system top-level directory, which in
345 turn refers to everything the operating system needs: its kernel, initrd,
346 system profile, boot script, and so on.")))
348 (define (compute-boot-script _ gexps)
349 ;; Reverse GEXPS so that extensions appear in the boot script in the right
350 ;; order. That is, user extensions would come first, and extensions added
351 ;; by 'essential-services' (e.g., running shepherd) are guaranteed to come
354 ;; Clean up and activate the system, then spawn shepherd.
355 #~(begin #$@(reverse gexps))))
357 (define (boot-script-entry mboot)
358 "Return, as a monadic value, an entry for the boot script in the system
360 (mlet %store-monad ((boot mboot))
361 (return `(("boot" ,boot)))))
363 (define boot-service-type
364 ;; The service of this type is extended by being passed gexps. It
365 ;; aggregates them in a single script, as a monadic value, which becomes its
367 (service-type (name 'boot)
369 (list (service-extension system-service-type
372 (extend compute-boot-script)
374 "Produce the operating system's boot script, which is spawned
375 by the initrd once the root file system is mounted.")))
377 (define %boot-service
378 ;; The service that produces the boot script.
379 (service boot-service-type #t))
383 ;;; Provenance tracking.
386 (define (object->pretty-string obj)
387 "Like 'object->string', but using 'pretty-print'."
388 (call-with-output-string
390 (pretty-print obj port))))
392 (define (channel->code channel)
393 "Return code to build CHANNEL, ready to be dropped in a 'channels.scm'
395 `(channel (name ',(channel-name channel))
396 (url ,(channel-url channel))
397 (branch ,(channel-branch channel))
398 (commit ,(channel-commit channel))))
400 (define (channel->sexp channel)
401 "Return an sexp describing CHANNEL. The sexp is _not_ code and is meant to
402 be parsed by tools; it's potentially more future-proof than code."
403 `(channel (name ,(channel-name channel))
404 (url ,(channel-url channel))
405 (branch ,(channel-branch channel))
406 (commit ,(channel-commit channel))))
408 (define (provenance-file channels config-file)
409 "Return a 'provenance' file describing CHANNELS, a list of channels, and
410 CONFIG-FILE, which can be either #f or a <local-file> containing the OS
411 configuration being used."
412 (scheme-file "provenance"
415 (channels #+@(if channels
416 (map channel->sexp channels)
418 (configuration-file #+config-file))))
420 (define (provenance-entry config-file)
421 "Return system entries describing the operating system provenance: the
422 channels in use and CONFIG-FILE, if it is true."
427 (and=> profile profile-channels))
430 (let ((config-file (cond ((string? config-file)
431 (local-file config-file "configuration.scm"))
436 (return `(("provenance" ,(provenance-file channels config-file))
439 ,(plain-file "channels.scm"
440 (object->pretty-string
442 ,@(map channel->code channels))))))
445 `(("configuration.scm" ,config-file))
448 (define provenance-service-type
449 (service-type (name 'provenance)
451 (list (service-extension system-service-type
453 (default-value #f) ;the OS config file
455 "Store provenance information about the system in the system
456 itself: the channels used when building the system, and its configuration
457 file, when available.")))
464 (define (cleanup-gexp _)
465 "Return a gexp to clean up /tmp and similar places upon boot."
466 (with-imported-modules '((guix build utils))
468 (use-modules (guix build utils))
470 ;; Clean out /tmp and /var/run.
472 ;; XXX This needs to happen before service activations, so it
473 ;; has to be here, but this also implicitly assumes that /tmp
474 ;; and /var/run are on the root partition.
475 (letrec-syntax ((fail-safe (syntax-rules ()
481 (fail-safe rest ...)))
484 ;; Ignore I/O errors so the system can boot.
486 ;; Remove stale Shadow lock files as they would lead to
487 ;; failures of 'useradd' & co.
488 (delete-file "/etc/group.lock")
489 (delete-file "/etc/passwd.lock")
490 (delete-file "/etc/.pwd.lock") ;from 'lckpwdf'
492 ;; Force file names to be decoded as UTF-8. See
493 ;; <https://bugs.gnu.org/26353>.
494 (setenv "GUIX_LOCPATH"
495 #+(file-append glibc-utf8-locales "/lib/locale"))
496 (setlocale LC_CTYPE "en_US.utf8")
497 (delete-file-recursively "/tmp")
498 (delete-file-recursively "/var/run")
501 (chmod "/tmp" #o1777)
503 (chmod "/var/run" #o755)
504 (delete-file-recursively "/run/udev/watch.old"))))))
506 (define cleanup-service-type
507 ;; Service that cleans things up in /tmp and similar.
508 (service-type (name 'cleanup)
510 (list (service-extension boot-service-type
513 "Delete files from @file{/tmp}, @file{/var/run}, and other
514 temporary locations at boot time.")))
516 (define* (activation-service->script service)
517 "Return as a monadic value the activation script for SERVICE, a service of
518 ACTIVATION-SCRIPT-TYPE."
519 (activation-script (service-value service)))
521 (define (activation-script gexps)
522 "Return the system's activation script, which evaluates GEXPS."
524 (map (cut program-file "activate-service.scm" <>) gexps))
526 (program-file "activate.scm"
527 (with-imported-modules (source-module-closure
528 '((gnu build activation)
531 (use-modules (gnu build activation)
534 ;; Make sure the user accounting database exists. If it
535 ;; does not exist, 'setutxent' does not create it and
536 ;; thus there is no accounting at all.
537 (close-port (open-file "/var/run/utmpx" "a0"))
539 ;; Same for 'wtmp', which is populated by mingetty et
542 (close-port (open-file "/var/log/wtmp" "a0"))
544 ;; Set up /run/current-system. Among other things this
545 ;; sets up locales, which the activation snippets
546 ;; executed below may expect.
547 (activate-current-system)
549 ;; Run the services' activation snippets.
550 ;; TODO: Use 'load-compiled'.
551 (for-each primitive-load '#$actions)))))
553 (define (gexps->activation-gexp gexps)
554 "Return a gexp that runs the activation script containing GEXPS."
555 #~(primitive-load #$(activation-script gexps)))
557 (define (second-argument a b) b)
559 (define activation-service-type
560 (service-type (name 'activate)
562 (list (service-extension boot-service-type
563 gexps->activation-gexp)))
565 (extend second-argument)
567 "Run @dfn{activation} code at boot time and upon
568 @command{guix system reconfigure} completion.")))
570 (define %activation-service
571 ;; The activation service produces the activation script from the gexps it
573 (service activation-service-type #t))
575 (define %modprobe-wrapper
576 ;; Wrapper for the 'modprobe' command that knows where modules live.
578 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
579 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
580 ;; environment variable is not set---hence the need for this wrapper.
581 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
582 (program-file "modprobe"
584 (setenv "LINUX_MODULE_DIRECTORY"
585 "/run/booted-system/kernel/lib/modules")
586 ;; FIXME: Remove this crutch when the patch #40422,
587 ;; updating to kmod 27 is merged.
588 (setenv "MODPROBE_OPTIONS"
589 "-C /etc/modprobe.d")
590 (apply execl #$modprobe
591 (cons #$modprobe (cdr (command-line))))))))
593 (define %linux-kernel-activation
594 ;; Activation of the Linux kernel running on the bare metal (as opposed to
595 ;; running in a container.)
597 ;; Tell the kernel to use our 'modprobe' command.
598 (activate-modprobe #$%modprobe-wrapper)
600 ;; Let users debug their own processes!
601 (activate-ptrace-attach)))
603 (define %linux-bare-metal-service
604 ;; The service that does things that are needed on the "bare metal", but not
605 ;; necessary or impossible in a container.
606 (simple-service 'linux-bare-metal
607 activation-service-type
608 %linux-kernel-activation))
610 (define %hurd-rc-script
611 ;; The RC script to be started upon boot.
613 (with-imported-modules (source-module-closure
615 (gnu build hurd-boot)
616 (guix build syscalls)))
618 (use-modules (guix build utils)
619 (gnu build hurd-boot)
620 (guix build syscalls)
625 (boot-hurd-system)))))
627 (define (hurd-rc-entry rc)
628 "Return, as a monadic value, an entry for the RC script in the system
630 (mlet %store-monad ((rc (lower-object rc)))
631 (return `(("rc" ,rc)))))
633 (define hurd-startup-service-type
634 ;; The service that creates the initial SYSTEM/rc startup file.
635 (service-type (name 'startup)
637 (list (service-extension system-service-type hurd-rc-entry)))
638 (default-value %hurd-rc-script)))
640 (define %hurd-startup-service
641 ;; The service that produces the RC script.
642 (service hurd-startup-service-type %hurd-rc-script))
644 (define special-files-service-type
645 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
647 (name 'special-files)
649 (list (service-extension activation-service-type
651 #~(activate-special-files '#$files)))))
652 (compose concatenate)
655 "Add special files to the root file system---e.g.,
656 @file{/usr/bin/env}.")))
658 (define (extra-special-file file target)
659 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
660 (file-append coreutils \"/bin/env\")
661 and FILE could be \"/usr/bin/env\"."
662 (simple-service (string->symbol (string-append "special-file-" file))
663 special-files-service-type
666 (define (etc-directory service)
667 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
668 (files->etc-directory (service-value service)))
670 (define (files->etc-directory files)
671 (define (assert-no-duplicates files)
672 (let loop ((files files)
677 (when (set-contains? seen file)
680 (message (format #f (G_ "duplicate '~a' entry for /etc")
682 (loop rest (set-insert file seen))))))
684 ;; Detect duplicates early instead of letting them through, eventually
685 ;; leading to a build failure of "etc.drv".
686 (assert-no-duplicates files)
688 (file-union "etc" files))
690 (define (etc-entry files)
691 "Return an entry for the /etc directory consisting of FILES in the system
693 (with-monad %store-monad
694 (return `(("etc" ,(files->etc-directory files))))))
696 (define etc-service-type
697 (service-type (name 'etc)
700 (service-extension activation-service-type
703 (files->etc-directory files)))
704 #~(activate-etc #$etc))))
705 (service-extension system-service-type etc-entry)))
706 (compose concatenate)
708 (description "Populate the @file{/etc} directory.")))
710 (define (etc-service files)
711 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
712 FILES must be a list of name/file-like object pairs."
713 (service etc-service-type files))
715 (define setuid-program-service-type
716 (service-type (name 'setuid-program)
718 (list (service-extension activation-service-type
720 #~(activate-setuid-programs
721 (list #$@programs))))))
722 (compose concatenate)
725 "Populate @file{/run/setuid-programs} with the specified
726 executables, making them setuid-root.")))
728 (define (packages->profile-entry packages)
729 "Return a system entry for the profile containing PACKAGES."
730 (with-monad %store-monad
731 (return `(("profile" ,(profile
732 (content (packages->manifest
733 (delete-duplicates packages eq?)))))))))
735 (define profile-service-type
736 ;; The service that populates the system's profile---i.e.,
737 ;; /run/current-system/profile. It is extended by package lists.
738 (service-type (name 'profile)
740 (list (service-extension system-service-type
741 packages->profile-entry)))
742 (compose concatenate)
745 "This is the @dfn{system profile}, available as
746 @file{/run/current-system/profile}. It contains packages that the sysadmin
747 wants to be globally available to all the system users.")))
749 (define (firmware->activation-gexp firmware)
750 "Return a gexp to make the packages listed in FIRMWARE loadable by the
752 (let ((directory (directory-union "firmware" firmware)))
753 ;; Tell the kernel where firmware is.
754 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
756 (define firmware-service-type
757 ;; The service that collects firmware.
758 (service-type (name 'firmware)
760 (list (service-extension activation-service-type
761 firmware->activation-gexp)))
762 (compose concatenate)
765 "Make ``firmware'' files loadable by the operating system
766 kernel. Firmware may then be uploaded to some of the machine's devices, such
769 (define (gc-roots->system-entry roots)
770 "Return an entry in the system's output containing symlinks to ROOTS."
771 (mlet %store-monad ((entry (gexp->derivation
773 #~(let ((roots '#$roots))
779 (iota (length roots))))))))
780 (return (if (null? roots)
782 `(("gc-roots" ,entry))))))
784 (define gc-root-service-type
785 ;; A service to associate extra garbage-collector roots to the system. This
786 ;; is a simple hack that guarantees that the system retains references to
787 ;; the given list of roots. Roots must be "lowerable" objects like
788 ;; packages, or derivations.
789 (service-type (name 'gc-roots)
791 (list (service-extension system-service-type
792 gc-roots->system-entry)))
793 (compose concatenate)
796 "Register garbage-collector roots---i.e., store items that
797 will not be reclaimed by the garbage collector.")
798 (default-value '())))
805 (define-condition-type &missing-target-service-error &service-error
806 missing-target-service-error?
807 (service missing-target-service-error-service)
808 (target-type missing-target-service-error-target-type))
810 (define-condition-type &ambiguous-target-service-error &service-error
811 ambiguous-target-service-error?
812 (service ambiguous-target-service-error-service)
813 (target-type ambiguous-target-service-error-target-type))
815 (define (missing-target-error service target-type)
817 (condition (&missing-target-service-error
819 (target-type target-type))
822 (format #f (G_ "no target of type '~a' for service '~a'")
823 (service-type-name target-type)
825 (service-kind service))))))))
827 (define (service-back-edges services)
828 "Return a procedure that, when passed a <service>, returns the list of
829 <service> objects that depend on it."
830 (define (add-edges service edges)
831 (define (add-edge extension edges)
832 (let ((target-type (service-extension-target extension)))
833 (match (filter (lambda (service)
834 (eq? (service-kind service) target-type))
837 (vhash-consq target service edges))
839 (missing-target-error service target-type))
842 (condition (&ambiguous-target-service-error
844 (target-type target-type))
848 (G_ "more than one target service of type '~a'")
849 (service-type-name target-type))))))))))
851 (fold add-edge edges (service-type-extensions (service-kind service))))
853 (let ((edges (fold add-edges vlist-null services)))
855 (reverse (vhash-foldq* cons '() node edges)))))
857 (define (instantiate-missing-services services)
858 "Return SERVICES, a list, augmented with any services targeted by extensions
859 and missing from SERVICES. Only service types with a default value can be
860 instantiated; other missing services lead to a
861 '&missing-target-service-error'."
862 (define (adjust-service-list svc result instances)
863 (fold2 (lambda (extension result instances)
865 (service-extension-target extension))
867 (match (vhash-assq target-type instances)
869 (let ((default (service-type-default-value target-type)))
870 (if (eq? &no-default-value default)
871 (missing-target-error svc target-type)
872 (let ((new (service target-type)))
873 (values (cons new result)
874 (vhash-consq target-type new instances))))))
876 (values result instances))))
879 (service-type-extensions (service-kind svc))))
881 (let loop ((services services))
883 (fold (lambda (service result)
884 (vhash-consq (service-kind service) service
886 vlist-null services))
889 (fold2 adjust-service-list
893 ;; If we instantiated services, they might in turn depend on missing
894 ;; services. Loop until we've reached fixed point.
895 (if (= (length adjusted) (vlist-length instances))
899 (define* (fold-services services
900 #:key (target-type system-service-type))
901 "Fold SERVICES by propagating their extensions down to the root of type
902 TARGET-TYPE; return the root service adjusted accordingly."
904 (service-back-edges services))
906 (define (matching-extension target)
907 (let ((target (service-kind target)))
909 (($ <service-extension> type)
910 (eq? type target)))))
912 (define (apply-extension target)
914 (match (find (matching-extension target)
915 (service-type-extensions (service-kind service)))
916 (($ <service-extension> _ compute)
917 (compute (service-value service))))))
919 (match (filter (lambda (service)
920 (eq? (service-kind service) target-type))
923 ;; Use the state monad to keep track of already-visited services in the
924 ;; graph and to memoize their value once folded.
926 (let loop ((sink sink))
927 (mlet %state-monad ((visited (current-state)))
928 (match (vhash-assq sink visited)
931 ((dependents (mapm %state-monad loop (dependents sink)))
932 (visited (current-state))
933 (extensions -> (map (apply-extension sink) dependents))
934 (extend -> (service-type-extend (service-kind sink)))
935 (compose -> (service-type-compose (service-kind sink)))
936 (params -> (service-value sink))
939 ;; Distinguish COMPOSE and EXTEND because PARAMS typically
940 ;; has a different type than the elements of EXTENSIONS.
942 (service (service-kind sink)
943 (extend params (compose extensions)))
946 (set-current-state (vhash-consq sink service visited))
948 ((_ . service) ;SINK was already visited
953 (condition (&missing-target-service-error
955 (target-type target-type))
957 (message (format #f (G_ "service of type '~a' not found")
958 (service-type-name target-type)))))))
961 (condition (&ambiguous-target-service-error
963 (target-type target-type))
967 (G_ "more than one target service of type '~a'")
968 (service-type-name target-type)))))))))
970 ;;; services.scm ends here.