diff options
author | Profpatsch <mail@profpatsch.de> | 2018-03-26 17:31:51 +0200 |
---|---|---|
committer | Profpatsch <mail@profpatsch.de> | 2018-03-29 16:53:06 +0200 |
commit | e01d485ce41f45dfc060ad1a6ec48c115fbdc1b6 (patch) | |
tree | cfe1acfe680a5da416c8a7532473d68b30f2db11 | |
parent | fa71407f3656b0bf65d40e94a20513d56bcf3c61 (diff) |
lib/generators: add an example of overriding defaults
An example of overriding the `toINI` generator is added,
hopefully clarifying the expressiveness of generators.
-rw-r--r-- | doc/functions.xml | 61 |
1 files changed, 57 insertions, 4 deletions
diff --git a/doc/functions.xml b/doc/functions.xml index 52bdf13eba9b..f790512e7db1 100644 --- a/doc/functions.xml +++ b/doc/functions.xml @@ -221,16 +221,69 @@ <para> All generators follow a similar call interface: <code>generatorName - configFunctions data</code>, where <literal>configFunctions</literal> is a - set of user-defined functions that format variable parts of the content. + 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 gets the name - of a section and returns a sanitized name. The default + ] 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> |