aboutsummaryrefslogtreecommitdiff
path: root/gnu/services.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2015-09-17 23:44:26 +0200
committerLudovic Courtès <ludo@gnu.org>2015-10-10 22:55:15 +0200
commit0adfe95a3eee335847c3127edde3de550e692440 (patch)
tree1c5a059d8f261f09254c0e420e61e1344c9edb45 /gnu/services.scm
parente79467f63a06811ba5dd8c8b0cc79553c5dd4e3a (diff)
downloadpatches-0adfe95a3eee335847c3127edde3de550e692440.tar
patches-0adfe95a3eee335847c3127edde3de550e692440.tar.gz
services: Introduce extensible services.
This patch rewrites GuixSD services to make them extensible. * gnu-system.am (GNU_SYSTEM_MODULES): Add gnu/services/dbus.scm. * gnu/services.scm (<service>): Replace with new record type. (<service-extension>, <service-type>): New record types. (write-service-type, compute-boot-script, second-argument): New procedures. (%boot-service, boot-service-type): New variables. (file-union, directory-union, modprobe-wrapper, activation-service->script, activation-script, gexps->activation-gexp): New procedures. (activation-service-type, %activation-service): New variables. (etc-directory, files->etc-directory, etc-service): New procedures. (etc-service-type, setuid-program-service, firmware-service-type): New variables. (firmware->activation-gexp): New procedure. (&service-error, &missing-target-service-error, &ambiguous-target-service-error): New condition types. (service-back-edges, fold-services): New procedures. * gnu/services/avahi.scm (<avahi-configuration>): New record type. (configuration-file): Replace keyword parameters with a single 'config' parameter. (%avahi-accounts, %avahi-activation, avahi-service-type): New variables. (avahi-dmd-service): New procedure. (avahi-service): Rewrite using 'service' and 'avahi-configuration'. * gnu/services/base.scm (%root-file-system-dmd-service, root-file-system-service-type): New variables. (root-file-system-service): Use them. (file-system->dmd-service-name): New procedure. (file-system-service-type): New variable. (file-system-service): Use it. Replace keyword parameters with a single 'file-system' object. (user-unmount-service-type): New variable. (user-unmount-service): Use it. (user-processes-service-type): New variable. (user-processes-service): Use it. (host-name-service-type): New variable. (host-name-service): Use it. (console-keymap-service-type): New variable. (console-keymap-service): Use it. (console-font-service-type): New variable. (console-font-service): Use it. (mingetty-pam-service, mingetty-dmd-service): New procedures. (mingetty-service-type): New variable. (mingetty-service): Use it. (nscd-dmd-service): New procedure. (nscd-activation, nscd-service-type): New variables. (nscd-service): Use the latter. (syslog-service-type): New variable. (syslog-service): Use it. (<guix-configuration>): New record type. (%default-guix-configuration): New variable. (guix-dmd-service, guix-accounts, guix-activation): New procedures. (guix-service-type): New variable. (guix-service): Replace list of keyword parameters with a single 'config' parameter. Rewrite using 'service'. (<udev-configuration>): New record type. (udev-dmd-service): New procedure. (udev-service-type): New variable. (udev-service): Use it. (device-mapping-service-type): New variable. (device-mapping-service): Use it. (swap-service-type): New variable. (swap-service): Use it. * gnu/services/databases.scm (<postgresql-configuration>): New record type. (%postgresql-accounts, postgresql-activation): New variables. (postgresql-dmd-service): New procedure. (postgresql-service): Rewrite using 'service' and 'postgresql-configuration'. * gnu/services/dbus.scm: New file. * gnu/services/desktop.scm (dbus-configuration-directory, dbus-service): Remove. (wrapped-dbus-service): New procedure. (<upower-configuration>): New record type. (upower-configuration-file): Replace keyword parameters with single <upower-configuration> parameter. (%upower-accounts, %upower-activation): New variables. (upower-dbus-service, upower-dmd-service): New procedures. (upower-service-type): New variable. (upower-service): Rewrite using 'service' and 'upower-configuration'. (%colord-activation, %colord-accounts): New variables. (colord-dmd-service): New procedure. (colord-service-type): New variable. (colord-service): Rewrite using 'service'. (<geoclue-configuration>): New record type. (geoclue-configuration-file): Replace keyword parameters with a single 'config' parameter. (geoclue-dbus-service, geoclue-dmd-service): New procedures. (%geoclue-accounts, geoclue-service-type): New variables. (geoclue-service): Rewrite using 'service' and 'geoclue-configuration'. (%polkit-accounts, %polkit-pam-services, polkit-service-type): New variables. (polkit-dmd-service): New procedure. (polkit-service): Rewrite using 'service'. (<elogind-configuration>)[elogind]: New field. (elogind-dmd-service): New procedure. (elogind-service-type): New variable. (elogind-service): Rewrite using 'service'. (%desktop-services): Remove argument to 'dbus-service'. Remove 'map' over %BASE-SERVICES. * gnu/services/dmd.scm (dmd-boot-gexp): New procedure. (dmd-root-service-type, %dmd-root-service): New variables. (dmd-service-type): New macro. (<dmd-service>): New record type. * gnu/services/lirc.scm (<lirc-configuration>): New record type. (%lirc-activation): New variable. (lirc-dmd-service): New procedure. (lirc-service-type): New variable. (lirc-service): Rewrite using 'service' and 'lirc-configuration'. * gnu/services/networking.scm (<static-networking>): New record type. (static-networking-service-type): New variable. (static-networking-service): Rewrite using 'service' and 'static-networking'. (dhcp-client-service-type): New variable. (dhcp-client-service): Rewrite using 'service'. (<ntp-configuration>): New record type. (ntp-dmd-service): New procedure. (ntp-service-type): New variable. (ntp-service): New procedure. (%tor-accounts, tor-service-type): New variable. (tor-dmd-service): New procedure. (tor-service): Rewrite using 'service'. (<bitlbee-configuration>): New record type. (bitlbee-dmd-service): New procedure. (%bitlbee-accounts, %bitlbee-activation, bitlbee-service-type): New variables. (bitlbee-service): Rewrite using 'service'. (%wicd-activation): New variable. (wicd-dmd-service): New procedure. (wicd-service-type): New variable. (wicd-service): Rewrite using 'service'. * gnu/services/ssh.scm (<lsh-configuration>): New record type. (activation): Rename to... (lsh-initialization): ... this. (lsh-activation, lsh-dmd-service, lsh-pam-services): New procedures. (lsh-service-type): New variable. (lsh-service): Rewrite using 'service' and 'lsh-configuration'. * gnu/services/web.scm (<nginx-configuration>): New record type. (%nginx-accounts): New variable. (nginx-activation, nginx-dmd-service): New procedures. (nginx-service-type): New variable. (nginx-service): Rewrite using 'service' and 'nginx-configuration'. * gnu/services/xorg.scm (<slim-configuration>): New record type. (slim-pam-service, slim-dmd-service): New procedures. (slim-service-type): New variable. (slim-service): Rewrite using 'service' and 'slim-configuration'. * gnu/system.scm (file-union): Remove. (other-file-system-services): Adjust to new 'file-system-service' signature. (essential-services): Add #:container? parameter. Add %DMD-ROOT-SERVICE, %ACTIVATION-SERVICE, and calls to 'pam-root-service', 'account-service', 'operating-system-etc-service', and a SETUID-PROGRAM-SERVICE instance. (operating-system-services): Pass #:container? to 'essential-services. (etc-directory): Remove. (operating-system-etc-service): New procedure. Rewrite as a call to 'etc-service'. (operating-system-accounts): Change to not return accounts required by services. (operating-system-etc-directory): Rewrite as a call to 'fold-services' and 'etc-directory'. (user-group->gexp, user-account->gexp, modprobe-wrapper): Remove. (operating-system-activation-script): Rewrite as a call to 'fold-services' and 'activation-service->script'. (operating-system-boot-script): Likewise. (operating-system-derivation): Add call to 'lower-object'. (emacs-site-file, emacs-site-directory, shells-file): Change to use 'computed-file' and 'scheme-file' instead of the monadic procedures. * gnu/system/install.scm (cow-store-service-type): New variable. (cow-store-service): Rewrite using 'service'. (/etc/configuration-files): New procedure. (configuration-template-service-type, %configuration-template-service): New variables. (configuration-template-service): Remove. (installation-services): Adjust accordingly. Adjust argument to 'guix-service'. * gnu/system/linux.scm (/etc-entry, pam-root-service): New procedures. (pam-root-service-type): New variable. * gnu/system/shadow.scm (user-group->gexp, user-account->gexp, account-activation, etc-skel, account-service): New procedures. (account-service-type): New variable. * tests/services.scm: New file. * doc/guix.texi (Base Services, Desktop Services): Adjust accordingly. (Defining Services): Rewrite. * doc/images/service-graph.dot: New file. * doc.am (DOT_FILES): Add it. * po/guix/POTFILES.in: Add gnu/services.scm.
Diffstat (limited to 'gnu/services.scm')
-rw-r--r--gnu/services.scm455
1 files changed, 417 insertions, 38 deletions
diff --git a/gnu/services.scm b/gnu/services.scm
index 43e51b998c..fdfa569b23 100644
--- a/gnu/services.scm
+++ b/gnu/services.scm
@@ -1,5 +1,5 @@
;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2015 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -18,49 +18,428 @@
(define-module (gnu services)
#:use-module (guix gexp)
+ #:use-module (guix monads)
+ #:use-module (guix store)
#:use-module (guix records)
- #:export (service?
+ #:use-module (guix sets)
+ #:use-module (guix ui)
+ #:use-module (gnu packages base)
+ #:use-module (gnu packages bash)
+ #:use-module (srfi srfi-1)
+ #:use-module (srfi srfi-9)
+ #:use-module (srfi srfi-9 gnu)
+ #:use-module (srfi srfi-26)
+ #:use-module (srfi srfi-34)
+ #:use-module (srfi srfi-35)
+ #:use-module (ice-9 vlist)
+ #:use-module (ice-9 match)
+ #:export (service-extension
+ service-extension?
+
+ service-type
+ service-type?
+
service
- service-documentation
- service-provision
- service-requirement
- service-respawn?
- service-start
- service-stop
- service-auto-start?
- service-activate
- service-user-accounts
- service-user-groups
- service-pam-services))
-
-;;; Commentary:
+ service?
+ service-kind
+ service-parameters
+
+ fold-services
+
+ service-error?
+ missing-target-service-error?
+ missing-target-service-error-service
+ missing-target-service-error-target-type
+ ambiguous-target-service-error?
+ ambiguous-target-service-error-service
+ ambiguous-target-service-error-target-type
+
+ boot-service-type
+ activation-service-type
+ activation-service->script
+ etc-service-type
+ etc-directory
+ setuid-program-service-type
+ firmware-service-type
+
+ %boot-service
+ %activation-service
+ etc-service
+
+ file-union)) ;XXX: for lack of a better place
+
+;;; Comment:
+;;;
+;;; This module defines a broad notion of "service types" and "services."
;;;
-;;; System services as cajoled by dmd.
+;;; A service type describe how its instances extend instances of other
+;;; service types. For instance, some services extend the instance of
+;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
+;;; others extend DMD-ROOT-SERVICE-TYPE by passing it instances of
+;;; <dmd-service>.
+;;;
+;;; When applicable, the service type defines how it can itself be extended,
+;;; by providing one procedure to compose extensions, and one procedure to
+;;; extend itself.
+;;;
+;;; A notable service type is BOOT-SERVICE-TYPE, which has a single instance,
+;;; %BOOT-SERVICE. %BOOT-SERVICE constitutes the root of the service DAG. It
+;;; produces the boot script that the initrd loads.
+;;;
+;;; The 'fold-services' procedure can be passed a list of procedures, which it
+;;; "folds" by propagating extensions down the graph; it returns the root
+;;; service after the applying all its extensions.
;;;
;;; Code:
-(define-record-type* <service>
- service make-service
+(define-record-type <service-extension>
+ (service-extension target compute)
+ service-extension?
+ (target service-extension-target) ;<service-type>
+ (compute service-extension-compute)) ;params -> params
+
+(define-record-type* <service-type> service-type make-service-type
+ service-type?
+ (name service-type-name) ;symbol (for debugging)
+
+ ;; Things extended by services of this type.
+ (extensions service-type-extensions) ;list of <service-extensions>
+
+ ;; Given a list of extensions, "compose" them.
+ (compose service-type-compose ;list of Any -> Any
+ (default #f))
+
+ ;; Extend the services' own parameters with the extension composition.
+ (extend service-type-extend ;list of Any -> parameters
+ (default #f)))
+
+(define (write-service-type type port)
+ (format port "#<service-type ~a ~a>"
+ (service-type-name type)
+ (number->string (object-address type) 16)))
+
+(set-record-type-printer! <service-type> write-service-type)
+
+;; Services of a given type.
+(define-record-type <service>
+ (service type parameters)
service?
- (documentation service-documentation ; string
- (default "[No documentation.]"))
- (provision service-provision) ; list of symbols
- (requirement service-requirement ; list of symbols
- (default '()))
- (respawn? service-respawn? ; Boolean
- (default #t))
- (start service-start) ; g-expression (procedure)
- (stop service-stop ; g-expression (procedure)
- (default #~(const #f)))
- (auto-start? service-auto-start? ; Boolean
- (default #t))
- (user-accounts service-user-accounts ; list of <user-account>
- (default '()))
- (user-groups service-user-groups ; list of <user-groups>
- (default '()))
- (pam-services service-pam-services ; list of <pam-service>
- (default '()))
- (activate service-activate ; gexp
- (default #f)))
+ (type service-kind)
+ (parameters service-parameters))
+
+
+
+
+;;;
+;;; Core services.
+;;;
+
+(define (compute-boot-script mexps)
+ (mlet %store-monad ((gexps (sequence %store-monad mexps)))
+ (gexp->file "boot"
+ #~(begin
+ (use-modules (guix build utils))
+
+ ;; Clean out /tmp and /var/run.
+ ;;
+ ;; XXX This needs to happen before service activations, so
+ ;; it has to be here, but this also implicitly assumes
+ ;; that /tmp and /var/run are on the root partition.
+ (false-if-exception (delete-file-recursively "/tmp"))
+ (false-if-exception (delete-file-recursively "/var/run"))
+ (false-if-exception (mkdir "/tmp"))
+ (false-if-exception (chmod "/tmp" #o1777))
+ (false-if-exception (mkdir "/var/run"))
+ (false-if-exception (chmod "/var/run" #o755))
+
+ ;; Activate the system and spawn dmd.
+ #$@gexps))))
+
+(define (second-argument a b) b)
+
+(define boot-service-type
+ ;; The service of this type is extended by being passed gexps as monadic
+ ;; values. It aggregates them in a single script, as a monadic value, which
+ ;; becomes its 'parameters'. It is the only service that extends nothing.
+ (service-type (name 'boot)
+ (extensions '())
+ (compose compute-boot-script)
+ (extend second-argument)))
+
+(define %boot-service
+ ;; This is the ultimate service, the root of the service DAG.
+ (service boot-service-type #t))
+
+(define* (file-union name files) ;FIXME: Factorize.
+ "Return a <computed-file> that builds a directory containing all of FILES.
+Each item in FILES must be a list where the first element is the file name to
+use in the new directory, and the second element is a gexp denoting the target
+file."
+ (computed-file name
+ #~(begin
+ (mkdir #$output)
+ (chdir #$output)
+ #$@(map (match-lambda
+ ((target source)
+ #~(symlink #$source #$target)))
+ files))))
+
+(define (directory-union name things)
+ "Return a directory that is the union of THINGS."
+ (match things
+ ((one)
+ ;; Only one thing; return it.
+ one)
+ (_
+ (computed-file name
+ #~(begin
+ (use-modules (guix build union))
+ (union-build #$output '#$things))
+ #:modules '((guix build union))))))
+
+(define (modprobe-wrapper)
+ "Return a wrapper for the 'modprobe' command that knows where modules live.
+
+This wrapper is typically invoked by the Linux kernel ('call_modprobe', in
+kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' environment
+variable is not set---hence the need for this wrapper."
+ (let ((modprobe "/run/current-system/profile/bin/modprobe"))
+ (gexp->script "modprobe"
+ #~(begin
+ (setenv "LINUX_MODULE_DIRECTORY"
+ "/run/booted-system/kernel/lib/modules")
+ (apply execl #$modprobe
+ (cons #$modprobe (cdr (command-line))))))))
+
+(define* (activation-service->script service)
+ "Return as a monadic value the activation script for SERVICE, a service of
+ACTIVATION-SCRIPT-TYPE."
+ (activation-script (service-parameters service)))
+
+(define (activation-script gexps)
+ "Return the system's activation script, which evaluates GEXPS."
+ (define %modules
+ '((gnu build activation)
+ (gnu build linux-boot)
+ (gnu build linux-modules)
+ (gnu build file-systems)
+ (guix build utils)
+ (guix build syscalls)
+ (guix elf)))
+
+ (define (service-activations)
+ ;; Return the activation scripts for SERVICES.
+ (mapm %store-monad
+ (cut gexp->file "activate-service" <>)
+ gexps))
+
+ (mlet* %store-monad ((actions (service-activations))
+ (modules (imported-modules %modules))
+ (compiled (compiled-modules %modules))
+ (modprobe (modprobe-wrapper)))
+ (gexp->file "activate"
+ #~(begin
+ (eval-when (expand load eval)
+ ;; Make sure 'use-modules' below succeeds.
+ (set! %load-path (cons #$modules %load-path))
+ (set! %load-compiled-path
+ (cons #$compiled %load-compiled-path)))
+
+ (use-modules (gnu build activation))
+
+ ;; Make sure /bin/sh is valid and current.
+ (activate-/bin/sh
+ (string-append #$(canonical-package bash) "/bin/sh"))
+
+ ;; Tell the kernel to use our 'modprobe' command.
+ (activate-modprobe #$modprobe)
+
+ ;; Let users debug their own processes!
+ (activate-ptrace-attach)
+
+ ;; Run the services' activation snippets.
+ ;; TODO: Use 'load-compiled'.
+ (for-each primitive-load '#$actions)
+
+ ;; Set up /run/current-system.
+ (activate-current-system)))))
+
+(define (gexps->activation-gexp gexps)
+ "Return a gexp that runs the activation script containing GEXPS."
+ (mlet %store-monad ((script (activation-script gexps)))
+ (return #~(primitive-load #$script))))
+
+(define activation-service-type
+ (service-type (name 'activate)
+ (extensions
+ (list (service-extension boot-service-type
+ gexps->activation-gexp)))
+ (compose append)
+ (extend second-argument)))
+
+(define %activation-service
+ ;; The activation service produces the activation script from the gexps it
+ ;; receives.
+ (service activation-service-type #t))
+
+(define (etc-directory service)
+ "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
+ (files->etc-directory (service-parameters service)))
+
+(define (files->etc-directory files)
+ (file-union "etc" files))
+
+(define etc-service-type
+ (service-type (name 'etc)
+ (extensions
+ (list
+ (service-extension activation-service-type
+ (lambda (files)
+ (let ((etc
+ (files->etc-directory files)))
+ #~(activate-etc #$etc))))))
+ (compose concatenate)
+ (extend append)))
+
+(define (etc-service files)
+ "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
+FILES must be a list of name/file-like object pairs."
+ (service etc-service-type files))
+
+(define setuid-program-service-type
+ (service-type (name 'setuid-program)
+ (extensions
+ (list (service-extension activation-service-type
+ (lambda (programs)
+ #~(activate-setuid-programs
+ (list #$@programs))))))
+ (compose concatenate)
+ (extend append)))
+
+(define (firmware->activation-gexp firmware)
+ "Return a gexp to make the packages listed in FIRMWARE loadable by the
+kernel."
+ (let ((directory (directory-union "firmware" firmware)))
+ ;; Tell the kernel where firmware is.
+ #~(activate-firmware (string-append #$directory "/lib/firmware"))))
+
+(define firmware-service-type
+ ;; The service that collects firmware.
+ (service-type (name 'firmware)
+ (extensions
+ (list (service-extension activation-service-type
+ firmware->activation-gexp)))
+ (compose concatenate)
+ (extend append)))
+
+
+;;;
+;;; Service folding.
+;;;
+
+(define-condition-type &service-error &error
+ service-error?)
+
+(define-condition-type &missing-target-service-error &service-error
+ missing-target-service-error?
+ (service missing-target-service-error-service)
+ (target-type missing-target-service-error-target-type))
+
+(define-condition-type &ambiguous-target-service-error &service-error
+ ambiguous-target-service-error?
+ (service ambiguous-target-service-error-service)
+ (target-type ambiguous-target-service-error-target-type))
+
+(define (service-back-edges services)
+ "Return a procedure that, when passed a <service>, returns the list of
+<service> objects that depend on it."
+ (define (add-edges service edges)
+ (define (add-edge extension edges)
+ (let ((target-type (service-extension-target extension)))
+ (match (filter (lambda (service)
+ (eq? (service-kind service) target-type))
+ services)
+ ((target)
+ (vhash-consq target service edges))
+ (()
+ (raise
+ (condition (&missing-target-service-error
+ (service service)
+ (target-type target-type))
+ (&message
+ (message
+ (format #f (_ "no target of type '~a' for service ~s")
+ (service-type-name target-type)
+ service))))))
+ (x
+ (raise
+ (condition (&ambiguous-target-service-error
+ (service service)
+ (target-type target-type))
+ (&message
+ (message
+ (format #f
+ (_ "more than one target service of type '~a'")
+ (service-type-name target-type))))))))))
+
+ (fold add-edge edges (service-type-extensions (service-kind service))))
+
+ (let ((edges (fold add-edges vlist-null services)))
+ (lambda (node)
+ (reverse (vhash-foldq* cons '() node edges)))))
+
+(define* (fold-services services #:key (target-type boot-service-type))
+ "Fold SERVICES by propagating their extensions down to the root of type
+TARGET-TYPE; return the root service adjusted accordingly."
+ (define dependents
+ (service-back-edges services))
+
+ (define (matching-extension target)
+ (let ((target (service-kind target)))
+ (match-lambda
+ (($ <service-extension> type)
+ (eq? type target)))))
+
+ (define (apply-extension target)
+ (lambda (service)
+ (match (find (matching-extension target)
+ (service-type-extensions (service-kind service)))
+ (($ <service-extension> _ compute)
+ (compute (service-parameters service))))))
+
+ (match (filter (lambda (service)
+ (eq? (service-kind service) target-type))
+ services)
+ ((sink)
+ (let loop ((sink sink))
+ (let* ((dependents (map loop (dependents sink)))
+ (extensions (map (apply-extension sink) dependents))
+ (extend (service-type-extend (service-kind sink)))
+ (compose (service-type-compose (service-kind sink)))
+ (params (service-parameters sink)))
+ ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
+ ;; different type than the elements of EXTENSIONS.
+ (if extend
+ (service (service-kind sink)
+ (extend params (compose extensions)))
+ sink))))
+ (()
+ (raise
+ (condition (&missing-target-service-error
+ (service #f)
+ (target-type target-type))
+ (&message
+ (message (format #f (_ "service of type '~a' not found")
+ (service-type-name target-type)))))))
+ (x
+ (raise
+ (condition (&ambiguous-target-service-error
+ (service #f)
+ (target-type target-type))
+ (&message
+ (message
+ (format #f
+ (_ "more than one target service of type '~a'")
+ (service-type-name target-type)))))))))
;;; services.scm ends here.