Home Explore Blog CI



nixpkgs

doc/functions/generators.section.md
a04c5ea529cd91f01d0e6ede662d2c7388b124adca99c3410000000300000857
# Generators {#sec-generators}
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`

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.

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:

```nix
let
  inherit (lib) generators isString;

  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 delegates all other values to the default generator
        else
          generators.mkValueStringDefault { } v;
    } ":";
  };

in
# the INI file can now be given as plain old nix values
customToINI {
  main = {
    pushinfo = true;
    autopush = false;
    host = "localhost";
    port = 42;
  };
  mergetool = {
    merge = "diff3";
  };
}
```

This will produce the following INI file as nix string:

```INI
[main]
autopush:"no"
host:"localhost"
port:42
pushinfo:"yes"
str\:ange:"very::strange"

[mergetool]
merge:"diff3"
```

::: {.note}
Nix store paths can be converted to strings by enclosing a derivation attribute like so: `"${drv}"`.
:::

Detailed documentation for each generator can be found [here](#sec-functions-library-generators)

Chunks
7f062fed (1st chunk of `doc/functions/generators.section.md`)
Title: Generators in Nix
Summary
Generators in Nix are functions that transform Nix data structures into various file formats like INI, JSON, and YAML. They share a common interface: `generatorName configFunctions data`. `configFunctions` allows customization of the output format through user-defined functions. The example demonstrates creating a custom INI generator with specific formatting rules for key-value pairs, boolean values, and string quoting. Nix store paths can be converted to strings using `${drv}`.