1/* Functions that generate widespread file
2 * formats from nix data structures.
3 *
4 * They all follow a similar interface:
5 * generator { config-attrs } data
6 *
7 * `config-attrs` are “holes” in the generators
8 * with sensible default implementations that
9 * can be overwritten. The default implementations
10 * are mostly generators themselves, called with
11 * their respective default values; they can be reused.
12 *
13 * Tests can be found in ./tests.nix
14 * Documentation in the manual, #sec-generators
15 */
16{ lib }:
17with (lib).trivial;
18let
19 libStr = lib.strings;
20 libAttr = lib.attrsets;
21
22 inherit (lib) isFunction;
23in
24
25rec {
26
27 ## -- HELPER FUNCTIONS & DEFAULTS --
28
29 /* Convert a value to a sensible default string representation.
30 * The builtin `toString` function has some strange defaults,
31 * suitable for bash scripts but not much else.
32 */
33 mkValueStringDefault = {}: v: with builtins;
34 let err = t: v: abort
35 ("generators.mkValueStringDefault: " +
36 "${t} not supported: ${toPretty {} v}");
37 in if isInt v then toString v
38 # convert derivations to store paths
39 else if lib.isDerivation v then toString v
40 # we default to not quoting strings
41 else if isString v then v
42 # isString returns "1", which is not a good default
43 else if true == v then "true"
44 # here it returns to "", which is even less of a good default
45 else if false == v then "false"
46 else if null == v then "null"
47 # if you have lists you probably want to replace this
48 else if isList v then err "lists" v
49 # same as for lists, might want to replace
50 else if isAttrs v then err "attrsets" v
51 # functions can’t be printed of course
52 else if isFunction v then err "functions" v
53 # Floats currently can't be converted to precise strings,
54 # condition warning on nix version once this isn't a problem anymore
55 # See https://github.com/NixOS/nix/pull/3480
56 else if isFloat v then libStr.floatToString v
57 else err "this value is" (toString v);
58
59
60 /* Generate a line of key k and value v, separated by
61 * character sep. If sep appears in k, it is escaped.
62 * Helper for synaxes with different separators.
63 *
64 * mkValueString specifies how values should be formatted.
65 *
66 * mkKeyValueDefault {} ":" "f:oo" "bar"
67 * > "f\:oo:bar"
68 */
69 mkKeyValueDefault = {
70 mkValueString ? mkValueStringDefault {}
71 }: sep: k: v:
72 "${libStr.escape [sep] k}${sep}${mkValueString v}";
73
74
75 ## -- FILE FORMAT GENERATORS --
76
77
78 /* Generate a key-value-style config file from an attrset.
79 *
80 * mkKeyValue is the same as in toINI.
81 */
82 toKeyValue = {
83 mkKeyValue ? mkKeyValueDefault {} "=",
84 listsAsDuplicateKeys ? false
85 }:
86 let mkLine = k: v: mkKeyValue k v + "\n";
87 mkLines = if listsAsDuplicateKeys
88 then k: v: map (mkLine k) (if lib.isList v then v else [v])
89 else k: v: [ (mkLine k v) ];
90 in attrs: libStr.concatStrings (lib.concatLists (libAttr.mapAttrsToList mkLines attrs));
91
92
93 /* Generate an INI-style config file from an
94 * attrset of sections to an attrset of key-value pairs.
95 *
96 * generators.toINI {} {
97 * foo = { hi = "${pkgs.hello}"; ciao = "bar"; };
98 * baz = { "also, integers" = 42; };
99 * }
100 *
101 *> [baz]
102 *> also, integers=42
103 *>
104 *> [foo]
105 *> ciao=bar
106 *> hi=/nix/store/y93qql1p5ggfnaqjjqhxcw0vqw95rlz0-hello-2.10
107 *
108 * The mk* configuration attributes can generically change
109 * the way sections and key-value strings are generated.
110 *
111 * For more examples see the test cases in ./tests.nix.
112 */
113 toINI = {
114 # apply transformations (e.g. escapes) to section names
115 mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
116 # format a setting line from key and value
117 mkKeyValue ? mkKeyValueDefault {} "=",
118 # allow lists as values for duplicate keys
119 listsAsDuplicateKeys ? false
120 }: attrsOfAttrs:
121 let
122 # map function to string for each key val
123 mapAttrsToStringsSep = sep: mapFn: attrs:
124 libStr.concatStringsSep sep
125 (libAttr.mapAttrsToList mapFn attrs);
126 mkSection = sectName: sectValues: ''
127 [${mkSectionName sectName}]
128 '' + toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues;
129 in
130 # map input to ini sections
131 mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
132
133 /* Generate a git-config file from an attrset.
134 *
135 * It has two major differences from the regular INI format:
136 *
137 * 1. values are indented with tabs
138 * 2. sections can have sub-sections
139 *
140 * generators.toGitINI {
141 * url."ssh://git@github.com/".insteadOf = "https://github.com";
142 * user.name = "edolstra";
143 * }
144 *
145 *> [url "ssh://git@github.com/"]
146 *> insteadOf = https://github.com/
147 *>
148 *> [user]
149 *> name = edolstra
150 */
151 toGitINI = attrs:
152 with builtins;
153 let
154 mkSectionName = name:
155 let
156 containsQuote = libStr.hasInfix ''"'' name;
157 sections = libStr.splitString "." name;
158 section = head sections;
159 subsections = tail sections;
160 subsection = concatStringsSep "." subsections;
161 in if containsQuote || subsections == [ ] then
162 name
163 else
164 ''${section} "${subsection}"'';
165
166 # generation for multiple ini values
167 mkKeyValue = k: v:
168 let mkKeyValue = mkKeyValueDefault { } " = " k;
169 in concatStringsSep "\n" (map (kv: "\t" + mkKeyValue kv) (lib.toList v));
170
171 # converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI
172 gitFlattenAttrs = let
173 recurse = path: value:
174 if isAttrs value && !lib.isDerivation value then
175 lib.mapAttrsToList (name: value: recurse ([ name ] ++ path) value) value
176 else if length path > 1 then {
177 ${concatStringsSep "." (lib.reverseList (tail path))}.${head path} = value;
178 } else {
179 ${head path} = value;
180 };
181 in attrs: lib.foldl lib.recursiveUpdate { } (lib.flatten (recurse [ ] attrs));
182
183 toINI_ = toINI { inherit mkKeyValue mkSectionName; };
184 in
185 toINI_ (gitFlattenAttrs attrs);
186
187 /* Generates JSON from an arbitrary (non-function) value.
188 * For more information see the documentation of the builtin.
189 */
190 toJSON = {}: builtins.toJSON;
191
192
193 /* YAML has been a strict superset of JSON since 1.2, so we
194 * use toJSON. Before it only had a few differences referring
195 * to implicit typing rules, so it should work with older
196 * parsers as well.
197 */
198 toYAML = {}@args: toJSON args;
199
200 withRecursion =
201 args@{
202 /* If this option is not null, the given value will stop evaluating at a certain depth */
203 depthLimit
204 /* If this option is true, an error will be thrown, if a certain given depth is exceeded */
205 , throwOnDepthLimit ? true
206 }:
207 assert builtins.isInt depthLimit;
208 let
209 transform = depth:
210 if depthLimit != null && depth > depthLimit then
211 if throwOnDepthLimit
212 then throw "Exceeded maximum eval-depth limit of ${toString depthLimit} while trying to evaluate with `generators.withRecursion'!"
213 else const "<unevaluated>"
214 else id;
215 mapAny = with builtins; depth: v:
216 let
217 evalNext = x: mapAny (depth + 1) (transform (depth + 1) x);
218 in
219 if isAttrs v then mapAttrs (const evalNext) v
220 else if isList v then map evalNext v
221 else transform (depth + 1) v;
222 in
223 mapAny 0;
224
225 /* Pretty print a value, akin to `builtins.trace`.
226 * Should probably be a builtin as well.
227 */
228 toPretty = {
229 /* If this option is true, attrsets like { __pretty = fn; val = …; }
230 will use fn to convert val to a pretty printed representation.
231 (This means fn is type Val -> String.) */
232 allowPrettyValues ? false,
233 /* If this option is true, the output is indented with newlines for attribute sets and lists */
234 multiline ? true
235 }@args:
236 let
237 go = indent: v: with builtins;
238 let isPath = v: typeOf v == "path";
239 introSpace = if multiline then "\n${indent} " else " ";
240 outroSpace = if multiline then "\n${indent}" else " ";
241 in if isInt v then toString v
242 else if isFloat v then "~${toString v}"
243 else if isString v then
244 let
245 # Separate a string into its lines
246 newlineSplits = filter (v: ! isList v) (builtins.split "\n" v);
247 # For a '' string terminated by a \n, which happens when the closing '' is on a new line
248 multilineResult = "''" + introSpace + concatStringsSep introSpace (lib.init newlineSplits) + outroSpace + "''";
249 # For a '' string not terminated by a \n, which happens when the closing '' is not on a new line
250 multilineResult' = "''" + introSpace + concatStringsSep introSpace newlineSplits + "''";
251 # For single lines, replace all newlines with their escaped representation
252 singlelineResult = "\"" + libStr.escape [ "\"" ] (concatStringsSep "\\n" newlineSplits) + "\"";
253 in if multiline && length newlineSplits > 1 then
254 if lib.last newlineSplits == "" then multilineResult else multilineResult'
255 else singlelineResult
256 else if true == v then "true"
257 else if false == v then "false"
258 else if null == v then "null"
259 else if isPath v then toString v
260 else if isList v then
261 if v == [] then "[ ]"
262 else "[" + introSpace
263 + libStr.concatMapStringsSep introSpace (go (indent + " ")) v
264 + outroSpace + "]"
265 else if isFunction v then
266 let fna = lib.functionArgs v;
267 showFnas = concatStringsSep ", " (libAttr.mapAttrsToList
268 (name: hasDefVal: if hasDefVal then name + "?" else name)
269 fna);
270 in if fna == {} then "<function>"
271 else "<function, args: {${showFnas}}>"
272 else if isAttrs v then
273 # apply pretty values if allowed
274 if attrNames v == [ "__pretty" "val" ] && allowPrettyValues
275 then v.__pretty v.val
276 else if v == {} then "{ }"
277 else if v ? type && v.type == "derivation" then
278 "<derivation ${v.drvPath or "???"}>"
279 else "{" + introSpace
280 + libStr.concatStringsSep introSpace (libAttr.mapAttrsToList
281 (name: value:
282 "${libStr.escapeNixIdentifier name} = ${go (indent + " ") value};") v)
283 + outroSpace + "}"
284 else abort "generators.toPretty: should never happen (v = ${v})";
285 in go "";
286
287 # PLIST handling
288 toPlist = {}: v: let
289 isFloat = builtins.isFloat or (x: false);
290 expr = ind: x: with builtins;
291 if x == null then "" else
292 if isBool x then bool ind x else
293 if isInt x then int ind x else
294 if isString x then str ind x else
295 if isList x then list ind x else
296 if isAttrs x then attrs ind x else
297 if isFloat x then float ind x else
298 abort "generators.toPlist: should never happen (v = ${v})";
299
300 literal = ind: x: ind + x;
301
302 bool = ind: x: literal ind (if x then "<true/>" else "<false/>");
303 int = ind: x: literal ind "<integer>${toString x}</integer>";
304 str = ind: x: literal ind "<string>${x}</string>";
305 key = ind: x: literal ind "<key>${x}</key>";
306 float = ind: x: literal ind "<real>${toString x}</real>";
307
308 indent = ind: expr "\t${ind}";
309
310 item = ind: libStr.concatMapStringsSep "\n" (indent ind);
311
312 list = ind: x: libStr.concatStringsSep "\n" [
313 (literal ind "<array>")
314 (item ind x)
315 (literal ind "</array>")
316 ];
317
318 attrs = ind: x: libStr.concatStringsSep "\n" [
319 (literal ind "<dict>")
320 (attr ind x)
321 (literal ind "</dict>")
322 ];
323
324 attr = let attrFilter = name: value: name != "_module" && value != null;
325 in ind: x: libStr.concatStringsSep "\n" (lib.flatten (lib.mapAttrsToList
326 (name: value: lib.optional (attrFilter name value) [
327 (key "\t${ind}" name)
328 (expr "\t${ind}" value)
329 ]) x));
330
331 in ''<?xml version="1.0" encoding="UTF-8"?>
332<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
333<plist version="1.0">
334${expr "" v}
335</plist>'';
336
337 /* Translate a simple Nix expression to Dhall notation.
338 * Note that integers are translated to Integer and never
339 * the Natural type.
340 */
341 toDhall = { }@args: v:
342 with builtins;
343 let concatItems = lib.strings.concatStringsSep ", ";
344 in if isAttrs v then
345 "{ ${
346 concatItems (lib.attrsets.mapAttrsToList
347 (key: value: "${key} = ${toDhall args value}") v)
348 } }"
349 else if isList v then
350 "[ ${concatItems (map (toDhall args) v)} ]"
351 else if isInt v then
352 "${if v < 0 then "" else "+"}${toString v}"
353 else if isBool v then
354 (if v then "True" else "False")
355 else if isFunction v then
356 abort "generators.toDhall: cannot convert a function to Dhall"
357 else if isNull v then
358 abort "generators.toDhall: cannot convert a null to Dhall"
359 else
360 builtins.toJSON v;
361}