query
On this page

toINI

lib.generators.toINI

Docs pulled from | This Revision | 10 minutes ago


Generate an INI-style config file from an attrset of sections to an attrset of key-value pairs.

Inputs

Structured function argument
mkSectionName (optional, default: (name: escape [ "[" "]" ] name))
apply transformations (e.g. escapes) to section names
mkKeyValue (optional, default: {} "=")
format a setting line from key and value
listsAsDuplicateKeys (optional, default: false)
allow lists as values for duplicate keys

Examples

lib.generators.toINI usage example

generators.toINI {} {
  foo = { hi = "${pkgs.hello}"; ciao = "bar"; };
  baz = { "also, integers" = 42; };
}

> [baz]
> also, integers=42
>
> [foo]
> ciao=bar
> hi=/nix/store/y93qql1p5ggfnaqjjqhxcw0vqw95rlz0-hello-2.10

The mk* configuration attributes can generically change the way sections and key-value strings are generated.

For more examples see the test cases in ./tests/misc.nix.


Noogle detected

Implementation

The following is the current implementation of this function.

toINI =
    {
      mkSectionName ? (name: escape [ "[" "]" ] name),
      mkKeyValue ? mkKeyValueDefault { } "=",
      listsAsDuplicateKeys ? false,
    }:
    attrsOfAttrs:
    let
      # map function to string for each key val
      mapAttrsToStringsSep =
        sep: mapFn: attrs:
        concatStringsSep sep (mapAttrsToList mapFn attrs);
      mkSection =
        sectName: sectValues:
        ''
          [${mkSectionName sectName}]
        ''
        + toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues;
    in
    # map input to ini sections
    mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;