lib: add generator for KDL

Added a generator for the KDL document language.
This is in order for home-manager to natively generate
the new config format for zellij, as described in nix-community#3364.

There is not a one to one mapping between KDL and nix types,
but attrset translation is heavily based on KDLs JSON-IN-KDL microsyntax.
The exception here is the `_args` and `_props` arguments, which lets you
specify arguments and properties as described in the spec.

See more here:
- https://kdl.dev/
- https://github.com/kdl-org/kdl/blob/main/SPEC.md

The generator also conforms to the interface from the nixpkgs manual:
https://nixos.org/manual/nixpkgs/stable/#sec-generators

Co-authored-by: Gaetan Lepage <gaetan@glepage.com>
This commit is contained in:
h7x4
2022-11-21 23:05:02 +01:00
committed by Robert Helgesson
parent 36999b8d19
commit fce9dbfeb4
7 changed files with 202 additions and 0 deletions

View File

@@ -0,0 +1 @@
{ generators-tokdl = ./tokdl.nix; }

View File

@@ -0,0 +1,41 @@
a 1
b "string"
bigFlatItems 23847590283751 1.239000 "multiline \" \" \"\nstring\n" null
c "multiline string\nwith special characters:\n\t \n \\" \"\n"
extraAttrs 2 true arg1=1 arg2=false {
nested {
a 1
b null
}
}
flatItems 1 2 "asdf" true null
listInAttrsInList {
list1 {
- {
a 1
}
- {
b true
}
- {
c null
d {
- {
e "asdfadfasdfasdf"
}
}
}
}
list2 {
- {
a 8
}
}
}
nested {
- 1 2
- true false
-
- null
}
unsafeString " \" \n "

View File

@@ -0,0 +1,53 @@
{ config, lib, ... }:
{
home.file."result.txt".text = lib.hm.generators.toKDL { } {
a = 1;
b = "string";
c = ''
multiline string
with special characters:
\t \n \" "
'';
unsafeString = " \" \n ";
flatItems = [ 1 2 "asdf" true null ];
bigFlatItems = [
23847590283751
1.239
''
multiline " " "
string
''
null
];
nested = [ [ 1 2 ] [ true false ] [ ] [ null ] ];
extraAttrs = {
_args = [ 2 true ];
_props = {
arg1 = 1;
arg2 = false;
};
nested = {
a = 1;
b = null;
};
};
listInAttrsInList = {
list1 = [
{ a = 1; }
{ b = true; }
{
c = null;
d = [{ e = "asdfadfasdfasdf"; }];
}
];
list2 = [{ a = 8; }];
};
};
nmt.script = ''
assertFileContent \
home-files/result.txt \
${./tokdl-result.txt}
'';
}