configuration.scm 7.9 KB

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