gnu: lablgtk: Build sequentially.
[guix.git] / gnu / services.scm
blobc8a2a2604f8b791866157c09804199fdc9de64f0
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
19 (define-module (gnu services)
20   #:use-module (guix gexp)
21   #:use-module (guix monads)
22   #:use-module (guix store)
23   #:use-module (guix records)
24   #:use-module (guix sets)
25   #:use-module (guix ui)
26   #:use-module (gnu packages base)
27   #:use-module (gnu packages bash)
28   #:use-module (srfi srfi-1)
29   #:use-module (srfi srfi-9)
30   #:use-module (srfi srfi-9 gnu)
31   #:use-module (srfi srfi-26)
32   #:use-module (srfi srfi-34)
33   #:use-module (srfi srfi-35)
34   #:use-module (ice-9 vlist)
35   #:use-module (ice-9 match)
36   #:export (service-extension
37             service-extension?
39             service-type
40             service-type?
41             service-type-name
42             service-type-extensions
43             service-type-compose
44             service-type-extend
46             service
47             service?
48             service-kind
49             service-parameters
51             modify-services
52             service-back-edges
53             fold-services
55             service-error?
56             missing-target-service-error?
57             missing-target-service-error-service
58             missing-target-service-error-target-type
59             ambiguous-target-service-error?
60             ambiguous-target-service-error-service
61             ambiguous-target-service-error-target-type
63             boot-service-type
64             activation-service-type
65             activation-service->script
66             %linux-bare-metal-service
67             etc-service-type
68             etc-directory
69             setuid-program-service-type
70             firmware-service-type
72             %boot-service
73             %activation-service
74             etc-service
76             file-union))                      ;XXX: for lack of a better place
78 ;;; Comment:
79 ;;;
80 ;;; This module defines a broad notion of "service types" and "services."
81 ;;;
82 ;;; A service type describe how its instances extend instances of other
83 ;;; service types.  For instance, some services extend the instance of
84 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
85 ;;; others extend DMD-ROOT-SERVICE-TYPE by passing it instances of
86 ;;; <dmd-service>.
87 ;;;
88 ;;; When applicable, the service type defines how it can itself be extended,
89 ;;; by providing one procedure to compose extensions, and one procedure to
90 ;;; extend itself.
91 ;;;
92 ;;; A notable service type is BOOT-SERVICE-TYPE, which has a single instance,
93 ;;; %BOOT-SERVICE.  %BOOT-SERVICE constitutes the root of the service DAG.  It
94 ;;; produces the boot script that the initrd loads.
95 ;;;
96 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
97 ;;; "folds" by propagating extensions down the graph; it returns the root
98 ;;; service after the applying all its extensions.
99 ;;;
100 ;;; Code:
102 (define-record-type <service-extension>
103   (service-extension target compute)
104   service-extension?
105   (target  service-extension-target)              ;<service-type>
106   (compute service-extension-compute))            ;params -> params
108 (define-record-type* <service-type> service-type make-service-type
109   service-type?
110   (name       service-type-name)                  ;symbol (for debugging)
112   ;; Things extended by services of this type.
113   (extensions service-type-extensions)            ;list of <service-extensions>
115   ;; Given a list of extensions, "compose" them.
116   (compose    service-type-compose                ;list of Any -> Any
117               (default #f))
119   ;; Extend the services' own parameters with the extension composition.
120   (extend     service-type-extend                 ;list of Any -> parameters
121               (default #f)))
123 (define (write-service-type type port)
124   (format port "#<service-type ~a ~a>"
125           (service-type-name type)
126           (number->string (object-address type) 16)))
128 (set-record-type-printer! <service-type> write-service-type)
130 ;; Services of a given type.
131 (define-record-type <service>
132   (service type parameters)
133   service?
134   (type       service-kind)
135   (parameters service-parameters))
138 (define-syntax %modify-service
139   (syntax-rules (=>)
140     ((_ service)
141      service)
142     ((_ svc (kind param => exp ...) clauses ...)
143      (if (eq? (service-kind svc) kind)
144          (let ((param (service-parameters svc)))
145            (service (service-kind svc)
146                     (begin exp ...)))
147          (%modify-service svc clauses ...)))))
149 (define-syntax modify-services
150   (syntax-rules ()
151     "Modify the services listed in SERVICES according to CLAUSES.  Each clause
152 must have the form:
154   (TYPE VARIABLE => BODY)
156 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
157 identifier that is bound within BODY to the value of the service of that
158 TYPE.  Consider this example:
160   (modify-services %base-services
161     (guix-service-type config =>
162                        (guix-configuration
163                         (inherit config)
164                         (use-substitutes? #f)
165                         (extra-options '(\"--gc-keep-derivations\"))))
166     (mingetty-service-type config =>
167                            (mingetty-configuration
168                             (inherit config)
169                             (motd (plain-file \"motd\" \"Hi there!\")))))
171 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
172 all the MINGETTY-SERVICE-TYPE instances.
174 This is a shorthand for (map (lambda (svc) ...) %base-services)."
175     ((_ services clauses ...)
176      (map (lambda (service)
177             (%modify-service service clauses ...))
178           services))))
182 ;;; Core services.
185 (define (compute-boot-script mexps)
186   (mlet %store-monad ((gexps (sequence %store-monad mexps)))
187     (gexp->file "boot"
188                 #~(begin
189                     (use-modules (guix build utils))
191                     ;; Clean out /tmp and /var/run.
192                     ;;
193                     ;; XXX This needs to happen before service activations, so
194                     ;; it has to be here, but this also implicitly assumes
195                     ;; that /tmp and /var/run are on the root partition.
196                     (false-if-exception (delete-file-recursively "/tmp"))
197                     (false-if-exception (delete-file-recursively "/var/run"))
198                     (false-if-exception (mkdir "/tmp"))
199                     (false-if-exception (chmod "/tmp" #o1777))
200                     (false-if-exception (mkdir "/var/run"))
201                     (false-if-exception (chmod "/var/run" #o755))
203                     ;; Activate the system and spawn dmd.
204                     #$@gexps))))
206 (define (second-argument a b) b)
208 (define boot-service-type
209   ;; The service of this type is extended by being passed gexps as monadic
210   ;; values.  It aggregates them in a single script, as a monadic value, which
211   ;; becomes its 'parameters'.  It is the only service that extends nothing.
212   (service-type (name 'boot)
213                 (extensions '())
214                 (compose compute-boot-script)
215                 (extend second-argument)))
217 (define %boot-service
218   ;; This is the ultimate service, the root of the service DAG.
219   (service boot-service-type #t))
221 (define* (file-union name files)                  ;FIXME: Factorize.
222   "Return a <computed-file> that builds a directory containing all of FILES.
223 Each item in FILES must be a list where the first element is the file name to
224 use in the new directory, and the second element is a gexp denoting the target
225 file."
226   (computed-file name
227                  #~(begin
228                      (mkdir #$output)
229                      (chdir #$output)
230                      #$@(map (match-lambda
231                                ((target source)
232                                 #~(symlink #$source #$target)))
233                              files))))
235 (define (directory-union name things)
236   "Return a directory that is the union of THINGS."
237   (match things
238     ((one)
239      ;; Only one thing; return it.
240      one)
241     (_
242      (computed-file name
243                     #~(begin
244                         (use-modules (guix build union))
245                         (union-build #$output '#$things))
246                     #:modules '((guix build union))))))
248 (define* (activation-service->script service)
249   "Return as a monadic value the activation script for SERVICE, a service of
250 ACTIVATION-SCRIPT-TYPE."
251   (activation-script (service-parameters service)))
253 (define (activation-script gexps)
254   "Return the system's activation script, which evaluates GEXPS."
255   (define %modules
256     '((gnu build activation)
257       (gnu build linux-boot)
258       (gnu build linux-modules)
259       (gnu build file-systems)
260       (guix build utils)
261       (guix build syscalls)
262       (guix elf)))
264   (define (service-activations)
265     ;; Return the activation scripts for SERVICES.
266     (mapm %store-monad
267           (cut gexp->file "activate-service" <>)
268           gexps))
270   (mlet* %store-monad ((actions  (service-activations))
271                        (modules  (imported-modules %modules))
272                        (compiled (compiled-modules %modules)))
273     (gexp->file "activate"
274                 #~(begin
275                     (eval-when (expand load eval)
276                       ;; Make sure 'use-modules' below succeeds.
277                       (set! %load-path (cons #$modules %load-path))
278                       (set! %load-compiled-path
279                         (cons #$compiled %load-compiled-path)))
281                     (use-modules (gnu build activation))
283                     ;; Make sure /bin/sh is valid and current.
284                     (activate-/bin/sh
285                      (string-append #$(canonical-package bash) "/bin/sh"))
287                     ;; Run the services' activation snippets.
288                     ;; TODO: Use 'load-compiled'.
289                     (for-each primitive-load '#$actions)
291                     ;; Set up /run/current-system.
292                     (activate-current-system)))))
294 (define (gexps->activation-gexp gexps)
295   "Return a gexp that runs the activation script containing GEXPS."
296   (mlet %store-monad ((script (activation-script gexps)))
297     (return #~(primitive-load #$script))))
299 (define activation-service-type
300   (service-type (name 'activate)
301                 (extensions
302                  (list (service-extension boot-service-type
303                                           gexps->activation-gexp)))
304                 (compose append)
305                 (extend second-argument)))
307 (define %activation-service
308   ;; The activation service produces the activation script from the gexps it
309   ;; receives.
310   (service activation-service-type #t))
312 (define %modprobe-wrapper
313   ;; Wrapper for the 'modprobe' command that knows where modules live.
314   ;;
315   ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
316   ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
317   ;; environment variable is not set---hence the need for this wrapper.
318   (let ((modprobe "/run/current-system/profile/bin/modprobe"))
319     (program-file "modprobe"
320                   #~(begin
321                       (setenv "LINUX_MODULE_DIRECTORY"
322                               "/run/booted-system/kernel/lib/modules")
323                       (apply execl #$modprobe
324                              (cons #$modprobe (cdr (command-line))))))))
326 (define %linux-kernel-activation
327   ;; Activation of the Linux kernel running on the bare metal (as opposed to
328   ;; running in a container.)
329   #~(begin
330       ;; Tell the kernel to use our 'modprobe' command.
331       (activate-modprobe #$%modprobe-wrapper)
333       ;; Let users debug their own processes!
334       (activate-ptrace-attach)))
336 (define linux-bare-metal-service-type
337   (service-type (name 'linux-bare-metal)
338                 (extensions
339                  (list (service-extension activation-service-type
340                                           (const %linux-kernel-activation))))))
342 (define %linux-bare-metal-service
343   ;; The service that does things that are needed on the "bare metal", but not
344   ;; necessary or impossible in a container.
345   (service linux-bare-metal-service-type #f))
347 (define (etc-directory service)
348   "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
349   (files->etc-directory (service-parameters service)))
351 (define (files->etc-directory files)
352   (file-union "etc" files))
354 (define etc-service-type
355   (service-type (name 'etc)
356                 (extensions
357                  (list
358                   (service-extension activation-service-type
359                                      (lambda (files)
360                                        (let ((etc
361                                               (files->etc-directory files)))
362                                          #~(activate-etc #$etc))))))
363                 (compose concatenate)
364                 (extend append)))
366 (define (etc-service files)
367   "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
368 FILES must be a list of name/file-like object pairs."
369   (service etc-service-type files))
371 (define setuid-program-service-type
372   (service-type (name 'setuid-program)
373                 (extensions
374                  (list (service-extension activation-service-type
375                                           (lambda (programs)
376                                             #~(activate-setuid-programs
377                                                (list #$@programs))))))
378                 (compose concatenate)
379                 (extend append)))
381 (define (firmware->activation-gexp firmware)
382   "Return a gexp to make the packages listed in FIRMWARE loadable by the
383 kernel."
384   (let ((directory (directory-union "firmware" firmware)))
385     ;; Tell the kernel where firmware is.
386     #~(activate-firmware (string-append #$directory "/lib/firmware"))))
388 (define firmware-service-type
389   ;; The service that collects firmware.
390   (service-type (name 'firmware)
391                 (extensions
392                  (list (service-extension activation-service-type
393                                           firmware->activation-gexp)))
394                 (compose concatenate)
395                 (extend append)))
399 ;;; Service folding.
402 (define-condition-type &service-error &error
403   service-error?)
405 (define-condition-type &missing-target-service-error &service-error
406   missing-target-service-error?
407   (service      missing-target-service-error-service)
408   (target-type  missing-target-service-error-target-type))
410 (define-condition-type &ambiguous-target-service-error &service-error
411   ambiguous-target-service-error?
412   (service      ambiguous-target-service-error-service)
413   (target-type  ambiguous-target-service-error-target-type))
415 (define (service-back-edges services)
416   "Return a procedure that, when passed a <service>, returns the list of
417 <service> objects that depend on it."
418   (define (add-edges service edges)
419     (define (add-edge extension edges)
420       (let ((target-type (service-extension-target extension)))
421         (match (filter (lambda (service)
422                          (eq? (service-kind service) target-type))
423                        services)
424           ((target)
425            (vhash-consq target service edges))
426           (()
427            (raise
428             (condition (&missing-target-service-error
429                         (service service)
430                         (target-type target-type))
431                        (&message
432                         (message
433                          (format #f (_ "no target of type '~a' for service ~s")
434                                  (service-type-name target-type)
435                                  service))))))
436           (x
437            (raise
438             (condition (&ambiguous-target-service-error
439                         (service service)
440                         (target-type target-type))
441                        (&message
442                         (message
443                          (format #f
444                                  (_ "more than one target service of type '~a'")
445                                  (service-type-name target-type))))))))))
447     (fold add-edge edges (service-type-extensions (service-kind service))))
449   (let ((edges (fold add-edges vlist-null services)))
450     (lambda (node)
451       (reverse (vhash-foldq* cons '() node edges)))))
453 (define* (fold-services services #:key (target-type boot-service-type))
454   "Fold SERVICES by propagating their extensions down to the root of type
455 TARGET-TYPE; return the root service adjusted accordingly."
456   (define dependents
457     (service-back-edges services))
459   (define (matching-extension target)
460     (let ((target (service-kind target)))
461       (match-lambda
462         (($ <service-extension> type)
463          (eq? type target)))))
465   (define (apply-extension target)
466     (lambda (service)
467       (match (find (matching-extension target)
468                    (service-type-extensions (service-kind service)))
469         (($ <service-extension> _ compute)
470          (compute (service-parameters service))))))
472   (match (filter (lambda (service)
473                    (eq? (service-kind service) target-type))
474                  services)
475     ((sink)
476      (let loop ((sink sink))
477        (let* ((dependents (map loop (dependents sink)))
478               (extensions (map (apply-extension sink) dependents))
479               (extend     (service-type-extend (service-kind sink)))
480               (compose    (service-type-compose (service-kind sink)))
481               (params     (service-parameters sink)))
482          ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
483          ;; different type than the elements of EXTENSIONS.
484          (if extend
485              (service (service-kind sink)
486                       (extend params (compose extensions)))
487              sink))))
488     (()
489      (raise
490       (condition (&missing-target-service-error
491                   (service #f)
492                   (target-type target-type))
493                  (&message
494                   (message (format #f (_ "service of type '~a' not found")
495                                    (service-type-name target-type)))))))
496     (x
497      (raise
498       (condition (&ambiguous-target-service-error
499                   (service #f)
500                   (target-type target-type))
501                  (&message
502                   (message
503                    (format #f
504                            (_ "more than one target service of type '~a'")
505                            (service-type-name target-type)))))))))
507 ;;; services.scm ends here.