Skip to content

Commit 9503ac5

Browse files
committed
Make the path fetcher lazy
Note: the path fetcher no longer computes lastModified, because that's not lazy.
1 parent 75598e5 commit 9503ac5

5 files changed

Lines changed: 23 additions & 32 deletions

File tree

src/libfetchers/fetch-to-store.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ std::pair<StorePath, Hash> fetchToStore2(
5858
}
5959
} else {
6060
static auto barf = getEnv("_NIX_TEST_BARF_ON_UNCACHEABLE").value_or("") == "1";
61-
if (barf && !filter)
61+
if (barf && !filter && !(path.to_string().starts_with("/") || path.to_string().starts_with("«path:/")))
6262
throw Error("source path '%s' is uncacheable (filter=%d)", path, (bool) filter);
6363
// FIXME: could still provide in-memory caching keyed on `SourcePath`.
6464
debug("source path '%s' is uncacheable", path);

src/libfetchers/path.cc

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -146,36 +146,23 @@ struct PathInputScheme : InputScheme
146146
auto absPath = getAbsPath(input);
147147

148148
// FIXME: check whether access to 'path' is allowed.
149+
150+
auto accessor = makeFSSourceAccessor(absPath);
151+
149152
auto storePath = store.maybeParseStorePath(absPath.string());
150153

151-
if (storePath)
154+
if (storePath) {
152155
store.addTempRoot(*storePath);
153156

154-
time_t mtime = 0;
155-
if (!storePath || storePath->name() != "source" || !store.isValidPath(*storePath)) {
156-
Activity act(*logger, lvlTalkative, actUnknown, fmt("copying %s to the store", absPath));
157-
// FIXME: try to substitute storePath.
158-
auto src = sinkToSource(
159-
[&](Sink & sink) { mtime = dumpPathAndGetMtime(absPath.string(), sink, defaultPathFilter); });
160-
storePath = store.addToStoreFromDump(*src, "source");
157+
// To prevent `fetchToStore()` copying the path again to Nix
158+
// store, pre-create an entry in the fetcher cache.
159+
auto info = store.queryPathInfo(*storePath);
160+
accessor->fingerprint = fmt("path:%s", info->narHash.to_string(HashFormat::SRI, true));
161+
settings.getCache()->upsert(
162+
makeSourcePathToHashCacheKey(*accessor->fingerprint, ContentAddressMethod::Raw::NixArchive, "/"),
163+
{{"hash", info->narHash.to_string(HashFormat::SRI, true)}});
161164
}
162165

163-
auto accessor = store.requireStoreObjectAccessor(*storePath);
164-
165-
// To prevent `fetchToStore()` copying the path again to Nix
166-
// store, pre-create an entry in the fetcher cache.
167-
auto info = store.queryPathInfo(*storePath);
168-
accessor->fingerprint =
169-
fmt("path:%s", store.queryPathInfo(*storePath)->narHash.to_string(HashFormat::SRI, true));
170-
settings.getCache()->upsert(
171-
makeSourcePathToHashCacheKey(*accessor->fingerprint, ContentAddressMethod::Raw::NixArchive, "/"),
172-
{{"hash", info->narHash.to_string(HashFormat::SRI, true)}});
173-
174-
/* Trust the lastModified value supplied by the user, if
175-
any. It's not a "secure" attribute so we don't care. */
176-
if (!input.getLastModified())
177-
input.attrs.insert_or_assign("lastModified", uint64_t(mtime));
178-
179166
return {accessor, std::move(input)};
180167
}
181168
};

tests/functional/fetchPath.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
source common.sh
44

55
touch "$TEST_ROOT/foo" -t 202211111111
6-
# We only check whether 2022-11-1* **:**:** is the last modified date since
7-
# `lastModified` is transformed into UTC in `builtins.fetchTarball`.
8-
[[ "$(nix eval --impure --raw --expr "(builtins.fetchTree \"path://$TEST_ROOT/foo\").lastModifiedDate")" =~ 2022111.* ]]
6+
7+
# The path fetcher does not return lastModified.
8+
[[ "$(nix eval --impure --expr "(builtins.fetchTree \"path://$TEST_ROOT/foo\") ? lastModifiedDate")" = false ]]
99

1010
# Check that we can override lastModified for "path:" inputs.
1111
[[ "$(nix eval --impure --expr "(builtins.fetchTree { type = \"path\"; path = \"$TEST_ROOT/foo\"; lastModified = 123; }).lastModified")" = 123 ]]

tests/functional/flakes/flakes.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ nix build -o "$TEST_ROOT"/result git+file://"$flakeGitBare"
381381
mkdir -p "$flake5Dir"
382382
writeDependentFlake "$flake5Dir"
383383
nix flake lock path://"$flake5Dir"
384-
[[ "$(nix flake metadata "path://$flake5Dir" --json | jq -r .fingerprint)" != null ]]
385384

386385
# Test tarball flakes.
387386
tar cfz "$TEST_ROOT"/flake.tar.gz -C "$TEST_ROOT" flake5

tests/functional/nix-profile.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ source common.sh
44

55
TODO_NixOS
66

7+
requireGit
8+
79
clearStore
810
clearProfiles
911

@@ -12,7 +14,7 @@ restartDaemon
1214

1315
# Make a flake.
1416
flake1Dir=$TEST_ROOT/flake1
15-
mkdir -p "$flake1Dir"
17+
createGitRepo "$flake1Dir"
1618

1719
# shellcheck disable=SC2154,SC1039
1820
cat > "$flake1Dir"/flake.nix <<EOF
@@ -50,6 +52,9 @@ printf false > "$flake1Dir"/ca.nix
5052

5153
cp "${config_nix}" "$flake1Dir"/
5254

55+
git -C "$flake1Dir" add flake.nix config.nix who version ca.nix
56+
git -C "$flake1Dir" commit -m 'Initial'
57+
5358
# Test upgrading from nix-env.
5459
nix-env -f ./user-envs.nix -i foo-1.0
5560
nix profile list | grep -A2 'Name:.*foo' | grep 'Store paths:.*foo-1.0'
@@ -224,11 +229,11 @@ error: An existing package already provides the following file:
224229
The conflicting packages have a priority of 5.
225230
To prioritise the new package:
226231
227-
nix profile add path:${flake2Dir}#packages.${system}.default --priority 4
232+
nix profile add git+file://${flake2Dir}#packages.${system}.default --priority 4
228233
229234
To prioritise the existing package:
230235
231-
nix profile add path:${flake2Dir}#packages.${system}.default --priority 6
236+
nix profile add git+file://${flake2Dir}#packages.${system}.default --priority 6
232237
EOF
233238
)
234239
[[ $("$TEST_HOME"/.nix-profile/bin/hello) = "Hello World" ]]

0 commit comments

Comments
 (0)