1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015-2022 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>
7 ;;; Copyright © 2020 Christine Lemmer-Webber <cwebber@dustycloud.org>
8 ;;; Copyright © 2020, 2021 Brice Waegeneire <brice@waegenei.re>
10 ;;; This file is part of GNU Guix.
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25 (define-module (gnu services)
26 #:use-module (guix gexp)
27 #:use-module (guix monads)
28 #:use-module (guix store)
29 #:use-module (guix records)
30 #:use-module (guix profiles)
31 #:use-module (guix discovery)
32 #:use-module (guix combinators)
33 #:use-module (guix channels)
34 #:use-module (guix describe)
35 #:use-module (guix sets)
36 #:use-module (guix ui)
37 #:use-module (guix diagnostics)
38 #:autoload (guix openpgp) (openpgp-format-fingerprint)
39 #:use-module (guix modules)
40 #:use-module (guix packages)
41 #:use-module (guix utils)
42 #:use-module (gnu packages base)
43 #:use-module (gnu packages bash)
44 #:use-module (gnu packages hurd)
45 #:use-module (gnu system setuid)
46 #:use-module (srfi srfi-1)
47 #:use-module (srfi srfi-9)
48 #:use-module (srfi srfi-9 gnu)
49 #:use-module (srfi srfi-26)
50 #:use-module (srfi srfi-34)
51 #:use-module (srfi srfi-35)
52 #:use-module (ice-9 vlist)
53 #:use-module (ice-9 match)
54 #:autoload (ice-9 pretty-print) (pretty-print)
55 #:export (service-extension
57 service-extension-target
58 service-extension-compute
63 service-type-extensions
66 service-type-default-value
67 service-type-description
78 service-parameters ;deprecated
83 instantiate-missing-services
87 missing-value-service-error?
88 missing-value-service-error-type
89 missing-value-service-error-location
90 missing-target-service-error?
91 missing-target-service-error-service
92 missing-target-service-error-target-type
93 ambiguous-target-service-error?
94 ambiguous-target-service-error-service
95 ambiguous-target-service-error-target-type
98 provenance-service-type
99 sexp->system-provenance
103 activation-service-type
104 activation-service->script
105 %linux-bare-metal-service
107 %hurd-startup-service
108 special-files-service-type
112 setuid-program-service-type
114 firmware-service-type
116 linux-builder-service-type
117 linux-builder-configuration
118 linux-builder-configuration?
119 linux-builder-configuration-kernel
120 linux-builder-configuration-modules
121 linux-loadable-module-service-type
126 #:re-export (;; Note: Re-export 'delete' to allow for proper syntax matching
127 ;; in 'modify-services' forms. See
128 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=26805#16>.
133 ;;; This module defines a broad notion of "service types" and "services."
135 ;;; A service type describe how its instances extend instances of other
136 ;;; service types. For instance, some services extend the instance of
137 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
138 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
139 ;;; <shepherd-service>.
141 ;;; When applicable, the service type defines how it can itself be extended,
142 ;;; by providing one procedure to compose extensions, and one procedure to
145 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
146 ;;; instance, which is the root of the service DAG. Its value is the
147 ;;; derivation that produces the 'system' directory as returned by
148 ;;; 'operating-system-derivation'.
150 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
151 ;;; "folds" by propagating extensions down the graph; it returns the root
152 ;;; service after the applying all its extensions.
156 (define-record-type <service-extension>
157 (service-extension target compute)
159 (target service-extension-target) ;<service-type>
160 (compute service-extension-compute)) ;params -> params
162 (define &no-default-value
163 ;; Value used to denote service types that have no associated default value.
166 (define-record-type* <service-type> service-type make-service-type
168 (name service-type-name) ;symbol (for debugging)
170 ;; Things extended by services of this type.
171 (extensions service-type-extensions) ;list of <service-extensions>
173 ;; Given a list of extensions, "compose" them.
174 (compose service-type-compose ;list of Any -> Any
177 ;; Extend the services' own parameters with the extension composition.
178 (extend service-type-extend ;list of Any -> parameters
181 ;; Optional default value for instances of this type.
182 (default-value service-type-default-value ;Any
183 (default &no-default-value))
186 (description service-type-description) ;string
187 (location service-type-location ;<location>
188 (default (and=> (current-source-location)
189 source-properties->location))
192 (define (write-service-type type port)
193 (format port "#<service-type ~a ~a>"
194 (service-type-name type)
195 (number->string (object-address type) 16)))
197 (set-record-type-printer! <service-type> write-service-type)
199 (define %distro-root-directory
200 ;; Absolute file name of the module hierarchy.
201 (dirname (search-path %load-path "guix.scm")))
203 (define %service-type-path
204 ;; Search path for service types.
205 (make-parameter `((,%distro-root-directory . "gnu/services")
206 (,%distro-root-directory . "gnu/system"))))
208 (define (all-service-modules)
209 "Return the default set of service modules."
210 (cons (resolve-interface '(gnu services))
211 (all-modules (%service-type-path)
212 #:warn warn-about-load-error)))
214 (define* (fold-service-types proc seed
216 (modules (all-service-modules)))
217 "For each service type exported by one of MODULES, call (PROC RESULT). SEED
218 is used as the initial value of RESULT."
219 (fold-module-public-variables (lambda (object result)
220 (if (service-type? object)
226 (define lookup-service-types
228 (delay (fold-service-types (lambda (type result)
229 (vhash-consq (service-type-name type)
233 "Return the list of services with the given NAME (a symbol)."
234 (vhash-foldq* cons '() name (force table)))))
236 ;; Services of a given type.
237 (define-record-type <service>
238 (make-service type value)
241 (value service-value))
243 (define-syntax service
245 "Return a service instance of TYPE. The service value is VALUE or, if
246 omitted, TYPE's default value."
248 (make-service type value))
250 (%service-with-default-value (current-source-location)
253 (define (%service-with-default-value location type)
254 "Return a instance of service type TYPE with its default value, if any. If
255 TYPE does not have a default value, an error is raised."
256 ;; TODO: Currently this is a run-time error but with a little bit macrology
257 ;; we could turn it into an expansion-time error.
258 (let ((default (service-type-default-value type)))
259 (if (eq? default &no-default-value)
260 (let ((location (source-properties->location location)))
262 (make-compound-condition
264 (&missing-value-service-error (type type) (location location)))
265 (formatted-message (G_ "~a: no value specified \
266 for service of type '~a'")
267 (location->string location)
268 (service-type-name type)))))
269 (service type default))))
271 (define-condition-type &service-error &error
274 (define-condition-type &missing-value-service-error &service-error
275 missing-value-service-error?
276 (type missing-value-service-error-type)
277 (location missing-value-service-error-location))
285 (define service-parameters
289 (define (simple-service name target value)
290 "Return a service that extends TARGET with VALUE. This works by creating a
291 singleton service type NAME, of which the returned service is an instance."
292 (let* ((extension (service-extension target identity))
293 (type (service-type (name name)
294 (extensions (list extension))
295 (description "This is a simple service."))))
296 (service type value)))
298 (define-syntax %modify-service
299 (syntax-rules (=> delete)
300 ((_ svc (delete kind) clauses ...)
301 (if (eq? (service-kind svc) kind)
303 (%modify-service svc clauses ...)))
306 ((_ svc (kind param => exp ...) clauses ...)
307 (if (eq? (service-kind svc) kind)
308 (let ((param (service-value svc)))
309 (service (service-kind svc)
311 (%modify-service svc clauses ...)))))
313 (define-syntax modify-services
315 "Modify the services listed in SERVICES according to CLAUSES and return
316 the resulting list of services. Each clause must have the form:
318 (TYPE VARIABLE => BODY)
320 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
321 identifier that is bound within BODY to the value of the service of that
324 Clauses can also remove services of a given type:
328 Consider this example:
330 (modify-services %base-services
331 (guix-service-type config =>
334 (use-substitutes? #f)
335 (extra-options '(\"--gc-keep-derivations\"))))
336 (mingetty-service-type config =>
337 (mingetty-configuration
339 (motd (plain-file \"motd\" \"Hi there!\"))))
340 (delete udev-service-type))
342 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
343 all the MINGETTY-SERVICE-TYPE instances, and it deletes instances of the
346 This is a shorthand for (filter-map (lambda (svc) ...) %base-services)."
347 ((_ services clauses ...)
348 (filter-map (lambda (service)
349 (%modify-service service clauses ...))
357 (define (system-derivation entries mextensions)
358 "Return as a monadic value the derivation of the 'system' directory
359 containing the given entries."
360 (mlet %store-monad ((extensions (mapm/accumulate-builds identity
364 (append entries (concatenate extensions))))))
366 (define system-service-type
367 ;; This is the ultimate service type, the root of the service DAG. The
368 ;; service of this type is extended by monadic name/item pairs. These items
369 ;; end up in the "system directory" as returned by
370 ;; 'operating-system-derivation'.
371 (service-type (name 'system)
374 (extend system-derivation)
376 "Build the operating system top-level directory, which in
377 turn refers to everything the operating system needs: its kernel, initrd,
378 system profile, boot script, and so on.")))
380 (define (compute-boot-script _ gexps)
381 ;; Reverse GEXPS so that extensions appear in the boot script in the right
382 ;; order. That is, user extensions would come first, and extensions added
383 ;; by 'essential-services' (e.g., running shepherd) are guaranteed to come
386 ;; Clean up and activate the system, then spawn shepherd.
387 #~(begin #$@(reverse gexps))))
389 (define (boot-script-entry mboot)
390 "Return, as a monadic value, an entry for the boot script in the system
392 (mlet %store-monad ((boot mboot))
393 (return `(("boot" ,boot)))))
395 (define boot-service-type
396 ;; The service of this type is extended by being passed gexps. It
397 ;; aggregates them in a single script, as a monadic value, which becomes its
399 (service-type (name 'boot)
401 (list (service-extension system-service-type
404 (extend compute-boot-script)
406 "Produce the operating system's boot script, which is spawned
407 by the initrd once the root file system is mounted.")))
409 (define %boot-service
410 ;; The service that produces the boot script.
411 (service boot-service-type #t))
415 ;;; Provenance tracking.
418 (define (object->pretty-string obj)
419 "Like 'object->string', but using 'pretty-print'."
420 (call-with-output-string
422 (pretty-print obj port))))
424 (define (channel->code channel)
425 "Return code to build CHANNEL, ready to be dropped in a 'channels.scm'
427 ;; Since the 'introduction' field is backward-incompatible, and since it's
428 ;; optional when using the "official" 'guix channel, include it if and only
429 ;; if we're referring to a different channel.
430 (let ((intro (and (not (equal? (list channel) %default-channels))
431 (channel-introduction channel))))
432 `(channel (name ',(channel-name channel))
433 (url ,(channel-url channel))
434 (branch ,(channel-branch channel))
435 (commit ,(channel-commit channel))
438 (make-channel-introduction
439 ,(channel-introduction-first-signed-commit intro)
441 ,(openpgp-format-fingerprint
442 (channel-introduction-first-commit-signer
446 (define (channel->sexp channel)
447 "Return an sexp describing CHANNEL. The sexp is _not_ code and is meant to
448 be parsed by tools; it's potentially more future-proof than code."
449 ;; TODO: Add CHANNEL's introduction. Currently we can't do that because
450 ;; older 'guix system describe' expect exactly name/url/branch/commit
451 ;; without any additional fields.
452 `(channel (name ,(channel-name channel))
453 (url ,(channel-url channel))
454 (branch ,(channel-branch channel))
455 (commit ,(channel-commit channel))))
457 (define (sexp->channel sexp)
458 "Return the channel corresponding to SEXP, an sexp as found in the
459 \"provenance\" file produced by 'provenance-service-type'."
461 (('channel ('name name)
466 ;; XXX: In the future REST may include a channel introduction.
467 (channel (name name) (url url)
468 (branch branch) (commit commit)))))
470 (define (provenance-file channels config-file)
471 "Return a 'provenance' file describing CHANNELS, a list of channels, and
472 CONFIG-FILE, which can be either #f or a <local-file> containing the OS
473 configuration being used."
474 (scheme-file "provenance"
477 (channels #+@(if channels
478 (map channel->sexp channels)
480 (configuration-file #+config-file))))
482 (define (provenance-entry config-file)
483 "Return system entries describing the operating system provenance: the
484 channels in use and CONFIG-FILE, if it is true."
489 (and=> profile profile-channels))
492 (let ((config-file (cond ((string? config-file)
493 ;; CONFIG-FILE has been passed typically via
494 ;; 'guix system reconfigure CONFIG-FILE' so we
495 ;; can assume it's valid: tell 'local-file' to
496 ;; not emit a warning.
497 (local-file (assume-valid-file-name config-file)
498 "configuration.scm"))
503 (return `(("provenance" ,(provenance-file channels config-file))
506 ,(plain-file "channels.scm"
507 (object->pretty-string
509 ,@(map channel->code channels))))))
512 `(("configuration.scm" ,config-file))
515 (define provenance-service-type
516 (service-type (name 'provenance)
518 (list (service-extension system-service-type
520 (default-value #f) ;the OS config file
522 "Store provenance information about the system in the system
523 itself: the channels used when building the system, and its configuration
524 file, when available.")))
526 (define (sexp->system-provenance sexp)
527 "Parse SEXP, an s-expression read from /run/current-system/provenance or
528 similar, and return two values: the list of channels listed therein, and the
529 OS configuration file or #f."
531 (('provenance ('version 0)
532 ('channels channels ...)
533 ('configuration-file config-file))
534 (values (map sexp->channel channels)
539 (define (system-provenance system)
540 "Given SYSTEM, the file name of a system generation, return two values: the
541 list of channels SYSTEM is built from, and its configuration file. If that
542 information is missing, return the empty list (for channels) and possibly
543 #false (for the configuration file)."
546 (sexp->system-provenance
547 (call-with-input-file (string-append system "/provenance")
556 (define (cleanup-gexp _)
557 "Return a gexp to clean up /tmp and similar places upon boot."
558 (with-imported-modules '((guix build utils))
560 (use-modules (guix build utils))
562 ;; Clean out /tmp and /var/run.
564 ;; XXX This needs to happen before service activations, so it
565 ;; has to be here, but this also implicitly assumes that /tmp
566 ;; and /var/run are on the root partition.
567 (letrec-syntax ((fail-safe (syntax-rules ()
573 (fail-safe rest ...)))
576 ;; Ignore I/O errors so the system can boot.
578 ;; Remove stale Shadow lock files as they would lead to
579 ;; failures of 'useradd' & co.
580 (delete-file "/etc/group.lock")
581 (delete-file "/etc/passwd.lock")
582 (delete-file "/etc/.pwd.lock") ;from 'lckpwdf'
584 ;; Force file names to be decoded as UTF-8. See
585 ;; <https://bugs.gnu.org/26353>.
586 (setenv "GUIX_LOCPATH"
587 #+(file-append glibc-utf8-locales "/lib/locale"))
588 (setlocale LC_CTYPE "en_US.utf8")
589 (delete-file-recursively "/tmp")
590 (delete-file-recursively "/var/run")
593 (chmod "/tmp" #o1777)
595 (chmod "/var/run" #o755)
596 (delete-file-recursively "/run/udev/watch.old"))))))
598 (define cleanup-service-type
599 ;; Service that cleans things up in /tmp and similar.
600 (service-type (name 'cleanup)
602 (list (service-extension boot-service-type
605 "Delete files from @file{/tmp}, @file{/var/run}, and other
606 temporary locations at boot time.")))
608 (define* (activation-service->script service)
609 "Return as a monadic value the activation script for SERVICE, a service of
610 ACTIVATION-SCRIPT-TYPE."
611 (activation-script (service-value service)))
613 (define (activation-script gexps)
614 "Return the system's activation script, which evaluates GEXPS."
616 (map (cut program-file "activate-service.scm" <>) gexps))
618 (program-file "activate.scm"
619 (with-imported-modules (source-module-closure
620 '((gnu build activation)
623 (use-modules (gnu build activation)
626 ;; Make sure the user accounting database exists. If it
627 ;; does not exist, 'setutxent' does not create it and
628 ;; thus there is no accounting at all.
629 (close-port (open-file "/var/run/utmpx" "a0"))
631 ;; Same for 'wtmp', which is populated by mingetty et
634 (close-port (open-file "/var/log/wtmp" "a0"))
636 ;; Set up /run/current-system. Among other things this
637 ;; sets up locales, which the activation snippets
638 ;; executed below may expect.
639 (activate-current-system)
641 ;; Run the services' activation snippets.
642 ;; TODO: Use 'load-compiled'.
643 (for-each primitive-load '#$actions)))))
645 (define (gexps->activation-gexp gexps)
646 "Return a gexp that runs the activation script containing GEXPS."
647 #~(primitive-load #$(activation-script gexps)))
649 (define (activation-profile-entry gexps)
650 "Return, as a monadic value, an entry for the activation script in the
652 (mlet %store-monad ((activate (lower-object (activation-script gexps))))
653 (return `(("activate" ,activate)))))
655 (define (second-argument a b) b)
657 (define activation-service-type
658 (service-type (name 'activate)
660 (list (service-extension boot-service-type
661 gexps->activation-gexp)
662 (service-extension system-service-type
663 activation-profile-entry)))
665 (extend second-argument)
667 "Run @dfn{activation} code at boot time and upon
668 @command{guix system reconfigure} completion.")))
670 (define %activation-service
671 ;; The activation service produces the activation script from the gexps it
673 (service activation-service-type #t))
675 (define %modprobe-wrapper
676 ;; Wrapper for the 'modprobe' command that knows where modules live.
678 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
679 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
680 ;; environment variable is not set---hence the need for this wrapper.
681 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
682 (program-file "modprobe"
684 (setenv "LINUX_MODULE_DIRECTORY"
685 "/run/booted-system/kernel/lib/modules")
686 ;; FIXME: Remove this crutch when the patch #40422,
687 ;; updating to kmod 27 is merged.
688 (setenv "MODPROBE_OPTIONS"
689 "-C /etc/modprobe.d")
690 (apply execl #$modprobe
691 (cons #$modprobe (cdr (command-line))))))))
693 (define %linux-kernel-activation
694 ;; Activation of the Linux kernel running on the bare metal (as opposed to
695 ;; running in a container.)
697 ;; Tell the kernel to use our 'modprobe' command.
698 (activate-modprobe #$%modprobe-wrapper)
700 ;; Let users debug their own processes!
701 (activate-ptrace-attach)))
703 (define %linux-bare-metal-service
704 ;; The service that does things that are needed on the "bare metal", but not
705 ;; necessary or impossible in a container.
706 (simple-service 'linux-bare-metal
707 activation-service-type
708 %linux-kernel-activation))
710 (define %hurd-rc-script
711 ;; The RC script to be started upon boot.
713 (with-imported-modules (source-module-closure
715 (gnu build hurd-boot)
716 (guix build syscalls)))
718 (use-modules (guix build utils)
719 (gnu build hurd-boot)
720 (guix build syscalls)
725 (boot-hurd-system)))))
727 (define (hurd-rc-entry rc)
728 "Return, as a monadic value, an entry for the RC script in the system
730 (mlet %store-monad ((rc (lower-object rc)))
731 (return `(("rc" ,rc)))))
733 (define hurd-startup-service-type
734 ;; The service that creates the initial SYSTEM/rc startup file.
735 (service-type (name 'startup)
737 (list (service-extension system-service-type hurd-rc-entry)))
738 (default-value %hurd-rc-script)
739 (description "This service creates an @file{rc} script in the
740 system; that script is responsible for booting the Hurd.")))
742 (define %hurd-startup-service
743 ;; The service that produces the RC script.
744 (service hurd-startup-service-type %hurd-rc-script))
746 (define special-files-service-type
747 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
749 (name 'special-files)
751 (list (service-extension activation-service-type
753 #~(activate-special-files '#$files)))))
754 (compose concatenate)
757 "Add special files to the root file system---e.g.,
758 @file{/usr/bin/env}.")))
760 (define (extra-special-file file target)
761 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
762 (file-append coreutils \"/bin/env\")
763 and FILE could be \"/usr/bin/env\"."
764 (simple-service (string->symbol (string-append "special-file-" file))
765 special-files-service-type
768 (define (etc-directory service)
769 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
770 (files->etc-directory (service-value service)))
772 (define (files->etc-directory files)
773 (define (assert-no-duplicates files)
774 (let loop ((files files)
779 (when (set-contains? seen file)
780 (raise (formatted-message (G_ "duplicate '~a' entry for /etc")
782 (loop rest (set-insert file seen))))))
784 ;; Detect duplicates early instead of letting them through, eventually
785 ;; leading to a build failure of "etc.drv".
786 (assert-no-duplicates files)
788 (file-union "etc" files))
790 (define (etc-entry files)
791 "Return an entry for the /etc directory consisting of FILES in the system
793 (with-monad %store-monad
794 (return `(("etc" ,(files->etc-directory files))))))
796 (define etc-service-type
797 (service-type (name 'etc)
800 (service-extension activation-service-type
803 (files->etc-directory files)))
804 #~(activate-etc #$etc))))
805 (service-extension system-service-type etc-entry)))
806 (compose concatenate)
808 (description "Populate the @file{/etc} directory.")))
810 (define (etc-service files)
811 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
812 FILES must be a list of name/file-like object pairs."
813 (service etc-service-type files))
815 (define (setuid-program->activation-gexp programs)
816 "Return an activation gexp for setuid-program from PROGRAMS."
817 (let ((programs (map (lambda (program)
818 ;; FIXME This is really ugly, I didn't managed to use
820 (let ((program-name (setuid-program-program program))
821 (setuid? (setuid-program-setuid? program))
822 (setgid? (setuid-program-setgid? program))
823 (user (setuid-program-user program))
824 (group (setuid-program-group program)) )
830 (program #$program-name))))
832 (with-imported-modules (source-module-closure
833 '((gnu system setuid)))
835 (use-modules (gnu system setuid))
837 (activate-setuid-programs (list #$@programs))))))
839 (define setuid-program-service-type
840 (service-type (name 'setuid-program)
842 (list (service-extension activation-service-type
843 setuid-program->activation-gexp)))
844 (compose concatenate)
845 (extend (lambda (config extensions)
846 (append config extensions)))
848 "Populate @file{/run/setuid-programs} with the specified
849 executables, making them setuid-root.")))
851 (define (packages->profile-entry packages)
852 "Return a system entry for the profile containing PACKAGES."
853 ;; XXX: 'mlet' is needed here for one reason: to get the proper
854 ;; '%current-target' and '%current-target-system' bindings when
855 ;; 'packages->manifest' is called, and thus when the 'package-inputs'
856 ;; etc. procedures are called on PACKAGES. That way, conditionals in those
857 ;; inputs see the "correct" value of these two parameters. See
858 ;; <https://issues.guix.gnu.org/44952>.
859 (mlet %store-monad ((_ (current-target-system)))
860 (return `(("profile" ,(profile
861 (content (packages->manifest
862 (delete-duplicates packages eq?)))))))))
864 (define profile-service-type
865 ;; The service that populates the system's profile---i.e.,
866 ;; /run/current-system/profile. It is extended by package lists.
867 (service-type (name 'profile)
869 (list (service-extension system-service-type
870 packages->profile-entry)))
871 (compose concatenate)
874 "This is the @dfn{system profile}, available as
875 @file{/run/current-system/profile}. It contains packages that the sysadmin
876 wants to be globally available to all the system users.")))
878 (define (firmware->activation-gexp firmware)
879 "Return a gexp to make the packages listed in FIRMWARE loadable by the
881 (let ((directory (directory-union "firmware" firmware)))
882 ;; Tell the kernel where firmware is.
883 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
885 (define firmware-service-type
886 ;; The service that collects firmware.
887 (service-type (name 'firmware)
889 (list (service-extension activation-service-type
890 firmware->activation-gexp)))
891 (compose concatenate)
894 "Make ``firmware'' files loadable by the operating system
895 kernel. Firmware may then be uploaded to some of the machine's devices, such
898 (define (gc-roots->system-entry roots)
899 "Return an entry in the system's output containing symlinks to ROOTS."
900 (mlet %store-monad ((entry (gexp->derivation
902 #~(let ((roots '#$roots))
908 (iota (length roots))))))))
909 (return (if (null? roots)
911 `(("gc-roots" ,entry))))))
913 (define gc-root-service-type
914 ;; A service to associate extra garbage-collector roots to the system. This
915 ;; is a simple hack that guarantees that the system retains references to
916 ;; the given list of roots. Roots must be "lowerable" objects like
917 ;; packages, or derivations.
918 (service-type (name 'gc-roots)
920 (list (service-extension system-service-type
921 gc-roots->system-entry)))
922 (compose concatenate)
925 "Register garbage-collector roots---i.e., store items that
926 will not be reclaimed by the garbage collector.")
927 (default-value '())))
929 ;; Configuration for the Linux kernel builder.
930 (define-record-type* <linux-builder-configuration>
931 linux-builder-configuration
932 make-linux-builder-configuration
933 linux-builder-configuration?
934 this-linux-builder-configuration
936 (kernel linux-builder-configuration-kernel) ; package
937 (modules linux-builder-configuration-modules (default '()))) ; list of packages
939 (define (package-for-kernel target-kernel module-package)
940 "Return a package like MODULE-PACKAGE, adapted for TARGET-KERNEL, if
941 possible (that is if there's a LINUX keyword argument in the build system)."
943 (inherit module-package)
945 (substitute-keyword-arguments (package-arguments module-package)
949 (define (linux-builder-configuration->system-entry config)
950 "Return the kernel entry of the 'system' directory."
951 (let* ((kernel (linux-builder-configuration-kernel config))
952 (modules (linux-builder-configuration-modules config))
954 (content (packages->manifest
956 (map (lambda (module)
959 (package-for-kernel kernel module))
960 ;; support (,package "kernel-module-output")
961 ((and (list? module) (package? (car module)))
962 (cons (package-for-kernel kernel
968 (hooks (list linux-module-database)))))
969 (with-monad %store-monad
970 (return `(("kernel" ,kernel))))))
972 (define linux-builder-service-type
973 (service-type (name 'linux-builder)
975 (list (service-extension system-service-type
976 linux-builder-configuration->system-entry)))
979 (extend (lambda (config modifiers)
980 (if (null? modifiers)
982 ((apply compose modifiers) config))))
983 (description "Builds the linux-libre kernel profile, containing
984 the kernel itself and any linux-loadable kernel modules. This can be extended
985 with a function that accepts the current configuration and returns a new
988 (define (linux-loadable-module-builder-modifier modules)
989 "Extends linux-builder-service-type by appending the given MODULES to the
990 configuration of linux-builder-service-type."
992 (linux-builder-configuration
994 (modules (append (linux-builder-configuration-modules config)
997 (define linux-loadable-module-service-type
998 (service-type (name 'linux-loadable-modules)
1000 (list (service-extension linux-builder-service-type
1001 linux-loadable-module-builder-modifier)))
1003 (compose concatenate)
1005 (description "Adds packages and package outputs as modules
1006 included in the booted linux-libre profile. Other services can extend this
1007 service type to add particular modules to the set of linux-loadable modules.")))
1012 ;;; Service folding.
1015 (define-condition-type &missing-target-service-error &service-error
1016 missing-target-service-error?
1017 (service missing-target-service-error-service)
1018 (target-type missing-target-service-error-target-type))
1020 (define-condition-type &ambiguous-target-service-error &service-error
1021 ambiguous-target-service-error?
1022 (service ambiguous-target-service-error-service)
1023 (target-type ambiguous-target-service-error-target-type))
1025 (define (missing-target-error service target-type)
1027 (condition (&missing-target-service-error
1029 (target-type target-type))
1032 (format #f (G_ "no target of type '~a' for service '~a'")
1033 (service-type-name target-type)
1035 (service-kind service))))))))
1037 (define (service-back-edges services)
1038 "Return a procedure that, when passed a <service>, returns the list of
1039 <service> objects that depend on it."
1040 (define (add-edges service edges)
1041 (define (add-edge extension edges)
1042 (let ((target-type (service-extension-target extension)))
1043 (match (filter (lambda (service)
1044 (eq? (service-kind service) target-type))
1047 (vhash-consq target service edges))
1049 (missing-target-error service target-type))
1052 (condition (&ambiguous-target-service-error
1054 (target-type target-type))
1058 (G_ "more than one target service of type '~a'")
1059 (service-type-name target-type))))))))))
1061 (fold add-edge edges (service-type-extensions (service-kind service))))
1063 (let ((edges (fold add-edges vlist-null services)))
1065 (reverse (vhash-foldq* cons '() node edges)))))
1067 (define (instantiate-missing-services services)
1068 "Return SERVICES, a list, augmented with any services targeted by extensions
1069 and missing from SERVICES. Only service types with a default value can be
1070 instantiated; other missing services lead to a
1071 '&missing-target-service-error'."
1072 (define (adjust-service-list svc result instances)
1073 (fold2 (lambda (extension result instances)
1075 (service-extension-target extension))
1077 (match (vhash-assq target-type instances)
1079 (let ((default (service-type-default-value target-type)))
1080 (if (eq? &no-default-value default)
1081 (missing-target-error svc target-type)
1082 (let ((new (service target-type)))
1083 (values (cons new result)
1084 (vhash-consq target-type new instances))))))
1086 (values result instances))))
1089 (service-type-extensions (service-kind svc))))
1091 (let loop ((services services))
1093 (fold (lambda (service result)
1094 (vhash-consq (service-kind service) service
1096 vlist-null services))
1099 (fold2 adjust-service-list
1103 ;; If we instantiated services, they might in turn depend on missing
1104 ;; services. Loop until we've reached fixed point.
1105 (if (= (length adjusted) (vlist-length instances))
1109 (define* (fold-services services
1110 #:key (target-type system-service-type))
1111 "Fold SERVICES by propagating their extensions down to the root of type
1112 TARGET-TYPE; return the root service adjusted accordingly."
1114 (service-back-edges services))
1116 (define (matching-extension target)
1117 (let ((target (service-kind target)))
1119 (($ <service-extension> type)
1120 (eq? type target)))))
1122 (define (apply-extension target)
1124 (match (find (matching-extension target)
1125 (service-type-extensions (service-kind service)))
1126 (($ <service-extension> _ compute)
1127 (compute (service-value service))))))
1129 (match (filter (lambda (service)
1130 (eq? (service-kind service) target-type))
1133 ;; Use the state monad to keep track of already-visited services in the
1134 ;; graph and to memoize their value once folded.
1136 (let loop ((sink sink))
1137 (mlet %state-monad ((visited (current-state)))
1138 (match (vhash-assq sink visited)
1141 ((dependents (mapm %state-monad loop (dependents sink)))
1142 (visited (current-state))
1143 (extensions -> (map (apply-extension sink) dependents))
1144 (extend -> (service-type-extend (service-kind sink)))
1145 (compose -> (service-type-compose (service-kind sink)))
1146 (params -> (service-value sink))
1149 ;; Distinguish COMPOSE and EXTEND because PARAMS typically
1150 ;; has a different type than the elements of EXTENSIONS.
1152 (service (service-kind sink)
1153 (extend params (compose extensions)))
1155 (mbegin %state-monad
1156 (set-current-state (vhash-consq sink service visited))
1158 ((_ . service) ;SINK was already visited
1159 (return service)))))
1163 (make-compound-condition
1164 (condition (&missing-target-service-error
1166 (target-type target-type)))
1167 (formatted-message (G_ "service of type '~a' not found")
1168 (service-type-name target-type)))))
1171 (condition (&ambiguous-target-service-error
1173 (target-type target-type))
1177 (G_ "more than one target service of type '~a'")
1178 (service-type-name target-type)))))))))
1180 ;;; services.scm ends here.