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
102 ;;; This module defines a broad notion of "service types" and "services."
104 ;;; A service type describe how its instances extend instances of other
105 ;;; service types. For instance, some services extend the instance of
106 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
107 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
108 ;;; <shepherd-service>.
110 ;;; When applicable, the service type defines how it can itself be extended,
111 ;;; by providing one procedure to compose extensions, and one procedure to
114 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
115 ;;; instance, which is the root of the service DAG. Its value is the
116 ;;; derivation that produces the 'system' directory as returned by
117 ;;; 'operating-system-derivation'.
119 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
120 ;;; "folds" by propagating extensions down the graph; it returns the root
121 ;;; service after the applying all its extensions.
125 (define-record-type <service-extension>
126 (service-extension target compute)
128 (target service-extension-target) ;<service-type>
129 (compute service-extension-compute)) ;params -> params
131 (define &no-default-value
132 ;; Value used to denote service types that have no associated default value.
135 (define-record-type* <service-type> service-type make-service-type
137 (name service-type-name) ;symbol (for debugging)
139 ;; Things extended by services of this type.
140 (extensions service-type-extensions) ;list of <service-extensions>
142 ;; Given a list of extensions, "compose" them.
143 (compose service-type-compose ;list of Any -> Any
146 ;; Extend the services' own parameters with the extension composition.
147 (extend service-type-extend ;list of Any -> parameters
150 ;; Optional default value for instances of this type.
151 (default-value service-type-default-value ;Any
152 (default &no-default-value))
155 (description service-type-description ;string
157 (location service-type-location ;<location>
158 (default (and=> (current-source-location)
159 source-properties->location))
162 (define (write-service-type type port)
163 (format port "#<service-type ~a ~a>"
164 (service-type-name type)
165 (number->string (object-address type) 16)))
167 (set-record-type-printer! <service-type> write-service-type)
169 (define %distro-root-directory
170 ;; Absolute file name of the module hierarchy.
171 (dirname (search-path %load-path "guix.scm")))
173 (define %service-type-path
174 ;; Search path for service types.
175 (make-parameter `((,%distro-root-directory . "gnu/services")
176 (,%distro-root-directory . "gnu/system"))))
178 (define* (fold-service-types proc seed
180 (modules (all-modules (%service-type-path))))
181 "For each service type exported by one of MODULES, call (PROC RESULT). SEED
182 is used as the initial value of RESULT."
183 (fold-module-public-variables (lambda (object result)
184 (if (service-type? object)
190 ;; Services of a given type.
191 (define-record-type <service>
192 (make-service type value)
195 (value service-value))
197 (define-syntax service
199 "Return a service instance of TYPE. The service value is VALUE or, if
200 omitted, TYPE's default value."
202 (make-service type value))
204 (%service-with-default-value (current-source-location)
207 (define (%service-with-default-value location type)
208 "Return a instance of service type TYPE with its default value, if any. If
209 TYPE does not have a default value, an error is raised."
210 ;; TODO: Currently this is a run-time error but with a little bit macrology
211 ;; we could turn it into an expansion-time error.
212 (let ((default (service-type-default-value type)))
213 (if (eq? default &no-default-value)
214 (let ((location (source-properties->location location)))
217 (&missing-value-service-error (type type) (location location))
219 (message (format #f (G_ "~a: no value specified \
220 for service of type '~a'")
221 (location->string location)
222 (service-type-name type)))))))
223 (service type default))))
225 (define-condition-type &service-error &error
228 (define-condition-type &missing-value-service-error &service-error
229 missing-value-service-error?
230 (type missing-value-service-error-type)
231 (location missing-value-service-error-location))
239 (define service-parameters
243 (define (simple-service name target value)
244 "Return a service that extends TARGET with VALUE. This works by creating a
245 singleton service type NAME, of which the returned service is an instance."
246 (let* ((extension (service-extension target identity))
247 (type (service-type (name name)
248 (extensions (list extension)))))
249 (service type value)))
251 (define-syntax %modify-service
255 ((_ svc (kind param => exp ...) clauses ...)
256 (if (eq? (service-kind svc) kind)
257 (let ((param (service-value svc)))
258 (service (service-kind svc)
260 (%modify-service svc clauses ...)))))
262 (define-syntax modify-services
264 "Modify the services listed in SERVICES according to CLAUSES and return
265 the resulting list of services. Each clause must have the form:
267 (TYPE VARIABLE => BODY)
269 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
270 identifier that is bound within BODY to the value of the service of that
271 TYPE. Consider this example:
273 (modify-services %base-services
274 (guix-service-type config =>
277 (use-substitutes? #f)
278 (extra-options '(\"--gc-keep-derivations\"))))
279 (mingetty-service-type config =>
280 (mingetty-configuration
282 (motd (plain-file \"motd\" \"Hi there!\")))))
284 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
285 all the MINGETTY-SERVICE-TYPE instances.
287 This is a shorthand for (map (lambda (svc) ...) %base-services)."
288 ((_ services clauses ...)
289 (map (lambda (service)
290 (%modify-service service clauses ...))
298 (define (system-derivation mentries mextensions)
299 "Return as a monadic value the derivation of the 'system' directory
300 containing the given entries."
301 (mlet %store-monad ((entries mentries)
302 (extensions (sequence %store-monad mextensions)))
305 (append entries (concatenate extensions))))))
307 (define system-service-type
308 ;; This is the ultimate service type, the root of the service DAG. The
309 ;; service of this type is extended by monadic name/item pairs. These items
310 ;; end up in the "system directory" as returned by
311 ;; 'operating-system-derivation'.
312 (service-type (name 'system)
315 (extend system-derivation)))
317 (define (compute-boot-script _ mexps)
318 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
320 ;; Clean up and activate the system, then spawn shepherd.
321 #~(begin #$@gexps))))
323 (define (boot-script-entry mboot)
324 "Return, as a monadic value, an entry for the boot script in the system
326 (mlet %store-monad ((boot mboot))
327 (return `(("boot" ,boot)))))
329 (define boot-service-type
330 ;; The service of this type is extended by being passed gexps as monadic
331 ;; values. It aggregates them in a single script, as a monadic value, which
332 ;; becomes its 'parameters'. It is the only service that extends nothing.
333 (service-type (name 'boot)
335 (list (service-extension system-service-type
338 (extend compute-boot-script)))
340 (define %boot-service
341 ;; The service that produces the boot script.
342 (service boot-service-type #t))
344 (define (cleanup-gexp _)
345 "Return as a monadic value a gexp to clean up /tmp and similar places upon
347 (with-monad %store-monad
348 (with-imported-modules '((guix build utils))
350 (use-modules (guix build utils))
352 ;; Clean out /tmp and /var/run.
354 ;; XXX This needs to happen before service activations, so it
355 ;; has to be here, but this also implicitly assumes that /tmp
356 ;; and /var/run are on the root partition.
357 (letrec-syntax ((fail-safe (syntax-rules ()
363 (fail-safe rest ...)))
366 ;; Ignore I/O errors so the system can boot.
368 ;; Remove stale Shadow lock files as they would lead to
369 ;; failures of 'useradd' & co.
370 (delete-file "/etc/group.lock")
371 (delete-file "/etc/passwd.lock")
372 (delete-file "/etc/.pwd.lock") ;from 'lckpwdf'
374 (delete-file-recursively "/tmp")
375 (delete-file-recursively "/var/run")
377 (chmod "/tmp" #o1777)
379 (chmod "/var/run" #o755))))))))
381 (define cleanup-service-type
382 ;; Service that cleans things up in /tmp and similar.
383 (service-type (name 'cleanup)
385 (list (service-extension boot-service-type
388 (define* (activation-service->script service)
389 "Return as a monadic value the activation script for SERVICE, a service of
390 ACTIVATION-SCRIPT-TYPE."
391 (activation-script (service-value service)))
393 (define (activation-script gexps)
394 "Return the system's activation script, which evaluates GEXPS."
395 (define (service-activations)
396 ;; Return the activation scripts for SERVICES.
398 (cut gexp->file "activate-service" <>)
401 (mlet* %store-monad ((actions (service-activations)))
402 (gexp->file "activate"
403 (with-imported-modules (source-module-closure
404 '((gnu build activation)
407 (use-modules (gnu build activation)
410 ;; Make sure the user accounting database exists. If it
411 ;; does not exist, 'setutxent' does not create it and
412 ;; thus there is no accounting at all.
413 (close-port (open-file "/var/run/utmpx" "a0"))
415 ;; Same for 'wtmp', which is populated by mingetty et
418 (close-port (open-file "/var/log/wtmp" "a0"))
420 ;; Set up /run/current-system. Among other things this
421 ;; sets up locales, which the activation snippets
422 ;; executed below may expect.
423 (activate-current-system)
425 ;; Run the services' activation snippets.
426 ;; TODO: Use 'load-compiled'.
427 (for-each primitive-load '#$actions))))))
429 (define (gexps->activation-gexp gexps)
430 "Return a gexp that runs the activation script containing GEXPS."
431 (mlet %store-monad ((script (activation-script gexps)))
432 (return #~(primitive-load #$script))))
434 (define (second-argument a b) b)
436 (define activation-service-type
437 (service-type (name 'activate)
439 (list (service-extension boot-service-type
440 gexps->activation-gexp)))
442 (extend second-argument)))
444 (define %activation-service
445 ;; The activation service produces the activation script from the gexps it
447 (service activation-service-type #t))
449 (define %modprobe-wrapper
450 ;; Wrapper for the 'modprobe' command that knows where modules live.
452 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
453 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
454 ;; environment variable is not set---hence the need for this wrapper.
455 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
456 (program-file "modprobe"
458 (setenv "LINUX_MODULE_DIRECTORY"
459 "/run/booted-system/kernel/lib/modules")
460 (apply execl #$modprobe
461 (cons #$modprobe (cdr (command-line))))))))
463 (define %linux-kernel-activation
464 ;; Activation of the Linux kernel running on the bare metal (as opposed to
465 ;; running in a container.)
467 ;; Tell the kernel to use our 'modprobe' command.
468 (activate-modprobe #$%modprobe-wrapper)
470 ;; Let users debug their own processes!
471 (activate-ptrace-attach)))
473 (define %linux-bare-metal-service
474 ;; The service that does things that are needed on the "bare metal", but not
475 ;; necessary or impossible in a container.
476 (simple-service 'linux-bare-metal
477 activation-service-type
478 %linux-kernel-activation))
481 (define special-files-service-type
482 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
484 (name 'special-files)
486 (list (service-extension activation-service-type
488 #~(activate-special-files '#$files)))))
489 (compose concatenate)
492 (define (extra-special-file file target)
493 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
494 (file-append coreutils \"/bin/env\")
495 and FILE could be \"/usr/bin/env\"."
496 (simple-service (string->symbol (string-append "special-file-" file))
497 special-files-service-type
500 (define (etc-directory service)
501 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
502 (files->etc-directory (service-value service)))
504 (define (files->etc-directory files)
505 (file-union "etc" files))
507 (define (etc-entry files)
508 "Return an entry for the /etc directory consisting of FILES in the system
510 (with-monad %store-monad
511 (return `(("etc" ,(files->etc-directory files))))))
513 (define etc-service-type
514 (service-type (name 'etc)
517 (service-extension activation-service-type
520 (files->etc-directory files)))
521 #~(activate-etc #$etc))))
522 (service-extension system-service-type etc-entry)))
523 (compose concatenate)
526 (define (etc-service files)
527 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
528 FILES must be a list of name/file-like object pairs."
529 (service etc-service-type files))
531 (define setuid-program-service-type
532 (service-type (name 'setuid-program)
534 (list (service-extension activation-service-type
536 #~(activate-setuid-programs
537 (list #$@programs))))))
538 (compose concatenate)
541 (define (packages->profile-entry packages)
542 "Return a system entry for the profile containing PACKAGES."
543 (mlet %store-monad ((profile (profile-derivation
545 (delete-duplicates packages eq?)))))
546 (return `(("profile" ,profile)))))
548 (define profile-service-type
549 ;; The service that populates the system's profile---i.e.,
550 ;; /run/current-system/profile. It is extended by package lists.
551 (service-type (name 'profile)
553 (list (service-extension system-service-type
554 packages->profile-entry)))
555 (compose concatenate)
558 (define (firmware->activation-gexp firmware)
559 "Return a gexp to make the packages listed in FIRMWARE loadable by the
561 (let ((directory (directory-union "firmware" firmware)))
562 ;; Tell the kernel where firmware is.
563 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
565 (define firmware-service-type
566 ;; The service that collects firmware.
567 (service-type (name 'firmware)
569 (list (service-extension activation-service-type
570 firmware->activation-gexp)))
571 (compose concatenate)
574 (define (gc-roots->system-entry roots)
575 "Return an entry in the system's output containing symlinks to ROOTS."
576 (mlet %store-monad ((entry (gexp->derivation
578 #~(let ((roots '#$roots))
584 (iota (length roots))))))))
585 (return (if (null? roots)
587 `(("gc-roots" ,entry))))))
589 (define gc-root-service-type
590 ;; A service to associate extra garbage-collector roots to the system. This
591 ;; is a simple hack that guarantees that the system retains references to
592 ;; the given list of roots. Roots must be "lowerable" objects like
593 ;; packages, or derivations.
594 (service-type (name 'gc-roots)
596 (list (service-extension system-service-type
597 gc-roots->system-entry)))
598 (compose concatenate)
606 (define-condition-type &missing-target-service-error &service-error
607 missing-target-service-error?
608 (service missing-target-service-error-service)
609 (target-type missing-target-service-error-target-type))
611 (define-condition-type &ambiguous-target-service-error &service-error
612 ambiguous-target-service-error?
613 (service ambiguous-target-service-error-service)
614 (target-type ambiguous-target-service-error-target-type))
616 (define (service-back-edges services)
617 "Return a procedure that, when passed a <service>, returns the list of
618 <service> objects that depend on it."
619 (define (add-edges service edges)
620 (define (add-edge extension edges)
621 (let ((target-type (service-extension-target extension)))
622 (match (filter (lambda (service)
623 (eq? (service-kind service) target-type))
626 (vhash-consq target service edges))
629 (condition (&missing-target-service-error
631 (target-type target-type))
634 (format #f (G_ "no target of type '~a' for service '~a'")
635 (service-type-name target-type)
637 (service-kind service))))))))
640 (condition (&ambiguous-target-service-error
642 (target-type target-type))
646 (G_ "more than one target service of type '~a'")
647 (service-type-name target-type))))))))))
649 (fold add-edge edges (service-type-extensions (service-kind service))))
651 (let ((edges (fold add-edges vlist-null services)))
653 (reverse (vhash-foldq* cons '() node edges)))))
655 (define* (fold-services services
656 #:key (target-type system-service-type))
657 "Fold SERVICES by propagating their extensions down to the root of type
658 TARGET-TYPE; return the root service adjusted accordingly."
660 (service-back-edges services))
662 (define (matching-extension target)
663 (let ((target (service-kind target)))
665 (($ <service-extension> type)
666 (eq? type target)))))
668 (define (apply-extension target)
670 (match (find (matching-extension target)
671 (service-type-extensions (service-kind service)))
672 (($ <service-extension> _ compute)
673 (compute (service-value service))))))
675 (match (filter (lambda (service)
676 (eq? (service-kind service) target-type))
679 (let loop ((sink sink))
680 (let* ((dependents (map loop (dependents sink)))
681 (extensions (map (apply-extension sink) dependents))
682 (extend (service-type-extend (service-kind sink)))
683 (compose (service-type-compose (service-kind sink)))
684 (params (service-value sink)))
685 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
686 ;; different type than the elements of EXTENSIONS.
688 (service (service-kind sink)
689 (extend params (compose extensions)))
693 (condition (&missing-target-service-error
695 (target-type target-type))
697 (message (format #f (G_ "service of type '~a' not found")
698 (service-type-name target-type)))))))
701 (condition (&ambiguous-target-service-error
703 (target-type target-type))
707 (G_ "more than one target service of type '~a'")
708 (service-type-name target-type)))))))))
710 ;;; services.scm ends here.