1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu services)
21 #:use-module (guix gexp)
22 #:use-module (guix monads)
23 #:use-module (guix store)
24 #:use-module (guix records)
25 #:use-module (guix profiles)
26 #:use-module (guix discovery)
27 #:use-module (guix combinators)
28 #:use-module (guix sets)
29 #:use-module (guix ui)
30 #:use-module ((guix utils) #:select (source-properties->location))
31 #:use-module (guix modules)
32 #:use-module (gnu packages base)
33 #:use-module (gnu packages bash)
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-9)
36 #:use-module (srfi srfi-9 gnu)
37 #:use-module (srfi srfi-26)
38 #:use-module (srfi srfi-34)
39 #:use-module (srfi srfi-35)
40 #:use-module (ice-9 vlist)
41 #:use-module (ice-9 match)
42 #:export (service-extension
44 service-extension-target
45 service-extension-compute
50 service-type-extensions
53 service-type-default-value
54 service-type-description
65 service-parameters ;deprecated
70 instantiate-missing-services
74 missing-value-service-error?
75 missing-value-service-error-type
76 missing-value-service-error-location
77 missing-target-service-error?
78 missing-target-service-error-service
79 missing-target-service-error-target-type
80 ambiguous-target-service-error?
81 ambiguous-target-service-error-service
82 ambiguous-target-service-error-target-type
87 activation-service-type
88 activation-service->script
89 %linux-bare-metal-service
90 special-files-service-type
94 setuid-program-service-type
105 ;;; This module defines a broad notion of "service types" and "services."
107 ;;; A service type describe how its instances extend instances of other
108 ;;; service types. For instance, some services extend the instance of
109 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
110 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
111 ;;; <shepherd-service>.
113 ;;; When applicable, the service type defines how it can itself be extended,
114 ;;; by providing one procedure to compose extensions, and one procedure to
117 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
118 ;;; instance, which is the root of the service DAG. Its value is the
119 ;;; derivation that produces the 'system' directory as returned by
120 ;;; 'operating-system-derivation'.
122 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
123 ;;; "folds" by propagating extensions down the graph; it returns the root
124 ;;; service after the applying all its extensions.
128 (define-record-type <service-extension>
129 (service-extension target compute)
131 (target service-extension-target) ;<service-type>
132 (compute service-extension-compute)) ;params -> params
134 (define &no-default-value
135 ;; Value used to denote service types that have no associated default value.
138 (define-record-type* <service-type> service-type make-service-type
140 (name service-type-name) ;symbol (for debugging)
142 ;; Things extended by services of this type.
143 (extensions service-type-extensions) ;list of <service-extensions>
145 ;; Given a list of extensions, "compose" them.
146 (compose service-type-compose ;list of Any -> Any
149 ;; Extend the services' own parameters with the extension composition.
150 (extend service-type-extend ;list of Any -> parameters
153 ;; Optional default value for instances of this type.
154 (default-value service-type-default-value ;Any
155 (default &no-default-value))
158 (description service-type-description ;string
160 (location service-type-location ;<location>
161 (default (and=> (current-source-location)
162 source-properties->location))
165 (define (write-service-type type port)
166 (format port "#<service-type ~a ~a>"
167 (service-type-name type)
168 (number->string (object-address type) 16)))
170 (set-record-type-printer! <service-type> write-service-type)
172 (define %distro-root-directory
173 ;; Absolute file name of the module hierarchy.
174 (dirname (search-path %load-path "guix.scm")))
176 (define %service-type-path
177 ;; Search path for service types.
178 (make-parameter `((,%distro-root-directory . "gnu/services")
179 (,%distro-root-directory . "gnu/system"))))
181 (define (all-service-modules)
182 "Return the default set of service modules."
183 (cons (resolve-interface '(gnu services))
184 (all-modules (%service-type-path)
185 #:warn warn-about-load-error)))
187 (define* (fold-service-types proc seed
189 (modules (all-service-modules)))
190 "For each service type exported by one of MODULES, call (PROC RESULT). SEED
191 is used as the initial value of RESULT."
192 (fold-module-public-variables (lambda (object result)
193 (if (service-type? object)
199 (define lookup-service-types
201 (delay (fold-service-types (lambda (type result)
202 (vhash-consq (service-type-name type)
206 "Return the list of services with the given NAME (a symbol)."
207 (vhash-foldq* cons '() name (force table)))))
209 ;; Services of a given type.
210 (define-record-type <service>
211 (make-service type value)
214 (value service-value))
216 (define-syntax service
218 "Return a service instance of TYPE. The service value is VALUE or, if
219 omitted, TYPE's default value."
221 (make-service type value))
223 (%service-with-default-value (current-source-location)
226 (define (%service-with-default-value location type)
227 "Return a instance of service type TYPE with its default value, if any. If
228 TYPE does not have a default value, an error is raised."
229 ;; TODO: Currently this is a run-time error but with a little bit macrology
230 ;; we could turn it into an expansion-time error.
231 (let ((default (service-type-default-value type)))
232 (if (eq? default &no-default-value)
233 (let ((location (source-properties->location location)))
236 (&missing-value-service-error (type type) (location location))
238 (message (format #f (G_ "~a: no value specified \
239 for service of type '~a'")
240 (location->string location)
241 (service-type-name type)))))))
242 (service type default))))
244 (define-condition-type &service-error &error
247 (define-condition-type &missing-value-service-error &service-error
248 missing-value-service-error?
249 (type missing-value-service-error-type)
250 (location missing-value-service-error-location))
258 (define service-parameters
262 (define (simple-service name target value)
263 "Return a service that extends TARGET with VALUE. This works by creating a
264 singleton service type NAME, of which the returned service is an instance."
265 (let* ((extension (service-extension target identity))
266 (type (service-type (name name)
267 (extensions (list extension)))))
268 (service type value)))
270 (define-syntax %modify-service
274 ((_ svc (kind param => exp ...) clauses ...)
275 (if (eq? (service-kind svc) kind)
276 (let ((param (service-value svc)))
277 (service (service-kind svc)
279 (%modify-service svc clauses ...)))))
281 (define-syntax modify-services
283 "Modify the services listed in SERVICES according to CLAUSES and return
284 the resulting list of services. Each clause must have the form:
286 (TYPE VARIABLE => BODY)
288 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
289 identifier that is bound within BODY to the value of the service of that
290 TYPE. Consider this example:
292 (modify-services %base-services
293 (guix-service-type config =>
296 (use-substitutes? #f)
297 (extra-options '(\"--gc-keep-derivations\"))))
298 (mingetty-service-type config =>
299 (mingetty-configuration
301 (motd (plain-file \"motd\" \"Hi there!\")))))
303 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
304 all the MINGETTY-SERVICE-TYPE instances.
306 This is a shorthand for (map (lambda (svc) ...) %base-services)."
307 ((_ services clauses ...)
308 (map (lambda (service)
309 (%modify-service service clauses ...))
317 (define (system-derivation mentries mextensions)
318 "Return as a monadic value the derivation of the 'system' directory
319 containing the given entries."
320 (mlet %store-monad ((entries mentries)
321 (extensions (sequence %store-monad mextensions)))
324 (append entries (concatenate extensions))))))
326 (define system-service-type
327 ;; This is the ultimate service type, the root of the service DAG. The
328 ;; service of this type is extended by monadic name/item pairs. These items
329 ;; end up in the "system directory" as returned by
330 ;; 'operating-system-derivation'.
331 (service-type (name 'system)
334 (extend system-derivation)))
336 (define (compute-boot-script _ mexps)
337 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
339 ;; Clean up and activate the system, then spawn shepherd.
340 #~(begin #$@gexps))))
342 (define (boot-script-entry mboot)
343 "Return, as a monadic value, an entry for the boot script in the system
345 (mlet %store-monad ((boot mboot))
346 (return `(("boot" ,boot)))))
348 (define boot-service-type
349 ;; The service of this type is extended by being passed gexps as monadic
350 ;; values. It aggregates them in a single script, as a monadic value, which
351 ;; becomes its 'parameters'. It is the only service that extends nothing.
352 (service-type (name 'boot)
354 (list (service-extension system-service-type
357 (extend compute-boot-script)))
359 (define %boot-service
360 ;; The service that produces the boot script.
361 (service boot-service-type #t))
363 (define (cleanup-gexp _)
364 "Return as a monadic value a gexp to clean up /tmp and similar places upon
366 (with-monad %store-monad
367 (with-imported-modules '((guix build utils))
369 (use-modules (guix build utils))
371 ;; Clean out /tmp and /var/run.
373 ;; XXX This needs to happen before service activations, so it
374 ;; has to be here, but this also implicitly assumes that /tmp
375 ;; and /var/run are on the root partition.
376 (letrec-syntax ((fail-safe (syntax-rules ()
382 (fail-safe rest ...)))
385 ;; Ignore I/O errors so the system can boot.
387 ;; Remove stale Shadow lock files as they would lead to
388 ;; failures of 'useradd' & co.
389 (delete-file "/etc/group.lock")
390 (delete-file "/etc/passwd.lock")
391 (delete-file "/etc/.pwd.lock") ;from 'lckpwdf'
393 (delete-file-recursively "/tmp")
394 (delete-file-recursively "/var/run")
396 (chmod "/tmp" #o1777)
398 (chmod "/var/run" #o755)
399 (delete-file-recursively "/run/udev/watch.old"))))))))
401 (define cleanup-service-type
402 ;; Service that cleans things up in /tmp and similar.
403 (service-type (name 'cleanup)
405 (list (service-extension boot-service-type
408 (define* (activation-service->script service)
409 "Return as a monadic value the activation script for SERVICE, a service of
410 ACTIVATION-SCRIPT-TYPE."
411 (activation-script (service-value service)))
413 (define (activation-script gexps)
414 "Return the system's activation script, which evaluates GEXPS."
415 (define (service-activations)
416 ;; Return the activation scripts for SERVICES.
418 (cut gexp->file "activate-service" <>)
421 (mlet* %store-monad ((actions (service-activations)))
422 (gexp->file "activate"
423 (with-imported-modules (source-module-closure
424 '((gnu build activation)
427 (use-modules (gnu build activation)
430 ;; Make sure the user accounting database exists. If it
431 ;; does not exist, 'setutxent' does not create it and
432 ;; thus there is no accounting at all.
433 (close-port (open-file "/var/run/utmpx" "a0"))
435 ;; Same for 'wtmp', which is populated by mingetty et
438 (close-port (open-file "/var/log/wtmp" "a0"))
440 ;; Set up /run/current-system. Among other things this
441 ;; sets up locales, which the activation snippets
442 ;; executed below may expect.
443 (activate-current-system)
445 ;; Run the services' activation snippets.
446 ;; TODO: Use 'load-compiled'.
447 (for-each primitive-load '#$actions))))))
449 (define (gexps->activation-gexp gexps)
450 "Return a gexp that runs the activation script containing GEXPS."
451 (mlet %store-monad ((script (activation-script gexps)))
452 (return #~(primitive-load #$script))))
454 (define (second-argument a b) b)
456 (define activation-service-type
457 (service-type (name 'activate)
459 (list (service-extension boot-service-type
460 gexps->activation-gexp)))
462 (extend second-argument)))
464 (define %activation-service
465 ;; The activation service produces the activation script from the gexps it
467 (service activation-service-type #t))
469 (define %modprobe-wrapper
470 ;; Wrapper for the 'modprobe' command that knows where modules live.
472 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
473 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
474 ;; environment variable is not set---hence the need for this wrapper.
475 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
476 (program-file "modprobe"
478 (setenv "LINUX_MODULE_DIRECTORY"
479 "/run/booted-system/kernel/lib/modules")
480 (apply execl #$modprobe
481 (cons #$modprobe (cdr (command-line))))))))
483 (define %linux-kernel-activation
484 ;; Activation of the Linux kernel running on the bare metal (as opposed to
485 ;; running in a container.)
487 ;; Tell the kernel to use our 'modprobe' command.
488 (activate-modprobe #$%modprobe-wrapper)
490 ;; Let users debug their own processes!
491 (activate-ptrace-attach)))
493 (define %linux-bare-metal-service
494 ;; The service that does things that are needed on the "bare metal", but not
495 ;; necessary or impossible in a container.
496 (simple-service 'linux-bare-metal
497 activation-service-type
498 %linux-kernel-activation))
501 (define special-files-service-type
502 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
504 (name 'special-files)
506 (list (service-extension activation-service-type
508 #~(activate-special-files '#$files)))))
509 (compose concatenate)
512 (define (extra-special-file file target)
513 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
514 (file-append coreutils \"/bin/env\")
515 and FILE could be \"/usr/bin/env\"."
516 (simple-service (string->symbol (string-append "special-file-" file))
517 special-files-service-type
520 (define (etc-directory service)
521 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
522 (files->etc-directory (service-value service)))
524 (define (files->etc-directory files)
525 (file-union "etc" files))
527 (define (etc-entry files)
528 "Return an entry for the /etc directory consisting of FILES in the system
530 (with-monad %store-monad
531 (return `(("etc" ,(files->etc-directory files))))))
533 (define etc-service-type
534 (service-type (name 'etc)
537 (service-extension activation-service-type
540 (files->etc-directory files)))
541 #~(activate-etc #$etc))))
542 (service-extension system-service-type etc-entry)))
543 (compose concatenate)
546 (define (etc-service files)
547 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
548 FILES must be a list of name/file-like object pairs."
549 (service etc-service-type files))
551 (define setuid-program-service-type
552 (service-type (name 'setuid-program)
554 (list (service-extension activation-service-type
556 #~(activate-setuid-programs
557 (list #$@programs))))))
558 (compose concatenate)
561 (define (packages->profile-entry packages)
562 "Return a system entry for the profile containing PACKAGES."
563 (mlet %store-monad ((profile (profile-derivation
565 (delete-duplicates packages eq?)))))
566 (return `(("profile" ,profile)))))
568 (define profile-service-type
569 ;; The service that populates the system's profile---i.e.,
570 ;; /run/current-system/profile. It is extended by package lists.
571 (service-type (name 'profile)
573 (list (service-extension system-service-type
574 packages->profile-entry)))
575 (compose concatenate)
578 (define (firmware->activation-gexp firmware)
579 "Return a gexp to make the packages listed in FIRMWARE loadable by the
581 (let ((directory (directory-union "firmware" firmware)))
582 ;; Tell the kernel where firmware is.
583 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
585 (define firmware-service-type
586 ;; The service that collects firmware.
587 (service-type (name 'firmware)
589 (list (service-extension activation-service-type
590 firmware->activation-gexp)))
591 (compose concatenate)
594 (define (gc-roots->system-entry roots)
595 "Return an entry in the system's output containing symlinks to ROOTS."
596 (mlet %store-monad ((entry (gexp->derivation
598 #~(let ((roots '#$roots))
604 (iota (length roots))))))))
605 (return (if (null? roots)
607 `(("gc-roots" ,entry))))))
609 (define gc-root-service-type
610 ;; A service to associate extra garbage-collector roots to the system. This
611 ;; is a simple hack that guarantees that the system retains references to
612 ;; the given list of roots. Roots must be "lowerable" objects like
613 ;; packages, or derivations.
614 (service-type (name 'gc-roots)
616 (list (service-extension system-service-type
617 gc-roots->system-entry)))
618 (compose concatenate)
626 (define-condition-type &missing-target-service-error &service-error
627 missing-target-service-error?
628 (service missing-target-service-error-service)
629 (target-type missing-target-service-error-target-type))
631 (define-condition-type &ambiguous-target-service-error &service-error
632 ambiguous-target-service-error?
633 (service ambiguous-target-service-error-service)
634 (target-type ambiguous-target-service-error-target-type))
636 (define (missing-target-error service target-type)
638 (condition (&missing-target-service-error
640 (target-type target-type))
643 (format #f (G_ "no target of type '~a' for service '~a'")
644 (service-type-name target-type)
646 (service-kind service))))))))
648 (define (service-back-edges services)
649 "Return a procedure that, when passed a <service>, returns the list of
650 <service> objects that depend on it."
651 (define (add-edges service edges)
652 (define (add-edge extension edges)
653 (let ((target-type (service-extension-target extension)))
654 (match (filter (lambda (service)
655 (eq? (service-kind service) target-type))
658 (vhash-consq target service edges))
660 (missing-target-error service target-type))
663 (condition (&ambiguous-target-service-error
665 (target-type target-type))
669 (G_ "more than one target service of type '~a'")
670 (service-type-name target-type))))))))))
672 (fold add-edge edges (service-type-extensions (service-kind service))))
674 (let ((edges (fold add-edges vlist-null services)))
676 (reverse (vhash-foldq* cons '() node edges)))))
678 (define (instantiate-missing-services services)
679 "Return SERVICES, a list, augmented with any services targeted by extensions
680 and missing from SERVICES. Only service types with a default value can be
681 instantiated; other missing services lead to a
682 '&missing-target-service-error'."
683 (define (adjust-service-list svc result instances)
684 (fold2 (lambda (extension result instances)
686 (service-extension-target extension))
688 (match (vhash-assq target-type instances)
690 (let ((default (service-type-default-value target-type)))
691 (if (eq? &no-default-value default)
692 (missing-target-error svc target-type)
693 (let ((new (service target-type)))
694 (values (cons new result)
695 (vhash-consq target-type new instances))))))
697 (values result instances))))
700 (service-type-extensions (service-kind svc))))
702 (let ((instances (fold (lambda (service result)
703 (vhash-consq (service-kind service) service
705 vlist-null services)))
706 (fold2 adjust-service-list
710 (define* (fold-services services
711 #:key (target-type system-service-type))
712 "Fold SERVICES by propagating their extensions down to the root of type
713 TARGET-TYPE; return the root service adjusted accordingly."
715 (service-back-edges services))
717 (define (matching-extension target)
718 (let ((target (service-kind target)))
720 (($ <service-extension> type)
721 (eq? type target)))))
723 (define (apply-extension target)
725 (match (find (matching-extension target)
726 (service-type-extensions (service-kind service)))
727 (($ <service-extension> _ compute)
728 (compute (service-value service))))))
730 (match (filter (lambda (service)
731 (eq? (service-kind service) target-type))
734 (let loop ((sink sink))
735 (let* ((dependents (map loop (dependents sink)))
736 (extensions (map (apply-extension sink) dependents))
737 (extend (service-type-extend (service-kind sink)))
738 (compose (service-type-compose (service-kind sink)))
739 (params (service-value sink)))
740 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
741 ;; different type than the elements of EXTENSIONS.
743 (service (service-kind sink)
744 (extend params (compose extensions)))
748 (condition (&missing-target-service-error
750 (target-type target-type))
752 (message (format #f (G_ "service of type '~a' not found")
753 (service-type-name target-type)))))))
756 (condition (&ambiguous-target-service-error
758 (target-type target-type))
762 (G_ "more than one target service of type '~a'")
763 (service-type-name target-type)))))))))
765 ;;; services.scm ends here.