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> |
bdcf0e6f | 4 | ;;; Copyright © 2017, 2018 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) | |
d132d9f9 | 25 | #:use-module ((guix utils) #:select (source-properties->location)) |
5305ed20 JL |
26 | #:autoload (texinfo) (texi-fragment->stexi) |
27 | #:autoload (texinfo serialize) (stexi->texi) | |
28 | #:use-module (ice-9 match) | |
29 | #:use-module ((srfi srfi-1) #:select (append-map)) | |
30 | #:use-module (srfi srfi-34) | |
31 | #:use-module (srfi srfi-35) | |
32 | #:export (configuration-field | |
33 | configuration-field-name | |
78cef99b | 34 | configuration-field-type |
5305ed20 JL |
35 | configuration-missing-field |
36 | configuration-field-error | |
8e3f813f JD |
37 | configuration-field-serializer |
38 | configuration-field-getter | |
78cef99b CL |
39 | configuration-field-default-value-thunk |
40 | configuration-field-documentation | |
5305ed20 | 41 | serialize-configuration |
e7c797f3 | 42 | define-maybe |
5305ed20 JL |
43 | define-configuration |
44 | validate-configuration | |
45 | generate-documentation | |
5305ed20 JL |
46 | serialize-package)) |
47 | ||
48 | ;;; Commentary: | |
49 | ;;; | |
50 | ;;; Syntax for creating Scheme bindings to complex configuration files. | |
51 | ;;; | |
52 | ;;; Code: | |
53 | ||
54 | (define-condition-type &configuration-error &error | |
55 | configuration-error?) | |
56 | ||
57 | (define (configuration-error message) | |
58 | (raise (condition (&message (message message)) | |
59 | (&configuration-error)))) | |
60 | (define (configuration-field-error field val) | |
61 | (configuration-error | |
62 | (format #f "Invalid value for field ~a: ~s" field val))) | |
63 | (define (configuration-missing-field kind field) | |
64 | (configuration-error | |
65 | (format #f "~a configuration missing required field ~a" kind field))) | |
66 | ||
67 | (define-record-type* <configuration-field> | |
68 | configuration-field make-configuration-field configuration-field? | |
69 | (name configuration-field-name) | |
70 | (type configuration-field-type) | |
71 | (getter configuration-field-getter) | |
72 | (predicate configuration-field-predicate) | |
73 | (serializer configuration-field-serializer) | |
74 | (default-value-thunk configuration-field-default-value-thunk) | |
75 | (documentation configuration-field-documentation)) | |
76 | ||
77 | (define (serialize-configuration config fields) | |
bdcf0e6f CL |
78 | #~(string-append |
79 | #$@(map (lambda (field) | |
80 | ((configuration-field-serializer field) | |
81 | (configuration-field-name field) | |
82 | ((configuration-field-getter field) config))) | |
83 | fields))) | |
5305ed20 JL |
84 | |
85 | (define (validate-configuration config fields) | |
86 | (for-each (lambda (field) | |
87 | (let ((val ((configuration-field-getter field) config))) | |
88 | (unless ((configuration-field-predicate field) val) | |
89 | (configuration-field-error | |
90 | (configuration-field-name field) val)))) | |
91 | fields)) | |
92 | ||
d02c3c22 MO |
93 | (define-syntax-rule (id ctx parts ...) |
94 | "Assemble PARTS into a raw (unhygienic) identifier." | |
95 | (datum->syntax ctx (symbol-append (syntax->datum parts) ...))) | |
e7c797f3 MO |
96 | |
97 | (define-syntax define-maybe | |
98 | (lambda (x) | |
99 | (syntax-case x () | |
100 | ((_ stem) | |
101 | (with-syntax | |
102 | ((stem? (id #'stem #'stem #'?)) | |
103 | (maybe-stem? (id #'stem #'maybe- #'stem #'?)) | |
104 | (serialize-stem (id #'stem #'serialize- #'stem)) | |
105 | (serialize-maybe-stem (id #'stem #'serialize-maybe- #'stem))) | |
106 | #'(begin | |
107 | (define (maybe-stem? val) | |
108 | (or (eq? val 'disabled) (stem? val))) | |
109 | (define (serialize-maybe-stem field-name val) | |
bdcf0e6f | 110 | (if (stem? val) (serialize-stem field-name val) "")))))))) |
e7c797f3 | 111 | |
5305ed20 JL |
112 | (define-syntax define-configuration |
113 | (lambda (stx) | |
5305ed20 JL |
114 | (syntax-case stx () |
115 | ((_ stem (field (field-type def) doc) ...) | |
116 | (with-syntax (((field-getter ...) | |
117 | (map (lambda (field) | |
118 | (id #'stem #'stem #'- field)) | |
119 | #'(field ...))) | |
120 | ((field-predicate ...) | |
121 | (map (lambda (type) | |
122 | (id #'stem type #'?)) | |
123 | #'(field-type ...))) | |
124 | ((field-serializer ...) | |
125 | (map (lambda (type) | |
126 | (id #'stem #'serialize- type)) | |
127 | #'(field-type ...)))) | |
128 | #`(begin | |
129 | (define-record-type* #,(id #'stem #'< #'stem #'>) | |
130 | #,(id #'stem #'% #'stem) | |
131 | #,(id #'stem #'make- #'stem) | |
132 | #,(id #'stem #'stem #'?) | |
d132d9f9 OP |
133 | (%location #,(id #'stem #'-location) |
134 | (default (and=> (current-source-location) | |
135 | source-properties->location)) | |
136 | (innate)) | |
5305ed20 JL |
137 | (field field-getter (default def)) |
138 | ...) | |
139 | (define #,(id #'stem #'stem #'-fields) | |
140 | (list (configuration-field | |
141 | (name 'field) | |
142 | (type 'field-type) | |
143 | (getter field-getter) | |
144 | (predicate field-predicate) | |
145 | (serializer field-serializer) | |
146 | (default-value-thunk (lambda () def)) | |
147 | (documentation doc)) | |
148 | ...)) | |
149 | (define-syntax-rule (stem arg (... ...)) | |
150 | (let ((conf (#,(id #'stem #'% #'stem) arg (... ...)))) | |
151 | (validate-configuration conf | |
152 | #,(id #'stem #'stem #'-fields)) | |
153 | conf)))))))) | |
154 | ||
5305ed20 | 155 | (define (serialize-package field-name val) |
bdcf0e6f | 156 | "") |
5305ed20 | 157 | |
5305ed20 JL |
158 | ;; A little helper to make it easier to document all those fields. |
159 | (define (generate-documentation documentation documentation-name) | |
160 | (define (str x) (object->string x)) | |
161 | (define (generate configuration-name) | |
162 | (match (assq-ref documentation configuration-name) | |
163 | ((fields . sub-documentation) | |
164 | `((para "Available " (code ,(str configuration-name)) " fields are:") | |
165 | ,@(map | |
166 | (lambda (f) | |
167 | (let ((field-name (configuration-field-name f)) | |
168 | (field-type (configuration-field-type f)) | |
169 | (field-docs (cdr (texi-fragment->stexi | |
170 | (configuration-field-documentation f)))) | |
171 | (default (catch #t | |
172 | (configuration-field-default-value-thunk f) | |
173 | (lambda _ '%invalid)))) | |
174 | (define (show-default? val) | |
19ff1f26 | 175 | (or (string? val) (number? val) (boolean? val) |
5305ed20 JL |
176 | (and (symbol? val) (not (eq? val '%invalid))) |
177 | (and (list? val) (and-map show-default? val)))) | |
178 | `(deftypevr (% (category | |
179 | (code ,(str configuration-name)) " parameter") | |
180 | (data-type ,(str field-type)) | |
181 | (name ,(str field-name))) | |
182 | ,@field-docs | |
183 | ,@(if (show-default? default) | |
184 | `((para "Defaults to " (samp ,(str default)) ".")) | |
185 | '()) | |
186 | ,@(append-map | |
187 | generate | |
188 | (or (assq-ref sub-documentation field-name) '()))))) | |
189 | fields))))) | |
190 | (stexi->texi `(*fragment* . ,(generate documentation-name)))) |