1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016 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 sets)
27 #:use-module (guix ui)
28 #:use-module (gnu packages base)
29 #:use-module (gnu packages bash)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-9)
32 #:use-module (srfi srfi-9 gnu)
33 #:use-module (srfi srfi-26)
34 #:use-module (srfi srfi-34)
35 #:use-module (srfi srfi-35)
36 #:use-module (ice-9 vlist)
37 #:use-module (ice-9 match)
38 #:export (service-extension
44 service-type-extensions
58 missing-target-service-error?
59 missing-target-service-error-service
60 missing-target-service-error-target-type
61 ambiguous-target-service-error?
62 ambiguous-target-service-error-service
63 ambiguous-target-service-error-target-type
68 activation-service-type
69 activation-service->script
70 %linux-bare-metal-service
73 setuid-program-service-type
82 file-union)) ;XXX: for lack of a better place
86 ;;; This module defines a broad notion of "service types" and "services."
88 ;;; A service type describe how its instances extend instances of other
89 ;;; service types. For instance, some services extend the instance of
90 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
91 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
92 ;;; <shepherd-service>.
94 ;;; When applicable, the service type defines how it can itself be extended,
95 ;;; by providing one procedure to compose extensions, and one procedure to
98 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
99 ;;; instance, which is the root of the service DAG. Its value is the
100 ;;; derivation that produces the 'system' directory as returned by
101 ;;; 'operating-system-derivation'.
103 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
104 ;;; "folds" by propagating extensions down the graph; it returns the root
105 ;;; service after the applying all its extensions.
109 (define-record-type <service-extension>
110 (service-extension target compute)
112 (target service-extension-target) ;<service-type>
113 (compute service-extension-compute)) ;params -> params
115 (define-record-type* <service-type> service-type make-service-type
117 (name service-type-name) ;symbol (for debugging)
119 ;; Things extended by services of this type.
120 (extensions service-type-extensions) ;list of <service-extensions>
122 ;; Given a list of extensions, "compose" them.
123 (compose service-type-compose ;list of Any -> Any
126 ;; Extend the services' own parameters with the extension composition.
127 (extend service-type-extend ;list of Any -> parameters
130 (define (write-service-type type port)
131 (format port "#<service-type ~a ~a>"
132 (service-type-name type)
133 (number->string (object-address type) 16)))
135 (set-record-type-printer! <service-type> write-service-type)
137 ;; Services of a given type.
138 (define-record-type <service>
139 (service type parameters)
142 (parameters service-parameters))
145 (define-syntax %modify-service
149 ((_ svc (kind param => exp ...) clauses ...)
150 (if (eq? (service-kind svc) kind)
151 (let ((param (service-parameters svc)))
152 (service (service-kind svc)
154 (%modify-service svc clauses ...)))))
156 (define-syntax modify-services
158 "Modify the services listed in SERVICES according to CLAUSES and return
159 the resulting list of services. Each clause must have the form:
161 (TYPE VARIABLE => BODY)
163 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
164 identifier that is bound within BODY to the value of the service of that
165 TYPE. Consider this example:
167 (modify-services %base-services
168 (guix-service-type config =>
171 (use-substitutes? #f)
172 (extra-options '(\"--gc-keep-derivations\"))))
173 (mingetty-service-type config =>
174 (mingetty-configuration
176 (motd (plain-file \"motd\" \"Hi there!\")))))
178 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
179 all the MINGETTY-SERVICE-TYPE instances.
181 This is a shorthand for (map (lambda (svc) ...) %base-services)."
182 ((_ services clauses ...)
183 (map (lambda (service)
184 (%modify-service service clauses ...))
192 (define (system-derivation mentries mextensions)
193 "Return as a monadic value the derivation of the 'system' directory
194 containing the given entries."
195 (mlet %store-monad ((entries mentries)
196 (extensions (sequence %store-monad mextensions)))
199 (append entries (concatenate extensions))))))
201 (define system-service-type
202 ;; This is the ultimate service type, the root of the service DAG. The
203 ;; service of this type is extended by monadic name/item pairs. These items
204 ;; end up in the "system directory" as returned by
205 ;; 'operating-system-derivation'.
206 (service-type (name 'system)
209 (extend system-derivation)))
211 (define (compute-boot-script _ mexps)
212 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
214 ;; Clean up and activate the system, then spawn shepherd.
215 #~(begin #$@gexps))))
217 (define (boot-script-entry mboot)
218 "Return, as a monadic value, an entry for the boot script in the system
220 (mlet %store-monad ((boot mboot))
221 (return `(("boot" ,boot)))))
223 (define boot-service-type
224 ;; The service of this type is extended by being passed gexps as monadic
225 ;; values. It aggregates them in a single script, as a monadic value, which
226 ;; becomes its 'parameters'. It is the only service that extends nothing.
227 (service-type (name 'boot)
229 (list (service-extension system-service-type
232 (extend compute-boot-script)))
234 (define %boot-service
235 ;; The service that produces the boot script.
236 (service boot-service-type #t))
238 (define (cleanup-gexp _)
239 "Return as a monadic value a gexp to clean up /tmp and similar places upon
242 '((guix build utils)))
244 (mlet %store-monad ((modules (imported-modules %modules))
245 (compiled (compiled-modules %modules)))
247 (eval-when (expand load eval)
248 ;; Make sure 'use-modules' below succeeds.
249 (set! %load-path (cons #$modules %load-path))
250 (set! %load-compiled-path
251 (cons #$compiled %load-compiled-path)))
253 (use-modules (guix build utils))
255 ;; Clean out /tmp and /var/run.
257 ;; XXX This needs to happen before service activations, so it
258 ;; has to be here, but this also implicitly assumes that /tmp
259 ;; and /var/run are on the root partition.
260 (letrec-syntax ((fail-safe (syntax-rules ()
266 (fail-safe rest ...)))
269 ;; Ignore I/O errors so the system can boot.
271 (delete-file-recursively "/tmp")
272 (delete-file-recursively "/var/run")
274 (chmod "/tmp" #o1777)
276 (chmod "/var/run" #o755)))))))
278 (define cleanup-service-type
279 ;; Service that cleans things up in /tmp and similar.
280 (service-type (name 'cleanup)
282 (list (service-extension boot-service-type
285 (define* (file-union name files) ;FIXME: Factorize.
286 "Return a <computed-file> that builds a directory containing all of FILES.
287 Each item in FILES must be a list where the first element is the file name to
288 use in the new directory, and the second element is a gexp denoting the target
294 #$@(map (match-lambda
297 ;; Stat the source to abort early if it
301 (symlink #$source #$target))))
304 (define (directory-union name things)
305 "Return a directory that is the union of THINGS."
308 ;; Only one thing; return it.
313 (use-modules (guix build union))
314 (union-build #$output '#$things))
315 #:modules '((guix build union))))))
317 (define* (activation-service->script service)
318 "Return as a monadic value the activation script for SERVICE, a service of
319 ACTIVATION-SCRIPT-TYPE."
320 (activation-script (service-parameters service)))
322 (define (activation-script gexps)
323 "Return the system's activation script, which evaluates GEXPS."
325 '((gnu build activation)
326 (gnu build linux-boot)
327 (gnu build linux-modules)
328 (gnu build file-systems)
330 (guix build syscalls)
331 (guix build bournish)
334 (define (service-activations)
335 ;; Return the activation scripts for SERVICES.
337 (cut gexp->file "activate-service" <>)
340 (mlet* %store-monad ((actions (service-activations))
341 (modules (imported-modules %modules))
342 (compiled (compiled-modules %modules)))
343 (gexp->file "activate"
345 (eval-when (expand load eval)
346 ;; Make sure 'use-modules' below succeeds.
347 (set! %load-path (cons #$modules %load-path))
348 (set! %load-compiled-path
349 (cons #$compiled %load-compiled-path)))
351 (use-modules (gnu build activation))
353 ;; Make sure /bin/sh is valid and current.
355 (string-append #$(canonical-package bash) "/bin/sh"))
357 ;; Run the services' activation snippets.
358 ;; TODO: Use 'load-compiled'.
359 (for-each primitive-load '#$actions)
361 ;; Set up /run/current-system.
362 (activate-current-system)))))
364 (define (gexps->activation-gexp gexps)
365 "Return a gexp that runs the activation script containing GEXPS."
366 (mlet %store-monad ((script (activation-script gexps)))
367 (return #~(primitive-load #$script))))
369 (define (second-argument a b) b)
371 (define activation-service-type
372 (service-type (name 'activate)
374 (list (service-extension boot-service-type
375 gexps->activation-gexp)))
377 (extend second-argument)))
379 (define %activation-service
380 ;; The activation service produces the activation script from the gexps it
382 (service activation-service-type #t))
384 (define %modprobe-wrapper
385 ;; Wrapper for the 'modprobe' command that knows where modules live.
387 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
388 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
389 ;; environment variable is not set---hence the need for this wrapper.
390 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
391 (program-file "modprobe"
393 (setenv "LINUX_MODULE_DIRECTORY"
394 "/run/booted-system/kernel/lib/modules")
395 (apply execl #$modprobe
396 (cons #$modprobe (cdr (command-line))))))))
398 (define %linux-kernel-activation
399 ;; Activation of the Linux kernel running on the bare metal (as opposed to
400 ;; running in a container.)
402 ;; Tell the kernel to use our 'modprobe' command.
403 (activate-modprobe #$%modprobe-wrapper)
405 ;; Let users debug their own processes!
406 (activate-ptrace-attach)))
408 (define linux-bare-metal-service-type
409 (service-type (name 'linux-bare-metal)
411 (list (service-extension activation-service-type
412 (const %linux-kernel-activation))))))
414 (define %linux-bare-metal-service
415 ;; The service that does things that are needed on the "bare metal", but not
416 ;; necessary or impossible in a container.
417 (service linux-bare-metal-service-type #f))
419 (define (etc-directory service)
420 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
421 (files->etc-directory (service-parameters service)))
423 (define (files->etc-directory files)
424 (file-union "etc" files))
426 (define (etc-entry files)
427 "Return an entry for the /etc directory consisting of FILES in the system
429 (with-monad %store-monad
430 (return `(("etc" ,(files->etc-directory files))))))
432 (define etc-service-type
433 (service-type (name 'etc)
436 (service-extension activation-service-type
439 (files->etc-directory files)))
440 #~(activate-etc #$etc))))
441 (service-extension system-service-type etc-entry)))
442 (compose concatenate)
445 (define (etc-service files)
446 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
447 FILES must be a list of name/file-like object pairs."
448 (service etc-service-type files))
450 (define setuid-program-service-type
451 (service-type (name 'setuid-program)
453 (list (service-extension activation-service-type
455 #~(activate-setuid-programs
456 (list #$@programs))))))
457 (compose concatenate)
460 (define (packages->profile-entry packages)
461 "Return a system entry for the profile containing PACKAGES."
462 (mlet %store-monad ((profile (profile-derivation
464 (delete-duplicates packages eq?)))))
465 (return `(("profile" ,profile)))))
467 (define profile-service-type
468 ;; The service that populates the system's profile---i.e.,
469 ;; /run/current-system/profile. It is extended by package lists.
470 (service-type (name 'profile)
472 (list (service-extension system-service-type
473 packages->profile-entry)))
474 (compose concatenate)
477 (define (firmware->activation-gexp firmware)
478 "Return a gexp to make the packages listed in FIRMWARE loadable by the
480 (let ((directory (directory-union "firmware" firmware)))
481 ;; Tell the kernel where firmware is.
482 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
484 (define firmware-service-type
485 ;; The service that collects firmware.
486 (service-type (name 'firmware)
488 (list (service-extension activation-service-type
489 firmware->activation-gexp)))
490 (compose concatenate)
493 (define (gc-roots->system-entry roots)
494 "Return an entry in the system's output containing symlinks to ROOTS."
495 (mlet %store-monad ((entry (gexp->derivation
497 #~(let ((roots '#$roots))
503 (iota (length roots))))))))
504 (return (if (null? roots)
506 `(("gc-roots" ,entry))))))
508 (define gc-root-service-type
509 ;; A service to associate extra garbage-collector roots to the system. This
510 ;; is a simple hack that guarantees that the system retains references to
511 ;; the given list of roots. Roots must be "lowerable" objects like
512 ;; packages, or derivations.
513 (service-type (name 'gc-roots)
515 (list (service-extension system-service-type
516 gc-roots->system-entry)))
517 (compose concatenate)
525 (define-condition-type &service-error &error
528 (define-condition-type &missing-target-service-error &service-error
529 missing-target-service-error?
530 (service missing-target-service-error-service)
531 (target-type missing-target-service-error-target-type))
533 (define-condition-type &ambiguous-target-service-error &service-error
534 ambiguous-target-service-error?
535 (service ambiguous-target-service-error-service)
536 (target-type ambiguous-target-service-error-target-type))
538 (define (service-back-edges services)
539 "Return a procedure that, when passed a <service>, returns the list of
540 <service> objects that depend on it."
541 (define (add-edges service edges)
542 (define (add-edge extension edges)
543 (let ((target-type (service-extension-target extension)))
544 (match (filter (lambda (service)
545 (eq? (service-kind service) target-type))
548 (vhash-consq target service edges))
551 (condition (&missing-target-service-error
553 (target-type target-type))
556 (format #f (_ "no target of type '~a' for service ~s")
557 (service-type-name target-type)
561 (condition (&ambiguous-target-service-error
563 (target-type target-type))
567 (_ "more than one target service of type '~a'")
568 (service-type-name target-type))))))))))
570 (fold add-edge edges (service-type-extensions (service-kind service))))
572 (let ((edges (fold add-edges vlist-null services)))
574 (reverse (vhash-foldq* cons '() node edges)))))
576 (define* (fold-services services
577 #:key (target-type system-service-type))
578 "Fold SERVICES by propagating their extensions down to the root of type
579 TARGET-TYPE; return the root service adjusted accordingly."
581 (service-back-edges services))
583 (define (matching-extension target)
584 (let ((target (service-kind target)))
586 (($ <service-extension> type)
587 (eq? type target)))))
589 (define (apply-extension target)
591 (match (find (matching-extension target)
592 (service-type-extensions (service-kind service)))
593 (($ <service-extension> _ compute)
594 (compute (service-parameters service))))))
596 (match (filter (lambda (service)
597 (eq? (service-kind service) target-type))
600 (let loop ((sink sink))
601 (let* ((dependents (map loop (dependents sink)))
602 (extensions (map (apply-extension sink) dependents))
603 (extend (service-type-extend (service-kind sink)))
604 (compose (service-type-compose (service-kind sink)))
605 (params (service-parameters sink)))
606 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
607 ;; different type than the elements of EXTENSIONS.
609 (service (service-kind sink)
610 (extend params (compose extensions)))
614 (condition (&missing-target-service-error
616 (target-type target-type))
618 (message (format #f (_ "service of type '~a' not found")
619 (service-type-name target-type)))))))
622 (condition (&ambiguous-target-service-error
624 (target-type target-type))
628 (_ "more than one target service of type '~a'")
629 (service-type-name target-type)))))))))
631 ;;; services.scm ends here.