diff --git a/src/libfetchers/fetch-to-store.cc b/src/libfetchers/fetch-to-store.cc index 65aa72a6c..c5dc6f565 100644 --- a/src/libfetchers/fetch-to-store.cc +++ b/src/libfetchers/fetch-to-store.cc @@ -4,6 +4,21 @@ namespace nix { +fetchers::Cache::Key makeFetchToStoreCacheKey( + const std::string &name, + const std::string &fingerprint, + ContentAddressMethod method, + const std::string &path) +{ + return fetchers::Cache::Key{"fetchToStore", { + {"name", name}, + {"fingerprint", fingerprint}, + {"method", std::string{method.render()}}, + {"path", path} + }}; + +} + StorePath fetchToStore( Store & store, const SourcePath & path, @@ -19,12 +34,7 @@ StorePath fetchToStore( std::optional cacheKey; if (!filter && path.accessor->fingerprint) { - cacheKey = fetchers::Cache::Key{"fetchToStore", { - {"name", std::string{name}}, - {"fingerprint", *path.accessor->fingerprint}, - {"method", std::string{method.render()}}, - {"path", path.path.abs()} - }}; + cacheKey = makeFetchToStoreCacheKey(std::string{name}, *path.accessor->fingerprint, method, path.path.abs()); if (auto res = fetchers::getCache()->lookupStorePath(*cacheKey, store)) { debug("store path cache hit for '%s'", path); return res->storePath; diff --git a/src/libfetchers/fetch-to-store.hh b/src/libfetchers/fetch-to-store.hh index c762629f3..4bc44e334 100644 --- a/src/libfetchers/fetch-to-store.hh +++ b/src/libfetchers/fetch-to-store.hh @@ -5,6 +5,7 @@ #include "file-system.hh" #include "repair-flag.hh" #include "file-content-address.hh" +#include "cache.hh" namespace nix { @@ -22,4 +23,7 @@ StorePath fetchToStore( PathFilter * filter = nullptr, RepairFlag repair = NoRepair); +fetchers::Cache::Key makeFetchToStoreCacheKey( + const std::string & name, const std::string & fingerprint, ContentAddressMethod method, const std::string & path); + } diff --git a/src/libfetchers/path.cc b/src/libfetchers/path.cc index e255dd228..c540bddfb 100644 --- a/src/libfetchers/path.cc +++ b/src/libfetchers/path.cc @@ -2,6 +2,7 @@ #include "store-api.hh" #include "archive.hh" #include "store-path-accessor.hh" +#include "fetch-to-store.hh" namespace nix::fetchers { @@ -157,6 +158,15 @@ struct PathInputScheme : InputScheme }); storePath = store->addToStoreFromDump(*src, "source"); } + + // To avoid copying the path again to the /nix/store, we need to add a cache entry. + ContentAddressMethod method = ContentAddressMethod::Raw::NixArchive; + auto fp = getFingerprint(store, input); + if (fp) { + auto cacheKey = makeFetchToStoreCacheKey(input.getName(), *fp, method, "/"); + fetchers::getCache()->upsert(cacheKey, *store, {}, *storePath); + } + input.attrs.insert_or_assign("lastModified", uint64_t(mtime)); return {makeStorePathAccessor(store, *storePath), std::move(input)};