Merge pull request #7796 from hercules-ci/fix-7263

Ensure that `self.outPath == ./.`
This commit is contained in:
Théophane Hufschmitt 2023-02-27 10:26:02 +01:00 committed by GitHub
commit 995bfeef3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 105 additions and 2 deletions

View file

@ -3,6 +3,12 @@
* A new function `builtins.readFileType` is available. It is similar to * A new function `builtins.readFileType` is available. It is similar to
`builtins.readDir` but acts on a single file or directory. `builtins.readDir` but acts on a single file or directory.
* In flakes, the `.outPath` attribute of a flake now always refers to the
directory containing the `flake.nix`. This was not the case for when
`flake.nix` was in a subdirectory of e.g. a git repository.
The root of the source of a flake in a subdirectory is still available in
`.sourceInfo.outPath`.
* The `builtins.readDir` function has been optimized when encountering not-yet-known * The `builtins.readDir` function has been optimized when encountering not-yet-known
file types from POSIX's `readdir`. In such cases the type of each file is/was file types from POSIX's `readdir`. In such cases the type of each file is/was
discovered by making multiple syscalls. This change makes these operations discovered by making multiple syscalls. This change makes these operations

View file

@ -16,7 +16,9 @@ let
subdir = if key == lockFile.root then rootSubdir else node.locked.dir or ""; subdir = if key == lockFile.root then rootSubdir else node.locked.dir or "";
flake = import (sourceInfo + (if subdir != "" then "/" else "") + subdir + "/flake.nix"); outPath = sourceInfo + ((if subdir == "" then "" else "/") + subdir);
flake = import (outPath + "/flake.nix");
inputs = builtins.mapAttrs inputs = builtins.mapAttrs
(inputName: inputSpec: allNodes.${resolveInput inputSpec}) (inputName: inputSpec: allNodes.${resolveInput inputSpec})
@ -43,7 +45,21 @@ let
outputs = flake.outputs (inputs // { self = result; }); outputs = flake.outputs (inputs // { self = result; });
result = outputs // sourceInfo // { inherit inputs; inherit outputs; inherit sourceInfo; _type = "flake"; }; result =
outputs
# We add the sourceInfo attribute for its metadata, as they are
# relevant metadata for the flake. However, the outPath of the
# sourceInfo does not necessarily match the outPath of the flake,
# as the flake may be in a subdirectory of a source.
# This is shadowed in the next //
// sourceInfo
// {
# This shadows the sourceInfo.outPath
inherit outPath;
inherit inputs; inherit outputs; inherit sourceInfo; _type = "flake";
};
in in
if node.flake or true then if node.flake or true then
assert builtins.isFunction flake.outputs; assert builtins.isFunction flake.outputs;

80
tests/flakes/inputs.sh Normal file
View file

@ -0,0 +1,80 @@
source ./common.sh
requireGit
test_subdir_self_path() {
baseDir=$TEST_ROOT/$RANDOM
flakeDir=$baseDir/b-low
mkdir -p $flakeDir
writeSimpleFlake $baseDir
writeSimpleFlake $flakeDir
echo all good > $flakeDir/message
cat > $flakeDir/flake.nix <<EOF
{
outputs = inputs: rec {
packages.$system = rec {
default =
assert builtins.readFile ./message == "all good\n";
assert builtins.readFile (inputs.self + "/message") == "all good\n";
import ./simple.nix;
};
};
}
EOF
(
nix build $baseDir?dir=b-low --no-link
)
}
test_subdir_self_path
test_git_subdir_self_path() {
repoDir=$TEST_ROOT/repo-$RANDOM
createGitRepo $repoDir
flakeDir=$repoDir/b-low
mkdir -p $flakeDir
writeSimpleFlake $repoDir
writeSimpleFlake $flakeDir
echo all good > $flakeDir/message
cat > $flakeDir/flake.nix <<EOF
{
outputs = inputs: rec {
packages.$system = rec {
default =
assert builtins.readFile ./message == "all good\n";
assert builtins.readFile (inputs.self + "/message") == "all good\n";
assert inputs.self.outPath == inputs.self.sourceInfo.outPath + "/b-low";
import ./simple.nix;
};
};
}
EOF
(
cd $flakeDir
git add .
git commit -m init
# nix build
)
clientDir=$TEST_ROOT/client-$RANDOM
mkdir -p $clientDir
cat > $clientDir/flake.nix <<EOF
{
inputs.inp = {
type = "git";
url = "file://$repoDir";
dir = "b-low";
};
outputs = inputs: rec {
packages = inputs.inp.packages;
};
}
EOF
nix build $clientDir --no-link
}
test_git_subdir_self_path

View file

@ -5,6 +5,7 @@ nix_tests = \
flakes/mercurial.sh \ flakes/mercurial.sh \
flakes/circular.sh \ flakes/circular.sh \
flakes/init.sh \ flakes/init.sh \
flakes/inputs.sh \
flakes/follow-paths.sh \ flakes/follow-paths.sh \
flakes/bundle.sh \ flakes/bundle.sh \
flakes/check.sh \ flakes/check.sh \