Skip to content

Commit 98506a5

Browse files
authored
Merge pull request #12266 from NixOS/mergify/bp/2.25-maintenance/pr-12107
fetchers/git: make relative path absolute for local repo (backport #12107)
2 parents b8264c4 + dcfd806 commit 98506a5

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/libfetchers/git.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,16 @@ struct GitInputScheme : InputScheme
425425
auto url = parseURL(getStrAttr(input.attrs, "url"));
426426
bool isBareRepository = url.scheme == "file" && !pathExists(url.path + "/.git");
427427
repoInfo.isLocal = url.scheme == "file" && !forceHttp && !isBareRepository;
428-
repoInfo.url = repoInfo.isLocal ? url.path : url.to_string();
428+
//
429+
// FIXME: here we turn a possibly relative path into an absolute path.
430+
// This allows relative git flake inputs to be resolved against the
431+
// **current working directory** (as in POSIX), which tends to work out
432+
// ok in the context of flakes, but is the wrong behavior,
433+
// as it should resolve against the flake.nix base directory instead.
434+
//
435+
// See: https://discourse.nixos.org/t/57783 and #9708
436+
//
437+
repoInfo.url = repoInfo.isLocal ? std::filesystem::absolute(url.path).string() : url.to_string();
429438

430439
// If this is a local directory and no ref or revision is
431440
// given, then allow the use of an unclean working tree.

tests/functional/flakes/flake-in-submodule.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,21 @@ git -C "$rootRepo" commit -m "Add flake.nix"
7676

7777
storePath=$(nix flake metadata --json "$rootRepo?submodules=1" | jq -r .path)
7878
[[ -e "$storePath/submodule" ]]
79+
80+
# The root repo may use the submodule repo as an input
81+
# through the relative path. This may change in the future;
82+
# see: https://discourse.nixos.org/t/57783 and #9708.
83+
cat > "$rootRepo"/flake.nix <<EOF
84+
{
85+
inputs.subRepo.url = "git+file:./submodule";
86+
outputs = { ... }: { };
87+
}
88+
EOF
89+
git -C "$rootRepo" add flake.nix
90+
git -C "$rootRepo" commit -m "Add subRepo input"
91+
(
92+
cd "$rootRepo"
93+
# The submodule must be locked to the relative path,
94+
# _not_ the absolute path:
95+
[[ $(nix flake metadata --json | jq -r .locks.nodes.subRepo.locked.url) = "file:./submodule" ]]
96+
)

tests/functional/flakes/flakes.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ nix build -o "$TEST_ROOT/result" "git+file://$flake1Dir#default"
231231
nix build -o "$TEST_ROOT/result" "$flake1Dir?ref=HEAD#default"
232232
nix build -o "$TEST_ROOT/result" "git+file://$flake1Dir?ref=HEAD#default"
233233

234+
# Check that relative paths are allowed for git flakes.
235+
# This may change in the future once git submodule support is refined.
236+
# See: https://discourse.nixos.org/t/57783 and #9708.
237+
(
238+
# This `cd` should not be required and is indicative of aforementioned bug.
239+
cd "$flake1Dir/.."
240+
nix build -o "$TEST_ROOT/result" "git+file:./$(basename "$flake1Dir")"
241+
)
242+
234243
# Check that store symlinks inside a flake are not interpreted as flakes.
235244
nix build -o "$flake1Dir/result" "git+file://$flake1Dir"
236245
nix path-info "$flake1Dir/result"

0 commit comments

Comments
 (0)