flakes: search up to git or filesystem boundary

While parsing a flakeref, upon not finding a flake.nix, search upwards
until git or filesystem boundary.
This commit is contained in:
Tom Bereknyei 2021-12-03 10:53:41 -05:00
parent 2e606e87c4
commit b6cc0a704d
4 changed files with 48 additions and 2 deletions

View file

@ -8,3 +8,5 @@
* New built-in function: `builtins.groupBy`, with the same functionality as
Nixpkgs' `lib.groupBy`, but faster.
* Nix now searches for a flake.nix up until git or filesystem boundary.

View file

@ -117,8 +117,27 @@ std::pair<FlakeRef, std::string> parseFlakeRefWithFragment(
if (!S_ISDIR(lstat(path).st_mode))
throw BadURL("path '%s' is not a flake (because it's not a directory)", path);
if (!allowMissing && !pathExists(path + "/flake.nix"))
throw BadURL("path '%s' is not a flake (because it doesn't contain a 'flake.nix' file)", path);
if (!allowMissing && !pathExists(path + "/flake.nix")){
notice("path '%s' does not contain a 'flake.nix', searching up",path);
// Save device to detect filesystem boundary
dev_t device = lstat(path).st_dev;
bool found = false;
while (path != "/") {
if (pathExists(path + "/flake.nix")) {
found = true;
break;
} else if (pathExists(path + "/.git"))
throw Error("unable to find a flake before encountering git boundary at '%s'", path);
else {
if (lstat(path).st_dev != device)
throw Error("unable to find a flake before encountering filesystem boundary at '%s'", path);
}
path = dirOf(path);
}
if (!found)
throw BadURL("could not find a flake.nix file");
}
auto flakeRoot = path;
std::string subdir;

24
tests/flake-searching.sh Normal file
View file

@ -0,0 +1,24 @@
source common.sh
clearStore
cp ./simple.nix ./simple.builder.sh ./config.nix $TEST_HOME
cd $TEST_HOME
cat <<EOF > flake.nix
{
outputs = a: {
defaultPackage.$system = import ./simple.nix;
packages.$system.test = import ./simple.nix;
};
}
EOF
mkdir subdir
cd subdir
for i in "" . "$PWD" .# .#test; do
nix build $i || fail "flake should be found by searching up directories"
done
for i in "path:$PWD"; do
! nix build $i || fail "flake should not search up directories when using 'path:'"
done

View file

@ -47,6 +47,7 @@ nix_tests = \
describe-stores.sh \
flakes.sh \
flake-local-settings.sh \
flake-searching.sh \
build.sh \
repl.sh ca/repl.sh \
ca/build.sh \