oops, add the flattenTree.nix file

This commit is contained in:
zimbatm 2020-07-22 11:29:59 +02:00
parent 92ade60501
commit 5973a46a75
No known key found for this signature in database
GPG Key ID: 71BAF6D40C1D63D7
1 changed files with 35 additions and 0 deletions

35
flattenTree.nix Normal file
View File

@ -0,0 +1,35 @@
tree:
let
op = sum: path: val:
let
pathStr = builtins.concatStringsSep "_" path;
in
if (builtins.typeOf val) != "set" then
# ignore that value
# builtins.trace "${pathStr} is not of type set"
sum
else if val ? type && val.type == "derivation" then
# builtins.trace "${pathStr} is a derivation"
# we used to use the derivation outPath as the key, but that crashes Nix
# so fallback on constructing a static key
(sum // {
"${pathStr}" = val;
})
else if val ? recurseForDerivations && val.recurseForDerivations == true then
# builtins.trace "${pathStr} is a recursive"
# recurse into that attribute set
(recurse sum path val)
else
# ignore that value
# builtins.trace "${pathStr} is something else"
sum
;
recurse = sum: path: val:
builtins.foldl'
(sum: key: op sum (path ++ [ key ]) val.${key})
sum
(builtins.attrNames val)
;
in
recurse { } [ ] tree