pkgs.formats.configobj: init
Co-authored-by: Matt Sturgeon <matt@sturgeon.me.uk>
This commit is contained in:
@@ -322,6 +322,14 @@ have a predefined type and string generator already declared under
|
||||
|
||||
The attribute `lib.type.atom` contains the used INI atom.
|
||||
|
||||
`pkgs.formats.configobj` { }
|
||||
|
||||
: A function taking an attribute set with values
|
||||
|
||||
It returns a set with [ConfigObj](https://pypi.org/project/configobj/)-specific attributes `type` and `generate` as specified [below](#pkgs-formats-result).
|
||||
The type of the input is an attribute mapping supporting both atoms and nested attribute sets (sections/subsections), as supported by ConfigObj.
|
||||
The renderer is based on Python's `configobj` module.
|
||||
|
||||
`pkgs.formats.iniWithGlobalSection` { *`listsAsDuplicateKeys`* ? false, *`listToValue`* ? null, \.\.\. }
|
||||
|
||||
: A function taking an attribute set with values
|
||||
|
||||
@@ -131,6 +131,8 @@ optionalAttrs allowAliases aliases
|
||||
|
||||
php = (import ./formats/php/default.nix { inherit lib pkgs; }).format;
|
||||
|
||||
configobj = (import ./formats/configobj/default.nix { inherit lib pkgs; }).format;
|
||||
|
||||
json =
|
||||
{ }:
|
||||
{
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
}:
|
||||
let
|
||||
inherit (lib)
|
||||
toJSON
|
||||
;
|
||||
|
||||
inherit (lib.types)
|
||||
serializableValueWith
|
||||
;
|
||||
in
|
||||
{
|
||||
format =
|
||||
{ }:
|
||||
{
|
||||
type = serializableValueWith { typeName = "ConfigObj mapping"; };
|
||||
|
||||
generate =
|
||||
name: value:
|
||||
pkgs.runCommandLocal name
|
||||
{
|
||||
nativeBuildInputs = [
|
||||
(pkgs.python3.withPackages (ps: [ ps.configobj ]))
|
||||
];
|
||||
|
||||
valuesJSON = toJSON value;
|
||||
__structuredAttrs = true;
|
||||
strictDeps = true;
|
||||
}
|
||||
''
|
||||
printf "%s" "$valuesJSON" | python3 ${./generate.py} > "$out"
|
||||
'';
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import json
|
||||
import sys
|
||||
from configobj import ConfigObj
|
||||
|
||||
config = ConfigObj(interpolation=False, encoding="UTF8")
|
||||
config.update(json.load(sys.stdin))
|
||||
config.write(sys.stdout.buffer)
|
||||
@@ -229,6 +229,110 @@ runBuildTests {
|
||||
'';
|
||||
};
|
||||
|
||||
configobjAtoms = shouldPass {
|
||||
format = formats.configobj { };
|
||||
input = {
|
||||
bool = true;
|
||||
int = 10;
|
||||
float = 3.141;
|
||||
str = "string";
|
||||
};
|
||||
expected = ''
|
||||
bool = True
|
||||
float = 3.141
|
||||
int = 10
|
||||
str = string
|
||||
'';
|
||||
};
|
||||
|
||||
configobjListWithoutListToValue = shouldPass {
|
||||
format = formats.configobj { };
|
||||
input = {
|
||||
items = [
|
||||
1
|
||||
true
|
||||
"x"
|
||||
];
|
||||
};
|
||||
expected = ''
|
||||
items = 1, True, x
|
||||
'';
|
||||
};
|
||||
|
||||
configobjNestedAttrsets = shouldPass {
|
||||
format = formats.configobj { };
|
||||
input = {
|
||||
server = {
|
||||
host = "127.0.0.1";
|
||||
port = 8080;
|
||||
enabled = true;
|
||||
tags = [
|
||||
"web"
|
||||
"nix"
|
||||
42
|
||||
];
|
||||
};
|
||||
|
||||
logging = {
|
||||
level = "info";
|
||||
rotate = true;
|
||||
};
|
||||
|
||||
interfaces = {
|
||||
local = {
|
||||
address = "123";
|
||||
coin = {
|
||||
foo = "bar";
|
||||
};
|
||||
};
|
||||
remote = {
|
||||
address = "456";
|
||||
};
|
||||
};
|
||||
};
|
||||
expected = ''
|
||||
[interfaces]
|
||||
[[local]]
|
||||
address = 123
|
||||
[[[coin]]]
|
||||
foo = bar
|
||||
[[remote]]
|
||||
address = 456
|
||||
[logging]
|
||||
level = info
|
||||
rotate = True
|
||||
[server]
|
||||
enabled = True
|
||||
host = 127.0.0.1
|
||||
port = 8080
|
||||
tags = web, nix, 42
|
||||
'';
|
||||
};
|
||||
|
||||
configobjNullableValues = shouldPass {
|
||||
format = formats.configobj { };
|
||||
input = {
|
||||
nullable = null;
|
||||
nested = {
|
||||
keep = "ok";
|
||||
missing = null;
|
||||
};
|
||||
};
|
||||
expected = ''
|
||||
nullable = None
|
||||
[nested]
|
||||
keep = ok
|
||||
missing = None
|
||||
'';
|
||||
};
|
||||
|
||||
configobjInvalidAtom = shouldFail {
|
||||
format = formats.configobj { };
|
||||
input = {
|
||||
function = _: 1;
|
||||
};
|
||||
};
|
||||
|
||||
iniInvalidAtom = shouldFail {
|
||||
format = formats.ini { };
|
||||
input = {
|
||||
|
||||
Reference in New Issue
Block a user