Created
December 31, 2021 16:47
-
-
Save sirkha/5c2d7bbfaaf6680b8a07abfd4180c749 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
# Import all .nix files and directories with a default.nix file from the given directory. | |
dirImports = importDir: let | |
dirSet = builtins.readDir importDir; | |
# Sort out symlinks | |
symSet = let | |
# Check if ${symlink}/ exists. If it does, it is a directory. | |
p = builtins.partition ( f: builtins.pathExists "${importDir}/${f}/") ( | |
builtins.filter (f: dirSet.${f} == "symlink") (builtins.attrNames dirSet) | |
); | |
in { symDirs = p.right; symFiles = p.wrong; }; | |
# Filter to files ending in ".nix" | |
nixFiles = builtins.filter (f: (builtins.substring (builtins.stringLength f -4) 4 f) == ".nix") ( | |
# Filter to files with a file name length greater than four to prevent errors in substring | |
builtins.filter (f: builtins.stringLength f > 4) ( | |
# Filter to regular files add symlinks to files | |
(builtins.filter (f: dirSet.${f} == "regular") (builtins.attrNames dirSet)) ++ symSet.symFiles | |
) | |
); | |
# Filter to directories with a default.nix | |
nixDirs = builtins.filter (d: builtins.hasAttr "default.nix" (builtins.readDir "${importDir}/${d}")) ( | |
# Filter to directories and directory symlinks | |
(builtins.filter (d: dirSet.${d} == "directory") (builtins.attrNames dirSet)) ++ symSet.symDirs | |
); | |
list = map ( n: "${importDir}/${n}" ) (nixFiles ++ nixDirs); | |
in list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment