Commit | Line | Data |
---|---|---|
5305ed20 JL |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2015 Andy Wingo <wingo@igalia.com> | |
e7c797f3 | 3 | ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com> |
19ff1f26 | 4 | ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org> |
5305ed20 JL |
5 | ;;; |
6 | ;;; This file is part of GNU Guix. | |
7 | ;;; | |
8 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
9 | ;;; under the terms of the GNU General Public License as published by | |
10 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
11 | ;;; your option) any later version. | |
12 | ;;; | |
13 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
14 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;;; GNU General Public License for more details. | |
17 | ;;; | |
18 | ;;; You should have received a copy of the GNU General Public License | |
19 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
20 | ||
21 | (define-module (gnu services configuration) | |
22 | #:use-module (guix packages) | |
23 | #:use-module (guix records) | |
24 | #:use-module (guix gexp) | |
25 | #:autoload (texinfo) (texi-fragment->stexi) | |
26 | #:autoload (texinfo serialize) (stexi->texi) | |
27 | #:use-module (ice-9 match) | |
28 | #:use-module ((srfi srfi-1) #:select (append-map)) | |
29 | #:use-module (srfi srfi-34) | |
30 | #:use-module (srfi srfi-35) | |
31 | #:export (configuration-field | |
32 | configuration-field-name | |
78cef99b | 33 | configuration-field-type |
5305ed20 JL |
34 | configuration-missing-field |
35 | configuration-field-error | |
8e3f813f JD |
36 | configuration-field-serializer |
37 | configuration-field-getter | |
78cef99b CL |
38 | configuration-field-default-value-thunk |
39 | configuration-field-documentation | |
5305ed20 | 40 | serialize-configuration |
e7c797f3 | 41 | define-maybe |
5305ed20 JL |
42 | define-configuration |
43 | validate-configuration | |
44 | generate-documentation | |
5305ed20 JL |
45 | serialize-package)) |
46 | ||
47 | ;;; Commentary: | |
48 | ;;; | |
49 | ;;; Syntax for creating Scheme bindings to complex configuration files. | |
50 | ;;; | |
51 | ;;; Code: | |
52 | ||
53 | (define-condition-type &configuration-error &error | |
54 | configuration-error?) | |
55 | ||
56 | (define (configuration-error message) | |
57 | (raise (condition (&message (message message)) | |
58 | (&configuration-error)))) | |
59 | (define (configuration-field-error field val) | |
60 | (configuration-error | |
61 | (format #f "Invalid value for field ~a: ~s" field val))) | |
62 | (define (configuration-missing-field kind field) | |
63 | (configuration-error | |
64 | (format #f "~a configuration missing required field ~a" kind field))) | |
65 | ||
66 | (define-record-type* <configuration-field> | |
67 | configuration-field make-configuration-field configuration-field? | |
68 | (name configuration-field-name) | |
69 | (type configuration-field-type) | |
70 | (getter configuration-field-getter) | |
71 | (predicate configuration-field-predicate) | |
72 | (serializer configuration-field-serializer) | |
73 | (default-value-thunk configuration-field-default-value-thunk) | |
74 | (documentation configuration-field-documentation)) | |
75 | ||
76 | (define (serialize-configuration config fields) | |
77 | (for-each (lambda (field) | |
78 | ((configuration-field-serializer field) | |
79 | (configuration-field-name field) | |
80 | ((configuration-field-getter field) config))) | |
81 | fields)) | |
82 | ||
83 | (define (validate-configuration config fields) | |
84 | (for-each (lambda (field) | |
85 | (let ((val ((configuration-field-getter field) config))) | |
86 | (unless ((configuration-field-predicate field) val) | |
87 | (configuration-field-error | |
88 | (configuration-field-name field) val)))) | |
89 | fields)) | |
90 | ||
d02c3c22 MO |
91 | (define-syntax-rule (id ctx parts ...) |
92 | "Assemble PARTS into a raw (unhygienic) identifier." | |
93 | (datum->syntax ctx (symbol-append (syntax->datum parts) ...))) | |
e7c797f3 MO |
94 | |
95 | (define-syntax define-maybe | |
96 | (lambda (x) | |
97 | (syntax-case x () | |
98 | ((_ stem) | |
99 | (with-syntax | |
100 | ((stem? (id #'stem #'stem #'?)) | |
101 | (maybe-stem? (id #'stem #'maybe- #'stem #'?)) | |
102 | (serialize-stem (id #'stem #'serialize- #'stem)) | |
103 | (serialize-maybe-stem (id #'stem #'serialize-maybe- #'stem))) | |
104 | #'(begin | |
105 | (define (maybe-stem? val) | |
106 | (or (eq? val 'disabled) (stem? val))) | |
107 | (define (serialize-maybe-stem field-name val) | |
108 | (when (stem? val) (serialize-stem field-name val))))))))) | |
109 | ||
5305ed20 JL |
110 | (define-syntax define-configuration |
111 | (lambda (stx) | |
5305ed20 JL |
112 | (syntax-case stx () |
113 | ((_ stem (field (field-type def) doc) ...) | |
114 | (with-syntax (((field-getter ...) | |
115 | (map (lambda (field) | |
116 | (id #'stem #'stem #'- field)) | |
117 | #'(field ...))) | |
118 | ((field-predicate ...) | |
119 | (map (lambda (type) | |
120 | (id #'stem type #'?)) | |
121 | #'(field-type ...))) | |
122 | ((field-serializer ...) | |
123 | (map (lambda (type) | |
124 | (id #'stem #'serialize- type)) | |
125 | #'(field-type ...)))) | |
126 | #`(begin | |
127 | (define-record-type* #,(id #'stem #'< #'stem #'>) | |
128 | #,(id #'stem #'% #'stem) | |
129 | #,(id #'stem #'make- #'stem) | |
130 | #,(id #'stem #'stem #'?) | |
131 | (field field-getter (default def)) | |
132 | ...) | |
133 | (define #,(id #'stem #'stem #'-fields) | |
134 | (list (configuration-field | |
135 | (name 'field) | |
136 | (type 'field-type) | |
137 | (getter field-getter) | |
138 | (predicate field-predicate) | |
139 | (serializer field-serializer) | |
140 | (default-value-thunk (lambda () def)) | |
141 | (documentation doc)) | |
142 | ...)) | |
143 | (define-syntax-rule (stem arg (... ...)) | |
144 | (let ((conf (#,(id #'stem #'% #'stem) arg (... ...)))) | |
145 | (validate-configuration conf | |
146 | #,(id #'stem #'stem #'-fields)) | |
147 | conf)))))))) | |
148 | ||
5305ed20 JL |
149 | (define (serialize-package field-name val) |
150 | #f) | |
151 | ||
5305ed20 JL |
152 | ;; A little helper to make it easier to document all those fields. |
153 | (define (generate-documentation documentation documentation-name) | |
154 | (define (str x) (object->string x)) | |
155 | (define (generate configuration-name) | |
156 | (match (assq-ref documentation configuration-name) | |
157 | ((fields . sub-documentation) | |
158 | `((para "Available " (code ,(str configuration-name)) " fields are:") | |
159 | ,@(map | |
160 | (lambda (f) | |
161 | (let ((field-name (configuration-field-name f)) | |
162 | (field-type (configuration-field-type f)) | |
163 | (field-docs (cdr (texi-fragment->stexi | |
164 | (configuration-field-documentation f)))) | |
165 | (default (catch #t | |
166 | (configuration-field-default-value-thunk f) | |
167 | (lambda _ '%invalid)))) | |
168 | (define (show-default? val) | |
19ff1f26 | 169 | (or (string? val) (number? val) (boolean? val) |
5305ed20 JL |
170 | (and (symbol? val) (not (eq? val '%invalid))) |
171 | (and (list? val) (and-map show-default? val)))) | |
172 | `(deftypevr (% (category | |
173 | (code ,(str configuration-name)) " parameter") | |
174 | (data-type ,(str field-type)) | |
175 | (name ,(str field-name))) | |
176 | ,@field-docs | |
177 | ,@(if (show-default? default) | |
178 | `((para "Defaults to " (samp ,(str default)) ".")) | |
179 | '()) | |
180 | ,@(append-map | |
181 | generate | |
182 | (or (assq-ref sub-documentation field-name) '()))))) | |
183 | fields))))) | |
184 | (stexi->texi `(*fragment* . ,(generate documentation-name)))) |