Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1814,8 +1814,13 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName
drv.fillInOutputPaths(*state.store);
}

/* Write the resulting term into the Nix store directory. */
auto drvPath = writeDerivation(*state.store, drv, state.repair);
/* Write the resulting term into the Nix store directory.

Unless we are in read-only mode, that is, in which case we do not
write anything. Users commonly do this to speed up evaluation in
contexts where they don't actually want to build anything. */
auto drvPath =
settings.readOnlyMode ? computeStorePath(*state.store, drv) : state.store->writeDerivation(drv, state.repair);
auto drvPathS = state.store->printStorePath(drvPath);

printMsg(lvlChatty, "instantiated '%1%' -> '%2%'", drvName, drvPathS);
Expand Down
7 changes: 6 additions & 1 deletion src/libstore-c/nix_api_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,12 @@ StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_deriv
if (context)
context->last_err_code = NIX_OK;
try {
auto ret = nix::writeDerivation(*store->ptr, derivation->drv, nix::NoRepair);
/* Quite dubious that users would want this to silently suceed
without actually writing the derivation if this setting is
set, but it was that way already, so we are doing this for
back-compat for now. */
auto ret = nix::settings.readOnlyMode ? nix::computeStorePath(*store->ptr, derivation->drv)
Comment on lines +311 to +315
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, ideally the behaviour of C API functions isn't affected in an observable way. The method by which we do stuff vs what the end result is a different matter.

@roberth, what do you think?

IMO it seems reasonable to fix this as a bugfix. I doubt any caller is using this hack to just compute a store path.

: store->ptr->writeDerivation(derivation->drv, nix::NoRepair);

return new StorePath{ret};
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstore-tests/derivation/invariants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class FillInOutputPathsTest : public LibStoreTest, public JsonCharacterizationTe
depDrv.fillInOutputPaths(*store);

// Write the dependency to the store
return writeDerivation(*store, depDrv, NoRepair);
return store->writeDerivation(depDrv, NoRepair);
}

public:
Expand Down
6 changes: 3 additions & 3 deletions src/libstore-tests/worker-substitution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ TEST_F(WorkerSubstitutionTest, floatingDerivationOutput)
};

// Write the derivation to the destination store
auto drvPath = writeDerivation(*dummyStore, drv);
auto drvPath = dummyStore->writeDerivation(drv);

// Snapshot the destination store before
checkpointJson("ca-drv/store-before", dummyStore);
Expand Down Expand Up @@ -297,7 +297,7 @@ TEST_F(WorkerSubstitutionTest, floatingDerivationOutputWithDepDrv)
};

// Write the dependency derivation to the destination store
auto depDrvPath = writeDerivation(*dummyStore, depDrv);
auto depDrvPath = dummyStore->writeDerivation(depDrv);

// Compute the hash modulo for the dependency derivation
auto depHashModulo = hashDerivationModulo(*dummyStore, depDrv, true);
Expand Down Expand Up @@ -348,7 +348,7 @@ TEST_F(WorkerSubstitutionTest, floatingDerivationOutputWithDepDrv)
rootDrv.inputDrvs = {.map = {{depDrvPath, {.value = {"out"}}}}};

// Write the root derivation to the destination store
auto rootDrvPath = writeDerivation(*dummyStore, rootDrv);
auto rootDrvPath = dummyStore->writeDerivation(rootDrv);

// Snapshot the destination store before
checkpointJson("issue-11928/store-before", dummyStore);
Expand Down
6 changes: 3 additions & 3 deletions src/libstore-tests/write-derivation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ TEST_F(WriteDerivationTest, addToStoreFromDumpCalledOnce)
{
auto drv = makeSimpleDrv();

auto path1 = writeDerivation(*store, drv, NoRepair);
auto path1 = store->writeDerivation(drv, NoRepair);
config->readOnly = true;
auto path2 = writeDerivation(*store, drv, NoRepair);
auto path2 = computeStorePath(*store, drv);
EXPECT_EQ(path1, path2);
EXPECT_THAT(
[&] { writeDerivation(*store, drv, Repair); },
[&] { store->writeDerivation(drv, Repair); },
::testing::ThrowsMessage<Error>(
testing::HasSubstrIgnoreANSIMatcher("operation 'writeDerivation' is not supported by store 'dummy://'")));
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/build/derivation-building-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Goal::Co DerivationBuildingGoal::gaveUpOnSubstitution(bool storeDerivation)
assert(drv->inputDrvs.map.empty());
/* Store the resolved derivation, as part of the record of
what we're actually building */
writeDerivation(worker.store, *drv);
worker.store.writeDerivation(*drv);
}

StorePathSet inputPaths;
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/build/derivation-resolution-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Goal::Co DerivationResolutionGoal::resolveDerivation()
}
assert(attempt);

auto pathResolved = writeDerivation(worker.store, *attempt, NoRepair, /*readOnly =*/true);
auto pathResolved = computeStorePath(worker.store, Derivation{*attempt});

auto msg =
fmt("resolved derivation: '%s' -> '%s'",
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ static void performOp(

Derivation drv2;
static_cast<BasicDerivation &>(drv2) = drv;
drvPath = writeDerivation(*store, Derivation{drv2});
drvPath = store->writeDerivation(Derivation{drv2});
}

auto res = store->buildDerivation(drvPath, drv, buildMode);
Expand Down
11 changes: 4 additions & 7 deletions src/libstore/derivations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ bool BasicDerivation::isBuiltin() const
return builder.substr(0, 8) == "builtin:";
}

static auto infoForDerivation(Store & store, const Derivation & drv)
static auto infoForDerivation(const StoreDirConfig & store, const Derivation & drv)
{
auto references = drv.inputSrcs;
for (auto & i : drv.inputDrvs.map)
Expand All @@ -126,13 +126,10 @@ static auto infoForDerivation(Store & store, const Derivation & drv)
};
}

StorePath writeDerivation(Store & store, const Derivation & drv, RepairFlag repair, bool readOnly)
StorePath computeStorePath(const StoreDirConfig & store, const Derivation & drv)
{
if (readOnly || settings.readOnlyMode) {
auto [_x, _y, _z, path] = infoForDerivation(store, drv);
return path;
} else
return store.writeDerivation(drv, repair);
auto [_suffix, _contents, _references, path] = infoForDerivation(store, drv);
return path;
}

StorePath Store::writeDerivation(const Derivation & drv, RepairFlag repair)
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/dummy-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ struct DummyStoreImpl : DummyStore

StorePath writeDerivation(const Derivation & drv, RepairFlag repair = NoRepair) override
{
auto drvPath = ::nix::writeDerivation(*this, drv, repair, /*readonly=*/true);
auto drvPath = nix::computeStorePath(*this, drv);

if (!derivations.contains(drvPath) || repair) {
if (config->readOnly)
Expand Down
6 changes: 4 additions & 2 deletions src/libstore/include/nix/store/derivations.hh
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,11 @@ struct Derivation : BasicDerivation
class Store;

/**
* Write a derivation to the Nix store, and return its path.
* Compute the store path that would be used for a derivation without writing it.
*
* This is a pure computation based on the derivation content and store directory.
*/
StorePath writeDerivation(Store & store, const Derivation & drv, RepairFlag repair = NoRepair, bool readOnly = false);
StorePath computeStorePath(const StoreDirConfig & store, const Derivation & drv);

/**
* Read a derivation from a file.
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ std::optional<StorePath> Store::getBuildDerivationPath(const StorePath & path)
// resolved derivation, so we need to get it first
auto resolvedDrv = drv.tryResolve(*this);
if (resolvedDrv)
return ::nix::writeDerivation(*this, *resolvedDrv, NoRepair, true);
return nix::computeStorePath(*this, Derivation{*resolvedDrv});
}

return path;
Expand Down
6 changes: 3 additions & 3 deletions src/nix/derivation-add.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "nix/store/store-api.hh"
#include "nix/util/archive.hh"
#include "nix/store/derivations.hh"
#include "nix/store/globals.hh"
#include <nlohmann/json.hpp>

using namespace nix;
Expand Down Expand Up @@ -35,9 +36,8 @@ struct CmdAddDerivation : MixDryRun, StoreCommand

auto drv = Derivation::parseJsonAndValidate(*store, json);

auto drvPath = writeDerivation(*store, drv, NoRepair, /* read only */ dryRun);

writeDerivation(*store, drv, NoRepair, dryRun);
auto drvPath =
(dryRun || settings.readOnlyMode) ? computeStorePath(*store, drv) : store->writeDerivation(drv, NoRepair);

logger->cout("%s", store->printStorePath(drvPath));
}
Expand Down
2 changes: 1 addition & 1 deletion src/nix/develop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ static StorePath getDerivationEnvironment(ref<Store> store, ref<Store> evalStore
}
drv.fillInOutputPaths(*evalStore);

auto shellDrvPath = writeDerivation(*evalStore, drv);
auto shellDrvPath = evalStore->writeDerivation(drv);

/* Build the derivation. */
store->buildPaths(
Expand Down
Loading