1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
| | ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
(define-module (guix parameters)
#:use-module (guix packages)
#:use-module (guix records)
#:use-module (guix diagnostics)
#:use-module (guix i18n)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-34)
#:use-module (srfi srfi-35)
#:use-module (ice-9 match)
#:export (package-parameter
package-parameter?
package-parameter-name
package-parameter-property
package-parameter-type
package-parameter-description
boolean
optionally
package-parameters
lookup-package-parameter
package-parameter-value
set-package-parameter-value))
;;; Commentary:
;;;
;;; This module provides a way to express high-level "package parameters",
;;; which allow users to customize how packages are built. Parameters are an
;;; interface that package developers define, where each parameter has a name
;;; and type. The user interface then converts parameter values from string
;;; to Scheme values and records them in the package properties.
;;;
;;; Package parameters are discoverable; their description is
;;; internationalized. The possible values of a parameter can be enumerated,
;;; and thus the Cartesian product of all possible parameter values for a
;;; package can be enumerated as well.
;;;
;;; Code:
;; Package parameter interface.
(define-record-type* <package-parameter> package-parameter
make-package-parameter
package-parameter?
(name package-parameter-name)
(property package-parameter-property (default (string->symbol name)))
(type package-parameter-type)
(description package-parameter-description))
;; Type of a package parameter.
(define-record-type* <parameter-type> parameter-type
make-parameter-type
parameter-type?
(name parameter-type-name) ;debugging purposes only!
(string->value parameter-type-string->value)
(value->string parameter-type-value->string)
(universe parameter-type-universe))
(define boolean
;; The Boolean parameter type.
(parameter-type (name 'boolean)
(universe '(#true #false))
(value->string
(match-lambda
(#f "false")
(#t "true")))
(string->value
(lambda (str)
(cond ((string-ci=? str "true")
#t)
((string-ci=? str "false")
#f)
(else
(raise (condition
(&message (message "wrong value"))))))))))
(define (package-parameters package)
(or (assq-ref (package-properties package) 'parameters)
'()))
(define (package-parameter-value package parameter)
(assq-ref (package-properties package)
(package-parameter-property parameter)))
(define (lookup-package-parameter package name)
(find (lambda (parameter)
(string=? (package-parameter-name parameter) name))
(package-parameters package)))
(define (set-package-parameter-value package name value)
(let ((parameter (lookup-package-parameter package name))
(location (package-field-location package 'properties)))
(unless parameter
(raise (apply make-compound-condition
(formatted-message
(G_ "~a: no such package parameter")
name)
(if location
(list (condition
(&error-location (location location))))
'()))))
(let* ((property (package-parameter-property parameter))
(type (package-parameter-type parameter))
(value ((parameter-type-string->value type) value)))
(package/inherit package
(properties
(alist-cons property value
(alist-delete property (package-properties package)
eq?)))))))
(define-syntax-rule (optionally property exp)
(if (assq-ref (package-properties this-package) property)
(list exp)
'()))
|