| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 |
|
| 19 | (define-module (gnu services configuration environment-variables)
|
| 20 | #:use-module (gnu services configuration)
|
| 21 | #:use-module (gnu services configuration utils)
|
| 22 | #:use-module (guix diagnostics)
|
| 23 | #:use-module (guix i18n)
|
| 24 | #:use-module (ice-9 match)
|
| 25 | #:use-module (srfi srfi-1)
|
| 26 | #:export (serialize-string-environment-variable
|
| 27 | serialize-boolean-environment-variable
|
| 28 | serialize-number-environment-variable
|
| 29 | serialize-maybe-string-environment-variable
|
| 30 | serialize-maybe-boolean-environment-variable
|
| 31 | serialize-maybe-number-environment-variable
|
| 32 |
|
| 33 | serialize-environment-variables))
|
| 34 |
|
| 35 | (define* (field-name->environment-variable field-name
|
| 36 | #:key prefix
|
| 37 | (uglify uglify-snake-case))
|
| 38 | "Serializes FIELD-NAME, a field name from @code{(gnu services configuration)},
|
| 39 | to an environment variable name through UGLIFY, by default a procedure that is
|
| 40 | passed FIELD-NAME, and returns a snake case string representation of
|
| 41 | the field name. Trailing @code{?} in the name are dropped and @code{-} get
|
| 42 | replaced by @code{_}. When PREFIX is a string, it is prepended to the result.
|
| 43 | The result of UGLIFY is then upcased and returned.
|
| 44 |
|
| 45 | For example the procedure would convert @code{'a-field} to @code{\"A_FIELD\"}."
|
| 46 | (let ((variable (string-upcase
|
| 47 | (uglify field-name))))
|
| 48 | (if (string? prefix)
|
| 49 | (string-append prefix variable)
|
| 50 | variable)))
|
| 51 |
|
| 52 | (define* (serialize-string-environment-variable field-name value
|
| 53 | #:key prefix
|
| 54 | #:allow-other-keys)
|
| 55 | (cons (field-name->environment-variable field-name #:prefix prefix)
|
| 56 | value))
|
| 57 |
|
| 58 | (define* (serialize-maybe-string-environment-variable field-name value
|
| 59 | #:key prefix
|
| 60 | #:allow-other-keys)
|
| 61 | (if (maybe-value-set? value)
|
| 62 | (serialize-string-environment-variable field-name value #:prefix prefix)
|
| 63 | #f))
|
| 64 |
|
| 65 | (define* (serialize-boolean-environment-variable field-name value
|
| 66 | #:key prefix
|
| 67 | (true-value "true")
|
| 68 | (false-value "false")
|
| 69 | #:allow-other-keys)
|
| 70 | (serialize-string-environment-variable
|
| 71 | field-name (if value true-value false-value)
|
| 72 | #:prefix prefix))
|
| 73 |
|
| 74 | (define* (serialize-maybe-boolean-environment-variable field-name value
|
| 75 | #:key prefix
|
| 76 | #:allow-other-keys)
|
| 77 | (if (maybe-value-set? value)
|
| 78 | (serialize-boolean-environment-variable field-name value #:prefix prefix)
|
| 79 | #f))
|
| 80 |
|
| 81 | (define* (serialize-number-environment-variable field-name value
|
| 82 | #:key prefix
|
| 83 | #:allow-other-keys)
|
| 84 | (cons (field-name->environment-variable field-name #:prefix prefix)
|
| 85 | (number->string value)))
|
| 86 |
|
| 87 | (define* (serialize-maybe-number-environment-variable field-name value
|
| 88 | #:key prefix
|
| 89 | #:allow-other-keys)
|
| 90 | (if (maybe-value-set? value)
|
| 91 | (serialize-number-environment-variable field-name value #:prefix prefix)
|
| 92 | #f))
|
| 93 |
|
| 94 | (define (environment-variable-serializer field)
|
| 95 | (define type (configuration-field-type field))
|
| 96 | (match type
|
| 97 | ('string serialize-string-environment-variable)
|
| 98 | ('maybe-string serialize-maybe-string-environment-variable)
|
| 99 | ('number serialize-number-environment-variable)
|
| 100 | ('integer serialize-number-environment-variable)
|
| 101 | ('positive serialize-number-environment-variable)
|
| 102 | ('maybe-number serialize-maybe-number-environment-variable)
|
| 103 | ('maybe-integer serialize-maybe-number-environment-variable)
|
| 104 | ('maybe-positive serialize-maybe-number-environment-variable)
|
| 105 | ('boolean serialize-boolean-environment-variable)
|
| 106 | ('maybe-boolean serialize-boolean-environment-variable)
|
| 107 | (_
|
| 108 | (raise
|
| 109 | (formatted-message
|
| 110 | (G_ "Unknown environment-variable field type: ~a")
|
| 111 | type)))))
|
| 112 |
|
| 113 | (define* (serialize-environment-variables config fields
|
| 114 | #:optional selection negate?
|
| 115 | #:key prefix
|
| 116 | (true-value "true")
|
| 117 | (false-value "false"))
|
| 118 | "Serializes the fields whose name is included in SELECTION from CONFIG, a
|
| 119 | configuration from @code{(gnu services configuration)}, and FIELDS, the
|
| 120 | list of its field records, to a list of pairs. When NEGATE? is #t all services
|
| 121 | not included in SELECTION will be serialized. Each pair represents an
|
| 122 | environment variable. The first element of each pair is the variable name, the
|
| 123 | second is the value. When PREFIX is a string it is prepended to the variable
|
| 124 | name. TRUE-VALUE and FALSE-VALUE will be used as a representation for
|
| 125 | respectfully @code{#t} and @code{#f}."
|
| 126 | (define selected-names
|
| 127 | (or selection
|
| 128 | (map configuration-field-name fields)))
|
| 129 | (define filtered
|
| 130 | (filter-configuration-fields fields selected-names negate?))
|
| 131 | (define getters
|
| 132 | (map configuration-field-getter filtered))
|
| 133 | (define names
|
| 134 | (map configuration-field-name filtered))
|
| 135 | (define serializers
|
| 136 | (map environment-variable-serializer filtered))
|
| 137 |
|
| 138 | (filter-map (match-lambda ((serializer name getter)
|
| 139 | (serializer name (getter config)
|
| 140 | #:prefix prefix
|
| 141 | #:true-value true-value
|
| 142 | #:false-value false-value)))
|
| 143 | (zip serializers names getters)))
|