configuration.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #:use-module ((guix diagnostics) #:select (location-file))
  28. #:use-module ((guix modules) #:select (file-name->module-name))
  29. #:autoload (texinfo) (texi-fragment->stexi)
  30. #:autoload (texinfo serialize) (stexi->texi)
  31. #:use-module (ice-9 match)
  32. #:use-module (srfi srfi-1)
  33. #:use-module (srfi srfi-34)
  34. #:use-module (srfi srfi-35)
  35. #:export (configuration-field
  36. configuration-field-name
  37. configuration-field-type
  38. configuration-missing-field
  39. configuration-field-error
  40. configuration-field-serializer
  41. configuration-field-getter
  42. configuration-field-default-value-thunk
  43. configuration-field-documentation
  44. configuration-error?
  45. define-configuration
  46. define-configuration/no-serialization
  47. no-serialization
  48. serialize-configuration
  49. define-maybe
  50. define-maybe/no-serialization
  51. validate-configuration
  52. generate-documentation
  53. configuration->documentation
  54. empty-serializer
  55. serialize-package))
  56. ;;; Commentary:
  57. ;;;
  58. ;;; Syntax for creating Scheme bindings to complex configuration files.
  59. ;;;
  60. ;;; Code:
  61. (define-condition-type &configuration-error &error
  62. configuration-error?)
  63. (define (configuration-error message)
  64. (raise (condition (&message (message message))
  65. (&configuration-error))))
  66. (define (configuration-field-error field val)
  67. (configuration-error
  68. (format #f "Invalid value for field ~a: ~s" field val)))
  69. (define (configuration-missing-field kind field)
  70. (configuration-error
  71. (format #f "~a configuration missing required field ~a" kind field)))
  72. (define (configuration-no-default-value kind field)
  73. (configuration-error
  74. (format #f "The field `~a' of the `~a' configuration record \
  75. does not have a default value" field kind)))
  76. (define-record-type* <configuration-field>
  77. configuration-field make-configuration-field configuration-field?
  78. (name configuration-field-name)
  79. (type configuration-field-type)
  80. (getter configuration-field-getter)
  81. (predicate configuration-field-predicate)
  82. (serializer configuration-field-serializer)
  83. (default-value-thunk configuration-field-default-value-thunk)
  84. (documentation configuration-field-documentation))
  85. (define (serialize-configuration config fields)
  86. #~(string-append
  87. #$@(map (lambda (field)
  88. ((configuration-field-serializer field)
  89. (configuration-field-name field)
  90. ((configuration-field-getter field) config)))
  91. fields)))
  92. (define (validate-configuration config fields)
  93. (for-each (lambda (field)
  94. (let ((val ((configuration-field-getter field) config)))
  95. (unless ((configuration-field-predicate field) val)
  96. (configuration-field-error
  97. (configuration-field-name field) val))))
  98. fields))
  99. (define-syntax-rule (id ctx parts ...)
  100. "Assemble PARTS into a raw (unhygienic) identifier."
  101. (datum->syntax ctx (symbol-append (syntax->datum parts) ...)))
  102. (define (define-maybe-helper serialize? prefix syn)
  103. (syntax-case syn ()
  104. ((_ stem)
  105. (with-syntax
  106. ((stem? (id #'stem #'stem #'?))
  107. (maybe-stem? (id #'stem #'maybe- #'stem #'?))
  108. (serialize-stem (if prefix
  109. (id #'stem prefix #'serialize- #'stem)
  110. (id #'stem #'serialize- #'stem)))
  111. (serialize-maybe-stem (if prefix
  112. (id #'stem prefix #'serialize-maybe- #'stem)
  113. (id #'stem #'serialize-maybe- #'stem))))
  114. #`(begin
  115. (define (maybe-stem? val)
  116. (or (eq? val 'disabled) (stem? val)))
  117. #,@(if serialize?
  118. (list #'(define (serialize-maybe-stem field-name val)
  119. (if (stem? val)
  120. (serialize-stem field-name val)
  121. "")))
  122. '()))))))
  123. (define-syntax define-maybe
  124. (lambda (x)
  125. (syntax-case x (no-serialization prefix)
  126. ((_ stem (no-serialization))
  127. (define-maybe-helper #f #f #'(_ stem)))
  128. ((_ stem (prefix serializer-prefix))
  129. (define-maybe-helper #t #'serializer-prefix #'(_ stem)))
  130. ((_ stem)
  131. (define-maybe-helper #t #f #'(_ stem))))))
  132. (define-syntax-rule (define-maybe/no-serialization stem)
  133. (define-maybe stem (no-serialization)))
  134. (define (define-configuration-helper serialize? serializer-prefix syn)
  135. (syntax-case syn ()
  136. ((_ stem (field (field-type def ...) doc custom-serializer ...) ...)
  137. (with-syntax (((field-getter ...)
  138. (map (lambda (field)
  139. (id #'stem #'stem #'- field))
  140. #'(field ...)))
  141. ((field-predicate ...)
  142. (map (lambda (type)
  143. (id #'stem type #'?))
  144. #'(field-type ...)))
  145. ((field-default ...)
  146. (map (match-lambda
  147. ((field-type default-value)
  148. default-value)
  149. ((field-type)
  150. ;; Quote `undefined' to prevent a possibly
  151. ;; unbound warning.
  152. (syntax 'undefined)))
  153. #'((field-type def ...) ...)))
  154. ((field-serializer ...)
  155. (map (lambda (type custom-serializer)
  156. (and serialize?
  157. (match custom-serializer
  158. ((serializer)
  159. serializer)
  160. (()
  161. (if serializer-prefix
  162. (id #'stem
  163. serializer-prefix
  164. #'serialize- type)
  165. (id #'stem #'serialize- type))))))
  166. #'(field-type ...)
  167. #'((custom-serializer ...) ...))))
  168. #`(begin
  169. (define-record-type* #,(id #'stem #'< #'stem #'>)
  170. #,(id #'stem #'% #'stem)
  171. #,(id #'stem #'make- #'stem)
  172. #,(id #'stem #'stem #'?)
  173. (%location #,(id #'stem #'stem #'-location)
  174. (default (and=> (current-source-location)
  175. source-properties->location))
  176. (innate))
  177. #,@(map (lambda (name getter def)
  178. (if (eq? (syntax->datum def) (quote 'undefined))
  179. #`(#,name #,getter)
  180. #`(#,name #,getter (default #,def))))
  181. #'(field ...)
  182. #'(field-getter ...)
  183. #'(field-default ...)))
  184. (define #,(id #'stem #'stem #'-fields)
  185. (list (configuration-field
  186. (name 'field)
  187. (type 'field-type)
  188. (getter field-getter)
  189. (predicate field-predicate)
  190. (serializer field-serializer)
  191. (default-value-thunk
  192. (lambda ()
  193. (display '#,(id #'stem #'% #'stem))
  194. (if (eq? (syntax->datum field-default)
  195. 'undefined)
  196. (configuration-no-default-value
  197. '#,(id #'stem #'% #'stem) 'field)
  198. field-default)))
  199. (documentation doc))
  200. ...))
  201. (define-syntax-rule (stem arg (... ...))
  202. (let ((conf (#,(id #'stem #'% #'stem) arg (... ...))))
  203. (validate-configuration conf
  204. #,(id #'stem #'stem #'-fields))
  205. conf)))))))
  206. (define no-serialization ;syntactic keyword for 'define-configuration'
  207. '(no serialization))
  208. (define-syntax define-configuration
  209. (lambda (s)
  210. (syntax-case s (no-serialization prefix)
  211. ((_ stem (field (field-type def ...) doc custom-serializer ...) ...
  212. (no-serialization))
  213. (define-configuration-helper
  214. #f #f #'(_ stem (field (field-type def ...) doc custom-serializer ...)
  215. ...)))
  216. ((_ stem (field (field-type def ...) doc custom-serializer ...) ...
  217. (prefix serializer-prefix))
  218. (define-configuration-helper
  219. #t #'serializer-prefix #'(_ stem (field (field-type def ...)
  220. doc custom-serializer ...)
  221. ...)))
  222. ((_ stem (field (field-type def ...) doc custom-serializer ...) ...)
  223. (define-configuration-helper
  224. #t #f #'(_ stem (field (field-type def ...) doc custom-serializer ...)
  225. ...))))))
  226. (define-syntax-rule (define-configuration/no-serialization
  227. stem (field (field-type def ...)
  228. doc custom-serializer ...) ...)
  229. (define-configuration stem (field (field-type def ...)
  230. doc custom-serializer ...) ...
  231. (no-serialization)))
  232. (define (empty-serializer field-name val) "")
  233. (define serialize-package empty-serializer)
  234. ;; A little helper to make it easier to document all those fields.
  235. (define (generate-documentation documentation documentation-name)
  236. (define (str x) (object->string x))
  237. (define (package->symbol package)
  238. "Return the first symbol name of a package that matches PACKAGE, else #f."
  239. (let* ((module (file-name->module-name
  240. (location-file (package-location package))))
  241. (symbols (filter-map
  242. identity
  243. (module-map (lambda (symbol var)
  244. (and (equal? package (variable-ref var))
  245. symbol))
  246. (resolve-module module)))))
  247. (if (null? symbols)
  248. #f
  249. (first symbols))))
  250. (define (generate configuration-name)
  251. (match (assq-ref documentation configuration-name)
  252. ((fields . sub-documentation)
  253. `((deftp (% (category "Data Type") (name ,(str configuration-name)))
  254. (para "Available " (code ,(str configuration-name)) " fields are:")
  255. (table
  256. (% (formatter (asis)))
  257. ,@(map
  258. (lambda (f)
  259. (let ((field-name (configuration-field-name f))
  260. (field-type (configuration-field-type f))
  261. (field-docs (cdr (texi-fragment->stexi
  262. (configuration-field-documentation f))))
  263. (default (catch #t
  264. (configuration-field-default-value-thunk f)
  265. (lambda _ '%invalid))))
  266. (define (show-default? val)
  267. (or (string? val) (number? val) (boolean? val)
  268. (package? val)
  269. (and (symbol? val) (not (eq? val '%invalid)))
  270. (and (list? val) (and-map show-default? val))))
  271. (define (show-default val)
  272. (cond
  273. ((package? val)
  274. (symbol->string (package->symbol val)))
  275. (else (str val))))
  276. `(entry (% (heading
  277. (code ,(str field-name))
  278. ,@(if (show-default? default)
  279. `(" (default: "
  280. (code ,(show-default default)) ")")
  281. '())
  282. " (type: " ,(str field-type) ")"))
  283. (para ,@field-docs)
  284. ,@(append-map
  285. generate
  286. (or (assq-ref sub-documentation field-name)
  287. '())))))
  288. fields)))))))
  289. (stexi->texi `(*fragment* . ,(generate documentation-name))))
  290. (define (configuration->documentation configuration-symbol)
  291. "Take CONFIGURATION-SYMBOL, the symbol corresponding to the name used when
  292. defining a configuration record with DEFINE-CONFIGURATION, and output the
  293. Texinfo documentation of its fields."
  294. ;; This is helper for a simple, straight-forward application of
  295. ;; GENERATE-DOCUMENTATION.
  296. (let ((fields-getter (module-ref (current-module)
  297. (symbol-append configuration-symbol
  298. '-fields))))
  299. (display (generate-documentation `((,configuration-symbol ,fields-getter))
  300. configuration-symbol))))