-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.nix
75 lines (49 loc) · 2.59 KB
/
filesystem.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{ lib }: let
inherit (builtins) readDir filter pathExists baseNameOf;
inherit (lib) mapAttrsToList concatLists filterAttrs hasPrefix hasSuffix;
# Read the content of a directory recursively and return a list of files and directories.
readDirRec = dirPath: filterFn: maxDepth: let
readRecursive = fullPath: prefix: currentDepth: let
# Read the content of the current directory
dirContent = if currentDepth > 0 || maxDepth == -1 then (readDir fullPath) else { };
filteredDirs = filterAttrs filterFn dirContent;
recurse = (name: type: let
# Full path to recursively read other dirs.
nextFullPath = "${fullPath}/${name}";
# Relative path to store in the output.
relativePath = if prefix == "" then name else "${prefix}/${name}";
current = [{ inherit type; path = relativePath; }];
# If the current directory is a directory, then recursively read it.
output = if type == "directory"
then current ++ readRecursive nextFullPath relativePath (currentDepth - 1)
else current;
in output);
in concatLists (mapAttrsToList recurse filteredDirs);
in readRecursive dirPath "" maxDepth;
in rec {
# Reads the contents of the given directory recursively. Allows to speciify a max depth and whether to include hidden files and directories.
readDirRecursive' = dirPath: { maxDepth ? -1, includeHidden ? false }: let
# Filter function to exclude hidden files and directories.
filterFn = if includeHidden then (_: _: true) else (name: _: (! hasPrefix "." name));
contents = readDirRec dirPath filterFn maxDepth;
in contents;
# Reads the contents of the entire given directory recursively.
readDirRecursive = dirPath: (readDirRecursive' dirPath { });
# Retrieves all the paths of non 'default.nix' files in the given folder.
getModulesRecursive = dirPath: let
# Read the content of the directory recursively.
contents = readDirRecursive dirPath;
# Filter out files that are not modules.
modules = filter ({ type, path }: let
# It's a file, it's not a default.nix and it has a .nix extension.
isModule = type == "regular" && (baseNameOf path) != "default.nix" && hasSuffix ".nix" path;
in isModule) contents;
in map ({ type, path }: path) modules;
# Maps the given function to all the modules in the given directory.
mapModulesRecursive = dirPath: fn: let
# Get all the modules in the given directory.
modules = getModulesRecursive dirPath;
# Map the given function to each module.
mapped = map (path: fn path) modules;
in mapped;
}