Skip to content

Instantly share code, notes, and snippets.

@thalesmg
Created February 15, 2020 17:24
Show Gist options
  • Save thalesmg/ae5dc3c5359aed78a33243add14a887d to your computer and use it in GitHub Desktop.
Save thalesmg/ae5dc3c5359aed78a33243add14a887d to your computer and use it in GitHub Desktop.
Recursively traverse a directory tree mapping config files for Nix Home Manager
ͳ 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; };
    };
  };
}
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