Add (guix build bournish) and use it in the initrd.
[jackhill/guix/guix.git] / gnu / services.scm
CommitLineData
db4fdc04 1;;; GNU Guix --- Functional package management for GNU
94af9daa 2;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org>
db4fdc04
LC
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/>.
18
19(define-module (gnu services)
bebc8681 20 #:use-module (guix gexp)
0adfe95a
LC
21 #:use-module (guix monads)
22 #:use-module (guix store)
db4fdc04 23 #:use-module (guix records)
af4c3fd5 24 #:use-module (guix profiles)
0adfe95a
LC
25 #:use-module (guix sets)
26 #:use-module (guix ui)
27 #:use-module (gnu packages base)
28 #:use-module (gnu packages bash)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-9)
31 #:use-module (srfi srfi-9 gnu)
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-34)
34 #:use-module (srfi srfi-35)
35 #:use-module (ice-9 vlist)
36 #:use-module (ice-9 match)
37 #:export (service-extension
38 service-extension?
39
40 service-type
41 service-type?
5152d13b
LC
42 service-type-name
43 service-type-extensions
44 service-type-compose
45 service-type-extend
0adfe95a 46
db4fdc04 47 service
0adfe95a
LC
48 service?
49 service-kind
50 service-parameters
51
cd6f6c22 52 modify-services
5152d13b 53 service-back-edges
0adfe95a
LC
54 fold-services
55
56 service-error?
57 missing-target-service-error?
58 missing-target-service-error-service
59 missing-target-service-error-target-type
60 ambiguous-target-service-error?
61 ambiguous-target-service-error-service
62 ambiguous-target-service-error-target-type
63
d62e201c 64 system-service-type
0adfe95a 65 boot-service-type
be7be9e8 66 cleanup-service-type
0adfe95a
LC
67 activation-service-type
68 activation-service->script
a241a7ac 69 %linux-bare-metal-service
0adfe95a
LC
70 etc-service-type
71 etc-directory
72 setuid-program-service-type
af4c3fd5 73 profile-service-type
0adfe95a
LC
74 firmware-service-type
75
76 %boot-service
77 %activation-service
78 etc-service
79
80 file-union)) ;XXX: for lack of a better place
81
82;;; Comment:
83;;;
84;;; This module defines a broad notion of "service types" and "services."
db4fdc04 85;;;
0adfe95a
LC
86;;; A service type describe how its instances extend instances of other
87;;; service types. For instance, some services extend the instance of
88;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
d4053c71
AK
89;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
90;;; <shepherd-service>.
0adfe95a
LC
91;;;
92;;; When applicable, the service type defines how it can itself be extended,
93;;; by providing one procedure to compose extensions, and one procedure to
94;;; extend itself.
95;;;
d62e201c
LC
96;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
97;;; instance, which is the root of the service DAG. Its value is the
98;;; derivation that produces the 'system' directory as returned by
99;;; 'operating-system-derivation'.
0adfe95a
LC
100;;;
101;;; The 'fold-services' procedure can be passed a list of procedures, which it
102;;; "folds" by propagating extensions down the graph; it returns the root
103;;; service after the applying all its extensions.
db4fdc04
LC
104;;;
105;;; Code:
106
0adfe95a
LC
107(define-record-type <service-extension>
108 (service-extension target compute)
109 service-extension?
110 (target service-extension-target) ;<service-type>
111 (compute service-extension-compute)) ;params -> params
112
113(define-record-type* <service-type> service-type make-service-type
114 service-type?
115 (name service-type-name) ;symbol (for debugging)
116
117 ;; Things extended by services of this type.
118 (extensions service-type-extensions) ;list of <service-extensions>
119
120 ;; Given a list of extensions, "compose" them.
121 (compose service-type-compose ;list of Any -> Any
122 (default #f))
123
124 ;; Extend the services' own parameters with the extension composition.
125 (extend service-type-extend ;list of Any -> parameters
126 (default #f)))
127
128(define (write-service-type type port)
129 (format port "#<service-type ~a ~a>"
130 (service-type-name type)
131 (number->string (object-address type) 16)))
132
133(set-record-type-printer! <service-type> write-service-type)
134
135;; Services of a given type.
136(define-record-type <service>
137 (service type parameters)
db4fdc04 138 service?
0adfe95a
LC
139 (type service-kind)
140 (parameters service-parameters))
141
142
cd6f6c22
LC
143(define-syntax %modify-service
144 (syntax-rules (=>)
145 ((_ service)
146 service)
147 ((_ svc (kind param => exp ...) clauses ...)
148 (if (eq? (service-kind svc) kind)
149 (let ((param (service-parameters svc)))
150 (service (service-kind svc)
151 (begin exp ...)))
152 (%modify-service svc clauses ...)))))
153
154(define-syntax modify-services
155 (syntax-rules ()
156 "Modify the services listed in SERVICES according to CLAUSES. Each clause
157must have the form:
158
159 (TYPE VARIABLE => BODY)
160
161where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
162identifier that is bound within BODY to the value of the service of that
163TYPE. Consider this example:
164
165 (modify-services %base-services
166 (guix-service-type config =>
167 (guix-configuration
168 (inherit config)
169 (use-substitutes? #f)
170 (extra-options '(\"--gc-keep-derivations\"))))
171 (mingetty-service-type config =>
172 (mingetty-configuration
173 (inherit config)
174 (motd (plain-file \"motd\" \"Hi there!\")))))
175
176It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
177all the MINGETTY-SERVICE-TYPE instances.
178
179This is a shorthand for (map (lambda (svc) ...) %base-services)."
180 ((_ services clauses ...)
181 (map (lambda (service)
182 (%modify-service service clauses ...))
183 services))))
0adfe95a
LC
184
185\f
186;;;
187;;; Core services.
188;;;
189
d62e201c
LC
190(define (system-derivation mentries mextensions)
191 "Return as a monadic value the derivation of the 'system' directory
192containing the given entries."
193 (mlet %store-monad ((entries mentries)
194 (extensions (sequence %store-monad mextensions)))
195 (lower-object
196 (file-union "system"
197 (append entries (concatenate extensions))))))
198
199(define system-service-type
200 ;; This is the ultimate service type, the root of the service DAG. The
201 ;; service of this type is extended by monadic name/item pairs. These items
202 ;; end up in the "system directory" as returned by
203 ;; 'operating-system-derivation'.
204 (service-type (name 'system)
205 (extensions '())
206 (compose identity)
207 (extend system-derivation)))
208
3a391e68 209(define (compute-boot-script _ mexps)
be7be9e8 210 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
0adfe95a 211 (gexp->file "boot"
d4053c71 212 ;; Clean up and activate the system, then spawn shepherd.
be7be9e8 213 #~(begin #$@gexps))))
0adfe95a 214
d62e201c
LC
215(define (boot-script-entry mboot)
216 "Return, as a monadic value, an entry for the boot script in the system
217directory."
218 (mlet %store-monad ((boot mboot))
219 (return `(("boot" ,boot)))))
220
0adfe95a
LC
221(define boot-service-type
222 ;; The service of this type is extended by being passed gexps as monadic
223 ;; values. It aggregates them in a single script, as a monadic value, which
224 ;; becomes its 'parameters'. It is the only service that extends nothing.
225 (service-type (name 'boot)
d62e201c
LC
226 (extensions
227 (list (service-extension system-service-type
228 boot-script-entry)))
3a391e68
LC
229 (compose append)
230 (extend compute-boot-script)))
0adfe95a
LC
231
232(define %boot-service
d62e201c 233 ;; The service that produces the boot script.
0adfe95a 234 (service boot-service-type #t))
be7be9e8
LC
235
236(define (cleanup-gexp _)
237 "Return as a monadic value a gexp to clean up /tmp and similar places upon
238boot."
239 (define %modules
240 '((guix build utils)))
241
242 (mlet %store-monad ((modules (imported-modules %modules))
243 (compiled (compiled-modules %modules)))
244 (return #~(begin
245 (eval-when (expand load eval)
246 ;; Make sure 'use-modules' below succeeds.
247 (set! %load-path (cons #$modules %load-path))
248 (set! %load-compiled-path
249 (cons #$compiled %load-compiled-path)))
250
251 (use-modules (guix build utils))
252
253 ;; Clean out /tmp and /var/run.
254 ;;
255 ;; XXX This needs to happen before service activations, so it
256 ;; has to be here, but this also implicitly assumes that /tmp
257 ;; and /var/run are on the root partition.
3c4c8c3e
LC
258 (letrec-syntax ((fail-safe (syntax-rules ()
259 ((_ exp rest ...)
260 (begin
261 (catch 'system-error
262 (lambda () exp)
263 (const #f))
264 (fail-safe rest ...)))
265 ((_)
266 #t))))
267 ;; Ignore I/O errors so the system can boot.
268 (fail-safe
269 (delete-file-recursively "/tmp")
270 (delete-file-recursively "/var/run")
271 (mkdir "/tmp")
272 (chmod "/tmp" #o1777)
273 (mkdir "/var/run")
274 (chmod "/var/run" #o755)))))))
be7be9e8
LC
275
276(define cleanup-service-type
277 ;; Service that cleans things up in /tmp and similar.
278 (service-type (name 'cleanup)
279 (extensions
280 (list (service-extension boot-service-type
281 cleanup-gexp)))))
0adfe95a
LC
282
283(define* (file-union name files) ;FIXME: Factorize.
284 "Return a <computed-file> that builds a directory containing all of FILES.
285Each item in FILES must be a list where the first element is the file name to
286use in the new directory, and the second element is a gexp denoting the target
287file."
288 (computed-file name
289 #~(begin
290 (mkdir #$output)
291 (chdir #$output)
292 #$@(map (match-lambda
293 ((target source)
37dd1e6a
LC
294 #~(begin
295 ;; Stat the source to abort early if it
296 ;; does not exist.
297 (stat #$source)
298
299 (symlink #$source #$target))))
0adfe95a
LC
300 files))))
301
302(define (directory-union name things)
303 "Return a directory that is the union of THINGS."
304 (match things
305 ((one)
306 ;; Only one thing; return it.
307 one)
308 (_
309 (computed-file name
310 #~(begin
311 (use-modules (guix build union))
312 (union-build #$output '#$things))
313 #:modules '((guix build union))))))
314
0adfe95a
LC
315(define* (activation-service->script service)
316 "Return as a monadic value the activation script for SERVICE, a service of
317ACTIVATION-SCRIPT-TYPE."
318 (activation-script (service-parameters service)))
319
320(define (activation-script gexps)
321 "Return the system's activation script, which evaluates GEXPS."
322 (define %modules
323 '((gnu build activation)
324 (gnu build linux-boot)
325 (gnu build linux-modules)
326 (gnu build file-systems)
327 (guix build utils)
328 (guix build syscalls)
329 (guix elf)))
330
331 (define (service-activations)
332 ;; Return the activation scripts for SERVICES.
333 (mapm %store-monad
334 (cut gexp->file "activate-service" <>)
335 gexps))
336
337 (mlet* %store-monad ((actions (service-activations))
338 (modules (imported-modules %modules))
a241a7ac 339 (compiled (compiled-modules %modules)))
0adfe95a
LC
340 (gexp->file "activate"
341 #~(begin
342 (eval-when (expand load eval)
343 ;; Make sure 'use-modules' below succeeds.
344 (set! %load-path (cons #$modules %load-path))
345 (set! %load-compiled-path
346 (cons #$compiled %load-compiled-path)))
347
348 (use-modules (gnu build activation))
349
350 ;; Make sure /bin/sh is valid and current.
351 (activate-/bin/sh
352 (string-append #$(canonical-package bash) "/bin/sh"))
353
0adfe95a
LC
354 ;; Run the services' activation snippets.
355 ;; TODO: Use 'load-compiled'.
356 (for-each primitive-load '#$actions)
357
358 ;; Set up /run/current-system.
359 (activate-current-system)))))
360
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))))
365
3a391e68
LC
366(define (second-argument a b) b)
367
0adfe95a
LC
368(define activation-service-type
369 (service-type (name 'activate)
370 (extensions
371 (list (service-extension boot-service-type
372 gexps->activation-gexp)))
373 (compose append)
374 (extend second-argument)))
375
376(define %activation-service
377 ;; The activation service produces the activation script from the gexps it
378 ;; receives.
379 (service activation-service-type #t))
380
a241a7ac
LC
381(define %modprobe-wrapper
382 ;; Wrapper for the 'modprobe' command that knows where modules live.
383 ;;
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"
389 #~(begin
390 (setenv "LINUX_MODULE_DIRECTORY"
391 "/run/booted-system/kernel/lib/modules")
392 (apply execl #$modprobe
393 (cons #$modprobe (cdr (command-line))))))))
394
395(define %linux-kernel-activation
396 ;; Activation of the Linux kernel running on the bare metal (as opposed to
397 ;; running in a container.)
398 #~(begin
399 ;; Tell the kernel to use our 'modprobe' command.
400 (activate-modprobe #$%modprobe-wrapper)
401
402 ;; Let users debug their own processes!
403 (activate-ptrace-attach)))
404
405(define linux-bare-metal-service-type
406 (service-type (name 'linux-bare-metal)
407 (extensions
408 (list (service-extension activation-service-type
409 (const %linux-kernel-activation))))))
410
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))
415
0adfe95a
LC
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)))
419
420(define (files->etc-directory files)
421 (file-union "etc" files))
422
d62e201c
LC
423(define (etc-entry files)
424 "Return an entry for the /etc directory consisting of FILES in the system
425directory."
426 (with-monad %store-monad
427 (return `(("etc" ,(files->etc-directory files))))))
428
0adfe95a
LC
429(define etc-service-type
430 (service-type (name 'etc)
431 (extensions
432 (list
433 (service-extension activation-service-type
434 (lambda (files)
435 (let ((etc
436 (files->etc-directory files)))
d62e201c
LC
437 #~(activate-etc #$etc))))
438 (service-extension system-service-type etc-entry)))
0adfe95a
LC
439 (compose concatenate)
440 (extend append)))
441
442(define (etc-service files)
443 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
444FILES must be a list of name/file-like object pairs."
445 (service etc-service-type files))
446
447(define setuid-program-service-type
448 (service-type (name 'setuid-program)
449 (extensions
450 (list (service-extension activation-service-type
451 (lambda (programs)
452 #~(activate-setuid-programs
453 (list #$@programs))))))
454 (compose concatenate)
455 (extend append)))
456
af4c3fd5
LC
457(define (packages->profile-entry packages)
458 "Return a system entry for the profile containing PACKAGES."
459 (mlet %store-monad ((profile (profile-derivation
460 (manifest (map package->manifest-entry
461 (delete-duplicates packages eq?))))))
462 (return `(("profile" ,profile)))))
463
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)
468 (extensions
469 (list (service-extension system-service-type
470 packages->profile-entry)))
471 (compose concatenate)
472 (extend append)))
473
0adfe95a
LC
474(define (firmware->activation-gexp firmware)
475 "Return a gexp to make the packages listed in FIRMWARE loadable by the
476kernel."
477 (let ((directory (directory-union "firmware" firmware)))
478 ;; Tell the kernel where firmware is.
479 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
480
481(define firmware-service-type
482 ;; The service that collects firmware.
483 (service-type (name 'firmware)
484 (extensions
485 (list (service-extension activation-service-type
486 firmware->activation-gexp)))
487 (compose concatenate)
488 (extend append)))
489
490\f
491;;;
492;;; Service folding.
493;;;
494
495(define-condition-type &service-error &error
496 service-error?)
497
498(define-condition-type &missing-target-service-error &service-error
499 missing-target-service-error?
500 (service missing-target-service-error-service)
501 (target-type missing-target-service-error-target-type))
502
503(define-condition-type &ambiguous-target-service-error &service-error
504 ambiguous-target-service-error?
505 (service ambiguous-target-service-error-service)
506 (target-type ambiguous-target-service-error-target-type))
507
508(define (service-back-edges services)
509 "Return a procedure that, when passed a <service>, returns the list of
510<service> objects that depend on it."
511 (define (add-edges service edges)
512 (define (add-edge extension edges)
513 (let ((target-type (service-extension-target extension)))
514 (match (filter (lambda (service)
515 (eq? (service-kind service) target-type))
516 services)
517 ((target)
518 (vhash-consq target service edges))
519 (()
520 (raise
521 (condition (&missing-target-service-error
522 (service service)
523 (target-type target-type))
524 (&message
525 (message
526 (format #f (_ "no target of type '~a' for service ~s")
527 (service-type-name target-type)
528 service))))))
529 (x
530 (raise
531 (condition (&ambiguous-target-service-error
532 (service service)
533 (target-type target-type))
534 (&message
535 (message
536 (format #f
537 (_ "more than one target service of type '~a'")
538 (service-type-name target-type))))))))))
539
540 (fold add-edge edges (service-type-extensions (service-kind service))))
541
542 (let ((edges (fold add-edges vlist-null services)))
543 (lambda (node)
544 (reverse (vhash-foldq* cons '() node edges)))))
545
d62e201c
LC
546(define* (fold-services services
547 #:key (target-type system-service-type))
0adfe95a
LC
548 "Fold SERVICES by propagating their extensions down to the root of type
549TARGET-TYPE; return the root service adjusted accordingly."
550 (define dependents
551 (service-back-edges services))
552
553 (define (matching-extension target)
554 (let ((target (service-kind target)))
555 (match-lambda
556 (($ <service-extension> type)
557 (eq? type target)))))
558
559 (define (apply-extension target)
560 (lambda (service)
561 (match (find (matching-extension target)
562 (service-type-extensions (service-kind service)))
563 (($ <service-extension> _ compute)
564 (compute (service-parameters service))))))
565
566 (match (filter (lambda (service)
567 (eq? (service-kind service) target-type))
568 services)
569 ((sink)
570 (let loop ((sink sink))
571 (let* ((dependents (map loop (dependents sink)))
572 (extensions (map (apply-extension sink) dependents))
573 (extend (service-type-extend (service-kind sink)))
574 (compose (service-type-compose (service-kind sink)))
575 (params (service-parameters sink)))
576 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
577 ;; different type than the elements of EXTENSIONS.
578 (if extend
579 (service (service-kind sink)
580 (extend params (compose extensions)))
581 sink))))
582 (()
583 (raise
584 (condition (&missing-target-service-error
585 (service #f)
586 (target-type target-type))
587 (&message
588 (message (format #f (_ "service of type '~a' not found")
589 (service-type-name target-type)))))))
590 (x
591 (raise
592 (condition (&ambiguous-target-service-error
593 (service #f)
594 (target-type target-type))
595 (&message
596 (message
597 (format #f
598 (_ "more than one target service of type '~a'")
599 (service-type-name target-type)))))))))
db4fdc04
LC
600
601;;; services.scm ends here.