configuration.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (gnu services configuration)
  23. #:use-module (guix packages)
  24. #:use-module (guix records)
  25. #:use-module (guix gexp)
  26. #:use-module ((guix utils) #:select (source-properties->location))
  27. #:autoload (texinfo) (texi-fragment->stexi)
  28. #:autoload (texinfo serialize) (stexi->texi)
  29. #:use-module (ice-9 match)
  30. #:use-module ((srfi srfi-1) #:select (append-map))
  31. #:use-module (srfi srfi-34)
  32. #:use-module (srfi srfi-35)
  33. #:export (configuration-field
  34. configuration-field-name
  35. configuration-field-type
  36. configuration-missing-field
  37. configuration-field-error
  38. configuration-field-serializer
  39. configuration-field-getter
  40. configuration-field-default-value-thunk
  41. configuration-field-documentation
  42. configuration-error?
  43. define-configuration
  44. define-configuration/no-serialization
  45. no-serialization
  46. serialize-configuration
  47. define-maybe
  48. define-maybe/no-serialization
  49. validate-configuration
  50. generate-documentation
  51. configuration->documentation
  52. empty-serializer
  53. serialize-package))
  54. ;;; Commentary:
  55. ;;;
  56. ;;; Syntax for creating Scheme bindings to complex configuration files.
  57. ;;;
  58. ;;; Code:
  59. (define-condition-type &configuration-error &error
  60. configuration-error?)
  61. (define (configuration-error message)
  62. (raise (condition (&message (message message))
  63. (&configuration-error))))
  64. (define (configuration-field-error field val)
  65. (configuration-error
  66. (format #f "Invalid value for field ~a: ~s" field val)))
  67. (define (configuration-missing-field kind field)
  68. (configuration-error
  69. (format #f "~a configuration missing required field ~a" kind field)))
  70. (define (configuration-no-default-value kind field)
  71. (configuration-error
  72. (format #f "The field `~a' of the `~a' configuration record \
  73. does not have a default value" field kind)))
  74. (define-record-type* <configuration-field>
  75. configuration-field make-configuration-field configuration-field?
  76. (name configuration-field-name)
  77. (type configuration-field-type)
  78. (getter configuration-field-getter)
  79. (predicate configuration-field-predicate)
  80. (serializer configuration-field-serializer)
  81. (default-value-thunk configuration-field-default-value-thunk)
  82. (documentation configuration-field-documentation))
  83. (define (serialize-configuration config fields)
  84. #~(string-append
  85. #$@(map (lambda (field)
  86. ((configuration-field-serializer field)
  87. (configuration-field-name field)
  88. ((configuration-field-getter field) config)))
  89. fields)))
  90. (define (validate-configuration config fields)
  91. (for-each (lambda (field)
  92. (let ((val ((configuration-field-getter field) config)))
  93. (unless ((configuration-field-predicate field) val)
  94. (configuration-field-error
  95. (configuration-field-name field) val))))
  96. fields))
  97. (define-syntax-rule (id ctx parts ...)
  98. "Assemble PARTS into a raw (unhygienic) identifier."
  99. (datum->syntax ctx (symbol-append (syntax->datum parts) ...)))
  100. (define (define-maybe-helper serialize? syn)
  101. (syntax-case syn ()
  102. ((_ stem)
  103. (with-syntax
  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)))
  108. #`(begin
  109. (define (maybe-stem? val)
  110. (or (eq? val 'disabled) (stem? val)))
  111. #,@(if serialize?
  112. (list #'(define (serialize-maybe-stem field-name val)
  113. (if (stem? val)
  114. (serialize-stem field-name val)
  115. "")))
  116. '()))))))
  117. (define-syntax define-maybe
  118. (lambda (x)
  119. (syntax-case x (no-serialization)
  120. ((_ stem (no-serialization))
  121. (define-maybe-helper #f #'(_ stem)))
  122. ((_ stem)
  123. (define-maybe-helper #t #'(_ stem))))))
  124. (define-syntax-rule (define-maybe/no-serialization stem)
  125. (define-maybe stem (no-serialization)))
  126. (define (define-configuration-helper serialize? syn)
  127. (syntax-case syn ()
  128. ((_ stem (field (field-type def ...) doc custom-serializer ...) ...)
  129. (with-syntax (((field-getter ...)
  130. (map (lambda (field)
  131. (id #'stem #'stem #'- field))
  132. #'(field ...)))
  133. ((field-predicate ...)
  134. (map (lambda (type)
  135. (id #'stem type #'?))
  136. #'(field-type ...)))
  137. ((field-default ...)
  138. (map (match-lambda
  139. ((field-type default-value)
  140. default-value)
  141. ((field-type)
  142. ;; Quote `undefined' to prevent a possibly
  143. ;; unbound warning.
  144. (syntax 'undefined)))
  145. #'((field-type def ...) ...)))
  146. ((field-serializer ...)
  147. (map (lambda (type custom-serializer)
  148. (and serialize?
  149. (match custom-serializer
  150. ((serializer)
  151. serializer)
  152. (()
  153. (id #'stem #'serialize- type)))))
  154. #'(field-type ...)
  155. #'((custom-serializer ...) ...))))
  156. #`(begin
  157. (define-record-type* #,(id #'stem #'< #'stem #'>)
  158. #,(id #'stem #'% #'stem)
  159. #,(id #'stem #'make- #'stem)
  160. #,(id #'stem #'stem #'?)
  161. (%location #,(id #'stem #'stem #'-location)
  162. (default (and=> (current-source-location)
  163. source-properties->location))
  164. (innate))
  165. #,@(map (lambda (name getter def)
  166. (if (eq? (syntax->datum def) (quote 'undefined))
  167. #`(#,name #,getter)
  168. #`(#,name #,getter (default #,def))))
  169. #'(field ...)
  170. #'(field-getter ...)
  171. #'(field-default ...)))
  172. (define #,(id #'stem #'stem #'-fields)
  173. (list (configuration-field
  174. (name 'field)
  175. (type 'field-type)
  176. (getter field-getter)
  177. (predicate field-predicate)
  178. (serializer field-serializer)
  179. (default-value-thunk
  180. (lambda ()
  181. (display '#,(id #'stem #'% #'stem))
  182. (if (eq? (syntax->datum field-default)
  183. 'undefined)
  184. (configuration-no-default-value
  185. '#,(id #'stem #'% #'stem) 'field)
  186. field-default)))
  187. (documentation doc))
  188. ...))
  189. (define-syntax-rule (stem arg (... ...))
  190. (let ((conf (#,(id #'stem #'% #'stem) arg (... ...))))
  191. (validate-configuration conf
  192. #,(id #'stem #'stem #'-fields))
  193. conf)))))))
  194. (define no-serialization ;syntactic keyword for 'define-configuration'
  195. '(no serialization))
  196. (define-syntax define-configuration
  197. (lambda (s)
  198. (syntax-case s (no-serialization)
  199. ((_ stem (field (field-type def ...) doc custom-serializer ...) ...
  200. (no-serialization))
  201. (define-configuration-helper
  202. #f #'(_ stem (field (field-type def ...) doc custom-serializer ...)
  203. ...)))
  204. ((_ stem (field (field-type def ...) doc custom-serializer ...) ...)
  205. (define-configuration-helper
  206. #t #'(_ stem (field (field-type def ...) doc custom-serializer ...)
  207. ...))))))
  208. (define-syntax-rule (define-configuration/no-serialization
  209. stem (field (field-type def ...)
  210. doc custom-serializer ...) ...)
  211. (define-configuration stem (field (field-type def ...)
  212. doc custom-serializer ...) ...
  213. (no-serialization)))
  214. (define (empty-serializer field-name val) "")
  215. (define serialize-package empty-serializer)
  216. ;; A little helper to make it easier to document all those fields.
  217. (define (generate-documentation documentation documentation-name)
  218. (define (str x) (object->string x))
  219. (define (generate configuration-name)
  220. (match (assq-ref documentation configuration-name)
  221. ((fields . sub-documentation)
  222. `((para "Available " (code ,(str configuration-name)) " fields are:")
  223. ,@(map
  224. (lambda (f)
  225. (let ((field-name (configuration-field-name f))
  226. (field-type (configuration-field-type f))
  227. (field-docs (cdr (texi-fragment->stexi
  228. (configuration-field-documentation f))))
  229. (default (catch #t
  230. (configuration-field-default-value-thunk f)
  231. (lambda _ '%invalid))))
  232. (define (show-default? val)
  233. (or (string? val) (number? val) (boolean? val)
  234. (and (symbol? val) (not (eq? val '%invalid)))
  235. (and (list? val) (and-map show-default? val))))
  236. `(deftypevr (% (category
  237. (code ,(str configuration-name)) " parameter")
  238. (data-type ,(str field-type))
  239. (name ,(str field-name)))
  240. ,@field-docs
  241. ,@(if (show-default? default)
  242. `((para "Defaults to " (samp ,(str default)) "."))
  243. '())
  244. ,@(append-map
  245. generate
  246. (or (assq-ref sub-documentation field-name) '())))))
  247. fields)))))
  248. (stexi->texi `(*fragment* . ,(generate documentation-name))))
  249. (define (configuration->documentation configuration-symbol)
  250. "Take CONFIGURATION-SYMBOL, the symbol corresponding to the name used when
  251. defining a configuration record with DEFINE-CONFIGURATION, and output the
  252. Texinfo documentation of its fields."
  253. ;; This is helper for a simple, straight-forward application of
  254. ;; GENERATE-DOCUMENTATION.
  255. (let ((fields-getter (module-ref (current-module)
  256. (symbol-append configuration-symbol
  257. '-fields))))
  258. (display (generate-documentation `((,configuration-symbol ,fields-getter))
  259. configuration-symbol))))