Skip to content

Commit 74305d5

Browse files
committed
libfetchers: avoid re-copying substituted inputs
Previously, Nix would not create a cache entry for substituted/cached inputs This led to severe slowdowns in some scenarios where a large input (like Nixpkgs) had already been unpacked to the store but didn't exist in a users cache, as described in #11228 Using the same method as #12911, we can create a cache entry for the fingerprint of substituted/cached inputs and avoid this problem entirely
1 parent ab7feb3 commit 74305d5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
synopsis: "Substituted flake inputs are no longer re-copied to the store"
3+
prs: [14041]
4+
---
5+
6+
Since 2.25, Nix would fail to store a cache entry for substituted flake inputs,
7+
which in turn would cause them to be re-copied to the store on initial
8+
evaluation. Caching these inputs results in a near doubling of a performance in
9+
some cases — especially on I/O-bound machines and when using commands that
10+
fetch many inputs, like `nix flake archive/prefetch-inputs`

src/libfetchers/fetchers.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "nix/util/json-utils.hh"
66
#include "nix/fetchers/store-path-accessor.hh"
77
#include "nix/fetchers/fetch-settings.hh"
8+
#include "nix/fetchers/fetch-to-store.hh"
89

910
#include <nlohmann/json.hpp>
1011

@@ -336,6 +337,15 @@ std::pair<ref<SourceAccessor>, Input> Input::getAccessorUnchecked(ref<Store> sto
336337

337338
accessor->fingerprint = getFingerprint(store);
338339

340+
// Store a cache entry for the substituted tree so later fetches
341+
// can reuse the existing nar instead of copying the unpacked
342+
// input back into the store on every evaluation.
343+
if (accessor->fingerprint) {
344+
ContentAddressMethod method = ContentAddressMethod::Raw::NixArchive;
345+
auto cacheKey = makeFetchToStoreCacheKey(getName(), *accessor->fingerprint, method, "/");
346+
settings->getCache()->upsert(cacheKey, *store, {}, storePath);
347+
}
348+
339349
accessor->setPathDisplay("«" + to_string() + "»");
340350

341351
return {accessor, *this};

0 commit comments

Comments
 (0)