query
On this page

toINIWithGlobalSection

lib.generators.toINIWithGlobalSection

Docs pulled from | This Revision | 10 minutes ago


Generate an INI-style config file from an attrset specifying the global section (no header), and an attrset of sections to an attrset of key-value pairs.

Inputs

1. 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
2. Structured function argument
globalSection (required)
global section key-value pairs
sections (optional, default: {})
attrset of sections to key-value pairs

Examples

lib.generators.toINIWithGlobalSection usage example

generators.toINIWithGlobalSection {} {
  globalSection = {
    someGlobalKey = "hi";
  };
  sections = {
    foo = { hi = "${pkgs.hello}"; ciao = "bar"; };
    baz = { "also, integers" = 42; };
}

> someGlobalKey=hi
>
> [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.

If you don’t need a global section, you can also use generators.toINI directly, which only takes the part in sections.


Noogle detected

Implementation

The following is the current implementation of this function.

toINIWithGlobalSection = {
    mkSectionName ? (name: escape [ "[" "]" ] name),
    mkKeyValue    ? mkKeyValueDefault {} "=",
    listsAsDuplicateKeys ? false
  }: { globalSection, sections ? {} }:
    ( if globalSection == {}
      then ""
      else (toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } globalSection)
           + "\n")
    + (toINI { inherit mkSectionName mkKeyValue listsAsDuplicateKeys; } sections);