Skip to content

Commit eaee40d

Browse files
committed
Implement nix/findFileRecursive.nix for efficiency
1 parent 295d7d5 commit eaee40d

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

flake.nix

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
let
3333
inherit (nixpkgs) lib;
3434
# Collect all the build.nix files (flake-parts module)
35-
buildDotNixes =
36-
with builtins;
37-
filter (lib.hasSuffix "build.nix") (
38-
lib.filesystem.listFilesRecursive (filterSource (_path: _type: true) ./.) # NOTE(bladyjoker): This is here to prevent errors due to invalid Nix store filenames (spaces, commas, etc.)
39-
);
35+
buildDotNixes = import ./nix/findFilesRecursive.nix {
36+
inherit lib;
37+
toInclude = lib.hasSuffix "build.nix";
38+
dir = ./.;
39+
};
4040
in
4141
flake-parts.lib.mkFlake { inherit inputs; } {
4242

nix/findFilesRecursive.nix

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# NOTE(bladyjoker): Sadly needed because the repo contains files and directories that are invalid Nix Store path names
2+
# TODO(bladyjoker): If the repo adjusts the naming scheme one can simply use lib.filesystem.listFilesRecursive
3+
{
4+
lib,
5+
toInclude,
6+
dir,
7+
}:
8+
let
9+
# Regex for characters allowed in Nix store paths:
10+
# Alphanumeric, dots (.), underscores (_), plus (+), and hyphens (-)
11+
isValidName = name: builtins.match "[a-zA-Z0-9._+-]+" name != null;
12+
13+
internalFunc =
14+
dir:
15+
(lib.mapAttrsToList (
16+
name: type:
17+
if isValidName name then
18+
if type == "directory" then
19+
internalFunc (dir + "/${name}")
20+
else if toInclude name then
21+
[ (dir + "/${name}") ]
22+
else
23+
[ ]
24+
else
25+
[ ]
26+
) (builtins.readDir dir));
27+
in
28+
lib.flatten (internalFunc dir)

0 commit comments

Comments
 (0)