git.iko.soy/typeable/nixpkgs
on master help sign in
nixpkgs docs: move generators to its own file
commit9ae39b3553146b3ae72fc5ac26654e1930baba3d authorGraham Christensen <graham@grahamc.com> dateTue 2 Oct 2018, 14:03 (8 years ago) parentf200a32
2 files changed+91−87
doc/functions.xml+1−87 doc/functions/generators.xml+90
doc/functions.xml+1−87
@@ -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
doc/functions/generators.xmlnew file+90
@@ -0,0 +1,90 @@
1+<section xmlns="http://docbook.org/ns/docbook"
2+ xmlns:xlink="http://www.w3.org/1999/xlink"
3+ xmlns:xi="http://www.w3.org/2001/XInclude"
4+ xml:id="sec-generators">
5+ <title>Generators</title>
6+
7+ <para>
8+ Generators are functions that create file formats from nix data structures,
9+ e. g. for configuration files. There are generators available for:
10+ <literal>INI</literal>, <literal>JSON</literal> and <literal>YAML</literal>
11+ </para>
12+
13+ <para>
14+ All generators follow a similar call interface: <code>generatorName
15+ configFunctions data</code>, where <literal>configFunctions</literal> is an
16+ attrset of user-defined functions that format nested parts of the content.
17+ They each have common defaults, so often they do not need to be set
18+ manually. An example is <code>mkSectionName ? (name: libStr.escape [ "[" "]"
19+ ] name)</code> from the <literal>INI</literal> generator. It receives the
20+ name of a section and sanitizes it. The default
21+ <literal>mkSectionName</literal> escapes <literal>[</literal> and
22+ <literal>]</literal> with a backslash.
23+ </para>
24+
25+ <para>
26+ Generators can be fine-tuned to produce exactly the file format required by
27+ your application/service. One example is an INI-file format which uses
28+ <literal>: </literal> as separator, the strings
29+ <literal>"yes"</literal>/<literal>"no"</literal> as boolean values and
30+ requires all string values to be quoted:
31+ </para>
32+
33+<programlisting>
34+with lib;
35+let
36+ customToINI = generators.toINI {
37+ # specifies how to format a key/value pair
38+ mkKeyValue = generators.mkKeyValueDefault {
39+ # specifies the generated string for a subset of nix values
40+ mkValueString = v:
41+ if v == true then ''"yes"''
42+ else if v == false then ''"no"''
43+ else if isString v then ''"${v}"''
44+ # and delegats all other values to the default generator
45+ else generators.mkValueStringDefault {} v;
46+ } ":";
47+ };
48+
49+# the INI file can now be given as plain old nix values
50+in customToINI {
51+ main = {
52+ pushinfo = true;
53+ autopush = false;
54+ host = "localhost";
55+ port = 42;
56+ };
57+ mergetool = {
58+ merge = "diff3";
59+ };
60+}
61+</programlisting>
62+
63+ <para>
64+ This will produce the following INI file as nix string:
65+ </para>
66+
67+<programlisting>
68+[main]
69+autopush:"no"
70+host:"localhost"
71+port:42
72+pushinfo:"yes"
73+str\:ange:"very::strange"
74+
75+[mergetool]
76+merge:"diff3"
77+</programlisting>
78+
79+ <note>
80+ <para>
81+ Nix store paths can be converted to strings by enclosing a derivation
82+ attribute like so: <code>"${drv}"</code>.
83+ </para>
84+ </note>
85+
86+ <para>
87+ Detailed documentation for each generator can be found in
88+ <literal>lib/generators.nix</literal>.
89+ </para>
90+ </section>