Skip to content

Commit

Permalink
Merge #225
Browse files Browse the repository at this point in the history
225: lib: only readDir if path exists with safeReadDir r=nrdxp a=Pacman99

After doing this for the second time, I realized it might be good to make a lib function for it and do it across lib.
Create a function called `safeReadDir` that only uses `builtins.readDir` if the path exists. With `optionalAttrs` any function that relies on the output won't fail since they still get an empty attrset.
Then replace all uses of `readDir` with the safe version.

Co-authored-by: Pacman99 <pachum99@gmail.com>
  • Loading branch information
bors[bot] and Pacman99 authored Apr 1, 2021
2 parents f14dcda + 3d1501c commit 01b0555
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/attrs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ rec {
lib.isDerivation v && !meta.broken && builtins.elem system platforms);
in
lib.filterAttrs filter packages;

safeReadDir = path: lib.optionalAttrs (builtins.pathExists path) (builtins.readDir path);
}
2 changes: 1 addition & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ lib.makeExtensible (final:
lists = callLibs ./lists.nix;
strings = callLibs ./strings.nix;

inherit (attrs) mapFilterAttrs genAttrs'
inherit (attrs) mapFilterAttrs genAttrs' safeReadDir
pathsToImportedAttrs concatAttrs filterPackages;
inherit (lists) pathsIn;
inherit (strings) rgxToString;
Expand Down
4 changes: 2 additions & 2 deletions lib/devos/mkProfileAttrs.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, ... }:
{ lib, dev, ... }:

let mkProfileAttrs =
/**
Expand All @@ -16,7 +16,7 @@ let mkProfileAttrs =
let
imports =
let
files = builtins.readDir dir;
files = dev.safeReadDir dir;

p = n: v:
v == "directory"
Expand Down
2 changes: 1 addition & 1 deletion lib/devos/recImport.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ dev.mapFilterAttrs
let name = lib.removeSuffix ".nix" n; in lib.nameValuePair (name) (_import name)
else
lib.nameValuePair ("") (null))
(builtins.readDir dir)
(dev.safeReadDir dir)
5 changes: 2 additions & 3 deletions lib/lists.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{ lib, ... }:
{ lib, dev, ... }:
{
pathsIn = dir:
let
fullPath = name: "${toString dir}/${name}";
in
map fullPath (lib.attrNames (lib.optionalAttrs
(builtins.pathExists dir) (builtins.readDir dir)));
map fullPath (lib.attrNames (dev.safeReadDir dir));
}
8 changes: 8 additions & 0 deletions tests/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ lib.runTests {
(rgxToString "hat" "foohatbar" == "hat")
];

testSafeReadDir = {
expr = safeReadDir "${self}/tests/profiles" // safeReadDir "${self}/nonexistentdir";
expected = {
foo = "directory";
t = "directory";
};
};

testSuites =
let
profiles = os.mkProfileAttrs (toString ./profiles);
Expand Down

0 comments on commit 01b0555

Please sign in to comment.