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>
4 ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
5 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
6 ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
7 ;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
9 ;;; This file is part of GNU Guix.
11 ;;; GNU Guix is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
16 ;;; GNU Guix is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24 (define-module (gnu services configuration)
25 #:use-module (guix packages)
26 #:use-module (guix records)
27 #:use-module (guix gexp)
28 #:use-module ((guix utils) #:select (source-properties->location))
29 #:use-module ((guix diagnostics) #:select (formatted-message location-file))
30 #:use-module ((guix modules) #:select (file-name->module-name))
31 #:use-module (guix i18n)
32 #:autoload (texinfo) (texi-fragment->stexi)
33 #:autoload (texinfo serialize) (stexi->texi)
34 #:use-module (ice-9 curried-definitions)
35 #:use-module (ice-9 match)
36 #:use-module (srfi srfi-1)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
39 #:export (configuration-field
40 configuration-field-name
41 configuration-field-type
42 configuration-missing-field
43 configuration-field-error
44 configuration-field-serializer
45 configuration-field-getter
46 configuration-field-default-value-thunk
47 configuration-field-documentation
52 define-configuration/no-serialization
55 serialize-configuration
57 define-maybe/no-serialization
58 validate-configuration
59 generate-documentation
60 configuration->documentation
64 filter-configuration-fields
74 generic-serialize-alist-entry
75 generic-serialize-alist))
79 ;;; Syntax for creating Scheme bindings to complex configuration files.
83 (define-condition-type &configuration-error &error
86 (define (configuration-error message)
87 (raise (condition (&message (message message))
88 (&configuration-error))))
89 (define (configuration-field-error field val)
91 (format #f "Invalid value for field ~a: ~s" field val)))
92 (define (configuration-missing-field kind field)
94 (format #f "~a configuration missing required field ~a" kind field)))
95 (define (configuration-no-default-value kind field)
97 (format #f "The field `~a' of the `~a' configuration record \
98 does not have a default value" field kind)))
100 (define-record-type* <configuration-field>
101 configuration-field make-configuration-field configuration-field?
102 (name configuration-field-name)
103 (type configuration-field-type)
104 (getter configuration-field-getter)
105 (predicate configuration-field-predicate)
106 (serializer configuration-field-serializer)
107 (default-value-thunk configuration-field-default-value-thunk)
108 (documentation configuration-field-documentation))
110 (define (serialize-configuration config fields)
112 #$@(map (lambda (field)
113 ((configuration-field-serializer field)
114 (configuration-field-name field)
115 ((configuration-field-getter field) config)))
118 (define (validate-configuration config fields)
119 (for-each (lambda (field)
120 (let ((val ((configuration-field-getter field) config)))
121 (unless ((configuration-field-predicate field) val)
122 (configuration-field-error
123 (configuration-field-name field) val))))
126 (define-syntax-rule (id ctx parts ...)
127 "Assemble PARTS into a raw (unhygienic) identifier."
128 (datum->syntax ctx (symbol-append (syntax->datum parts) ...)))
130 (define (define-maybe-helper serialize? prefix syn)
134 ((stem? (id #'stem #'stem #'?))
135 (maybe-stem? (id #'stem #'maybe- #'stem #'?))
136 (serialize-stem (if prefix
137 (id #'stem prefix #'serialize- #'stem)
138 (id #'stem #'serialize- #'stem)))
139 (serialize-maybe-stem (if prefix
140 (id #'stem prefix #'serialize-maybe- #'stem)
141 (id #'stem #'serialize-maybe- #'stem))))
143 (define (maybe-stem? val)
144 (or (eq? val 'disabled) (stem? val)))
146 (list #'(define (serialize-maybe-stem field-name val)
148 (serialize-stem field-name val)
152 (define-syntax define-maybe
154 (syntax-case x (no-serialization prefix)
155 ((_ stem (no-serialization))
156 (define-maybe-helper #f #f #'(_ stem)))
157 ((_ stem (prefix serializer-prefix))
158 (define-maybe-helper #t #'serializer-prefix #'(_ stem)))
160 (define-maybe-helper #t #f #'(_ stem))))))
162 (define-syntax-rule (define-maybe/no-serialization stem)
163 (define-maybe stem (no-serialization)))
165 (define (define-configuration-helper serialize? serializer-prefix syn)
167 ((_ stem (field (field-type def ...) doc custom-serializer ...) ...)
168 (with-syntax (((field-getter ...)
170 (id #'stem #'stem #'- field))
172 ((field-predicate ...)
174 (id #'stem type #'?))
178 ((field-type default-value)
181 ;; Quote `undefined' to prevent a possibly
183 (syntax 'undefined)))
184 #'((field-type def ...) ...)))
185 ((field-serializer ...)
186 (map (lambda (type custom-serializer)
188 (match custom-serializer
192 (if serializer-prefix
196 (id #'stem #'serialize- type))))))
198 #'((custom-serializer ...) ...))))
200 (define-record-type* #,(id #'stem #'< #'stem #'>)
201 #,(id #'stem #'% #'stem)
202 #,(id #'stem #'make- #'stem)
203 #,(id #'stem #'stem #'?)
204 (%location #,(id #'stem #'stem #'-location)
205 (default (and=> (current-source-location)
206 source-properties->location))
208 #,@(map (lambda (name getter def)
209 (if (eq? (syntax->datum def) (quote 'undefined))
211 #`(#,name #,getter (default #,def))))
214 #'(field-default ...)))
215 (define #,(id #'stem #'stem #'-fields)
216 (list (configuration-field
219 (getter field-getter)
220 (predicate field-predicate)
221 (serializer field-serializer)
224 (display '#,(id #'stem #'% #'stem))
225 (if (eq? (syntax->datum field-default)
227 (configuration-no-default-value
228 '#,(id #'stem #'% #'stem) 'field)
232 (define-syntax-rule (stem arg (... ...))
233 (let ((conf (#,(id #'stem #'% #'stem) arg (... ...))))
234 (validate-configuration conf
235 #,(id #'stem #'stem #'-fields))
238 (define no-serialization ;syntactic keyword for 'define-configuration'
241 (define-syntax define-configuration
243 (syntax-case s (no-serialization prefix)
244 ((_ stem (field (field-type def ...) doc custom-serializer ...) ...
246 (define-configuration-helper
247 #f #f #'(_ stem (field (field-type def ...) doc custom-serializer ...)
249 ((_ stem (field (field-type def ...) doc custom-serializer ...) ...
250 (prefix serializer-prefix))
251 (define-configuration-helper
252 #t #'serializer-prefix #'(_ stem (field (field-type def ...)
253 doc custom-serializer ...)
255 ((_ stem (field (field-type def ...) doc custom-serializer ...) ...)
256 (define-configuration-helper
257 #t #f #'(_ stem (field (field-type def ...) doc custom-serializer ...)
260 (define-syntax-rule (define-configuration/no-serialization
261 stem (field (field-type def ...)
262 doc custom-serializer ...) ...)
263 (define-configuration stem (field (field-type def ...)
264 doc custom-serializer ...) ...
267 (define (empty-serializer field-name val) "")
268 (define serialize-package empty-serializer)
270 ;; A little helper to make it easier to document all those fields.
271 (define (generate-documentation documentation documentation-name)
272 (define (str x) (object->string x))
274 (define (package->symbol package)
275 "Return the first symbol name of a package that matches PACKAGE, else #f."
276 (let* ((module (file-name->module-name
277 (location-file (package-location package))))
280 (module-map (lambda (symbol var)
281 (and (equal? package (variable-ref var))
283 (resolve-module module)))))
288 (define (generate configuration-name)
289 (match (assq-ref documentation configuration-name)
290 ((fields . sub-documentation)
291 `((deftp (% (category "Data Type") (name ,(str configuration-name)))
292 (para "Available " (code ,(str configuration-name)) " fields are:")
294 (% (formatter (asis)))
297 (let ((field-name (configuration-field-name f))
298 (field-type (configuration-field-type f))
299 (field-docs (cdr (texi-fragment->stexi
300 (configuration-field-documentation f))))
302 (configuration-field-default-value-thunk f)
303 (lambda _ '%invalid))))
304 (define (show-default? val)
305 (or (string? val) (number? val) (boolean? val)
307 (and (symbol? val) (not (eq? val '%invalid)))
308 (and (list? val) (and-map show-default? val))))
310 (define (show-default val)
313 (symbol->string (package->symbol val)))
317 (code ,(str field-name))
318 ,@(if (show-default? default)
320 (code ,(show-default default)) ")")
322 " (type: " ,(str field-type) ")"))
326 (or (assq-ref sub-documentation field-name)
329 (stexi->texi `(*fragment* . ,(generate documentation-name))))
331 (define (configuration->documentation configuration-symbol)
332 "Take CONFIGURATION-SYMBOL, the symbol corresponding to the name used when
333 defining a configuration record with DEFINE-CONFIGURATION, and output the
334 Texinfo documentation of its fields."
335 ;; This is helper for a simple, straight-forward application of
336 ;; GENERATE-DOCUMENTATION.
337 (let ((fields-getter (module-ref (current-module)
338 (symbol-append configuration-symbol
340 (display (generate-documentation `((,configuration-symbol ,fields-getter))
341 configuration-symbol))))
343 (define* (filter-configuration-fields configuration-fields fields
345 "Retrieve the fields listed in FIELDS from CONFIGURATION-FIELDS.
346 If NEGATE? is @code{#t}, retrieve all fields except FIELDS."
347 (filter (lambda (field)
348 (let ((member? (member (configuration-field-name field) fields)))
349 (if (not negate?) member? (not member?))))
350 configuration-fields))
353 (define* (interpose ls #:optional (delimiter "\n") (grammar 'infix))
354 "Same as @code{string-join}, but without join and string, returns an
355 DELIMITER interposed LS. Support 'infix and 'suffix GRAMMAR values."
356 (when (not (member grammar '(infix suffix)))
359 (G_ "The GRAMMAR value must be 'infix or 'suffix, but ~a provided.")
361 (fold-right (lambda (e acc)
363 (if (and (null? acc) (eq? grammar 'infix))
365 (cons delimiter acc))))
368 (define (list-of pred?)
369 "Return a procedure that takes a list and check if all the elements of
370 the list result in @code{#t} when applying PRED? on them."
377 (define list-of-strings?
380 (define alist? list?)
382 (define serialize-file-like empty-serializer)
384 (define (text-config? config)
385 (list-of file-like?))
386 (define (serialize-text-config field-name val)
392 (use-modules (ice-9 rdelim))
393 (with-fluids ((%default-port-encoding "UTF-8"))
394 (with-input-from-file #$e read-string))))
398 (define ((generic-serialize-alist-entry serialize-field) entry)
399 "Apply the SERIALIZE-FIELD procedure on the field and value of ENTRY."
401 ((field . val) (serialize-field field val))))
403 (define (generic-serialize-alist combine serialize-field fields)
404 "Generate a configuration from an association list FIELDS.
406 SERIALIZE-FIELD is a procedure that takes two arguments, it will be
407 applied on the fields and values of FIELDS using the
408 @code{generic-serialize-alist-entry} procedure.
410 COMBINE is a procedure that takes one or more arguments and combines
411 all the alist entries into one value, @code{string-append} or
412 @code{append} are usually good candidates for this.
414 See the @code{serialize-alist} procedure in `@code{(gnu home services
415 version-control}' for an example usage.)}"
417 (map (generic-serialize-alist-entry serialize-field) fields)))