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 (guix modules)
29 #:use-module (gnu packages base)
30 #:use-module (gnu packages bash)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-9)
33 #:use-module (srfi srfi-9 gnu)
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-34)
36 #:use-module (srfi srfi-35)
37 #:use-module (ice-9 vlist)
38 #:use-module (ice-9 match)
39 #:export (service-extension
45 service-type-extensions
60 missing-target-service-error?
61 missing-target-service-error-service
62 missing-target-service-error-target-type
63 ambiguous-target-service-error?
64 ambiguous-target-service-error-service
65 ambiguous-target-service-error-target-type
70 activation-service-type
71 activation-service->script
72 %linux-bare-metal-service
75 setuid-program-service-type
84 file-union)) ;XXX: for lack of a better place
88 ;;; This module defines a broad notion of "service types" and "services."
90 ;;; A service type describe how its instances extend instances of other
91 ;;; service types. For instance, some services extend the instance of
92 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
93 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
94 ;;; <shepherd-service>.
96 ;;; When applicable, the service type defines how it can itself be extended,
97 ;;; by providing one procedure to compose extensions, and one procedure to
100 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
101 ;;; instance, which is the root of the service DAG. Its value is the
102 ;;; derivation that produces the 'system' directory as returned by
103 ;;; 'operating-system-derivation'.
105 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
106 ;;; "folds" by propagating extensions down the graph; it returns the root
107 ;;; service after the applying all its extensions.
111 (define-record-type <service-extension>
112 (service-extension target compute)
114 (target service-extension-target) ;<service-type>
115 (compute service-extension-compute)) ;params -> params
117 (define-record-type* <service-type> service-type make-service-type
119 (name service-type-name) ;symbol (for debugging)
121 ;; Things extended by services of this type.
122 (extensions service-type-extensions) ;list of <service-extensions>
124 ;; Given a list of extensions, "compose" them.
125 (compose service-type-compose ;list of Any -> Any
128 ;; Extend the services' own parameters with the extension composition.
129 (extend service-type-extend ;list of Any -> parameters
132 (define (write-service-type type port)
133 (format port "#<service-type ~a ~a>"
134 (service-type-name type)
135 (number->string (object-address type) 16)))
137 (set-record-type-printer! <service-type> write-service-type)
139 ;; Services of a given type.
140 (define-record-type <service>
141 (service type parameters)
144 (parameters service-parameters))
146 (define (simple-service name target value)
147 "Return a service that extends TARGET with VALUE. This works by creating a
148 singleton service type NAME, of which the returned service is an instance."
149 (let* ((extension (service-extension target identity))
150 (type (service-type (name name)
151 (extensions (list extension)))))
152 (service type value)))
154 (define-syntax %modify-service
158 ((_ svc (kind param => exp ...) clauses ...)
159 (if (eq? (service-kind svc) kind)
160 (let ((param (service-parameters svc)))
161 (service (service-kind svc)
163 (%modify-service svc clauses ...)))))
165 (define-syntax modify-services
167 "Modify the services listed in SERVICES according to CLAUSES and return
168 the resulting list of services. Each clause must have the form:
170 (TYPE VARIABLE => BODY)
172 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
173 identifier that is bound within BODY to the value of the service of that
174 TYPE. Consider this example:
176 (modify-services %base-services
177 (guix-service-type config =>
180 (use-substitutes? #f)
181 (extra-options '(\"--gc-keep-derivations\"))))
182 (mingetty-service-type config =>
183 (mingetty-configuration
185 (motd (plain-file \"motd\" \"Hi there!\")))))
187 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
188 all the MINGETTY-SERVICE-TYPE instances.
190 This is a shorthand for (map (lambda (svc) ...) %base-services)."
191 ((_ services clauses ...)
192 (map (lambda (service)
193 (%modify-service service clauses ...))
201 (define (system-derivation mentries mextensions)
202 "Return as a monadic value the derivation of the 'system' directory
203 containing the given entries."
204 (mlet %store-monad ((entries mentries)
205 (extensions (sequence %store-monad mextensions)))
208 (append entries (concatenate extensions))))))
210 (define system-service-type
211 ;; This is the ultimate service type, the root of the service DAG. The
212 ;; service of this type is extended by monadic name/item pairs. These items
213 ;; end up in the "system directory" as returned by
214 ;; 'operating-system-derivation'.
215 (service-type (name 'system)
218 (extend system-derivation)))
220 (define (compute-boot-script _ mexps)
221 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
223 ;; Clean up and activate the system, then spawn shepherd.
224 #~(begin #$@gexps))))
226 (define (boot-script-entry mboot)
227 "Return, as a monadic value, an entry for the boot script in the system
229 (mlet %store-monad ((boot mboot))
230 (return `(("boot" ,boot)))))
232 (define boot-service-type
233 ;; The service of this type is extended by being passed gexps as monadic
234 ;; values. It aggregates them in a single script, as a monadic value, which
235 ;; becomes its 'parameters'. It is the only service that extends nothing.
236 (service-type (name 'boot)
238 (list (service-extension system-service-type
241 (extend compute-boot-script)))
243 (define %boot-service
244 ;; The service that produces the boot script.
245 (service boot-service-type #t))
247 (define (cleanup-gexp _)
248 "Return as a monadic value a gexp to clean up /tmp and similar places upon
250 (with-monad %store-monad
251 (with-imported-modules '((guix build utils))
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.
312 (with-imported-modules '((guix build union))
314 (use-modules (guix build union))
315 (union-build #$output '#$things)))))))
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."
324 (define (service-activations)
325 ;; Return the activation scripts for SERVICES.
327 (cut gexp->file "activate-service" <>)
330 (mlet* %store-monad ((actions (service-activations)))
331 (gexp->file "activate"
332 (with-imported-modules (source-module-closure
333 '((gnu build activation)))
335 (use-modules (gnu build activation))
337 ;; Make sure /bin/sh is valid and current.
339 (string-append #$(canonical-package bash) "/bin/sh"))
341 ;; Set up /run/current-system. Among other things this
342 ;; sets up locales, which the activation snippets
343 ;; executed below may expect.
344 (activate-current-system)
346 ;; Run the services' activation snippets.
347 ;; TODO: Use 'load-compiled'.
348 (for-each primitive-load '#$actions))))))
350 (define (gexps->activation-gexp gexps)
351 "Return a gexp that runs the activation script containing GEXPS."
352 (mlet %store-monad ((script (activation-script gexps)))
353 (return #~(primitive-load #$script))))
355 (define (second-argument a b) b)
357 (define activation-service-type
358 (service-type (name 'activate)
360 (list (service-extension boot-service-type
361 gexps->activation-gexp)))
363 (extend second-argument)))
365 (define %activation-service
366 ;; The activation service produces the activation script from the gexps it
368 (service activation-service-type #t))
370 (define %modprobe-wrapper
371 ;; Wrapper for the 'modprobe' command that knows where modules live.
373 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
374 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
375 ;; environment variable is not set---hence the need for this wrapper.
376 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
377 (program-file "modprobe"
379 (setenv "LINUX_MODULE_DIRECTORY"
380 "/run/booted-system/kernel/lib/modules")
381 (apply execl #$modprobe
382 (cons #$modprobe (cdr (command-line))))))))
384 (define %linux-kernel-activation
385 ;; Activation of the Linux kernel running on the bare metal (as opposed to
386 ;; running in a container.)
388 ;; Tell the kernel to use our 'modprobe' command.
389 (activate-modprobe #$%modprobe-wrapper)
391 ;; Let users debug their own processes!
392 (activate-ptrace-attach)))
394 (define linux-bare-metal-service-type
395 (service-type (name 'linux-bare-metal)
397 (list (service-extension activation-service-type
398 (const %linux-kernel-activation))))))
400 (define %linux-bare-metal-service
401 ;; The service that does things that are needed on the "bare metal", but not
402 ;; necessary or impossible in a container.
403 (service linux-bare-metal-service-type #f))
405 (define (etc-directory service)
406 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
407 (files->etc-directory (service-parameters service)))
409 (define (files->etc-directory files)
410 (file-union "etc" files))
412 (define (etc-entry files)
413 "Return an entry for the /etc directory consisting of FILES in the system
415 (with-monad %store-monad
416 (return `(("etc" ,(files->etc-directory files))))))
418 (define etc-service-type
419 (service-type (name 'etc)
422 (service-extension activation-service-type
425 (files->etc-directory files)))
426 #~(activate-etc #$etc))))
427 (service-extension system-service-type etc-entry)))
428 (compose concatenate)
431 (define (etc-service files)
432 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
433 FILES must be a list of name/file-like object pairs."
434 (service etc-service-type files))
436 (define setuid-program-service-type
437 (service-type (name 'setuid-program)
439 (list (service-extension activation-service-type
441 #~(activate-setuid-programs
442 (list #$@programs))))))
443 (compose concatenate)
446 (define (packages->profile-entry packages)
447 "Return a system entry for the profile containing PACKAGES."
448 (mlet %store-monad ((profile (profile-derivation
450 (delete-duplicates packages eq?)))))
451 (return `(("profile" ,profile)))))
453 (define profile-service-type
454 ;; The service that populates the system's profile---i.e.,
455 ;; /run/current-system/profile. It is extended by package lists.
456 (service-type (name 'profile)
458 (list (service-extension system-service-type
459 packages->profile-entry)))
460 (compose concatenate)
463 (define (firmware->activation-gexp firmware)
464 "Return a gexp to make the packages listed in FIRMWARE loadable by the
466 (let ((directory (directory-union "firmware" firmware)))
467 ;; Tell the kernel where firmware is.
468 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
470 (define firmware-service-type
471 ;; The service that collects firmware.
472 (service-type (name 'firmware)
474 (list (service-extension activation-service-type
475 firmware->activation-gexp)))
476 (compose concatenate)
479 (define (gc-roots->system-entry roots)
480 "Return an entry in the system's output containing symlinks to ROOTS."
481 (mlet %store-monad ((entry (gexp->derivation
483 #~(let ((roots '#$roots))
489 (iota (length roots))))))))
490 (return (if (null? roots)
492 `(("gc-roots" ,entry))))))
494 (define gc-root-service-type
495 ;; A service to associate extra garbage-collector roots to the system. This
496 ;; is a simple hack that guarantees that the system retains references to
497 ;; the given list of roots. Roots must be "lowerable" objects like
498 ;; packages, or derivations.
499 (service-type (name 'gc-roots)
501 (list (service-extension system-service-type
502 gc-roots->system-entry)))
503 (compose concatenate)
511 (define-condition-type &service-error &error
514 (define-condition-type &missing-target-service-error &service-error
515 missing-target-service-error?
516 (service missing-target-service-error-service)
517 (target-type missing-target-service-error-target-type))
519 (define-condition-type &ambiguous-target-service-error &service-error
520 ambiguous-target-service-error?
521 (service ambiguous-target-service-error-service)
522 (target-type ambiguous-target-service-error-target-type))
524 (define (service-back-edges services)
525 "Return a procedure that, when passed a <service>, returns the list of
526 <service> objects that depend on it."
527 (define (add-edges service edges)
528 (define (add-edge extension edges)
529 (let ((target-type (service-extension-target extension)))
530 (match (filter (lambda (service)
531 (eq? (service-kind service) target-type))
534 (vhash-consq target service edges))
537 (condition (&missing-target-service-error
539 (target-type target-type))
542 (format #f (_ "no target of type '~a' for service ~s")
543 (service-type-name target-type)
547 (condition (&ambiguous-target-service-error
549 (target-type target-type))
553 (_ "more than one target service of type '~a'")
554 (service-type-name target-type))))))))))
556 (fold add-edge edges (service-type-extensions (service-kind service))))
558 (let ((edges (fold add-edges vlist-null services)))
560 (reverse (vhash-foldq* cons '() node edges)))))
562 (define* (fold-services services
563 #:key (target-type system-service-type))
564 "Fold SERVICES by propagating their extensions down to the root of type
565 TARGET-TYPE; return the root service adjusted accordingly."
567 (service-back-edges services))
569 (define (matching-extension target)
570 (let ((target (service-kind target)))
572 (($ <service-extension> type)
573 (eq? type target)))))
575 (define (apply-extension target)
577 (match (find (matching-extension target)
578 (service-type-extensions (service-kind service)))
579 (($ <service-extension> _ compute)
580 (compute (service-parameters service))))))
582 (match (filter (lambda (service)
583 (eq? (service-kind service) target-type))
586 (let loop ((sink sink))
587 (let* ((dependents (map loop (dependents sink)))
588 (extensions (map (apply-extension sink) dependents))
589 (extend (service-type-extend (service-kind sink)))
590 (compose (service-type-compose (service-kind sink)))
591 (params (service-parameters sink)))
592 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
593 ;; different type than the elements of EXTENSIONS.
595 (service (service-kind sink)
596 (extend params (compose extensions)))
600 (condition (&missing-target-service-error
602 (target-type target-type))
604 (message (format #f (_ "service of type '~a' not found")
605 (service-type-name target-type)))))))
608 (condition (&ambiguous-target-service-error
610 (target-type target-type))
614 (_ "more than one target service of type '~a'")
615 (service-type-name target-type)))))))))
617 ;;; services.scm ends here.