58 lines
2 KiB
Nix
58 lines
2 KiB
Nix
# WARNING: THIS IS AN EXTREMELY FRAGILE TOML IMPLEMENTATION. I KNOW THIS VERY WELL, AND
|
|
# WILL AUDIT THIS THING (or use a more mature existing one) BEFORE CONSIDERING THIS FLAKE
|
|
# TO BE FINISHED
|
|
{
|
|
lib,
|
|
hasSystem,
|
|
...
|
|
}:
|
|
let
|
|
toTomlInner =
|
|
path: value:
|
|
let
|
|
leaf = lib.filterAttrs (_: v: builtins.isInt v || builtins.isBool v || builtins.isFloat v) value;
|
|
leafStr = lib.concatMapAttrsStringSep "" (
|
|
name: value: "${name} = ${builtins.toString value}\n"
|
|
) leaf;
|
|
strings = lib.filterAttrs (_: v: builtins.isString v || builtins.isPath v) value;
|
|
stringsStr = lib.concatMapAttrsStringSep "" (name: value: "${name} = \"${value}\"\n") strings;
|
|
lists = lib.filterAttrs (_: builtins.isList) value;
|
|
listsStr = lib.concatMapAttrsStringSep "" (
|
|
name: list:
|
|
let
|
|
inner = lib.concatStringsSep " " (
|
|
map (
|
|
elem:
|
|
if builtins.isString elem || builtins.isPath elem then
|
|
"\"${elem}\""
|
|
else if builtins.isInt elem || builtins.isBool elem || builtins.isFloat elem then
|
|
builtins.toString elem
|
|
else
|
|
throw "'${builtins.typeOf elem}' list elements are not supported at the moment"
|
|
) list
|
|
);
|
|
in
|
|
"${name} = [${inner}]\n"
|
|
) lists;
|
|
inner = lib.filterAttrs (_: builtins.isAttrs) value;
|
|
innerStr = lib.concatMapAttrsStringSep "\n" (
|
|
name: value: toTomlInner (path ++ [ name ]) value
|
|
) inner;
|
|
headerStr = if builtins.length path > 0 then "[${lib.concatStringsSep "." path}]" else "";
|
|
in
|
|
assert lib.assertMsg (builtins.isAttrs value) "only attrsets can be converted into TOML";
|
|
builtins.addErrorContext
|
|
("serializing " + (if builtins.length path > 0 then lib.last path else "(root)"))
|
|
''
|
|
${headerStr}
|
|
${leafStr}
|
|
${stringsStr}
|
|
${listsStr}
|
|
|
|
${innerStr}
|
|
'';
|
|
toToml = value: toTomlInner [ ] value;
|
|
in
|
|
lib.optionalAttrs (!hasSystem) {
|
|
config.lib.toToml = toToml;
|
|
}
|