Skip to content

Commit

Permalink
lib/pathsToImportedAttrs: allow directories
Browse files Browse the repository at this point in the history
check if directory has a default.nix and use directory name as key

Co-authored-by: Timothy DeHerrera <[email protected]>
  • Loading branch information
Pacman99 and nrdxp committed Mar 29, 2021
1 parent ef1ee6e commit 836e70b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/attrs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ rec {
genAttrs' = values: f: lib.listToAttrs (map f values);

# Convert a list of file paths to attribute set
# that has the filenames stripped of nix extension as keys
# where the key is the folder or filename stripped
# of nix extension or folder as keys
# and imported content of the file as value.
#
pathsToImportedAttrs = paths:
let
paths' = lib.filter (lib.hasSuffix ".nix") paths;
paths' = lib.filter
(path: lib.hasSuffix ".nix" path
|| lib.pathExists "${path}/default.nix")
paths;
in
genAttrs' paths' (path: {
name = lib.removeSuffix ".nix"
Expand Down
10 changes: 6 additions & 4 deletions tests/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ lib.runTests {
testPathsToImportedAttrs = {
expr =
pathsToImportedAttrs [
./testPathsToImportedAttrs/foo.nix
./testPathsToImportedAttrs/bar.nix
./testPathsToImportedAttrs/t.nix
./testPathsToImportedAttrs/f.nix
"${self}/tests/testPathsToImportedAttrs/dir"
"${self}/tests/testPathsToImportedAttrs/foo.nix"
"${self}/tests/testPathsToImportedAttrs/bar.nix"
"${self}/tests/testPathsToImportedAttrs/t.nix"
"${self}/tests/testPathsToImportedAttrs/f.nix"
];

expected = {
dir = { a = 5; };
foo = { bar = 1; };
bar = { foo = 2; };
t = true;
Expand Down
1 change: 1 addition & 0 deletions tests/testPathsToImportedAttrs/dir/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ a = 5; }

0 comments on commit 836e70b

Please sign in to comment.