diff --git a/lib/filesystem.nix b/lib/filesystem.nix index f604eb90434b..13f4ebb26402 100644 --- a/lib/filesystem.nix +++ b/lib/filesystem.nix @@ -22,7 +22,12 @@ in Returns the type of a path: regular (for file), symlink, or directory. */ pathType = path: - (readDir (dirOf path)).${baseNameOf path}; + # The filesystem root is the only path where `dirOf / == /` and + # `baseNameOf /` is not valid. We can detect this and directly return + # "directory", since we know the filesystem root can't be anything else. + if dirOf path == path + then "directory" + else (readDir (dirOf path)).${baseNameOf path}; /* Returns true if the path exists and is a directory, false otherwise. diff --git a/lib/tests/filesystem.sh b/lib/tests/filesystem.sh index ab1504abb490..61710da92ba2 100755 --- a/lib/tests/filesystem.sh +++ b/lib/tests/filesystem.sh @@ -46,6 +46,7 @@ checkPathType() { fi } +checkPathType "/" '"directory"' checkPathType "$PWD/directory" '"directory"' checkPathType "$PWD/regular" '"regular"' checkPathType "$PWD/symlink" '"symlink"' @@ -62,6 +63,7 @@ checkPathIsDirectory() { fi } +checkPathIsDirectory "/" "true" checkPathIsDirectory "$PWD/directory" "true" checkPathIsDirectory "$PWD/regular" "false" checkPathIsDirectory "$PWD/symlink" "false" @@ -79,6 +81,7 @@ checkPathIsRegularFile() { fi } +checkPathIsRegularFile "/" "false" checkPathIsRegularFile "$PWD/directory" "false" checkPathIsRegularFile "$PWD/regular" "true" checkPathIsRegularFile "$PWD/symlink" "false"