Skip to content

Commit

Permalink
Input::fetchToStore(): Don't try to substitute
Browse files Browse the repository at this point in the history
Having a narHash doesn't mean that we have the other attributes
returned by the fetcher (such as lastModified or rev). For instance,

   $ nix flake metadata github:NixOS/patchelf/7c2f768bf9601268a4e71c2ebe91e2011918a70f
   Last modified: 2024-01-15 10:51:22

but

   $ nix flake metadata github:NixOS/patchelf/7c2f768bf9601268a4e71c2ebe91e2011918a70f?narHash=sha256-PPXqKY2hJng4DBVE0I4xshv/vGLUskL7jl53roB8UdU%3D
   (does not print a "Last modified")

The latter only happens if the store path already exists or is
substitutable, which made this impure behaviour unpredictable.

Fixes NixOS#10601.
  • Loading branch information
edolstra committed Apr 26, 2024
1 parent 84e0c46 commit ff107d9
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 21 deletions.
18 changes: 0 additions & 18 deletions src/libfetchers/fetchers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,6 @@ std::pair<StorePath, Input> Input::fetchToStore(ref<Store> store) const
if (!scheme)
throw Error("cannot fetch unsupported input '%s'", attrsToJSON(toAttrs()));

/* The tree may already be in the Nix store, or it could be
substituted (which is often faster than fetching from the
original source). So check that. */
if (getNarHash()) {
try {
auto storePath = computeStorePath(*store);

store->ensurePath(storePath);

debug("using substituted/cached input '%s' in '%s'",
to_string(), store->printStorePath(storePath));

return {std::move(storePath), *this};
} catch (Error & e) {
debug("substitution of input '%s' failed: %s", to_string(), e.what());
}
}

auto [storePath, input] = [&]() -> std::pair<StorePath, Input> {
try {
auto [accessor, final] = getAccessorUnchecked(store);
Expand Down
3 changes: 0 additions & 3 deletions tests/functional/tarball.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ test_tarball() {
nix-build -o $TEST_ROOT/result -E "import (fetchTree file://$tarball)"
nix-build -o $TEST_ROOT/result -E "import (fetchTree { type = \"tarball\"; url = file://$tarball; })"
nix-build -o $TEST_ROOT/result -E "import (fetchTree { type = \"tarball\"; url = file://$tarball; narHash = \"$hash\"; })"
# Do not re-fetch paths already present
nix-build -o $TEST_ROOT/result -E "import (fetchTree { type = \"tarball\"; url = file:///does-not-exist/must-remain-unused/$tarball; narHash = \"$hash\"; })"
expectStderr 102 nix-build -o $TEST_ROOT/result -E "import (fetchTree { type = \"tarball\"; url = file://$tarball; narHash = \"sha256-xdKv2pq/IiwLSnBBJXW8hNowI4MrdZfW+SYqDQs7Tzc=\"; })" | grep 'NAR hash mismatch in input'

[[ $(nix eval --impure --expr "(fetchTree file://$tarball).lastModified") = 1000000000 ]]

Expand Down

0 comments on commit ff107d9

Please sign in to comment.