@@ -9,93 +9,7 @@
99 </para>
1010
1111 <xi:include href="functions/overrides.xml" />
12− <section xml:id="sec-generators">
13− <title>Generators</title>
14−
15− <para>
16− Generators are functions that create file formats from nix data structures,
17− e. g. for configuration files. There are generators available for:
18− <literal>INI</literal>, <literal>JSON</literal> and <literal>YAML</literal>
19− </para>
20−
21− <para>
22− All generators follow a similar call interface: <code>generatorName
23− configFunctions data</code>, where <literal>configFunctions</literal> is an
24− attrset of user-defined functions that format nested parts of the content.
25− They each have common defaults, so often they do not need to be set
26− manually. An example is <code>mkSectionName ? (name: libStr.escape [ "[" "]"
27− ] name)</code> from the <literal>INI</literal> generator. It receives the
28− name of a section and sanitizes it. The default
29− <literal>mkSectionName</literal> escapes <literal>[</literal> and
30− <literal>]</literal> with a backslash.
31− </para>
32−
33− <para>
34− Generators can be fine-tuned to produce exactly the file format required by
35− your application/service. One example is an INI-file format which uses
36− <literal>: </literal> as separator, the strings
37− <literal>"yes"</literal>/<literal>"no"</literal> as boolean values and
38− requires all string values to be quoted:
39− </para>
40−
41−<programlisting>
42−with lib;
43−let
44− customToINI = generators.toINI {
45− # specifies how to format a key/value pair
46− mkKeyValue = generators.mkKeyValueDefault {
47− # specifies the generated string for a subset of nix values
48− mkValueString = v:
49− if v == true then ''"yes"''
50− else if v == false then ''"no"''
51− else if isString v then ''"${v}"''
52− # and delegats all other values to the default generator
53− else generators.mkValueStringDefault {} v;
54− } ":";
55− };
56−
57−# the INI file can now be given as plain old nix values
58−in customToINI {
59− main = {
60− pushinfo = true;
61− autopush = false;
62− host = "localhost";
63− port = 42;
64− };
65− mergetool = {
66− merge = "diff3";
67− };
68−}
69−</programlisting>
70−
71− <para>
72− This will produce the following INI file as nix string:
73− </para>
74−
75−<programlisting>
76−[main]
77−autopush:"no"
78−host:"localhost"
79−port:42
80−pushinfo:"yes"
81−str\:ange:"very::strange"
82−
83−[mergetool]
84−merge:"diff3"
85−</programlisting>
86−
87− <note>
88− <para>
89− Nix store paths can be converted to strings by enclosing a derivation
90− attribute like so: <code>"${drv}"</code>.
91− </para>
92− </note>
93−
94− <para>
95− Detailed documentation for each generator can be found in
96− <literal>lib/generators.nix</literal>.
97− </para>
98− </section>
12+ <xi:include href="functions/generators.xml" />
9913 <section xml:id="sec-debug">
10014 <title>Debugging Nix Expressions</title>
10115