diff options
author | Graham Christensen <graham@grahamc.com> | 2018-10-02 14:03:59 -0400 |
---|---|---|
committer | Graham Christensen <graham@grahamc.com> | 2018-10-02 14:03:59 -0400 |
commit | 9ae39b3553146b3ae72fc5ac26654e1930baba3d (patch) | |
tree | 21fe4721d3435d5b06cf33e0b7907a4167832a07 /doc | |
parent | f200a322c4f55c853d6543e47ebdbe7457262a61 (diff) | |
download | nixlib-9ae39b3553146b3ae72fc5ac26654e1930baba3d.tar nixlib-9ae39b3553146b3ae72fc5ac26654e1930baba3d.tar.gz nixlib-9ae39b3553146b3ae72fc5ac26654e1930baba3d.tar.bz2 nixlib-9ae39b3553146b3ae72fc5ac26654e1930baba3d.tar.lz nixlib-9ae39b3553146b3ae72fc5ac26654e1930baba3d.tar.xz nixlib-9ae39b3553146b3ae72fc5ac26654e1930baba3d.tar.zst nixlib-9ae39b3553146b3ae72fc5ac26654e1930baba3d.zip |
nixpkgs docs: move generators to its own file
Diffstat (limited to 'doc')
-rw-r--r-- | doc/functions.xml | 88 | ||||
-rw-r--r-- | doc/functions/generators.xml | 90 |
2 files changed, 91 insertions, 87 deletions
diff --git a/doc/functions.xml b/doc/functions.xml index 754159bff4f1..333ed65986b1 100644 --- a/doc/functions.xml +++ b/doc/functions.xml @@ -9,93 +9,7 @@ </para> <xi:include href="functions/overrides.xml" /> - <section xml:id="sec-generators"> - <title>Generators</title> - - <para> - 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> - </para> - - <para> - 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. - </para> - - <para> - 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: - </para> - -<programlisting> -with lib; -let - customToINI = generators.toINI { - # specifies how to format a key/value pair - mkKeyValue = generators.mkKeyValueDefault { - # specifies the generated string for a subset of nix values - mkValueString = v: - if v == true then ''"yes"'' - else if v == false then ''"no"'' - else if isString v then ''"${v}"'' - # and delegats all other values to the default generator - else generators.mkValueStringDefault {} v; - } ":"; - }; - -# the INI file can now be given as plain old nix values -in customToINI { - main = { - pushinfo = true; - autopush = false; - host = "localhost"; - port = 42; - }; - mergetool = { - merge = "diff3"; - }; -} -</programlisting> - - <para> - This will produce the following INI file as nix string: - </para> - -<programlisting> -[main] -autopush:"no" -host:"localhost" -port:42 -pushinfo:"yes" -str\:ange:"very::strange" - -[mergetool] -merge:"diff3" -</programlisting> - - <note> - <para> - Nix store paths can be converted to strings by enclosing a derivation - attribute like so: <code>"${drv}"</code>. - </para> - </note> - - <para> - Detailed documentation for each generator can be found in - <literal>lib/generators.nix</literal>. - </para> - </section> + <xi:include href="functions/generators.xml" /> <section xml:id="sec-debug"> <title>Debugging Nix Expressions</title> diff --git a/doc/functions/generators.xml b/doc/functions/generators.xml new file mode 100644 index 000000000000..0a9c346145f9 --- /dev/null +++ b/doc/functions/generators.xml @@ -0,0 +1,90 @@ +<section xmlns="http://docbook.org/ns/docbook" + xmlns:xlink="http://www.w3.org/1999/xlink" + xmlns:xi="http://www.w3.org/2001/XInclude" + xml:id="sec-generators"> + <title>Generators</title> + + <para> + 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> + </para> + + <para> + 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. + </para> + + <para> + 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: + </para> + +<programlisting> +with lib; +let + customToINI = generators.toINI { + # specifies how to format a key/value pair + mkKeyValue = generators.mkKeyValueDefault { + # specifies the generated string for a subset of nix values + mkValueString = v: + if v == true then ''"yes"'' + else if v == false then ''"no"'' + else if isString v then ''"${v}"'' + # and delegats all other values to the default generator + else generators.mkValueStringDefault {} v; + } ":"; + }; + +# the INI file can now be given as plain old nix values +in customToINI { + main = { + pushinfo = true; + autopush = false; + host = "localhost"; + port = 42; + }; + mergetool = { + merge = "diff3"; + }; +} +</programlisting> + + <para> + This will produce the following INI file as nix string: + </para> + +<programlisting> +[main] +autopush:"no" +host:"localhost" +port:42 +pushinfo:"yes" +str\:ange:"very::strange" + +[mergetool] +merge:"diff3" +</programlisting> + + <note> + <para> + Nix store paths can be converted to strings by enclosing a derivation + attribute like so: <code>"${drv}"</code>. + </para> + </note> + + <para> + Detailed documentation for each generator can be found in + <literal>lib/generators.nix</literal>. + </para> + </section> |