ͳ nix eval '(let f = import ./recurseTree.nix; in f "")' | nixfmt
{
xdg = {
configFile = {
"something/b/d/e" = { source = /tmp/nixtest/b/d/e; };
"something/c" = { source = /tmp/nixtest/c; };
"something/recurseTree.nix" = { source = /tmp/nixtest/recurseTree.nix; };
};
};
}
Created
February 15, 2020 17:24
-
-
Save thalesmg/ae5dc3c5359aed78a33243add14a887d to your computer and use it in GitHub Desktop.
Recursively traverse a directory tree mapping config files for Nix Home Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with (import <nixpkgs> { }); | |
let | |
inherit (builtins) map readDir foldl' attrNames dirOf toString; | |
inherit (lib.attrsets) | |
nameValuePair filterAttrs listToAttrs mapAttrs' setAttrByPath | |
recursiveUpdate; | |
listRecursive = pathStr: listRecursive' { } pathStr; | |
listRecursive' = acc: pathStr: | |
let | |
toPath = s: path + "/${s}"; | |
path = ./. + pathStr; | |
contents = readDir path; | |
dirs = filterAttrs (k: v: v == "directory") contents; | |
files = filterAttrs (k: v: v == "regular") contents; | |
dirs' = | |
foldl' (acc: d: recursiveUpdate acc (listRecursive (pathStr + "/" + d))) | |
{ } (attrNames dirs); | |
files' = foldl' (acc: f: | |
recursiveUpdate acc (setAttrByPath [ | |
"xdg" | |
"configFile" | |
"something${pathStr}/${f}" | |
"source" | |
] (toPath f))) { } (attrNames files); | |
in recursiveUpdate dirs' files'; | |
in listRecursive |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment