1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2017 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 sets)
28 #:use-module (guix ui)
29 #:use-module ((guix utils) #:select (source-properties->location))
30 #:use-module (guix modules)
31 #:use-module (gnu packages base)
32 #:use-module (gnu packages bash)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-9)
35 #:use-module (srfi srfi-9 gnu)
36 #:use-module (srfi srfi-26)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
39 #:use-module (ice-9 vlist)
40 #:use-module (ice-9 match)
41 #:export (service-extension
43 service-extension-target
44 service-extension-compute
49 service-type-extensions
52 service-type-default-value
53 service-type-description
63 service-parameters ;deprecated
71 missing-value-service-error?
72 missing-value-service-error-type
73 missing-value-service-error-location
74 missing-target-service-error?
75 missing-target-service-error-service
76 missing-target-service-error-target-type
77 ambiguous-target-service-error?
78 ambiguous-target-service-error-service
79 ambiguous-target-service-error-target-type
84 activation-service-type
85 activation-service->script
86 %linux-bare-metal-service
87 special-files-service-type
91 setuid-program-service-type
100 file-union ;XXX: for lack of a better place
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* (fold-service-types proc seed
183 (modules (all-modules (%service-type-path))))
184 "For each service type exported by one of MODULES, call (PROC RESULT). SEED
185 is used as the initial value of RESULT."
186 (fold-module-public-variables (lambda (object result)
187 (if (service-type? object)
193 ;; Services of a given type.
194 (define-record-type <service>
195 (make-service type value)
198 (value service-value))
200 (define-syntax service
202 "Return a service instance of TYPE. The service value is VALUE or, if
203 omitted, TYPE's default value."
205 (make-service type value))
207 (%service-with-default-value (current-source-location)
210 (define (%service-with-default-value location type)
211 "Return a instance of service type TYPE with its default value, if any. If
212 TYPE does not have a default value, an error is raised."
213 ;; TODO: Currently this is a run-time error but with a little bit macrology
214 ;; we could turn it into an expansion-time error.
215 (let ((default (service-type-default-value type)))
216 (if (eq? default &no-default-value)
217 (let ((location (source-properties->location location)))
220 (&missing-value-service-error (type type) (location location))
222 (message (format #f (G_ "~a: no value specified \
223 for service of type '~a'")
224 (location->string location)
225 (service-type-name type)))))))
226 (service type default))))
228 (define-condition-type &service-error &error
231 (define-condition-type &missing-value-service-error &service-error
232 missing-value-service-error?
233 (type missing-value-service-error-type)
234 (location missing-value-service-error-location))
242 (define service-parameters
246 (define (simple-service name target value)
247 "Return a service that extends TARGET with VALUE. This works by creating a
248 singleton service type NAME, of which the returned service is an instance."
249 (let* ((extension (service-extension target identity))
250 (type (service-type (name name)
251 (extensions (list extension)))))
252 (service type value)))
254 (define-syntax %modify-service
258 ((_ svc (kind param => exp ...) clauses ...)
259 (if (eq? (service-kind svc) kind)
260 (let ((param (service-value svc)))
261 (service (service-kind svc)
263 (%modify-service svc clauses ...)))))
265 (define-syntax modify-services
267 "Modify the services listed in SERVICES according to CLAUSES and return
268 the resulting list of services. Each clause must have the form:
270 (TYPE VARIABLE => BODY)
272 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
273 identifier that is bound within BODY to the value of the service of that
274 TYPE. Consider this example:
276 (modify-services %base-services
277 (guix-service-type config =>
280 (use-substitutes? #f)
281 (extra-options '(\"--gc-keep-derivations\"))))
282 (mingetty-service-type config =>
283 (mingetty-configuration
285 (motd (plain-file \"motd\" \"Hi there!\")))))
287 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
288 all the MINGETTY-SERVICE-TYPE instances.
290 This is a shorthand for (map (lambda (svc) ...) %base-services)."
291 ((_ services clauses ...)
292 (map (lambda (service)
293 (%modify-service service clauses ...))
301 (define (system-derivation mentries mextensions)
302 "Return as a monadic value the derivation of the 'system' directory
303 containing the given entries."
304 (mlet %store-monad ((entries mentries)
305 (extensions (sequence %store-monad mextensions)))
308 (append entries (concatenate extensions))))))
310 (define system-service-type
311 ;; This is the ultimate service type, the root of the service DAG. The
312 ;; service of this type is extended by monadic name/item pairs. These items
313 ;; end up in the "system directory" as returned by
314 ;; 'operating-system-derivation'.
315 (service-type (name 'system)
318 (extend system-derivation)))
320 (define (compute-boot-script _ mexps)
321 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
323 ;; Clean up and activate the system, then spawn shepherd.
324 #~(begin #$@gexps))))
326 (define (boot-script-entry mboot)
327 "Return, as a monadic value, an entry for the boot script in the system
329 (mlet %store-monad ((boot mboot))
330 (return `(("boot" ,boot)))))
332 (define boot-service-type
333 ;; The service of this type is extended by being passed gexps as monadic
334 ;; values. It aggregates them in a single script, as a monadic value, which
335 ;; becomes its 'parameters'. It is the only service that extends nothing.
336 (service-type (name 'boot)
338 (list (service-extension system-service-type
341 (extend compute-boot-script)))
343 (define %boot-service
344 ;; The service that produces the boot script.
345 (service boot-service-type #t))
347 (define (cleanup-gexp _)
348 "Return as a monadic value a gexp to clean up /tmp and similar places upon
350 (with-monad %store-monad
351 (with-imported-modules '((guix build utils))
353 (use-modules (guix build utils))
355 ;; Clean out /tmp and /var/run.
357 ;; XXX This needs to happen before service activations, so it
358 ;; has to be here, but this also implicitly assumes that /tmp
359 ;; and /var/run are on the root partition.
360 (letrec-syntax ((fail-safe (syntax-rules ()
366 (fail-safe rest ...)))
369 ;; Ignore I/O errors so the system can boot.
371 (delete-file-recursively "/tmp")
372 (delete-file-recursively "/var/run")
374 (chmod "/tmp" #o1777)
376 (chmod "/var/run" #o755))))))))
378 (define cleanup-service-type
379 ;; Service that cleans things up in /tmp and similar.
380 (service-type (name 'cleanup)
382 (list (service-extension boot-service-type
385 (define* (file-union name files) ;FIXME: Factorize.
386 "Return a <computed-file> that builds a directory containing all of FILES.
387 Each item in FILES must be a list where the first element is the file name to
388 use in the new directory, and the second element is a gexp denoting the target
394 #$@(map (match-lambda
397 ;; Stat the source to abort early if it
401 (symlink #$source #$target))))
404 (define (directory-union name things)
405 "Return a directory that is the union of THINGS."
408 ;; Only one thing; return it.
412 (with-imported-modules '((guix build union))
414 (use-modules (guix build union))
415 (union-build #$output '#$things)))))))
417 (define* (activation-service->script service)
418 "Return as a monadic value the activation script for SERVICE, a service of
419 ACTIVATION-SCRIPT-TYPE."
420 (activation-script (service-value service)))
422 (define (activation-script gexps)
423 "Return the system's activation script, which evaluates GEXPS."
424 (define (service-activations)
425 ;; Return the activation scripts for SERVICES.
427 (cut gexp->file "activate-service" <>)
430 (mlet* %store-monad ((actions (service-activations)))
431 (gexp->file "activate"
432 (with-imported-modules (source-module-closure
433 '((gnu build activation)
436 (use-modules (gnu build activation)
439 ;; Make sure the user accounting database exists. If it
440 ;; does not exist, 'setutxent' does not create it and
441 ;; thus there is no accounting at all.
442 (close-port (open-file "/var/run/utmpx" "a0"))
444 ;; Same for 'wtmp', which is populated by mingetty et
447 (close-port (open-file "/var/log/wtmp" "a0"))
449 ;; Set up /run/current-system. Among other things this
450 ;; sets up locales, which the activation snippets
451 ;; executed below may expect.
452 (activate-current-system)
454 ;; Run the services' activation snippets.
455 ;; TODO: Use 'load-compiled'.
456 (for-each primitive-load '#$actions))))))
458 (define (gexps->activation-gexp gexps)
459 "Return a gexp that runs the activation script containing GEXPS."
460 (mlet %store-monad ((script (activation-script gexps)))
461 (return #~(primitive-load #$script))))
463 (define (second-argument a b) b)
465 (define activation-service-type
466 (service-type (name 'activate)
468 (list (service-extension boot-service-type
469 gexps->activation-gexp)))
471 (extend second-argument)))
473 (define %activation-service
474 ;; The activation service produces the activation script from the gexps it
476 (service activation-service-type #t))
478 (define %modprobe-wrapper
479 ;; Wrapper for the 'modprobe' command that knows where modules live.
481 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
482 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
483 ;; environment variable is not set---hence the need for this wrapper.
484 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
485 (program-file "modprobe"
487 (setenv "LINUX_MODULE_DIRECTORY"
488 "/run/booted-system/kernel/lib/modules")
489 (apply execl #$modprobe
490 (cons #$modprobe (cdr (command-line))))))))
492 (define %linux-kernel-activation
493 ;; Activation of the Linux kernel running on the bare metal (as opposed to
494 ;; running in a container.)
496 ;; Tell the kernel to use our 'modprobe' command.
497 (activate-modprobe #$%modprobe-wrapper)
499 ;; Let users debug their own processes!
500 (activate-ptrace-attach)))
502 (define %linux-bare-metal-service
503 ;; The service that does things that are needed on the "bare metal", but not
504 ;; necessary or impossible in a container.
505 (simple-service 'linux-bare-metal
506 activation-service-type
507 %linux-kernel-activation))
510 (define special-files-service-type
511 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
513 (name 'special-files)
515 (list (service-extension activation-service-type
517 #~(activate-special-files '#$files)))))
518 (compose concatenate)
521 (define (extra-special-file file target)
522 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
523 (file-append coreutils \"/bin/env\")
524 and FILE could be \"/usr/bin/env\"."
525 (simple-service (string->symbol (string-append "special-file-" file))
526 special-files-service-type
529 (define (etc-directory service)
530 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
531 (files->etc-directory (service-value service)))
533 (define (files->etc-directory files)
534 (file-union "etc" files))
536 (define (etc-entry files)
537 "Return an entry for the /etc directory consisting of FILES in the system
539 (with-monad %store-monad
540 (return `(("etc" ,(files->etc-directory files))))))
542 (define etc-service-type
543 (service-type (name 'etc)
546 (service-extension activation-service-type
549 (files->etc-directory files)))
550 #~(activate-etc #$etc))))
551 (service-extension system-service-type etc-entry)))
552 (compose concatenate)
555 (define (etc-service files)
556 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
557 FILES must be a list of name/file-like object pairs."
558 (service etc-service-type files))
560 (define setuid-program-service-type
561 (service-type (name 'setuid-program)
563 (list (service-extension activation-service-type
565 #~(activate-setuid-programs
566 (list #$@programs))))))
567 (compose concatenate)
570 (define (packages->profile-entry packages)
571 "Return a system entry for the profile containing PACKAGES."
572 (mlet %store-monad ((profile (profile-derivation
574 (delete-duplicates packages eq?)))))
575 (return `(("profile" ,profile)))))
577 (define profile-service-type
578 ;; The service that populates the system's profile---i.e.,
579 ;; /run/current-system/profile. It is extended by package lists.
580 (service-type (name 'profile)
582 (list (service-extension system-service-type
583 packages->profile-entry)))
584 (compose concatenate)
587 (define (firmware->activation-gexp firmware)
588 "Return a gexp to make the packages listed in FIRMWARE loadable by the
590 (let ((directory (directory-union "firmware" firmware)))
591 ;; Tell the kernel where firmware is.
592 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
594 (define firmware-service-type
595 ;; The service that collects firmware.
596 (service-type (name 'firmware)
598 (list (service-extension activation-service-type
599 firmware->activation-gexp)))
600 (compose concatenate)
603 (define (gc-roots->system-entry roots)
604 "Return an entry in the system's output containing symlinks to ROOTS."
605 (mlet %store-monad ((entry (gexp->derivation
607 #~(let ((roots '#$roots))
613 (iota (length roots))))))))
614 (return (if (null? roots)
616 `(("gc-roots" ,entry))))))
618 (define gc-root-service-type
619 ;; A service to associate extra garbage-collector roots to the system. This
620 ;; is a simple hack that guarantees that the system retains references to
621 ;; the given list of roots. Roots must be "lowerable" objects like
622 ;; packages, or derivations.
623 (service-type (name 'gc-roots)
625 (list (service-extension system-service-type
626 gc-roots->system-entry)))
627 (compose concatenate)
635 (define-condition-type &missing-target-service-error &service-error
636 missing-target-service-error?
637 (service missing-target-service-error-service)
638 (target-type missing-target-service-error-target-type))
640 (define-condition-type &ambiguous-target-service-error &service-error
641 ambiguous-target-service-error?
642 (service ambiguous-target-service-error-service)
643 (target-type ambiguous-target-service-error-target-type))
645 (define (service-back-edges services)
646 "Return a procedure that, when passed a <service>, returns the list of
647 <service> objects that depend on it."
648 (define (add-edges service edges)
649 (define (add-edge extension edges)
650 (let ((target-type (service-extension-target extension)))
651 (match (filter (lambda (service)
652 (eq? (service-kind service) target-type))
655 (vhash-consq target service edges))
658 (condition (&missing-target-service-error
660 (target-type target-type))
663 (format #f (G_ "no target of type '~a' for service '~a'")
664 (service-type-name target-type)
666 (service-kind service))))))))
669 (condition (&ambiguous-target-service-error
671 (target-type target-type))
675 (G_ "more than one target service of type '~a'")
676 (service-type-name target-type))))))))))
678 (fold add-edge edges (service-type-extensions (service-kind service))))
680 (let ((edges (fold add-edges vlist-null services)))
682 (reverse (vhash-foldq* cons '() node edges)))))
684 (define* (fold-services services
685 #:key (target-type system-service-type))
686 "Fold SERVICES by propagating their extensions down to the root of type
687 TARGET-TYPE; return the root service adjusted accordingly."
689 (service-back-edges services))
691 (define (matching-extension target)
692 (let ((target (service-kind target)))
694 (($ <service-extension> type)
695 (eq? type target)))))
697 (define (apply-extension target)
699 (match (find (matching-extension target)
700 (service-type-extensions (service-kind service)))
701 (($ <service-extension> _ compute)
702 (compute (service-value service))))))
704 (match (filter (lambda (service)
705 (eq? (service-kind service) target-type))
708 (let loop ((sink sink))
709 (let* ((dependents (map loop (dependents sink)))
710 (extensions (map (apply-extension sink) dependents))
711 (extend (service-type-extend (service-kind sink)))
712 (compose (service-type-compose (service-kind sink)))
713 (params (service-value sink)))
714 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
715 ;; different type than the elements of EXTENSIONS.
717 (service (service-kind sink)
718 (extend params (compose extensions)))
722 (condition (&missing-target-service-error
724 (target-type target-type))
726 (message (format #f (G_ "service of type '~a' not found")
727 (service-type-name target-type)))))))
730 (condition (&ambiguous-target-service-error
732 (target-type target-type))
736 (G_ "more than one target service of type '~a'")
737 (service-type-name target-type)))))))))
739 ;;; services.scm ends here.