projects/guix·blob python-team · refs

143 lines · 6.6 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2026 Giacomo Leidi <[email protected]>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
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)},
39to an environment variable name through UGLIFY, by default a procedure that is
40passed FIELD-NAME, and returns a snake case string representation of
41the field name. Trailing @code{?} in the name are dropped and @code{-} get
42replaced by @code{_}. When PREFIX is a string, it is prepended to the result.
43The result of UGLIFY is then upcased and returned.
44
45For 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
119configuration from @code{(gnu services configuration)}, and FIELDS, the
120list of its field records, to a list of pairs. When NEGATE? is #t all services
121not included in SELECTION will be serialized. Each pair represents an
122environment variable. The first element of each pair is the variable name, the
123second is the value. When PREFIX is a string it is prepended to the variable
124name. TRUE-VALUE and FALSE-VALUE will be used as a representation for
125respectfully @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)))