formats.ini: expose INI atom from all ini formats

This commit is contained in:
Johannes Kirschbauer 2024-11-09 17:01:11 +01:00
parent b35c45a2c1
commit 6b53949b0c
No known key found for this signature in database
3 changed files with 24 additions and 14 deletions

View File

@ -312,6 +312,8 @@ have a predefined type and string generator already declared under
may be transformed into multiple key-value pairs depending on may be transformed into multiple key-value pairs depending on
`listToValue`). `listToValue`).
The attribute `lib.type.atom` contains the used INI atom.
`pkgs.formats.iniWithGlobalSection` { *`listsAsDuplicateKeys`* ? false, *`listToValue`* ? null, \.\.\. } `pkgs.formats.iniWithGlobalSection` { *`listsAsDuplicateKeys`* ? false, *`listToValue`* ? null, \.\.\. }
: A function taking an attribute set with values : A function taking an attribute set with values
@ -333,6 +335,8 @@ have a predefined type and string generator already declared under
attrset of key-value pairs for a single section, the global section which attrset of key-value pairs for a single section, the global section which
preceedes the section definitions. preceedes the section definitions.
The attribute `lib.type.atom` contains the used INI atom.
`pkgs.formats.toml` { } `pkgs.formats.toml` { }
: A function taking an empty attribute set (for future extensibility) : A function taking an empty attribute set (for future extensibility)

View File

@ -7,7 +7,7 @@ let
stateDir = "/var/lib/public-inbox"; stateDir = "/var/lib/public-inbox";
gitIni = pkgs.formats.gitIni { listsAsDuplicateKeys = true; }; gitIni = pkgs.formats.gitIni { listsAsDuplicateKeys = true; };
iniAtom = elemAt gitIni.type/*attrsOf*/.functor.wrapped/*attrsOf*/.functor.wrapped/*either*/.functor.wrapped 0; iniAtom = gitIni.lib.types.atom;
useSpamAssassin = cfg.settings.publicinboxmda.spamcheck == "spamc" || useSpamAssassin = cfg.settings.publicinboxmda.spamcheck == "spamc" ||
cfg.settings.publicinboxwatch.spamcheck == "spamc"; cfg.settings.publicinboxwatch.spamcheck == "spamc";

View File

@ -123,9 +123,9 @@ rec {
} }
else else
singleIniAtom; singleIniAtom;
iniSection = { listsAsDuplicateKeys, listToValue, atomsCoercedToLists }@args: iniSection = atom:
attrsOf (iniAtom args) // { attrsOf atom // {
description = "section of an INI file (attrs of " + (iniAtom args).description + ")"; description = "section of an INI file (attrs of " + atom.description + ")";
}; };
maybeToList = listToValue: if listToValue != null then lib.mapAttrs (key: val: if lib.isList val then listToValue val else val) else lib.id; maybeToList = listToValue: if listToValue != null then lib.mapAttrs (key: val: if lib.isList val then listToValue val else val) else lib.id;
@ -144,12 +144,14 @@ rec {
assert atomsCoercedToLists != null -> (listsAsDuplicateKeys || listToValue != null); assert atomsCoercedToLists != null -> (listsAsDuplicateKeys || listToValue != null);
let let
atomsCoercedToLists' = if atomsCoercedToLists == null then false else atomsCoercedToLists; atomsCoercedToLists' = if atomsCoercedToLists == null then false else atomsCoercedToLists;
atom = iniAtom { inherit listsAsDuplicateKeys listToValue; atomsCoercedToLists = atomsCoercedToLists'; };
in in
{ {
type = lib.types.attrsOf ( type = lib.types.attrsOf (
iniSection { inherit listsAsDuplicateKeys listToValue; atomsCoercedToLists = atomsCoercedToLists'; } iniSection atom
); );
lib.types.atom = atom;
generate = name: value: generate = name: value:
lib.pipe value lib.pipe value
@ -174,24 +176,26 @@ rec {
assert atomsCoercedToLists != null -> (listsAsDuplicateKeys || listToValue != null); assert atomsCoercedToLists != null -> (listsAsDuplicateKeys || listToValue != null);
let let
atomsCoercedToLists' = if atomsCoercedToLists == null then false else atomsCoercedToLists; atomsCoercedToLists' = if atomsCoercedToLists == null then false else atomsCoercedToLists;
atom = iniAtom { inherit listsAsDuplicateKeys listToValue; atomsCoercedToLists = atomsCoercedToLists'; };
in in
{ {
type = lib.types.submodule { type = lib.types.submodule {
options = { options = {
sections = lib.mkOption rec { sections = lib.mkOption rec {
type = lib.types.attrsOf ( type = lib.types.attrsOf (
iniSection { inherit listsAsDuplicateKeys listToValue; atomsCoercedToLists = atomsCoercedToLists'; } iniSection atom
); );
default = {}; default = {};
description = type.description; description = type.description;
}; };
globalSection = lib.mkOption rec { globalSection = lib.mkOption rec {
type = iniSection { inherit listsAsDuplicateKeys listToValue; atomsCoercedToLists = atomsCoercedToLists'; }; type = iniSection atom;
default = {}; default = {};
description = "global " + type.description; description = "global " + type.description;
}; };
}; };
}; };
lib.types.atom = atom;
generate = name: { sections ? {}, globalSection ? {}, ... }: generate = name: { sections ? {}, globalSection ? {}, ... }:
pkgs.writeText name (lib.generators.toINIWithGlobalSection (removeAttrs args ["listToValue" "atomsCoercedToLists"]) pkgs.writeText name (lib.generators.toINIWithGlobalSection (removeAttrs args ["listToValue" "atomsCoercedToLists"])
{ {
@ -200,17 +204,19 @@ rec {
}); });
}; };
gitIni = { listsAsDuplicateKeys ? false, ... }@args: { gitIni = { listsAsDuplicateKeys ? false, ... }@args:
type = let let
atom = iniAtom { atom = iniAtom {
listsAsDuplicateKeys = listsAsDuplicateKeys; inherit listsAsDuplicateKeys;
listToValue = null; listToValue = null;
atomsCoercedToLists = false; atomsCoercedToLists = false;
}; };
in attrsOf (attrsOf (either atom (attrsOf atom))); in
{
generate = name: value: pkgs.writeText name (lib.generators.toGitINI value); type = attrsOf (attrsOf (either atom (attrsOf atom)));
}; lib.types.atom = atom;
generate = name: value: pkgs.writeText name (lib.generators.toGitINI value);
};
}) ini iniWithGlobalSection gitIni; }) ini iniWithGlobalSection gitIni;