···77 The nixpkgs repository has several utility functions to manipulate Nix expressions.
88 </para>
99 <xi:include href="functions/library.xml" />
1010- <xi:include href="functions/generators.xml" />
1010+ <xi:include href="functions/generators.section.xml" />
1111 <xi:include href="functions/debug.section.xml" />
1212 <xi:include href="functions/prefer-remote-fetch.section.xml" />
1313 <xi:include href="functions/nix-gitignore.section.xml" />
+56
doc/functions/generators.section.md
···11+# Generators {#sec-generators}
22+Generators are functions that create file formats from nix data structures, e. g. for configuration files. There are generators available for: `INI`, `JSON` and `YAML`
33+44+All generators follow a similar call interface: `generatorName configFunctions data`, where `configFunctions` is an attrset of user-defined functions that format nested parts of the content. They each have common defaults, so often they do not need to be set manually. An example is `mkSectionName ? (name: libStr.escape [ "[" "]" ] name)` from the `INI` generator. It receives the name of a section and sanitizes it. The default `mkSectionName` escapes `[` and `]` with a backslash.
55+66+Generators can be fine-tuned to produce exactly the file format required by your application/service. One example is an INI-file format which uses `: ` as separator, the strings `"yes"`/`"no"` as boolean values and requires all string values to be quoted:
77+88+```nix
99+with lib;
1010+let
1111+ customToINI = generators.toINI {
1212+ # specifies how to format a key/value pair
1313+ mkKeyValue = generators.mkKeyValueDefault {
1414+ # specifies the generated string for a subset of nix values
1515+ mkValueString = v:
1616+ if v == true then ''"yes"''
1717+ else if v == false then ''"no"''
1818+ else if isString v then ''"${v}"''
1919+ # and delegats all other values to the default generator
2020+ else generators.mkValueStringDefault {} v;
2121+ } ":";
2222+ };
2323+2424+# the INI file can now be given as plain old nix values
2525+in customToINI {
2626+ main = {
2727+ pushinfo = true;
2828+ autopush = false;
2929+ host = "localhost";
3030+ port = 42;
3131+ };
3232+ mergetool = {
3333+ merge = "diff3";
3434+ };
3535+}
3636+```
3737+3838+This will produce the following INI file as nix string:
3939+4040+```INI
4141+[main]
4242+autopush:"no"
4343+host:"localhost"
4444+port:42
4545+pushinfo:"yes"
4646+str\:ange:"very::strange"
4747+4848+[mergetool]
4949+merge:"diff3"
5050+```
5151+5252+::: note
5353+Nix store paths can be converted to strings by enclosing a derivation attribute like so: `"${drv}"`.
5454+:::
5555+5656+Detailed documentation for each generator can be found in `lib/generators.nix`.
-74
doc/functions/generators.xml
···11-<section xmlns="http://docbook.org/ns/docbook"
22- xmlns:xlink="http://www.w3.org/1999/xlink"
33- xmlns:xi="http://www.w3.org/2001/XInclude"
44- xml:id="sec-generators">
55- <title>Generators</title>
66-77- <para>
88- Generators are functions that create file formats from nix data structures, e. g. for configuration files. There are generators available for: <literal>INI</literal>, <literal>JSON</literal> and <literal>YAML</literal>
99- </para>
1010-1111- <para>
1212- All generators follow a similar call interface: <code>generatorName configFunctions data</code>, where <literal>configFunctions</literal> is an attrset of user-defined functions that format nested parts of the content. They each have common defaults, so often they do not need to be set manually. An example is <code>mkSectionName ? (name: libStr.escape [ "[" "]" ] name)</code> from the <literal>INI</literal> generator. It receives the name of a section and sanitizes it. The default <literal>mkSectionName</literal> escapes <literal>[</literal> and <literal>]</literal> with a backslash.
1313- </para>
1414-1515- <para>
1616- Generators can be fine-tuned to produce exactly the file format required by your application/service. One example is an INI-file format which uses <literal>: </literal> as separator, the strings <literal>"yes"</literal>/<literal>"no"</literal> as boolean values and requires all string values to be quoted:
1717- </para>
1818-1919-<programlisting>
2020-with lib;
2121-let
2222- customToINI = generators.toINI {
2323- # specifies how to format a key/value pair
2424- mkKeyValue = generators.mkKeyValueDefault {
2525- # specifies the generated string for a subset of nix values
2626- mkValueString = v:
2727- if v == true then ''"yes"''
2828- else if v == false then ''"no"''
2929- else if isString v then ''"${v}"''
3030- # and delegats all other values to the default generator
3131- else generators.mkValueStringDefault {} v;
3232- } ":";
3333- };
3434-3535-# the INI file can now be given as plain old nix values
3636-in customToINI {
3737- main = {
3838- pushinfo = true;
3939- autopush = false;
4040- host = "localhost";
4141- port = 42;
4242- };
4343- mergetool = {
4444- merge = "diff3";
4545- };
4646-}
4747-</programlisting>
4848-4949- <para>
5050- This will produce the following INI file as nix string:
5151- </para>
5252-5353-<programlisting>
5454-[main]
5555-autopush:"no"
5656-host:"localhost"
5757-port:42
5858-pushinfo:"yes"
5959-str\:ange:"very::strange"
6060-6161-[mergetool]
6262-merge:"diff3"
6363-</programlisting>
6464-6565- <note>
6666- <para>
6767- Nix store paths can be converted to strings by enclosing a derivation attribute like so: <code>"${drv}"</code>.
6868- </para>
6969- </note>
7070-7171- <para>
7272- Detailed documentation for each generator can be found in <literal>lib/generators.nix</literal>.
7373- </para>
7474-</section>