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 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
41 service-extension-target
42 service-extension-compute
47 service-type-extensions
62 missing-target-service-error?
63 missing-target-service-error-service
64 missing-target-service-error-target-type
65 ambiguous-target-service-error?
66 ambiguous-target-service-error-service
67 ambiguous-target-service-error-target-type
72 activation-service-type
73 activation-service->script
74 %linux-bare-metal-service
77 setuid-program-service-type
86 file-union)) ;XXX: for lack of a better place
90 ;;; This module defines a broad notion of "service types" and "services."
92 ;;; A service type describe how its instances extend instances of other
93 ;;; service types. For instance, some services extend the instance of
94 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
95 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
96 ;;; <shepherd-service>.
98 ;;; When applicable, the service type defines how it can itself be extended,
99 ;;; by providing one procedure to compose extensions, and one procedure to
102 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
103 ;;; instance, which is the root of the service DAG. Its value is the
104 ;;; derivation that produces the 'system' directory as returned by
105 ;;; 'operating-system-derivation'.
107 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
108 ;;; "folds" by propagating extensions down the graph; it returns the root
109 ;;; service after the applying all its extensions.
113 (define-record-type <service-extension>
114 (service-extension target compute)
116 (target service-extension-target) ;<service-type>
117 (compute service-extension-compute)) ;params -> params
119 (define-record-type* <service-type> service-type make-service-type
121 (name service-type-name) ;symbol (for debugging)
123 ;; Things extended by services of this type.
124 (extensions service-type-extensions) ;list of <service-extensions>
126 ;; Given a list of extensions, "compose" them.
127 (compose service-type-compose ;list of Any -> Any
130 ;; Extend the services' own parameters with the extension composition.
131 (extend service-type-extend ;list of Any -> parameters
134 (define (write-service-type type port)
135 (format port "#<service-type ~a ~a>"
136 (service-type-name type)
137 (number->string (object-address type) 16)))
139 (set-record-type-printer! <service-type> write-service-type)
141 ;; Services of a given type.
142 (define-record-type <service>
143 (service type parameters)
146 (parameters service-parameters))
148 (define (simple-service name target value)
149 "Return a service that extends TARGET with VALUE. This works by creating a
150 singleton service type NAME, of which the returned service is an instance."
151 (let* ((extension (service-extension target identity))
152 (type (service-type (name name)
153 (extensions (list extension)))))
154 (service type value)))
156 (define-syntax %modify-service
160 ((_ svc (kind param => exp ...) clauses ...)
161 (if (eq? (service-kind svc) kind)
162 (let ((param (service-parameters svc)))
163 (service (service-kind svc)
165 (%modify-service svc clauses ...)))))
167 (define-syntax modify-services
169 "Modify the services listed in SERVICES according to CLAUSES and return
170 the resulting list of services. Each clause must have the form:
172 (TYPE VARIABLE => BODY)
174 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
175 identifier that is bound within BODY to the value of the service of that
176 TYPE. Consider this example:
178 (modify-services %base-services
179 (guix-service-type config =>
182 (use-substitutes? #f)
183 (extra-options '(\"--gc-keep-derivations\"))))
184 (mingetty-service-type config =>
185 (mingetty-configuration
187 (motd (plain-file \"motd\" \"Hi there!\")))))
189 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
190 all the MINGETTY-SERVICE-TYPE instances.
192 This is a shorthand for (map (lambda (svc) ...) %base-services)."
193 ((_ services clauses ...)
194 (map (lambda (service)
195 (%modify-service service clauses ...))
203 (define (system-derivation mentries mextensions)
204 "Return as a monadic value the derivation of the 'system' directory
205 containing the given entries."
206 (mlet %store-monad ((entries mentries)
207 (extensions (sequence %store-monad mextensions)))
210 (append entries (concatenate extensions))))))
212 (define system-service-type
213 ;; This is the ultimate service type, the root of the service DAG. The
214 ;; service of this type is extended by monadic name/item pairs. These items
215 ;; end up in the "system directory" as returned by
216 ;; 'operating-system-derivation'.
217 (service-type (name 'system)
220 (extend system-derivation)))
222 (define (compute-boot-script _ mexps)
223 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
225 ;; Clean up and activate the system, then spawn shepherd.
226 #~(begin #$@gexps))))
228 (define (boot-script-entry mboot)
229 "Return, as a monadic value, an entry for the boot script in the system
231 (mlet %store-monad ((boot mboot))
232 (return `(("boot" ,boot)))))
234 (define boot-service-type
235 ;; The service of this type is extended by being passed gexps as monadic
236 ;; values. It aggregates them in a single script, as a monadic value, which
237 ;; becomes its 'parameters'. It is the only service that extends nothing.
238 (service-type (name 'boot)
240 (list (service-extension system-service-type
243 (extend compute-boot-script)))
245 (define %boot-service
246 ;; The service that produces the boot script.
247 (service boot-service-type #t))
249 (define (cleanup-gexp _)
250 "Return as a monadic value a gexp to clean up /tmp and similar places upon
252 (with-monad %store-monad
253 (with-imported-modules '((guix build utils))
255 (use-modules (guix build utils))
257 ;; Clean out /tmp and /var/run.
259 ;; XXX This needs to happen before service activations, so it
260 ;; has to be here, but this also implicitly assumes that /tmp
261 ;; and /var/run are on the root partition.
262 (letrec-syntax ((fail-safe (syntax-rules ()
268 (fail-safe rest ...)))
271 ;; Ignore I/O errors so the system can boot.
273 (delete-file-recursively "/tmp")
274 (delete-file-recursively "/var/run")
276 (chmod "/tmp" #o1777)
278 (chmod "/var/run" #o755))))))))
280 (define cleanup-service-type
281 ;; Service that cleans things up in /tmp and similar.
282 (service-type (name 'cleanup)
284 (list (service-extension boot-service-type
287 (define* (file-union name files) ;FIXME: Factorize.
288 "Return a <computed-file> that builds a directory containing all of FILES.
289 Each item in FILES must be a list where the first element is the file name to
290 use in the new directory, and the second element is a gexp denoting the target
296 #$@(map (match-lambda
299 ;; Stat the source to abort early if it
303 (symlink #$source #$target))))
306 (define (directory-union name things)
307 "Return a directory that is the union of THINGS."
310 ;; Only one thing; return it.
314 (with-imported-modules '((guix build union))
316 (use-modules (guix build union))
317 (union-build #$output '#$things)))))))
319 (define* (activation-service->script service)
320 "Return as a monadic value the activation script for SERVICE, a service of
321 ACTIVATION-SCRIPT-TYPE."
322 (activation-script (service-parameters service)))
324 (define (activation-script gexps)
325 "Return the system's activation script, which evaluates GEXPS."
326 (define (service-activations)
327 ;; Return the activation scripts for SERVICES.
329 (cut gexp->file "activate-service" <>)
332 (mlet* %store-monad ((actions (service-activations)))
333 (gexp->file "activate"
334 (with-imported-modules (source-module-closure
335 '((gnu build activation)))
337 (use-modules (gnu build activation))
339 ;; Make sure /bin/sh is valid and current.
341 (string-append #$(canonical-package bash) "/bin/sh"))
343 ;; Make sure the user accounting database exists. If it
344 ;; does not exist, 'setutxent' does not create it and
345 ;; thus there is no accounting at all.
346 (close-port (open-file "/var/run/utmpx" "a0"))
348 ;; Same for 'wtmp', which is populated by mingetty et
350 (close-port (open-file "/var/log/wtmp" "a0"))
352 ;; Set up /run/current-system. Among other things this
353 ;; sets up locales, which the activation snippets
354 ;; executed below may expect.
355 (activate-current-system)
357 ;; Run the services' activation snippets.
358 ;; TODO: Use 'load-compiled'.
359 (for-each primitive-load '#$actions))))))
361 (define (gexps->activation-gexp gexps)
362 "Return a gexp that runs the activation script containing GEXPS."
363 (mlet %store-monad ((script (activation-script gexps)))
364 (return #~(primitive-load #$script))))
366 (define (second-argument a b) b)
368 (define activation-service-type
369 (service-type (name 'activate)
371 (list (service-extension boot-service-type
372 gexps->activation-gexp)))
374 (extend second-argument)))
376 (define %activation-service
377 ;; The activation service produces the activation script from the gexps it
379 (service activation-service-type #t))
381 (define %modprobe-wrapper
382 ;; Wrapper for the 'modprobe' command that knows where modules live.
384 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
385 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
386 ;; environment variable is not set---hence the need for this wrapper.
387 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
388 (program-file "modprobe"
390 (setenv "LINUX_MODULE_DIRECTORY"
391 "/run/booted-system/kernel/lib/modules")
392 (apply execl #$modprobe
393 (cons #$modprobe (cdr (command-line))))))))
395 (define %linux-kernel-activation
396 ;; Activation of the Linux kernel running on the bare metal (as opposed to
397 ;; running in a container.)
399 ;; Tell the kernel to use our 'modprobe' command.
400 (activate-modprobe #$%modprobe-wrapper)
402 ;; Let users debug their own processes!
403 (activate-ptrace-attach)))
405 (define linux-bare-metal-service-type
406 (service-type (name 'linux-bare-metal)
408 (list (service-extension activation-service-type
409 (const %linux-kernel-activation))))))
411 (define %linux-bare-metal-service
412 ;; The service that does things that are needed on the "bare metal", but not
413 ;; necessary or impossible in a container.
414 (service linux-bare-metal-service-type #f))
416 (define (etc-directory service)
417 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
418 (files->etc-directory (service-parameters service)))
420 (define (files->etc-directory files)
421 (file-union "etc" files))
423 (define (etc-entry files)
424 "Return an entry for the /etc directory consisting of FILES in the system
426 (with-monad %store-monad
427 (return `(("etc" ,(files->etc-directory files))))))
429 (define etc-service-type
430 (service-type (name 'etc)
433 (service-extension activation-service-type
436 (files->etc-directory files)))
437 #~(activate-etc #$etc))))
438 (service-extension system-service-type etc-entry)))
439 (compose concatenate)
442 (define (etc-service files)
443 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
444 FILES must be a list of name/file-like object pairs."
445 (service etc-service-type files))
447 (define setuid-program-service-type
448 (service-type (name 'setuid-program)
450 (list (service-extension activation-service-type
452 #~(activate-setuid-programs
453 (list #$@programs))))))
454 (compose concatenate)
457 (define (packages->profile-entry packages)
458 "Return a system entry for the profile containing PACKAGES."
459 (mlet %store-monad ((profile (profile-derivation
461 (delete-duplicates packages eq?)))))
462 (return `(("profile" ,profile)))))
464 (define profile-service-type
465 ;; The service that populates the system's profile---i.e.,
466 ;; /run/current-system/profile. It is extended by package lists.
467 (service-type (name 'profile)
469 (list (service-extension system-service-type
470 packages->profile-entry)))
471 (compose concatenate)
474 (define (firmware->activation-gexp firmware)
475 "Return a gexp to make the packages listed in FIRMWARE loadable by the
477 (let ((directory (directory-union "firmware" firmware)))
478 ;; Tell the kernel where firmware is.
479 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
481 (define firmware-service-type
482 ;; The service that collects firmware.
483 (service-type (name 'firmware)
485 (list (service-extension activation-service-type
486 firmware->activation-gexp)))
487 (compose concatenate)
490 (define (gc-roots->system-entry roots)
491 "Return an entry in the system's output containing symlinks to ROOTS."
492 (mlet %store-monad ((entry (gexp->derivation
494 #~(let ((roots '#$roots))
500 (iota (length roots))))))))
501 (return (if (null? roots)
503 `(("gc-roots" ,entry))))))
505 (define gc-root-service-type
506 ;; A service to associate extra garbage-collector roots to the system. This
507 ;; is a simple hack that guarantees that the system retains references to
508 ;; the given list of roots. Roots must be "lowerable" objects like
509 ;; packages, or derivations.
510 (service-type (name 'gc-roots)
512 (list (service-extension system-service-type
513 gc-roots->system-entry)))
514 (compose concatenate)
522 (define-condition-type &service-error &error
525 (define-condition-type &missing-target-service-error &service-error
526 missing-target-service-error?
527 (service missing-target-service-error-service)
528 (target-type missing-target-service-error-target-type))
530 (define-condition-type &ambiguous-target-service-error &service-error
531 ambiguous-target-service-error?
532 (service ambiguous-target-service-error-service)
533 (target-type ambiguous-target-service-error-target-type))
535 (define (service-back-edges services)
536 "Return a procedure that, when passed a <service>, returns the list of
537 <service> objects that depend on it."
538 (define (add-edges service edges)
539 (define (add-edge extension edges)
540 (let ((target-type (service-extension-target extension)))
541 (match (filter (lambda (service)
542 (eq? (service-kind service) target-type))
545 (vhash-consq target service edges))
548 (condition (&missing-target-service-error
550 (target-type target-type))
553 (format #f (_ "no target of type '~a' for service ~s")
554 (service-type-name target-type)
558 (condition (&ambiguous-target-service-error
560 (target-type target-type))
564 (_ "more than one target service of type '~a'")
565 (service-type-name target-type))))))))))
567 (fold add-edge edges (service-type-extensions (service-kind service))))
569 (let ((edges (fold add-edges vlist-null services)))
571 (reverse (vhash-foldq* cons '() node edges)))))
573 (define* (fold-services services
574 #:key (target-type system-service-type))
575 "Fold SERVICES by propagating their extensions down to the root of type
576 TARGET-TYPE; return the root service adjusted accordingly."
578 (service-back-edges services))
580 (define (matching-extension target)
581 (let ((target (service-kind target)))
583 (($ <service-extension> type)
584 (eq? type target)))))
586 (define (apply-extension target)
588 (match (find (matching-extension target)
589 (service-type-extensions (service-kind service)))
590 (($ <service-extension> _ compute)
591 (compute (service-parameters service))))))
593 (match (filter (lambda (service)
594 (eq? (service-kind service) target-type))
597 (let loop ((sink sink))
598 (let* ((dependents (map loop (dependents sink)))
599 (extensions (map (apply-extension sink) dependents))
600 (extend (service-type-extend (service-kind sink)))
601 (compose (service-type-compose (service-kind sink)))
602 (params (service-parameters sink)))
603 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
604 ;; different type than the elements of EXTENSIONS.
606 (service (service-kind sink)
607 (extend params (compose extensions)))
611 (condition (&missing-target-service-error
613 (target-type target-type))
615 (message (format #f (_ "service of type '~a' not found")
616 (service-type-name target-type)))))))
619 (condition (&ambiguous-target-service-error
621 (target-type target-type))
625 (_ "more than one target service of type '~a'")
626 (service-type-name target-type)))))))))
628 ;;; services.scm ends here.