blob: 318ee0143320a84bb8eb70d31d3b5d6e8e1f9d83 (
plain) (
blame)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
{ lib
, pkgs
}:
let
inherit (pkgs) buildPackages callPackage;
hocon-generator = buildPackages.rustPlatform.buildRustPackage {
name = "hocon-generator";
version = "0.1.0";
src = ./src;
passthru.updateScript = ./update.sh;
cargoLock.lockFile = ./src/Cargo.lock;
};
hocon-validator = pkgs.writers.writePython3Bin "hocon-validator" {
libraries = [ pkgs.python3Packages.pyhocon ];
} ''
from sys import argv
from pyhocon import ConfigFactory
if not len(argv) == 2:
print("USAGE: hocon-validator <file>")
ConfigFactory.parse_file(argv[1])
'';
in
{
# https://github.com/lightbend/config/blob/main/HOCON.md
format = {
generator ? hocon-generator
, validator ? hocon-validator
# `include classpath("")` is not implemented in pyhocon.
# In the case that you need this functionality,
# you will have to disable pyhocon validation.
, doCheck ? true
}: let
hoconLib = {
mkInclude = value: let
includeStatement = if lib.isAttrs value && !(lib.isDerivation value) then {
required = false;
type = null;
_type = "include";
} // value else {
value = toString value;
required = false;
type = null;
_type = "include";
};
in
assert lib.assertMsg (lib.elem includeStatement.type [ "file" "url" "classpath" null ]) ''
Type of HOCON mkInclude is not of type 'file', 'url' or 'classpath':
${(lib.generators.toPretty {}) includeStatement}
'';
includeStatement;
mkAppend = value: {
inherit value;
_type = "append";
};
mkSubstitution = value:
if lib.isString value
then
{
inherit value;
optional = false;
_type = "substitution";
}
else
assert lib.assertMsg (lib.isAttrs value) ''
Value of invalid type provided to `hocon.lib.mkSubstition`: ${lib.typeOf value}
'';
assert lib.assertMsg (value ? "value") ''
Argument to `hocon.lib.mkSubstition` is missing a `value`:
${builtins.toJSON value}
'';
{
value = value.value;
optional = value.optional or false;
_type = "substitution";
};
};
in {
type = let
type' = with lib.types; let
atomType = nullOr (oneOf [
bool
float
int
path
str
]);
in (oneOf [
atomType
(listOf atomType)
(attrsOf type')
]) // {
description = "HOCON value";
};
in type';
lib = hoconLib;
generate = name: value:
let
# TODO: remove in 24.11
# Backwards compatability for generators in the following locations:
# - nixos/modules/services/networking/jibri/default.nix (__hocon_envvar)
# - nixos/modules/services/networking/jicofo.nix (__hocon_envvar, __hocon_unquoted_string)
# - nixos/modules/services/networking/jitsi-videobridge.nix (__hocon_envvar)
replaceOldIndicators = value:
if lib.isAttrs value then
(if value ? "__hocon_envvar"
then
lib.warn ''
Use of `__hocon_envvar` has been deprecated, and will
be removed in the future.
Please use `(pkgs.formats.hocon {}).lib.mkSubstitution` instead.
''
(hoconLib.mkSubstitution value.__hocon_envvar)
else if value ? "__hocon_unquoted_string"
then
lib.warn ''
Use of `__hocon_unquoted_string` has been deprecated, and will
be removed in the future.
Please make use of the freeform options of
`(pkgs.formats.hocon {}).format` instead.
''
{
value = value.__hocon_unquoted_string;
_type = "unquoted_string";
}
else lib.mapAttrs (_: replaceOldIndicators) value)
else if lib.isList value
then map replaceOldIndicators value
else value;
finalValue = replaceOldIndicators value;
in
callPackage
({
stdenvNoCC
, hocon-generator
, hocon-validator
, writeText
}:
stdenvNoCC.mkDerivation rec {
inherit name;
dontUnpack = true;
json = builtins.toJSON finalValue;
passAsFile = [ "json" ];
strictDeps = true;
nativeBuildInputs = [ hocon-generator ];
buildPhase = ''
runHook preBuild
hocon-generator < $jsonPath > output.conf
runHook postBuild
'';
inherit doCheck;
nativeCheckInputs = [ hocon-validator ];
checkPhase = ''
runHook preCheck
hocon-validator output.conf
runHook postCheck
'';
installPhase = ''
runHook preInstall
mv output.conf $out
runHook postInstall
'';
passthru.json = writeText "${name}.json" json;
})
{
hocon-generator = generator;
hocon-validator = validator;
};
};
}
|