services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
|
|
|
;;; Copyright © 2015 Andy Wingo <wingo@igalia.com>
|
2017-03-24 10:00:13 +00:00
|
|
|
;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
|
services: messaging: Prosody config supports file-like objects.
* doc/guix.texi (Messaging Services): Update accordingly.
* gnu/services/configuration.scm (serialize-configuration,
serialize-maybe-stem, serialize-package): Return strings or string-valued
gexps (these procedures were only used for their side-effects).
* gnu/services/messaging.scm (serialize-field, serialize-field-list,
enclose-quotes, serialize-raw-content, serialize-ssl-configuration,
serialize-virtualhost-configuration-list,
serialize-int-component-configuration-list,
serialize-ext-component-configuration-list,
serialize-virtualhost-configuration, serialize-int-component-configuration,
serialize-ext-component-configuration, serialize-prosody-configuration):
Return strings or string-valued gexps and stop printing.
(prosody-activation): Use SERIALIZE-PROSODY-CONFIGURATION's return value with
MIXED-TEXT-FILE instead of using its output with PLAIN-FILE.
(serialize-non-negative-integer, serialize-non-negative-integer-list): Convert
numbers to strings.
(file-object?, serialize-file-object, file-object-list?,
serialize-file-object-list): New procedures.
(ssl-configuration)[capath, cafile], (prosody-configuration)[plugin-paths,
groups-file]: Replace FILE-NAME with FILE-OBJECT.
* guix/gexp.scm (file-like?): New exported procedure.
2018-02-26 00:12:24 +00:00
|
|
|
;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
|
2021-05-01 11:24:43 +00:00
|
|
|
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
|
2022-07-27 19:18:51 +00:00
|
|
|
;;; Copyright © 2021, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
|
2021-10-02 16:05:02 +00:00
|
|
|
;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
|
2022-05-17 11:39:26 +00:00
|
|
|
;;; Copyright © 2022 Maxime Devos <maximedevos@telenet.be>
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
;;;
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (gnu services configuration)
|
|
|
|
#:use-module (guix packages)
|
|
|
|
#:use-module (guix records)
|
|
|
|
#:use-module (guix gexp)
|
2018-12-22 20:30:40 +00:00
|
|
|
#:use-module ((guix utils) #:select (source-properties->location))
|
2022-06-18 20:37:20 +00:00
|
|
|
#:use-module ((guix diagnostics)
|
|
|
|
#:select (formatted-message location-file &error-location))
|
2021-06-09 13:06:27 +00:00
|
|
|
#:use-module ((guix modules) #:select (file-name->module-name))
|
2021-10-02 16:05:02 +00:00
|
|
|
#:use-module (guix i18n)
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
#:autoload (texinfo) (texi-fragment->stexi)
|
|
|
|
#:autoload (texinfo serialize) (stexi->texi)
|
2021-10-02 16:05:02 +00:00
|
|
|
#:use-module (ice-9 curried-definitions)
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
#:use-module (ice-9 match)
|
2021-06-09 13:06:27 +00:00
|
|
|
#:use-module (srfi srfi-1)
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
#:use-module (srfi srfi-35)
|
|
|
|
#:export (configuration-field
|
|
|
|
configuration-field-name
|
2017-01-08 19:08:14 +00:00
|
|
|
configuration-field-type
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
configuration-missing-field
|
|
|
|
configuration-field-error
|
2016-12-03 12:12:56 +00:00
|
|
|
configuration-field-serializer
|
|
|
|
configuration-field-getter
|
2017-01-08 19:08:14 +00:00
|
|
|
configuration-field-default-value-thunk
|
|
|
|
configuration-field-documentation
|
2021-05-08 02:39:54 +00:00
|
|
|
|
|
|
|
configuration-error?
|
|
|
|
|
|
|
|
define-configuration
|
2021-05-17 14:18:34 +00:00
|
|
|
define-configuration/no-serialization
|
2021-05-08 13:06:31 +00:00
|
|
|
no-serialization
|
2021-05-08 02:39:54 +00:00
|
|
|
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
serialize-configuration
|
2017-03-24 10:00:13 +00:00
|
|
|
define-maybe
|
2021-05-16 05:09:39 +00:00
|
|
|
define-maybe/no-serialization
|
2022-08-24 12:40:39 +00:00
|
|
|
%unset-value
|
2022-08-24 12:40:40 +00:00
|
|
|
maybe-value
|
2022-08-24 12:40:38 +00:00
|
|
|
maybe-value-set?
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
generate-documentation
|
2021-04-12 20:58:15 +00:00
|
|
|
configuration->documentation
|
2021-05-08 02:39:54 +00:00
|
|
|
empty-serializer
|
2021-10-02 16:05:02 +00:00
|
|
|
serialize-package
|
|
|
|
|
|
|
|
filter-configuration-fields
|
|
|
|
|
|
|
|
interpose
|
|
|
|
list-of
|
|
|
|
|
|
|
|
list-of-strings?
|
|
|
|
alist?
|
|
|
|
serialize-file-like
|
|
|
|
text-config?
|
|
|
|
serialize-text-config
|
|
|
|
generic-serialize-alist-entry
|
|
|
|
generic-serialize-alist))
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; Syntax for creating Scheme bindings to complex configuration files.
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(define-condition-type &configuration-error &error
|
|
|
|
configuration-error?)
|
|
|
|
|
|
|
|
(define (configuration-error message)
|
|
|
|
(raise (condition (&message (message message))
|
|
|
|
(&configuration-error))))
|
2022-06-18 20:37:20 +00:00
|
|
|
(define (configuration-field-error loc field value)
|
|
|
|
(raise (apply
|
|
|
|
make-compound-condition
|
|
|
|
(formatted-message (G_ "invalid value ~s for field '~a'")
|
|
|
|
value field)
|
|
|
|
(condition (&configuration-error))
|
|
|
|
(if loc
|
|
|
|
(list (condition
|
|
|
|
(&error-location (location loc))))
|
|
|
|
'()))))
|
|
|
|
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
(define (configuration-missing-field kind field)
|
|
|
|
(configuration-error
|
|
|
|
(format #f "~a configuration missing required field ~a" kind field)))
|
2022-05-17 11:39:28 +00:00
|
|
|
(define (configuration-missing-default-value kind field)
|
2021-05-01 11:24:43 +00:00
|
|
|
(configuration-error
|
|
|
|
(format #f "The field `~a' of the `~a' configuration record \
|
|
|
|
does not have a default value" field kind)))
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
|
|
|
|
(define-record-type* <configuration-field>
|
|
|
|
configuration-field make-configuration-field configuration-field?
|
|
|
|
(name configuration-field-name)
|
|
|
|
(type configuration-field-type)
|
|
|
|
(getter configuration-field-getter)
|
|
|
|
(predicate configuration-field-predicate)
|
|
|
|
(serializer configuration-field-serializer)
|
|
|
|
(default-value-thunk configuration-field-default-value-thunk)
|
|
|
|
(documentation configuration-field-documentation))
|
|
|
|
|
|
|
|
(define (serialize-configuration config fields)
|
services: messaging: Prosody config supports file-like objects.
* doc/guix.texi (Messaging Services): Update accordingly.
* gnu/services/configuration.scm (serialize-configuration,
serialize-maybe-stem, serialize-package): Return strings or string-valued
gexps (these procedures were only used for their side-effects).
* gnu/services/messaging.scm (serialize-field, serialize-field-list,
enclose-quotes, serialize-raw-content, serialize-ssl-configuration,
serialize-virtualhost-configuration-list,
serialize-int-component-configuration-list,
serialize-ext-component-configuration-list,
serialize-virtualhost-configuration, serialize-int-component-configuration,
serialize-ext-component-configuration, serialize-prosody-configuration):
Return strings or string-valued gexps and stop printing.
(prosody-activation): Use SERIALIZE-PROSODY-CONFIGURATION's return value with
MIXED-TEXT-FILE instead of using its output with PLAIN-FILE.
(serialize-non-negative-integer, serialize-non-negative-integer-list): Convert
numbers to strings.
(file-object?, serialize-file-object, file-object-list?,
serialize-file-object-list): New procedures.
(ssl-configuration)[capath, cafile], (prosody-configuration)[plugin-paths,
groups-file]: Replace FILE-NAME with FILE-OBJECT.
* guix/gexp.scm (file-like?): New exported procedure.
2018-02-26 00:12:24 +00:00
|
|
|
#~(string-append
|
|
|
|
#$@(map (lambda (field)
|
|
|
|
((configuration-field-serializer field)
|
|
|
|
(configuration-field-name field)
|
|
|
|
((configuration-field-getter field) config)))
|
|
|
|
fields)))
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
|
2017-03-24 10:00:14 +00:00
|
|
|
(define-syntax-rule (id ctx parts ...)
|
2021-05-08 01:46:51 +00:00
|
|
|
"Assemble PARTS into a raw (unhygienic) identifier."
|
2017-03-24 10:00:14 +00:00
|
|
|
(datum->syntax ctx (symbol-append (syntax->datum parts) ...)))
|
2017-03-24 10:00:13 +00:00
|
|
|
|
2021-06-12 19:17:08 +00:00
|
|
|
(define (define-maybe-helper serialize? prefix syn)
|
2021-05-16 05:09:39 +00:00
|
|
|
(syntax-case syn ()
|
|
|
|
((_ stem)
|
|
|
|
(with-syntax
|
|
|
|
((stem? (id #'stem #'stem #'?))
|
|
|
|
(maybe-stem? (id #'stem #'maybe- #'stem #'?))
|
2021-06-12 19:17:08 +00:00
|
|
|
(serialize-stem (if prefix
|
|
|
|
(id #'stem prefix #'serialize- #'stem)
|
|
|
|
(id #'stem #'serialize- #'stem)))
|
|
|
|
(serialize-maybe-stem (if prefix
|
|
|
|
(id #'stem prefix #'serialize-maybe- #'stem)
|
|
|
|
(id #'stem #'serialize-maybe- #'stem))))
|
2021-05-16 05:09:39 +00:00
|
|
|
#`(begin
|
|
|
|
(define (maybe-stem? val)
|
2022-08-24 12:40:38 +00:00
|
|
|
(or (not (maybe-value-set? val))
|
|
|
|
(stem? val)))
|
2021-05-16 05:09:39 +00:00
|
|
|
#,@(if serialize?
|
|
|
|
(list #'(define (serialize-maybe-stem field-name val)
|
|
|
|
(if (stem? val)
|
|
|
|
(serialize-stem field-name val)
|
|
|
|
"")))
|
|
|
|
'()))))))
|
|
|
|
|
2017-03-24 10:00:13 +00:00
|
|
|
(define-syntax define-maybe
|
|
|
|
(lambda (x)
|
2021-06-12 19:17:08 +00:00
|
|
|
(syntax-case x (no-serialization prefix)
|
2021-05-16 05:09:39 +00:00
|
|
|
((_ stem (no-serialization))
|
2021-06-12 19:17:08 +00:00
|
|
|
(define-maybe-helper #f #f #'(_ stem)))
|
|
|
|
((_ stem (prefix serializer-prefix))
|
|
|
|
(define-maybe-helper #t #'serializer-prefix #'(_ stem)))
|
2017-03-24 10:00:13 +00:00
|
|
|
((_ stem)
|
2021-06-12 19:17:08 +00:00
|
|
|
(define-maybe-helper #t #f #'(_ stem))))))
|
2021-05-16 05:09:39 +00:00
|
|
|
|
|
|
|
(define-syntax-rule (define-maybe/no-serialization stem)
|
|
|
|
(define-maybe stem (no-serialization)))
|
2017-03-24 10:00:13 +00:00
|
|
|
|
2022-05-17 11:39:26 +00:00
|
|
|
(define (normalize-field-type+def s)
|
|
|
|
(syntax-case s ()
|
|
|
|
((field-type def)
|
|
|
|
(identifier? #'field-type)
|
|
|
|
(values #'(field-type def)))
|
|
|
|
((field-type)
|
|
|
|
(identifier? #'field-type)
|
2022-08-24 12:40:39 +00:00
|
|
|
(values #'(field-type %unset-value)))
|
2022-05-17 11:39:26 +00:00
|
|
|
(field-type
|
|
|
|
(identifier? #'field-type)
|
2022-08-24 12:40:39 +00:00
|
|
|
(values #'(field-type %unset-value)))))
|
2022-05-17 11:39:26 +00:00
|
|
|
|
2021-06-12 19:17:08 +00:00
|
|
|
(define (define-configuration-helper serialize? serializer-prefix syn)
|
2021-05-08 01:46:51 +00:00
|
|
|
(syntax-case syn ()
|
2022-05-17 11:39:26 +00:00
|
|
|
((_ stem (field field-type+def doc custom-serializer ...) ...)
|
|
|
|
(with-syntax
|
|
|
|
((((field-type def) ...)
|
|
|
|
(map normalize-field-type+def #'(field-type+def ...))))
|
|
|
|
(with-syntax
|
|
|
|
(((field-getter ...)
|
|
|
|
(map (lambda (field)
|
|
|
|
(id #'stem #'stem #'- field))
|
|
|
|
#'(field ...)))
|
|
|
|
((field-predicate ...)
|
|
|
|
(map (lambda (type)
|
|
|
|
(id #'stem type #'?))
|
|
|
|
#'(field-type ...)))
|
|
|
|
((field-default ...)
|
|
|
|
(map (match-lambda
|
|
|
|
((field-type default-value)
|
|
|
|
default-value))
|
|
|
|
#'((field-type def) ...)))
|
|
|
|
((field-serializer ...)
|
|
|
|
(map (lambda (type custom-serializer)
|
|
|
|
(and serialize?
|
|
|
|
(match custom-serializer
|
|
|
|
((serializer)
|
|
|
|
serializer)
|
|
|
|
(()
|
|
|
|
(if serializer-prefix
|
|
|
|
(id #'stem
|
|
|
|
serializer-prefix
|
|
|
|
#'serialize- type)
|
|
|
|
(id #'stem #'serialize- type))))))
|
|
|
|
#'(field-type ...)
|
|
|
|
#'((custom-serializer ...) ...))))
|
2022-06-18 20:37:20 +00:00
|
|
|
(define (field-sanitizer name pred)
|
|
|
|
;; Define a macro for use as a record field sanitizer, where NAME
|
|
|
|
;; is the name of the field and PRED is the predicate that tells
|
|
|
|
;; whether a value is valid for this field.
|
|
|
|
#`(define-syntax #,(id #'stem #'validate- #'stem #'- name)
|
|
|
|
(lambda (s)
|
|
|
|
;; Make sure the given VALUE, for field NAME, passes PRED.
|
|
|
|
(syntax-case s ()
|
|
|
|
((_ value)
|
|
|
|
(with-syntax ((name #'#,name)
|
|
|
|
(pred #'#,pred)
|
|
|
|
(loc (datum->syntax #'value
|
|
|
|
(syntax-source #'value))))
|
|
|
|
#'(if (pred value)
|
|
|
|
value
|
|
|
|
(configuration-field-error
|
|
|
|
(and=> 'loc source-properties->location)
|
|
|
|
'name value))))))))
|
|
|
|
|
2022-05-17 11:39:26 +00:00
|
|
|
#`(begin
|
2022-06-18 20:37:20 +00:00
|
|
|
;; Define field validation macros.
|
|
|
|
#,@(map field-sanitizer
|
|
|
|
#'(field ...)
|
|
|
|
#'(field-predicate ...))
|
|
|
|
|
2022-05-17 11:39:26 +00:00
|
|
|
(define-record-type* #,(id #'stem #'< #'stem #'>)
|
2022-06-18 20:37:20 +00:00
|
|
|
stem
|
2022-05-17 11:39:26 +00:00
|
|
|
#,(id #'stem #'make- #'stem)
|
|
|
|
#,(id #'stem #'stem #'?)
|
|
|
|
(%location #,(id #'stem #'stem #'-location)
|
|
|
|
(default (and=> (current-source-location)
|
|
|
|
source-properties->location))
|
|
|
|
(innate))
|
|
|
|
#,@(map (lambda (name getter def)
|
2022-06-18 20:37:20 +00:00
|
|
|
#`(#,name #,getter (default #,def)
|
|
|
|
(sanitize
|
|
|
|
#,(id #'stem #'validate- #'stem #'- name))))
|
2022-05-17 11:39:26 +00:00
|
|
|
#'(field ...)
|
|
|
|
#'(field-getter ...)
|
|
|
|
#'(field-default ...)))
|
2022-06-18 20:37:20 +00:00
|
|
|
|
2022-05-17 11:39:26 +00:00
|
|
|
(define #,(id #'stem #'stem #'-fields)
|
|
|
|
(list (configuration-field
|
|
|
|
(name 'field)
|
|
|
|
(type 'field-type)
|
|
|
|
(getter field-getter)
|
|
|
|
(predicate field-predicate)
|
|
|
|
(serializer field-serializer)
|
|
|
|
(default-value-thunk
|
|
|
|
(lambda ()
|
|
|
|
(display '#,(id #'stem #'% #'stem))
|
2022-08-24 12:40:38 +00:00
|
|
|
(if (maybe-value-set? (syntax->datum field-default))
|
|
|
|
field-default
|
2022-05-17 11:39:28 +00:00
|
|
|
(configuration-missing-default-value
|
2022-08-24 12:40:38 +00:00
|
|
|
'#,(id #'stem #'% #'stem) 'field))))
|
2022-05-17 11:39:26 +00:00
|
|
|
(documentation doc))
|
2022-06-18 20:37:20 +00:00
|
|
|
...))))))))
|
2021-05-08 01:46:51 +00:00
|
|
|
|
2021-05-08 13:06:31 +00:00
|
|
|
(define no-serialization ;syntactic keyword for 'define-configuration'
|
|
|
|
'(no serialization))
|
|
|
|
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
(define-syntax define-configuration
|
2021-05-08 01:46:51 +00:00
|
|
|
(lambda (s)
|
2021-06-12 19:17:08 +00:00
|
|
|
(syntax-case s (no-serialization prefix)
|
2022-05-17 11:39:26 +00:00
|
|
|
((_ stem (field field-type+def doc custom-serializer ...) ...
|
2021-05-08 02:39:54 +00:00
|
|
|
(no-serialization))
|
2021-05-08 01:46:51 +00:00
|
|
|
(define-configuration-helper
|
2022-05-17 11:39:26 +00:00
|
|
|
#f #f #'(_ stem (field field-type+def doc custom-serializer ...)
|
2021-06-12 19:17:08 +00:00
|
|
|
...)))
|
2022-05-17 11:39:26 +00:00
|
|
|
((_ stem (field field-type+def doc custom-serializer ...) ...
|
2021-06-12 19:17:08 +00:00
|
|
|
(prefix serializer-prefix))
|
|
|
|
(define-configuration-helper
|
2022-05-17 11:39:26 +00:00
|
|
|
#t #'serializer-prefix #'(_ stem (field field-type+def
|
2021-06-12 19:17:08 +00:00
|
|
|
doc custom-serializer ...)
|
2021-05-08 02:39:54 +00:00
|
|
|
...)))
|
2022-05-17 11:39:26 +00:00
|
|
|
((_ stem (field field-type+def doc custom-serializer ...) ...)
|
2021-05-08 01:46:51 +00:00
|
|
|
(define-configuration-helper
|
2022-05-17 11:39:26 +00:00
|
|
|
#t #f #'(_ stem (field field-type+def doc custom-serializer ...)
|
2021-05-08 02:39:54 +00:00
|
|
|
...))))))
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
|
2021-05-17 14:18:34 +00:00
|
|
|
(define-syntax-rule (define-configuration/no-serialization
|
2022-05-17 11:39:26 +00:00
|
|
|
stem (field field-type+def
|
2021-05-17 14:18:34 +00:00
|
|
|
doc custom-serializer ...) ...)
|
2022-05-17 11:39:26 +00:00
|
|
|
(define-configuration stem (field field-type+def
|
2021-05-17 14:18:34 +00:00
|
|
|
doc custom-serializer ...) ...
|
|
|
|
(no-serialization)))
|
|
|
|
|
2021-05-08 02:39:54 +00:00
|
|
|
(define (empty-serializer field-name val) "")
|
|
|
|
(define serialize-package empty-serializer)
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
|
2022-08-24 12:40:39 +00:00
|
|
|
;; Ideally this should be an implementation detail, but we export it
|
|
|
|
;; to provide a simpler API that enables unsetting a configuration
|
2022-08-24 12:40:42 +00:00
|
|
|
;; field that has a maybe type, but also a default value. We give it
|
|
|
|
;; a value that sticks out to the reader when something goes wrong.
|
2022-08-24 12:40:39 +00:00
|
|
|
;;
|
|
|
|
;; An example use-case would be something like a network application
|
|
|
|
;; that uses a default port, but the field can explicitly be unset to
|
|
|
|
;; request a random port at startup.
|
2022-08-24 12:40:42 +00:00
|
|
|
(define %unset-value '%unset-marker%)
|
2022-08-24 12:40:39 +00:00
|
|
|
|
2022-08-24 12:40:38 +00:00
|
|
|
(define (maybe-value-set? value)
|
|
|
|
"Predicate to check whether a 'maybe' value was explicitly provided."
|
2022-08-24 12:40:39 +00:00
|
|
|
(not (eq? %unset-value value)))
|
2022-08-24 12:40:38 +00:00
|
|
|
|
2022-08-24 12:40:40 +00:00
|
|
|
;; Ideally there should be a compiler macro for this predicate, that expands
|
|
|
|
;; to a conditional that only instantiates the default value when needed.
|
|
|
|
(define* (maybe-value value #:optional (default #f))
|
|
|
|
"Returns VALUE, unless it is the unset value, in which case it returns
|
|
|
|
DEFAULT."
|
|
|
|
(if (maybe-value-set? value)
|
|
|
|
value
|
|
|
|
default))
|
|
|
|
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
;; A little helper to make it easier to document all those fields.
|
|
|
|
(define (generate-documentation documentation documentation-name)
|
|
|
|
(define (str x) (object->string x))
|
2021-06-09 13:06:27 +00:00
|
|
|
|
|
|
|
(define (package->symbol package)
|
|
|
|
"Return the first symbol name of a package that matches PACKAGE, else #f."
|
|
|
|
(let* ((module (file-name->module-name
|
|
|
|
(location-file (package-location package))))
|
|
|
|
(symbols (filter-map
|
|
|
|
identity
|
|
|
|
(module-map (lambda (symbol var)
|
|
|
|
(and (equal? package (variable-ref var))
|
|
|
|
symbol))
|
|
|
|
(resolve-module module)))))
|
|
|
|
(if (null? symbols)
|
|
|
|
#f
|
|
|
|
(first symbols))))
|
|
|
|
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
(define (generate configuration-name)
|
|
|
|
(match (assq-ref documentation configuration-name)
|
|
|
|
((fields . sub-documentation)
|
2021-06-09 13:06:26 +00:00
|
|
|
`((deftp (% (category "Data Type") (name ,(str configuration-name)))
|
|
|
|
(para "Available " (code ,(str configuration-name)) " fields are:")
|
|
|
|
(table
|
|
|
|
(% (formatter (asis)))
|
|
|
|
,@(map
|
|
|
|
(lambda (f)
|
|
|
|
(let ((field-name (configuration-field-name f))
|
|
|
|
(field-type (configuration-field-type f))
|
|
|
|
(field-docs (cdr (texi-fragment->stexi
|
|
|
|
(configuration-field-documentation f))))
|
|
|
|
(default (catch #t
|
|
|
|
(configuration-field-default-value-thunk f)
|
|
|
|
(lambda _ '%invalid))))
|
|
|
|
(define (show-default? val)
|
|
|
|
(or (string? val) (number? val) (boolean? val)
|
2021-06-09 13:06:27 +00:00
|
|
|
(package? val)
|
2021-06-09 13:06:26 +00:00
|
|
|
(and (symbol? val) (not (eq? val '%invalid)))
|
|
|
|
(and (list? val) (and-map show-default? val))))
|
|
|
|
|
2021-06-09 13:06:27 +00:00
|
|
|
(define (show-default val)
|
|
|
|
(cond
|
|
|
|
((package? val)
|
|
|
|
(symbol->string (package->symbol val)))
|
|
|
|
(else (str val))))
|
|
|
|
|
2021-06-09 13:06:26 +00:00
|
|
|
`(entry (% (heading
|
|
|
|
(code ,(str field-name))
|
|
|
|
,@(if (show-default? default)
|
|
|
|
`(" (default: "
|
2021-06-09 13:06:27 +00:00
|
|
|
(code ,(show-default default)) ")")
|
2021-06-09 13:06:26 +00:00
|
|
|
'())
|
|
|
|
" (type: " ,(str field-type) ")"))
|
|
|
|
(para ,@field-docs)
|
|
|
|
,@(append-map
|
|
|
|
generate
|
|
|
|
(or (assq-ref sub-documentation field-name)
|
|
|
|
'())))))
|
|
|
|
fields)))))))
|
services: Factorize configuration abstraction.
* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error)
(configuration-error, configuration-field-error)
(configuration-missing-field, configuration-field, serialize-configuration)
(validate-configuration, define-configuration, uglify-field-name)
(serialize-field, serialize-package, serialize-string)
(serialize-space-separated-string-list, space-separated-string-list?)
(serialize-file-name, file-name?, serialize-field-name)
(generate-documentation): Move duplicate code...
* gnu/services/configuration.scm: ...to this new file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2016-11-23 20:43:42 +00:00
|
|
|
(stexi->texi `(*fragment* . ,(generate documentation-name))))
|
2021-04-12 20:58:15 +00:00
|
|
|
|
|
|
|
(define (configuration->documentation configuration-symbol)
|
|
|
|
"Take CONFIGURATION-SYMBOL, the symbol corresponding to the name used when
|
|
|
|
defining a configuration record with DEFINE-CONFIGURATION, and output the
|
|
|
|
Texinfo documentation of its fields."
|
|
|
|
;; This is helper for a simple, straight-forward application of
|
|
|
|
;; GENERATE-DOCUMENTATION.
|
|
|
|
(let ((fields-getter (module-ref (current-module)
|
|
|
|
(symbol-append configuration-symbol
|
|
|
|
'-fields))))
|
2021-05-07 13:40:47 +00:00
|
|
|
(display (generate-documentation `((,configuration-symbol ,fields-getter))
|
|
|
|
configuration-symbol))))
|
2021-10-02 16:05:02 +00:00
|
|
|
|
|
|
|
(define* (filter-configuration-fields configuration-fields fields
|
|
|
|
#:optional negate?)
|
|
|
|
"Retrieve the fields listed in FIELDS from CONFIGURATION-FIELDS.
|
|
|
|
If NEGATE? is @code{#t}, retrieve all fields except FIELDS."
|
|
|
|
(filter (lambda (field)
|
|
|
|
(let ((member? (member (configuration-field-name field) fields)))
|
|
|
|
(if (not negate?) member? (not member?))))
|
|
|
|
configuration-fields))
|
|
|
|
|
|
|
|
|
|
|
|
(define* (interpose ls #:optional (delimiter "\n") (grammar 'infix))
|
|
|
|
"Same as @code{string-join}, but without join and string, returns an
|
|
|
|
DELIMITER interposed LS. Support 'infix and 'suffix GRAMMAR values."
|
|
|
|
(when (not (member grammar '(infix suffix)))
|
|
|
|
(raise
|
|
|
|
(formatted-message
|
|
|
|
(G_ "The GRAMMAR value must be 'infix or 'suffix, but ~a provided.")
|
|
|
|
grammar)))
|
|
|
|
(fold-right (lambda (e acc)
|
2021-11-01 07:43:12 +00:00
|
|
|
(cons e
|
2021-10-02 16:05:02 +00:00
|
|
|
(if (and (null? acc) (eq? grammar 'infix))
|
|
|
|
acc
|
|
|
|
(cons delimiter acc))))
|
|
|
|
'() ls))
|
|
|
|
|
|
|
|
(define (list-of pred?)
|
|
|
|
"Return a procedure that takes a list and check if all the elements of
|
|
|
|
the list result in @code{#t} when applying PRED? on them."
|
|
|
|
(lambda (x)
|
|
|
|
(if (list? x)
|
|
|
|
(every pred? x)
|
|
|
|
#f)))
|
|
|
|
|
|
|
|
|
|
|
|
(define list-of-strings?
|
|
|
|
(list-of string?))
|
|
|
|
|
|
|
|
(define alist? list?)
|
|
|
|
|
|
|
|
(define serialize-file-like empty-serializer)
|
|
|
|
|
|
|
|
(define (text-config? config)
|
|
|
|
(list-of file-like?))
|
|
|
|
(define (serialize-text-config field-name val)
|
2021-11-01 07:43:12 +00:00
|
|
|
#~(string-append
|
|
|
|
#$@(interpose
|
|
|
|
(map
|
|
|
|
(lambda (e)
|
|
|
|
#~(begin
|
|
|
|
(use-modules (ice-9 rdelim))
|
|
|
|
(with-fluids ((%default-port-encoding "UTF-8"))
|
|
|
|
(with-input-from-file #$e read-string))))
|
|
|
|
val)
|
|
|
|
"\n" 'suffix)))
|
2021-10-02 16:05:02 +00:00
|
|
|
|
|
|
|
(define ((generic-serialize-alist-entry serialize-field) entry)
|
|
|
|
"Apply the SERIALIZE-FIELD procedure on the field and value of ENTRY."
|
|
|
|
(match entry
|
|
|
|
((field . val) (serialize-field field val))))
|
|
|
|
|
|
|
|
(define (generic-serialize-alist combine serialize-field fields)
|
|
|
|
"Generate a configuration from an association list FIELDS.
|
|
|
|
|
|
|
|
SERIALIZE-FIELD is a procedure that takes two arguments, it will be
|
|
|
|
applied on the fields and values of FIELDS using the
|
|
|
|
@code{generic-serialize-alist-entry} procedure.
|
|
|
|
|
|
|
|
COMBINE is a procedure that takes one or more arguments and combines
|
|
|
|
all the alist entries into one value, @code{string-append} or
|
|
|
|
@code{append} are usually good candidates for this.
|
|
|
|
|
|
|
|
See the @code{serialize-alist} procedure in `@code{(gnu home services
|
|
|
|
version-control}' for an example usage.)}"
|
|
|
|
(apply combine
|
|
|
|
(map (generic-serialize-alist-entry serialize-field) fields)))
|