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 ;;; Copyright © 2020, 2021 Ricardo Wurmus <rekado@elephly.net>
6 ;;; Copyright © 2021 raid5atemyhomework <raid5atemyhomework@protonmail.com>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (gnu services)
24 #:use-module (guix gexp)
25 #:use-module (guix monads)
26 #:use-module (guix store)
27 #:use-module (guix records)
28 #:use-module (guix profiles)
29 #:use-module (guix discovery)
30 #:use-module (guix combinators)
31 #:use-module (guix channels)
32 #:use-module (guix describe)
33 #:use-module (guix sets)
34 #:use-module (guix ui)
35 #:use-module (guix diagnostics)
36 #:autoload (guix openpgp) (openpgp-format-fingerprint)
37 #:use-module (guix modules)
38 #:use-module (guix packages)
39 #:use-module (guix utils)
40 #:use-module (gnu packages base)
41 #:use-module (gnu packages bash)
42 #:use-module (gnu packages hurd)
43 #:use-module (srfi srfi-1)
44 #:use-module (srfi srfi-9)
45 #:use-module (srfi srfi-9 gnu)
46 #:use-module (srfi srfi-26)
47 #:use-module (srfi srfi-34)
48 #:use-module (srfi srfi-35)
49 #:use-module (ice-9 vlist)
50 #:use-module (ice-9 match)
51 #:autoload (ice-9 pretty-print) (pretty-print)
52 #:export (service-extension
54 service-extension-target
55 service-extension-compute
60 service-type-extensions
63 service-type-default-value
64 service-type-description
75 service-parameters ;deprecated
80 instantiate-missing-services
84 missing-value-service-error?
85 missing-value-service-error-type
86 missing-value-service-error-location
87 missing-target-service-error?
88 missing-target-service-error-service
89 missing-target-service-error-target-type
90 ambiguous-target-service-error?
91 ambiguous-target-service-error-service
92 ambiguous-target-service-error-target-type
95 provenance-service-type
96 sexp->system-provenance
100 activation-service-type
101 activation-service->script
102 %linux-bare-metal-service
104 %hurd-startup-service
105 special-files-service-type
109 setuid-program-service-type
111 firmware-service-type
113 linux-builder-service-type
114 linux-builder-configuration
115 linux-builder-configuration?
116 linux-builder-configuration-kernel
117 linux-builder-configuration-modules
118 linux-loadable-module-service-type
123 #:re-export (;; Note: Re-export 'delete' to allow for proper syntax matching
124 ;; in 'modify-services' forms. See
125 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=26805#16>.
130 ;;; This module defines a broad notion of "service types" and "services."
132 ;;; A service type describe how its instances extend instances of other
133 ;;; service types. For instance, some services extend the instance of
134 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
135 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
136 ;;; <shepherd-service>.
138 ;;; When applicable, the service type defines how it can itself be extended,
139 ;;; by providing one procedure to compose extensions, and one procedure to
142 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
143 ;;; instance, which is the root of the service DAG. Its value is the
144 ;;; derivation that produces the 'system' directory as returned by
145 ;;; 'operating-system-derivation'.
147 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
148 ;;; "folds" by propagating extensions down the graph; it returns the root
149 ;;; service after the applying all its extensions.
153 (define-record-type <service-extension>
154 (service-extension target compute)
156 (target service-extension-target) ;<service-type>
157 (compute service-extension-compute)) ;params -> params
159 (define &no-default-value
160 ;; Value used to denote service types that have no associated default value.
163 (define-record-type* <service-type> service-type make-service-type
165 (name service-type-name) ;symbol (for debugging)
167 ;; Things extended by services of this type.
168 (extensions service-type-extensions) ;list of <service-extensions>
170 ;; Given a list of extensions, "compose" them.
171 (compose service-type-compose ;list of Any -> Any
174 ;; Extend the services' own parameters with the extension composition.
175 (extend service-type-extend ;list of Any -> parameters
178 ;; Optional default value for instances of this type.
179 (default-value service-type-default-value ;Any
180 (default &no-default-value))
183 (description service-type-description ;string
185 (location service-type-location ;<location>
186 (default (and=> (current-source-location)
187 source-properties->location))
190 (define (write-service-type type port)
191 (format port "#<service-type ~a ~a>"
192 (service-type-name type)
193 (number->string (object-address type) 16)))
195 (set-record-type-printer! <service-type> write-service-type)
197 (define %distro-root-directory
198 ;; Absolute file name of the module hierarchy.
199 (dirname (search-path %load-path "guix.scm")))
201 (define %service-type-path
202 ;; Search path for service types.
203 (make-parameter `((,%distro-root-directory . "gnu/services")
204 (,%distro-root-directory . "gnu/system"))))
206 (define (all-service-modules)
207 "Return the default set of service modules."
208 (cons (resolve-interface '(gnu services))
209 (all-modules (%service-type-path)
210 #:warn warn-about-load-error)))
212 (define* (fold-service-types proc seed
214 (modules (all-service-modules)))
215 "For each service type exported by one of MODULES, call (PROC RESULT). SEED
216 is used as the initial value of RESULT."
217 (fold-module-public-variables (lambda (object result)
218 (if (service-type? object)
224 (define lookup-service-types
226 (delay (fold-service-types (lambda (type result)
227 (vhash-consq (service-type-name type)
231 "Return the list of services with the given NAME (a symbol)."
232 (vhash-foldq* cons '() name (force table)))))
234 ;; Services of a given type.
235 (define-record-type <service>
236 (make-service type value)
239 (value service-value))
241 (define-syntax service
243 "Return a service instance of TYPE. The service value is VALUE or, if
244 omitted, TYPE's default value."
246 (make-service type value))
248 (%service-with-default-value (current-source-location)
251 (define (%service-with-default-value location type)
252 "Return a instance of service type TYPE with its default value, if any. If
253 TYPE does not have a default value, an error is raised."
254 ;; TODO: Currently this is a run-time error but with a little bit macrology
255 ;; we could turn it into an expansion-time error.
256 (let ((default (service-type-default-value type)))
257 (if (eq? default &no-default-value)
258 (let ((location (source-properties->location location)))
260 (make-compound-condition
262 (&missing-value-service-error (type type) (location location)))
263 (formatted-message (G_ "~a: no value specified \
264 for service of type '~a'")
265 (location->string location)
266 (service-type-name type)))))
267 (service type default))))
269 (define-condition-type &service-error &error
272 (define-condition-type &missing-value-service-error &service-error
273 missing-value-service-error?
274 (type missing-value-service-error-type)
275 (location missing-value-service-error-location))
283 (define service-parameters
287 (define (simple-service name target value)
288 "Return a service that extends TARGET with VALUE. This works by creating a
289 singleton service type NAME, of which the returned service is an instance."
290 (let* ((extension (service-extension target identity))
291 (type (service-type (name name)
292 (extensions (list extension)))))
293 (service type value)))
295 (define-syntax %modify-service
296 (syntax-rules (=> delete)
297 ((_ svc (delete kind) clauses ...)
298 (if (eq? (service-kind svc) kind)
300 (%modify-service svc clauses ...)))
303 ((_ svc (kind param => exp ...) clauses ...)
304 (if (eq? (service-kind svc) kind)
305 (let ((param (service-value svc)))
306 (service (service-kind svc)
308 (%modify-service svc clauses ...)))))
310 (define-syntax modify-services
312 "Modify the services listed in SERVICES according to CLAUSES and return
313 the resulting list of services. Each clause must have the form:
315 (TYPE VARIABLE => BODY)
317 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
318 identifier that is bound within BODY to the value of the service of that
319 TYPE. Consider this example:
321 (modify-services %base-services
322 (guix-service-type config =>
325 (use-substitutes? #f)
326 (extra-options '(\"--gc-keep-derivations\"))))
327 (mingetty-service-type config =>
328 (mingetty-configuration
330 (motd (plain-file \"motd\" \"Hi there!\"))))
331 (delete udev-service-type))
333 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
334 all the MINGETTY-SERVICE-TYPE instances, and it deletes instances of the
337 This is a shorthand for (filter-map (lambda (svc) ...) %base-services)."
338 ((_ services clauses ...)
339 (filter-map (lambda (service)
340 (%modify-service service clauses ...))
348 (define (system-derivation entries mextensions)
349 "Return as a monadic value the derivation of the 'system' directory
350 containing the given entries."
351 (mlet %store-monad ((extensions (mapm/accumulate-builds identity
355 (append entries (concatenate extensions))))))
357 (define system-service-type
358 ;; This is the ultimate service type, the root of the service DAG. The
359 ;; service of this type is extended by monadic name/item pairs. These items
360 ;; end up in the "system directory" as returned by
361 ;; 'operating-system-derivation'.
362 (service-type (name 'system)
365 (extend system-derivation)
367 "Build the operating system top-level directory, which in
368 turn refers to everything the operating system needs: its kernel, initrd,
369 system profile, boot script, and so on.")))
371 (define (compute-boot-script _ gexps)
372 ;; Reverse GEXPS so that extensions appear in the boot script in the right
373 ;; order. That is, user extensions would come first, and extensions added
374 ;; by 'essential-services' (e.g., running shepherd) are guaranteed to come
377 ;; Clean up and activate the system, then spawn shepherd.
378 #~(begin #$@(reverse gexps))))
380 (define (boot-script-entry mboot)
381 "Return, as a monadic value, an entry for the boot script in the system
383 (mlet %store-monad ((boot mboot))
384 (return `(("boot" ,boot)))))
386 (define boot-service-type
387 ;; The service of this type is extended by being passed gexps. It
388 ;; aggregates them in a single script, as a monadic value, which becomes its
390 (service-type (name 'boot)
392 (list (service-extension system-service-type
395 (extend compute-boot-script)
397 "Produce the operating system's boot script, which is spawned
398 by the initrd once the root file system is mounted.")))
400 (define %boot-service
401 ;; The service that produces the boot script.
402 (service boot-service-type #t))
406 ;;; Provenance tracking.
409 (define (object->pretty-string obj)
410 "Like 'object->string', but using 'pretty-print'."
411 (call-with-output-string
413 (pretty-print obj port))))
415 (define (channel->code channel)
416 "Return code to build CHANNEL, ready to be dropped in a 'channels.scm'
418 ;; Since the 'introduction' field is backward-incompatible, and since it's
419 ;; optional when using the "official" 'guix channel, include it if and only
420 ;; if we're referring to a different channel.
421 (let ((intro (and (not (equal? (list channel) %default-channels))
422 (channel-introduction channel))))
423 `(channel (name ',(channel-name channel))
424 (url ,(channel-url channel))
425 (branch ,(channel-branch channel))
426 (commit ,(channel-commit channel))
429 (make-channel-introduction
430 ,(channel-introduction-first-signed-commit intro)
432 ,(openpgp-format-fingerprint
433 (channel-introduction-first-commit-signer
437 (define (channel->sexp channel)
438 "Return an sexp describing CHANNEL. The sexp is _not_ code and is meant to
439 be parsed by tools; it's potentially more future-proof than code."
440 ;; TODO: Add CHANNEL's introduction. Currently we can't do that because
441 ;; older 'guix system describe' expect exactly name/url/branch/commit
442 ;; without any additional fields.
443 `(channel (name ,(channel-name channel))
444 (url ,(channel-url channel))
445 (branch ,(channel-branch channel))
446 (commit ,(channel-commit channel))))
448 (define (sexp->channel sexp)
449 "Return the channel corresponding to SEXP, an sexp as found in the
450 \"provenance\" file produced by 'provenance-service-type'."
452 (('channel ('name name)
457 ;; XXX: In the future REST may include a channel introduction.
458 (channel (name name) (url url)
459 (branch branch) (commit commit)))))
461 (define (provenance-file channels config-file)
462 "Return a 'provenance' file describing CHANNELS, a list of channels, and
463 CONFIG-FILE, which can be either #f or a <local-file> containing the OS
464 configuration being used."
465 (scheme-file "provenance"
468 (channels #+@(if channels
469 (map channel->sexp channels)
471 (configuration-file #+config-file))))
473 (define (provenance-entry config-file)
474 "Return system entries describing the operating system provenance: the
475 channels in use and CONFIG-FILE, if it is true."
480 (and=> profile profile-channels))
483 (let ((config-file (cond ((string? config-file)
484 ;; CONFIG-FILE has been passed typically via
485 ;; 'guix system reconfigure CONFIG-FILE' so we
486 ;; can assume it's valid: tell 'local-file' to
487 ;; not emit a warning.
488 (local-file (assume-valid-file-name config-file)
489 "configuration.scm"))
494 (return `(("provenance" ,(provenance-file channels config-file))
497 ,(plain-file "channels.scm"
498 (object->pretty-string
500 ,@(map channel->code channels))))))
503 `(("configuration.scm" ,config-file))
506 (define provenance-service-type
507 (service-type (name 'provenance)
509 (list (service-extension system-service-type
511 (default-value #f) ;the OS config file
513 "Store provenance information about the system in the system
514 itself: the channels used when building the system, and its configuration
515 file, when available.")))
517 (define (sexp->system-provenance sexp)
518 "Parse SEXP, an s-expression read from /run/current-system/provenance or
519 similar, and return two values: the list of channels listed therein, and the
520 OS configuration file or #f."
522 (('provenance ('version 0)
523 ('channels channels ...)
524 ('configuration-file config-file))
525 (values (map sexp->channel channels)
530 (define (system-provenance system)
531 "Given SYSTEM, the file name of a system generation, return two values: the
532 list of channels SYSTEM is built from, and its configuration file. If that
533 information is missing, return the empty list (for channels) and possibly
534 #false (for the configuration file)."
537 (sexp->system-provenance
538 (call-with-input-file (string-append system "/provenance")
547 (define (cleanup-gexp _)
548 "Return a gexp to clean up /tmp and similar places upon boot."
549 (with-imported-modules '((guix build utils))
551 (use-modules (guix build utils))
553 ;; Clean out /tmp and /var/run.
555 ;; XXX This needs to happen before service activations, so it
556 ;; has to be here, but this also implicitly assumes that /tmp
557 ;; and /var/run are on the root partition.
558 (letrec-syntax ((fail-safe (syntax-rules ()
564 (fail-safe rest ...)))
567 ;; Ignore I/O errors so the system can boot.
569 ;; Remove stale Shadow lock files as they would lead to
570 ;; failures of 'useradd' & co.
571 (delete-file "/etc/group.lock")
572 (delete-file "/etc/passwd.lock")
573 (delete-file "/etc/.pwd.lock") ;from 'lckpwdf'
575 ;; Force file names to be decoded as UTF-8. See
576 ;; <https://bugs.gnu.org/26353>.
577 (setenv "GUIX_LOCPATH"
578 #+(file-append glibc-utf8-locales "/lib/locale"))
579 (setlocale LC_CTYPE "en_US.utf8")
580 (delete-file-recursively "/tmp")
581 (delete-file-recursively "/var/run")
584 (chmod "/tmp" #o1777)
586 (chmod "/var/run" #o755)
587 (delete-file-recursively "/run/udev/watch.old"))))))
589 (define cleanup-service-type
590 ;; Service that cleans things up in /tmp and similar.
591 (service-type (name 'cleanup)
593 (list (service-extension boot-service-type
596 "Delete files from @file{/tmp}, @file{/var/run}, and other
597 temporary locations at boot time.")))
599 (define* (activation-service->script service)
600 "Return as a monadic value the activation script for SERVICE, a service of
601 ACTIVATION-SCRIPT-TYPE."
602 (activation-script (service-value service)))
604 (define (activation-script gexps)
605 "Return the system's activation script, which evaluates GEXPS."
607 (map (cut program-file "activate-service.scm" <>) gexps))
609 (program-file "activate.scm"
610 (with-imported-modules (source-module-closure
611 '((gnu build activation)
614 (use-modules (gnu build activation)
617 ;; Make sure the user accounting database exists. If it
618 ;; does not exist, 'setutxent' does not create it and
619 ;; thus there is no accounting at all.
620 (close-port (open-file "/var/run/utmpx" "a0"))
622 ;; Same for 'wtmp', which is populated by mingetty et
625 (close-port (open-file "/var/log/wtmp" "a0"))
627 ;; Set up /run/current-system. Among other things this
628 ;; sets up locales, which the activation snippets
629 ;; executed below may expect.
630 (activate-current-system)
632 ;; Run the services' activation snippets.
633 ;; TODO: Use 'load-compiled'.
634 (for-each primitive-load '#$actions)))))
636 (define (gexps->activation-gexp gexps)
637 "Return a gexp that runs the activation script containing GEXPS."
638 #~(primitive-load #$(activation-script gexps)))
640 (define (activation-profile-entry gexps)
641 "Return, as a monadic value, an entry for the activation script in the
643 (mlet %store-monad ((activate (lower-object (activation-script gexps))))
644 (return `(("activate" ,activate)))))
646 (define (second-argument a b) b)
648 (define activation-service-type
649 (service-type (name 'activate)
651 (list (service-extension boot-service-type
652 gexps->activation-gexp)
653 (service-extension system-service-type
654 activation-profile-entry)))
656 (extend second-argument)
658 "Run @dfn{activation} code at boot time and upon
659 @command{guix system reconfigure} completion.")))
661 (define %activation-service
662 ;; The activation service produces the activation script from the gexps it
664 (service activation-service-type #t))
666 (define %modprobe-wrapper
667 ;; Wrapper for the 'modprobe' command that knows where modules live.
669 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
670 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
671 ;; environment variable is not set---hence the need for this wrapper.
672 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
673 (program-file "modprobe"
675 (setenv "LINUX_MODULE_DIRECTORY"
676 "/run/booted-system/kernel/lib/modules")
677 ;; FIXME: Remove this crutch when the patch #40422,
678 ;; updating to kmod 27 is merged.
679 (setenv "MODPROBE_OPTIONS"
680 "-C /etc/modprobe.d")
681 (apply execl #$modprobe
682 (cons #$modprobe (cdr (command-line))))))))
684 (define %linux-kernel-activation
685 ;; Activation of the Linux kernel running on the bare metal (as opposed to
686 ;; running in a container.)
688 ;; Tell the kernel to use our 'modprobe' command.
689 (activate-modprobe #$%modprobe-wrapper)
691 ;; Let users debug their own processes!
692 (activate-ptrace-attach)))
694 (define %linux-bare-metal-service
695 ;; The service that does things that are needed on the "bare metal", but not
696 ;; necessary or impossible in a container.
697 (simple-service 'linux-bare-metal
698 activation-service-type
699 %linux-kernel-activation))
701 (define %hurd-rc-script
702 ;; The RC script to be started upon boot.
704 (with-imported-modules (source-module-closure
706 (gnu build hurd-boot)
707 (guix build syscalls)))
709 (use-modules (guix build utils)
710 (gnu build hurd-boot)
711 (guix build syscalls)
716 (boot-hurd-system)))))
718 (define (hurd-rc-entry rc)
719 "Return, as a monadic value, an entry for the RC script in the system
721 (mlet %store-monad ((rc (lower-object rc)))
722 (return `(("rc" ,rc)))))
724 (define hurd-startup-service-type
725 ;; The service that creates the initial SYSTEM/rc startup file.
726 (service-type (name 'startup)
728 (list (service-extension system-service-type hurd-rc-entry)))
729 (default-value %hurd-rc-script)))
731 (define %hurd-startup-service
732 ;; The service that produces the RC script.
733 (service hurd-startup-service-type %hurd-rc-script))
735 (define special-files-service-type
736 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
738 (name 'special-files)
740 (list (service-extension activation-service-type
742 #~(activate-special-files '#$files)))))
743 (compose concatenate)
746 "Add special files to the root file system---e.g.,
747 @file{/usr/bin/env}.")))
749 (define (extra-special-file file target)
750 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
751 (file-append coreutils \"/bin/env\")
752 and FILE could be \"/usr/bin/env\"."
753 (simple-service (string->symbol (string-append "special-file-" file))
754 special-files-service-type
757 (define (etc-directory service)
758 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
759 (files->etc-directory (service-value service)))
761 (define (files->etc-directory files)
762 (define (assert-no-duplicates files)
763 (let loop ((files files)
768 (when (set-contains? seen file)
769 (raise (formatted-message (G_ "duplicate '~a' entry for /etc")
771 (loop rest (set-insert file seen))))))
773 ;; Detect duplicates early instead of letting them through, eventually
774 ;; leading to a build failure of "etc.drv".
775 (assert-no-duplicates files)
777 (file-union "etc" files))
779 (define (etc-entry files)
780 "Return an entry for the /etc directory consisting of FILES in the system
782 (with-monad %store-monad
783 (return `(("etc" ,(files->etc-directory files))))))
785 (define etc-service-type
786 (service-type (name 'etc)
789 (service-extension activation-service-type
792 (files->etc-directory files)))
793 #~(activate-etc #$etc))))
794 (service-extension system-service-type etc-entry)))
795 (compose concatenate)
797 (description "Populate the @file{/etc} directory.")))
799 (define (etc-service files)
800 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
801 FILES must be a list of name/file-like object pairs."
802 (service etc-service-type files))
804 (define setuid-program-service-type
805 (service-type (name 'setuid-program)
807 (list (service-extension activation-service-type
809 #~(activate-setuid-programs
810 (list #$@programs))))))
811 (compose concatenate)
814 "Populate @file{/run/setuid-programs} with the specified
815 executables, making them setuid-root.")))
817 (define (packages->profile-entry packages)
818 "Return a system entry for the profile containing PACKAGES."
819 ;; XXX: 'mlet' is needed here for one reason: to get the proper
820 ;; '%current-target' and '%current-target-system' bindings when
821 ;; 'packages->manifest' is called, and thus when the 'package-inputs'
822 ;; etc. procedures are called on PACKAGES. That way, conditionals in those
823 ;; inputs see the "correct" value of these two parameters. See
824 ;; <https://issues.guix.gnu.org/44952>.
825 (mlet %store-monad ((_ (current-target-system)))
826 (return `(("profile" ,(profile
827 (content (packages->manifest
828 (delete-duplicates packages eq?)))))))))
830 (define profile-service-type
831 ;; The service that populates the system's profile---i.e.,
832 ;; /run/current-system/profile. It is extended by package lists.
833 (service-type (name 'profile)
835 (list (service-extension system-service-type
836 packages->profile-entry)))
837 (compose concatenate)
840 "This is the @dfn{system profile}, available as
841 @file{/run/current-system/profile}. It contains packages that the sysadmin
842 wants to be globally available to all the system users.")))
844 (define (firmware->activation-gexp firmware)
845 "Return a gexp to make the packages listed in FIRMWARE loadable by the
847 (let ((directory (directory-union "firmware" firmware)))
848 ;; Tell the kernel where firmware is.
849 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
851 (define firmware-service-type
852 ;; The service that collects firmware.
853 (service-type (name 'firmware)
855 (list (service-extension activation-service-type
856 firmware->activation-gexp)))
857 (compose concatenate)
860 "Make ``firmware'' files loadable by the operating system
861 kernel. Firmware may then be uploaded to some of the machine's devices, such
864 (define (gc-roots->system-entry roots)
865 "Return an entry in the system's output containing symlinks to ROOTS."
866 (mlet %store-monad ((entry (gexp->derivation
868 #~(let ((roots '#$roots))
874 (iota (length roots))))))))
875 (return (if (null? roots)
877 `(("gc-roots" ,entry))))))
879 (define gc-root-service-type
880 ;; A service to associate extra garbage-collector roots to the system. This
881 ;; is a simple hack that guarantees that the system retains references to
882 ;; the given list of roots. Roots must be "lowerable" objects like
883 ;; packages, or derivations.
884 (service-type (name 'gc-roots)
886 (list (service-extension system-service-type
887 gc-roots->system-entry)))
888 (compose concatenate)
891 "Register garbage-collector roots---i.e., store items that
892 will not be reclaimed by the garbage collector.")
893 (default-value '())))
895 ;; Configuration for the Linux kernel builder.
896 (define-record-type* <linux-builder-configuration>
897 linux-builder-configuration
898 make-linux-builder-configuration
899 linux-builder-configuration?
900 this-linux-builder-configuration
902 (kernel linux-builder-configuration-kernel) ; package
903 (modules linux-builder-configuration-modules (default '()))) ; list of packages
905 (define (package-for-kernel target-kernel module-package)
906 "Return a package like MODULE-PACKAGE, adapted for TARGET-KERNEL, if
907 possible (that is if there's a LINUX keyword argument in the build system)."
909 (inherit module-package)
911 (substitute-keyword-arguments (package-arguments module-package)
915 (define (linux-builder-configuration->system-entry config)
916 "Return the kernel entry of the 'system' directory."
917 (let* ((kernel (linux-builder-configuration-kernel config))
918 (modules (linux-builder-configuration-modules config))
920 (content (packages->manifest
922 (map (lambda (module)
925 (package-for-kernel kernel module))
926 ;; support (,package "kernel-module-output")
927 ((and (list? module) (package? (car module)))
928 (cons (package-for-kernel kernel
934 (hooks (list linux-module-database)))))
935 (with-monad %store-monad
936 (return `(("kernel" ,kernel))))))
938 (define linux-builder-service-type
939 (service-type (name 'linux-builder)
941 (list (service-extension system-service-type
942 linux-builder-configuration->system-entry)))
945 (extend (lambda (config modifiers)
946 (if (null? modifiers)
948 ((apply compose modifiers) config))))
949 (description "Builds the linux-libre kernel profile, containing
950 the kernel itself and any linux-loadable kernel modules. This can be extended
951 with a function that accepts the current configuration and returns a new
954 (define (linux-loadable-module-builder-modifier modules)
955 "Extends linux-builder-service-type by appending the given MODULES to the
956 configuration of linux-builder-service-type."
958 (linux-builder-configuration
960 (modules (append (linux-builder-configuration-modules config)
963 (define linux-loadable-module-service-type
964 (service-type (name 'linux-loadable-modules)
966 (list (service-extension linux-builder-service-type
967 linux-loadable-module-builder-modifier)))
969 (compose concatenate)
971 (description "Adds packages and package outputs as modules
972 included in the booted linux-libre profile. Other services can extend this
973 service type to add particular modules to the set of linux-loadable modules.")))
981 (define-condition-type &missing-target-service-error &service-error
982 missing-target-service-error?
983 (service missing-target-service-error-service)
984 (target-type missing-target-service-error-target-type))
986 (define-condition-type &ambiguous-target-service-error &service-error
987 ambiguous-target-service-error?
988 (service ambiguous-target-service-error-service)
989 (target-type ambiguous-target-service-error-target-type))
991 (define (missing-target-error service target-type)
993 (condition (&missing-target-service-error
995 (target-type target-type))
998 (format #f (G_ "no target of type '~a' for service '~a'")
999 (service-type-name target-type)
1001 (service-kind service))))))))
1003 (define (service-back-edges services)
1004 "Return a procedure that, when passed a <service>, returns the list of
1005 <service> objects that depend on it."
1006 (define (add-edges service edges)
1007 (define (add-edge extension edges)
1008 (let ((target-type (service-extension-target extension)))
1009 (match (filter (lambda (service)
1010 (eq? (service-kind service) target-type))
1013 (vhash-consq target service edges))
1015 (missing-target-error service target-type))
1018 (condition (&ambiguous-target-service-error
1020 (target-type target-type))
1024 (G_ "more than one target service of type '~a'")
1025 (service-type-name target-type))))))))))
1027 (fold add-edge edges (service-type-extensions (service-kind service))))
1029 (let ((edges (fold add-edges vlist-null services)))
1031 (reverse (vhash-foldq* cons '() node edges)))))
1033 (define (instantiate-missing-services services)
1034 "Return SERVICES, a list, augmented with any services targeted by extensions
1035 and missing from SERVICES. Only service types with a default value can be
1036 instantiated; other missing services lead to a
1037 '&missing-target-service-error'."
1038 (define (adjust-service-list svc result instances)
1039 (fold2 (lambda (extension result instances)
1041 (service-extension-target extension))
1043 (match (vhash-assq target-type instances)
1045 (let ((default (service-type-default-value target-type)))
1046 (if (eq? &no-default-value default)
1047 (missing-target-error svc target-type)
1048 (let ((new (service target-type)))
1049 (values (cons new result)
1050 (vhash-consq target-type new instances))))))
1052 (values result instances))))
1055 (service-type-extensions (service-kind svc))))
1057 (let loop ((services services))
1059 (fold (lambda (service result)
1060 (vhash-consq (service-kind service) service
1062 vlist-null services))
1065 (fold2 adjust-service-list
1069 ;; If we instantiated services, they might in turn depend on missing
1070 ;; services. Loop until we've reached fixed point.
1071 (if (= (length adjusted) (vlist-length instances))
1075 (define* (fold-services services
1076 #:key (target-type system-service-type))
1077 "Fold SERVICES by propagating their extensions down to the root of type
1078 TARGET-TYPE; return the root service adjusted accordingly."
1080 (service-back-edges services))
1082 (define (matching-extension target)
1083 (let ((target (service-kind target)))
1085 (($ <service-extension> type)
1086 (eq? type target)))))
1088 (define (apply-extension target)
1090 (match (find (matching-extension target)
1091 (service-type-extensions (service-kind service)))
1092 (($ <service-extension> _ compute)
1093 (compute (service-value service))))))
1095 (match (filter (lambda (service)
1096 (eq? (service-kind service) target-type))
1099 ;; Use the state monad to keep track of already-visited services in the
1100 ;; graph and to memoize their value once folded.
1102 (let loop ((sink sink))
1103 (mlet %state-monad ((visited (current-state)))
1104 (match (vhash-assq sink visited)
1107 ((dependents (mapm %state-monad loop (dependents sink)))
1108 (visited (current-state))
1109 (extensions -> (map (apply-extension sink) dependents))
1110 (extend -> (service-type-extend (service-kind sink)))
1111 (compose -> (service-type-compose (service-kind sink)))
1112 (params -> (service-value sink))
1115 ;; Distinguish COMPOSE and EXTEND because PARAMS typically
1116 ;; has a different type than the elements of EXTENSIONS.
1118 (service (service-kind sink)
1119 (extend params (compose extensions)))
1121 (mbegin %state-monad
1122 (set-current-state (vhash-consq sink service visited))
1124 ((_ . service) ;SINK was already visited
1125 (return service)))))
1129 (make-compound-condition
1130 (condition (&missing-target-service-error
1132 (target-type target-type)))
1133 (formatted-message (G_ "service of type '~a' not found")
1134 (service-type-name target-type)))))
1137 (condition (&ambiguous-target-service-error
1139 (target-type target-type))
1143 (G_ "more than one target service of type '~a'")
1144 (service-type-name target-type)))))))))
1146 ;;; services.scm ends here.