1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 02:21:16 +02:00

create cache entry for paths already in the nix store

This allows path:/nix/store/* paths to not be copied twice to the nix
store.

(cherry picked from commit 61c6210dbf)
This commit is contained in:
Jörg Thalheim 2025-04-03 13:27:39 +02:00
parent b47bd02a6a
commit 2a4e2ecd89
3 changed files with 30 additions and 6 deletions

View file

@ -4,6 +4,21 @@
namespace nix { 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( StorePath fetchToStore(
Store & store, Store & store,
const SourcePath & path, const SourcePath & path,
@ -19,12 +34,7 @@ StorePath fetchToStore(
std::optional<fetchers::Cache::Key> cacheKey; std::optional<fetchers::Cache::Key> cacheKey;
if (!filter && path.accessor->fingerprint) { if (!filter && path.accessor->fingerprint) {
cacheKey = fetchers::Cache::Key{"fetchToStore", { cacheKey = makeFetchToStoreCacheKey(std::string{name}, *path.accessor->fingerprint, method, path.path.abs());
{"name", std::string{name}},
{"fingerprint", *path.accessor->fingerprint},
{"method", std::string{method.render()}},
{"path", path.path.abs()}
}};
if (auto res = fetchers::getCache()->lookupStorePath(*cacheKey, store)) { if (auto res = fetchers::getCache()->lookupStorePath(*cacheKey, store)) {
debug("store path cache hit for '%s'", path); debug("store path cache hit for '%s'", path);
return res->storePath; return res->storePath;

View file

@ -5,6 +5,7 @@
#include "file-system.hh" #include "file-system.hh"
#include "repair-flag.hh" #include "repair-flag.hh"
#include "file-content-address.hh" #include "file-content-address.hh"
#include "cache.hh"
namespace nix { namespace nix {
@ -22,4 +23,7 @@ StorePath fetchToStore(
PathFilter * filter = nullptr, PathFilter * filter = nullptr,
RepairFlag repair = NoRepair); RepairFlag repair = NoRepair);
fetchers::Cache::Key makeFetchToStoreCacheKey(
const std::string & name, const std::string & fingerprint, ContentAddressMethod method, const std::string & path);
} }

View file

@ -2,6 +2,7 @@
#include "store-api.hh" #include "store-api.hh"
#include "archive.hh" #include "archive.hh"
#include "store-path-accessor.hh" #include "store-path-accessor.hh"
#include "fetch-to-store.hh"
namespace nix::fetchers { namespace nix::fetchers {
@ -157,6 +158,15 @@ struct PathInputScheme : InputScheme
}); });
storePath = store->addToStoreFromDump(*src, "source"); 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)); input.attrs.insert_or_assign("lastModified", uint64_t(mtime));
return {makeStorePathAccessor(store, *storePath), std::move(input)}; return {makeStorePathAccessor(store, *storePath), std::move(input)};