選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

75 行
2.6 KiB

  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. <para>
  7. 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>
  8. </para>
  9. <para>
  10. 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.
  11. </para>
  12. <para>
  13. 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:
  14. </para>
  15. <programlisting>
  16. with lib;
  17. let
  18. customToINI = generators.toINI {
  19. # specifies how to format a key/value pair
  20. mkKeyValue = generators.mkKeyValueDefault {
  21. # specifies the generated string for a subset of nix values
  22. mkValueString = v:
  23. if v == true then ''"yes"''
  24. else if v == false then ''"no"''
  25. else if isString v then ''"${v}"''
  26. # and delegats all other values to the default generator
  27. else generators.mkValueStringDefault {} v;
  28. } ":";
  29. };
  30. # the INI file can now be given as plain old nix values
  31. in customToINI {
  32. main = {
  33. pushinfo = true;
  34. autopush = false;
  35. host = "localhost";
  36. port = 42;
  37. };
  38. mergetool = {
  39. merge = "diff3";
  40. };
  41. }
  42. </programlisting>
  43. <para>
  44. This will produce the following INI file as nix string:
  45. </para>
  46. <programlisting>
  47. [main]
  48. autopush:"no"
  49. host:"localhost"
  50. port:42
  51. pushinfo:"yes"
  52. str\:ange:"very::strange"
  53. [mergetool]
  54. merge:"diff3"
  55. </programlisting>
  56. <note>
  57. <para>
  58. Nix store paths can be converted to strings by enclosing a derivation attribute like so: <code>"${drv}"</code>.
  59. </para>
  60. </note>
  61. <para>
  62. Detailed documentation for each generator can be found in <literal>lib/generators.nix</literal>.
  63. </para>
  64. </section>