Skip to content

Commit a6c5d56

Browse files
authored
Merge pull request #13177 from obsidiansystems/less-useDerivation
Remove `useDerivation`
2 parents fad975b + 01207fd commit a6c5d56

File tree

7 files changed

+46
-64
lines changed

7 files changed

+46
-64
lines changed

src/libstore/build/derivation-goal.cc

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ namespace nix {
2525

2626
DerivationGoal::DerivationGoal(const StorePath & drvPath,
2727
const OutputsSpec & wantedOutputs, Worker & worker, BuildMode buildMode)
28-
: Goal(worker)
29-
, useDerivation(true)
28+
: Goal(worker, loadDerivation())
3029
, drvPath(drvPath)
3130
, wantedOutputs(wantedOutputs)
3231
, buildMode(buildMode)
@@ -43,8 +42,7 @@ DerivationGoal::DerivationGoal(const StorePath & drvPath,
4342

4443
DerivationGoal::DerivationGoal(const StorePath & drvPath, const BasicDerivation & drv,
4544
const OutputsSpec & wantedOutputs, Worker & worker, BuildMode buildMode)
46-
: Goal(worker)
47-
, useDerivation(false)
45+
: Goal(worker, haveDerivation())
4846
, drvPath(drvPath)
4947
, wantedOutputs(wantedOutputs)
5048
, buildMode(buildMode)
@@ -143,10 +141,10 @@ void DerivationGoal::addWantedOutputs(const OutputsSpec & outputs)
143141
}
144142

145143

146-
Goal::Co DerivationGoal::init() {
147-
trace("init");
144+
Goal::Co DerivationGoal::loadDerivation() {
145+
trace("local derivation");
148146

149-
if (useDerivation) {
147+
{
150148
/* The first thing to do is to make sure that the derivation
151149
exists. If it doesn't, it may be created through a
152150
substitute. */
@@ -355,7 +353,7 @@ Goal::Co DerivationGoal::gaveUpOnSubstitution()
355353

356354
std::map<ref<const SingleDerivedPath>, GoalPtr, value_comparison> inputGoals;
357355

358-
if (useDerivation) {
356+
{
359357
std::function<void(ref<const SingleDerivedPath>, const DerivedPathMap<StringSet>::ChildNode &)> addWaiteeDerivedPath;
360358

361359
addWaiteeDerivedPath = [&](ref<const SingleDerivedPath> inputDrv, const DerivedPathMap<StringSet>::ChildNode & inputNode) {
@@ -375,7 +373,7 @@ Goal::Co DerivationGoal::gaveUpOnSubstitution()
375373
childNode);
376374
};
377375

378-
for (const auto & [inputDrvPath, inputNode] : dynamic_cast<Derivation *>(drv.get())->inputDrvs.map) {
376+
for (const auto & [inputDrvPath, inputNode] : drv->inputDrvs.map) {
379377
/* Ensure that pure, non-fixed-output derivations don't
380378
depend on impure derivations. */
381379
if (experimentalFeatureSettings.isEnabled(Xp::ImpureDerivations) && !drv->type().isImpure() && !drv->type().isFixed()) {
@@ -417,8 +415,6 @@ Goal::Co DerivationGoal::gaveUpOnSubstitution()
417415
trace("all inputs realised");
418416

419417
if (nrFailed != 0) {
420-
if (!useDerivation)
421-
throw Error("some dependencies of '%s' are missing", worker.store.printStorePath(drvPath));
422418
auto msg = fmt(
423419
"Cannot build '%s'.\n"
424420
"Reason: " ANSI_RED "%d %s failed" ANSI_NORMAL ".",
@@ -435,8 +431,8 @@ Goal::Co DerivationGoal::gaveUpOnSubstitution()
435431
/* Determine the full set of input paths. */
436432

437433
/* First, the input derivations. */
438-
if (useDerivation) {
439-
auto & fullDrv = *dynamic_cast<Derivation *>(drv.get());
434+
{
435+
auto & fullDrv = *drv;
440436

441437
auto drvType = fullDrv.type();
442438
bool resolveDrv = std::visit(overloaded {
@@ -918,7 +914,12 @@ Goal::Co DerivationGoal::repairClosure()
918914
derivation is responsible for which path in the output
919915
closure. */
920916
StorePathSet inputClosure;
921-
if (useDerivation) worker.store.computeFSClosure(drvPath, inputClosure);
917+
918+
/* If we're working from an in-memory derivation with no in-store
919+
`*.drv` file, we cannot do this part. */
920+
if (worker.store.isValidPath(drvPath))
921+
worker.store.computeFSClosure(drvPath, inputClosure);
922+
922923
std::map<StorePath, StorePath> outputsToDrv;
923924
for (auto & i : inputClosure)
924925
if (i.isDerivation()) {
@@ -1133,7 +1134,9 @@ HookReply DerivationGoal::tryBuildHook()
11331134
#ifdef _WIN32 // TODO enable build hook on Windows
11341135
return rpDecline;
11351136
#else
1136-
if (settings.buildHook.get().empty() || !worker.tryBuildHook || !useDerivation) return rpDecline;
1137+
/* This should use `worker.evalStore`, but per #13179 the build hook
1138+
doesn't work with eval store anyways. */
1139+
if (settings.buildHook.get().empty() || !worker.tryBuildHook || !worker.store.isValidPath(drvPath)) return rpDecline;
11371140

11381141
if (!worker.hook)
11391142
worker.hook = std::make_unique<HookInstance>();
@@ -1399,33 +1402,32 @@ void DerivationGoal::flushLine()
13991402
std::map<std::string, std::optional<StorePath>> DerivationGoal::queryPartialDerivationOutputMap()
14001403
{
14011404
assert(!drv->type().isImpure());
1402-
if (!useDerivation || drv->type().hasKnownOutputPaths()) {
1403-
std::map<std::string, std::optional<StorePath>> res;
1404-
for (auto & [name, output] : drv->outputs)
1405-
res.insert_or_assign(name, output.path(worker.store, drv->name, name));
1406-
return res;
1407-
} else {
1408-
for (auto * drvStore : { &worker.evalStore, &worker.store })
1409-
if (drvStore->isValidPath(drvPath))
1410-
return worker.store.queryPartialDerivationOutputMap(drvPath, drvStore);
1411-
assert(false);
1412-
}
1405+
1406+
for (auto * drvStore : { &worker.evalStore, &worker.store })
1407+
if (drvStore->isValidPath(drvPath))
1408+
return worker.store.queryPartialDerivationOutputMap(drvPath, drvStore);
1409+
1410+
/* In-memory derivation will naturally fall back on this case, where
1411+
we do best-effort with static information. */
1412+
std::map<std::string, std::optional<StorePath>> res;
1413+
for (auto & [name, output] : drv->outputs)
1414+
res.insert_or_assign(name, output.path(worker.store, drv->name, name));
1415+
return res;
14131416
}
14141417

14151418
OutputPathMap DerivationGoal::queryDerivationOutputMap()
14161419
{
14171420
assert(!drv->type().isImpure());
1418-
if (!useDerivation || drv->type().hasKnownOutputPaths()) {
1419-
OutputPathMap res;
1420-
for (auto & [name, output] : drv->outputsAndOptPaths(worker.store))
1421-
res.insert_or_assign(name, *output.second);
1422-
return res;
1423-
} else {
1424-
for (auto * drvStore : { &worker.evalStore, &worker.store })
1425-
if (drvStore->isValidPath(drvPath))
1426-
return worker.store.queryDerivationOutputMap(drvPath, drvStore);
1427-
assert(false);
1428-
}
1421+
1422+
for (auto * drvStore : { &worker.evalStore, &worker.store })
1423+
if (drvStore->isValidPath(drvPath))
1424+
return worker.store.queryDerivationOutputMap(drvPath, drvStore);
1425+
1426+
// See comment in `DerivationGoal::queryPartialDerivationOutputMap`.
1427+
OutputPathMap res;
1428+
for (auto & [name, output] : drv->outputsAndOptPaths(worker.store))
1429+
res.insert_or_assign(name, *output.second);
1430+
return res;
14291431
}
14301432

14311433

src/libstore/build/drv-output-substitution-goal.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ DrvOutputSubstitutionGoal::DrvOutputSubstitutionGoal(
1212
Worker & worker,
1313
RepairFlag repair,
1414
std::optional<ContentAddress> ca)
15-
: Goal(worker)
15+
: Goal(worker, init())
1616
, id(id)
1717
{
1818
name = fmt("substitution of '%s'", id.to_string());

src/libstore/build/substitution-goal.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace nix {
1010

1111
PathSubstitutionGoal::PathSubstitutionGoal(const StorePath & storePath, Worker & worker, RepairFlag repair, std::optional<ContentAddress> ca)
12-
: Goal(worker)
12+
: Goal(worker, init())
1313
, storePath(storePath)
1414
, repair(repair)
1515
, ca(ca)

src/libstore/include/nix/store/build/derivation-goal.hh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ void runPostBuildHook(
3333
*/
3434
struct DerivationGoal : public Goal
3535
{
36-
/**
37-
* Whether to use an on-disk .drv file.
38-
*/
39-
bool useDerivation;
40-
4136
/** The path of the derivation. */
4237
StorePath drvPath;
4338

@@ -166,7 +161,7 @@ struct DerivationGoal : public Goal
166161
/**
167162
* The states.
168163
*/
169-
Co init() override;
164+
Co loadDerivation();
170165
Co haveDerivation();
171166
Co gaveUpOnSubstitution();
172167
Co tryToBuild();

src/libstore/include/nix/store/build/drv-output-substitution-goal.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public:
3333
typedef void (DrvOutputSubstitutionGoal::*GoalState)();
3434
GoalState state;
3535

36-
Co init() override;
36+
Co init();
3737
Co realisationFetched(Goals waitees, std::shared_ptr<const Realisation> outputInfo, nix::ref<nix::Store> sub);
3838

3939
void timedOut(Error && ex) override { unreachable(); };

src/libstore/include/nix/store/build/goal.hh

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,6 @@ protected:
338338
*/
339339
std::optional<Co> top_co;
340340

341-
/**
342-
* The entry point for the goal
343-
*/
344-
virtual Co init() = 0;
345-
346-
/**
347-
* Wrapper around @ref init since virtual functions
348-
* can't be used in constructors.
349-
*/
350-
inline Co init_wrapper();
351-
352341
/**
353342
* Signals that the goal is done.
354343
* `co_return` the result. If you're not inside a coroutine, you can ignore
@@ -376,8 +365,8 @@ public:
376365
*/
377366
std::optional<Error> ex;
378367

379-
Goal(Worker & worker)
380-
: worker(worker), top_co(init_wrapper())
368+
Goal(Worker & worker, Co init)
369+
: worker(worker), top_co(std::move(init))
381370
{
382371
// top_co shouldn't have a goal already, should be nullptr.
383372
assert(!top_co->handle.promise().goal);
@@ -440,7 +429,3 @@ template<typename... ArgTypes>
440429
struct std::coroutine_traits<nix::Goal::Co, ArgTypes...> {
441430
using promise_type = nix::Goal::promise_type;
442431
};
443-
444-
nix::Goal::Co nix::Goal::init_wrapper() {
445-
co_return init();
446-
}

src/libstore/include/nix/store/build/substitution-goal.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public:
6464
/**
6565
* The states.
6666
*/
67-
Co init() override;
67+
Co init();
6868
Co gotInfo();
6969
Co tryToRun(StorePath subPath, nix::ref<Store> sub, std::shared_ptr<const ValidPathInfo> info, bool & substituterFailed);
7070
Co finished();

0 commit comments

Comments
 (0)