Skip to content

Commit 28b73ca

Browse files
committed
Make reading and writing derivations store methods
This allows for different representations.
1 parent 234f029 commit 28b73ca

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/libstore/derivations.cc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool BasicDerivation::isBuiltin() const
105105
return builder.substr(0, 8) == "builtin:";
106106
}
107107

108-
StorePath writeDerivation(Store & store, const Derivation & drv, RepairFlag repair, bool readOnly)
108+
static auto infoForDerivation(Store & store, const Derivation & drv)
109109
{
110110
auto references = drv.inputSrcs;
111111
for (auto & i : drv.inputDrvs.map)
@@ -117,13 +117,32 @@ StorePath writeDerivation(Store & store, const Derivation & drv, RepairFlag repa
117117
auto contents = drv.unparse(store, false);
118118
auto hash = hashString(HashAlgorithm::SHA256, contents);
119119
auto ca = TextInfo{.hash = hash, .references = references};
120-
auto path = store.makeFixedOutputPathFromCA(suffix, ca);
120+
return std::tuple{
121+
suffix,
122+
contents,
123+
references,
124+
store.makeFixedOutputPathFromCA(suffix, ca),
125+
};
126+
}
127+
128+
StorePath writeDerivation(Store & store, const Derivation & drv, RepairFlag repair, bool readOnly)
129+
{
130+
if (readOnly || settings.readOnlyMode) {
131+
auto [_x, _y, _z, path] = infoForDerivation(store, drv);
132+
return path;
133+
} else
134+
return store.writeDerivation(drv, repair);
135+
}
136+
137+
StorePath Store::writeDerivation(const Derivation & drv, RepairFlag repair)
138+
{
139+
auto [suffix, contents, references, path] = infoForDerivation(*this, drv);
121140

122-
if (readOnly || settings.readOnlyMode || (store.isValidPath(path) && !repair))
141+
if (isValidPath(path) && !repair)
123142
return path;
124143

125144
StringSource s{contents};
126-
auto path2 = store.addToStoreFromDump(
145+
auto path2 = addToStoreFromDump(
127146
s,
128147
suffix,
129148
FileSerialisationMethod::Flat,

src/libstore/include/nix/store/store-api.hh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,15 +778,20 @@ public:
778778
*/
779779
Derivation derivationFromPath(const StorePath & drvPath);
780780

781+
/**
782+
* Write a derivation to the Nix store, and return its path.
783+
*/
784+
virtual StorePath writeDerivation(const Derivation & drv, RepairFlag repair = NoRepair);
785+
781786
/**
782787
* Read a derivation (which must already be valid).
783788
*/
784-
Derivation readDerivation(const StorePath & drvPath);
789+
virtual Derivation readDerivation(const StorePath & drvPath);
785790

786791
/**
787792
* Read a derivation from a potentially invalid path.
788793
*/
789-
Derivation readInvalidDerivation(const StorePath & drvPath);
794+
virtual Derivation readInvalidDerivation(const StorePath & drvPath);
790795

791796
/**
792797
* @param [out] out Place in here the set of all store paths in the

src/libstore/store-api.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ std::optional<StorePath> Store::getBuildDerivationPath(const StorePath & path)
11701170
// resolved derivation, so we need to get it first
11711171
auto resolvedDrv = drv.tryResolve(*this);
11721172
if (resolvedDrv)
1173-
return writeDerivation(*this, *resolvedDrv, NoRepair, true);
1173+
return ::nix::writeDerivation(*this, *resolvedDrv, NoRepair, true);
11741174
}
11751175

11761176
return path;

0 commit comments

Comments
 (0)