1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-08 11:36:03 +01:00

Make reading and writing derivations store methods

This allows for different representations.
This commit is contained in:
John Ericson 2025-10-13 15:04:56 -04:00
parent 234f029940
commit 28b73cabcc
3 changed files with 31 additions and 7 deletions

View file

@ -105,7 +105,7 @@ bool BasicDerivation::isBuiltin() const
return builder.substr(0, 8) == "builtin:"; return builder.substr(0, 8) == "builtin:";
} }
StorePath writeDerivation(Store & store, const Derivation & drv, RepairFlag repair, bool readOnly) static auto infoForDerivation(Store & store, const Derivation & drv)
{ {
auto references = drv.inputSrcs; auto references = drv.inputSrcs;
for (auto & i : drv.inputDrvs.map) for (auto & i : drv.inputDrvs.map)
@ -117,13 +117,32 @@ StorePath writeDerivation(Store & store, const Derivation & drv, RepairFlag repa
auto contents = drv.unparse(store, false); auto contents = drv.unparse(store, false);
auto hash = hashString(HashAlgorithm::SHA256, contents); auto hash = hashString(HashAlgorithm::SHA256, contents);
auto ca = TextInfo{.hash = hash, .references = references}; auto ca = TextInfo{.hash = hash, .references = references};
auto path = store.makeFixedOutputPathFromCA(suffix, ca); return std::tuple{
suffix,
contents,
references,
store.makeFixedOutputPathFromCA(suffix, ca),
};
}
if (readOnly || settings.readOnlyMode || (store.isValidPath(path) && !repair)) StorePath writeDerivation(Store & store, const Derivation & drv, RepairFlag repair, bool readOnly)
{
if (readOnly || settings.readOnlyMode) {
auto [_x, _y, _z, path] = infoForDerivation(store, drv);
return path;
} else
return store.writeDerivation(drv, repair);
}
StorePath Store::writeDerivation(const Derivation & drv, RepairFlag repair)
{
auto [suffix, contents, references, path] = infoForDerivation(*this, drv);
if (isValidPath(path) && !repair)
return path; return path;
StringSource s{contents}; StringSource s{contents};
auto path2 = store.addToStoreFromDump( auto path2 = addToStoreFromDump(
s, s,
suffix, suffix,
FileSerialisationMethod::Flat, FileSerialisationMethod::Flat,

View file

@ -778,15 +778,20 @@ public:
*/ */
Derivation derivationFromPath(const StorePath & drvPath); Derivation derivationFromPath(const StorePath & drvPath);
/**
* Write a derivation to the Nix store, and return its path.
*/
virtual StorePath writeDerivation(const Derivation & drv, RepairFlag repair = NoRepair);
/** /**
* Read a derivation (which must already be valid). * Read a derivation (which must already be valid).
*/ */
Derivation readDerivation(const StorePath & drvPath); virtual Derivation readDerivation(const StorePath & drvPath);
/** /**
* Read a derivation from a potentially invalid path. * Read a derivation from a potentially invalid path.
*/ */
Derivation readInvalidDerivation(const StorePath & drvPath); virtual Derivation readInvalidDerivation(const StorePath & drvPath);
/** /**
* @param [out] out Place in here the set of all store paths in the * @param [out] out Place in here the set of all store paths in the

View file

@ -1170,7 +1170,7 @@ std::optional<StorePath> Store::getBuildDerivationPath(const StorePath & path)
// resolved derivation, so we need to get it first // resolved derivation, so we need to get it first
auto resolvedDrv = drv.tryResolve(*this); auto resolvedDrv = drv.tryResolve(*this);
if (resolvedDrv) if (resolvedDrv)
return writeDerivation(*this, *resolvedDrv, NoRepair, true); return ::nix::writeDerivation(*this, *resolvedDrv, NoRepair, true);
} }
return path; return path;