1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 Andy Wingo <wingo@igalia.com>
3 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@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 configuration)
21 #:use-module (guix packages)
22 #:use-module (guix records)
23 #:use-module (guix gexp)
24 #:autoload (texinfo) (texi-fragment->stexi)
25 #:autoload (texinfo serialize) (stexi->texi)
26 #:use-module (ice-9 match)
27 #:use-module ((srfi srfi-1) #:select (append-map))
28 #:use-module (srfi srfi-34)
29 #:use-module (srfi srfi-35)
30 #:export (configuration-field
31 configuration-field-name
32 configuration-field-type
33 configuration-missing-field
34 configuration-field-error
35 configuration-field-serializer
36 configuration-field-getter
37 configuration-field-default-value-thunk
38 configuration-field-documentation
39 serialize-configuration
42 validate-configuration
43 generate-documentation
48 ;;; Syntax for creating Scheme bindings to complex configuration files.
52 (define-condition-type &configuration-error &error
55 (define (configuration-error message)
56 (raise (condition (&message (message message))
57 (&configuration-error))))
58 (define (configuration-field-error field val)
60 (format #f "Invalid value for field ~a: ~s" field val)))
61 (define (configuration-missing-field kind field)
63 (format #f "~a configuration missing required field ~a" kind field)))
65 (define-record-type* <configuration-field>
66 configuration-field make-configuration-field configuration-field?
67 (name configuration-field-name)
68 (type configuration-field-type)
69 (getter configuration-field-getter)
70 (predicate configuration-field-predicate)
71 (serializer configuration-field-serializer)
72 (default-value-thunk configuration-field-default-value-thunk)
73 (documentation configuration-field-documentation))
75 (define (serialize-configuration config fields)
76 (for-each (lambda (field)
77 ((configuration-field-serializer field)
78 (configuration-field-name field)
79 ((configuration-field-getter field) config)))
82 (define (validate-configuration config fields)
83 (for-each (lambda (field)
84 (let ((val ((configuration-field-getter field) config)))
85 (unless ((configuration-field-predicate field) val)
86 (configuration-field-error
87 (configuration-field-name field) val))))
90 (define (id ctx part . parts)
91 (let ((part (syntax->datum part)))
96 (parts (symbol-append part
97 (syntax->datum (apply id ctx parts))))))))
99 (define-syntax define-maybe
104 ((stem? (id #'stem #'stem #'?))
105 (maybe-stem? (id #'stem #'maybe- #'stem #'?))
106 (serialize-stem (id #'stem #'serialize- #'stem))
107 (serialize-maybe-stem (id #'stem #'serialize-maybe- #'stem)))
109 (define (maybe-stem? val)
110 (or (eq? val 'disabled) (stem? val)))
111 (define (serialize-maybe-stem field-name val)
112 (when (stem? val) (serialize-stem field-name val)))))))))
114 (define-syntax define-configuration
117 ((_ stem (field (field-type def) doc) ...)
118 (with-syntax (((field-getter ...)
120 (id #'stem #'stem #'- field))
122 ((field-predicate ...)
124 (id #'stem type #'?))
126 ((field-serializer ...)
128 (id #'stem #'serialize- type))
129 #'(field-type ...))))
131 (define-record-type* #,(id #'stem #'< #'stem #'>)
132 #,(id #'stem #'% #'stem)
133 #,(id #'stem #'make- #'stem)
134 #,(id #'stem #'stem #'?)
135 (field field-getter (default def))
137 (define #,(id #'stem #'stem #'-fields)
138 (list (configuration-field
141 (getter field-getter)
142 (predicate field-predicate)
143 (serializer field-serializer)
144 (default-value-thunk (lambda () def))
147 (define-syntax-rule (stem arg (... ...))
148 (let ((conf (#,(id #'stem #'% #'stem) arg (... ...))))
149 (validate-configuration conf
150 #,(id #'stem #'stem #'-fields))
153 (define (serialize-package field-name val)
156 ;; A little helper to make it easier to document all those fields.
157 (define (generate-documentation documentation documentation-name)
158 (define (str x) (object->string x))
159 (define (generate configuration-name)
160 (match (assq-ref documentation configuration-name)
161 ((fields . sub-documentation)
162 `((para "Available " (code ,(str configuration-name)) " fields are:")
165 (let ((field-name (configuration-field-name f))
166 (field-type (configuration-field-type f))
167 (field-docs (cdr (texi-fragment->stexi
168 (configuration-field-documentation f))))
170 (configuration-field-default-value-thunk f)
171 (lambda _ '%invalid))))
172 (define (show-default? val)
173 (or (string? default) (number? default) (boolean? default)
174 (and (symbol? val) (not (eq? val '%invalid)))
175 (and (list? val) (and-map show-default? val))))
176 `(deftypevr (% (category
177 (code ,(str configuration-name)) " parameter")
178 (data-type ,(str field-type))
179 (name ,(str field-name)))
181 ,@(if (show-default? default)
182 `((para "Defaults to " (samp ,(str default)) "."))
186 (or (assq-ref sub-documentation field-name) '())))))
188 (stexi->texi `(*fragment* . ,(generate documentation-name))))