Skip to content

Commit 97c966c

Browse files
authored
Merge pull request #13765 from obsidiansystems/simplify-derivation-building-goal
Simplify `DerivationBuildingGoal`
2 parents 316fef3 + 9ccbe23 commit 97c966c

File tree

7 files changed

+30
-52
lines changed

7 files changed

+30
-52
lines changed

src/libstore/build/derivation-building-goal.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ Goal::Co DerivationBuildingGoal::gaveUpOnSubstitution()
157157
we care about all outputs. */
158158
auto outputHashes = staticOutputHashes(worker.evalStore, *drv);
159159
for (auto & [outputName, outputHash] : outputHashes) {
160-
InitialOutput v{
161-
.wanted = true, // Will be refined later
162-
.outputHash = outputHash};
160+
InitialOutput v{.outputHash = outputHash};
163161

164162
/* TODO we might want to also allow randomizing the paths
165163
for regular CA derivations, e.g. for sake of checking
@@ -675,10 +673,13 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
675673
}
676674
};
677675

676+
auto * localStoreP = dynamic_cast<LocalStore *>(&worker.store);
677+
assert(localStoreP);
678+
678679
/* If we have to wait and retry (see below), then `builder` will
679680
already be created, so we don't need to create it again. */
680681
builder = makeDerivationBuilder(
681-
worker.store,
682+
*localStoreP,
682683
std::make_unique<DerivationBuildingGoalCallbacks>(*this, builder),
683684
DerivationBuilderParams{
684685
drvPath,
@@ -1202,7 +1203,6 @@ std::pair<bool, SingleDrvOutputs> DerivationBuildingGoal::checkPathValidity()
12021203
// this is an invalid output, gets caught with (!wantedOutputsLeft.empty())
12031204
continue;
12041205
auto & info = *initialOutput;
1205-
info.wanted = true;
12061206
if (i.second) {
12071207
auto outputPath = *i.second;
12081208
info.known = {
@@ -1237,8 +1237,6 @@ std::pair<bool, SingleDrvOutputs> DerivationBuildingGoal::checkPathValidity()
12371237

12381238
bool allValid = true;
12391239
for (auto & [_, status] : initialOutputs) {
1240-
if (!status.wanted)
1241-
continue;
12421240
if (!status.known || !status.known->isValid()) {
12431241
allValid = false;
12441242
break;

src/libstore/include/nix/store/build/derivation-building-misc.hh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ struct InitialOutputStatus
4545

4646
struct InitialOutput
4747
{
48-
bool wanted;
4948
Hash outputHash;
5049
std::optional<InitialOutputStatus> known;
5150
};

src/libstore/unix/build/chroot-derivation-builder.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace nix {
55
struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
66
{
77
ChrootDerivationBuilder(
8-
Store & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params)
8+
LocalStore & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params)
99
: DerivationBuilderImpl{store, std::move(miscMethods), std::move(params)}
1010
{
1111
}
@@ -178,7 +178,7 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
178178
continue;
179179
if (buildMode != bmCheck && status.known->isValid())
180180
continue;
181-
auto p = store.toRealPath(status.known->path);
181+
auto p = store.Store::toRealPath(status.known->path);
182182
if (pathExists(chrootRootDir + p))
183183
std::filesystem::rename((chrootRootDir + p), p);
184184
}

src/libstore/unix/build/darwin-derivation-builder.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct DarwinDerivationBuilder : DerivationBuilderImpl
2121
bool useSandbox;
2222

2323
DarwinDerivationBuilder(
24-
Store & store,
24+
LocalStore & store,
2525
std::unique_ptr<DerivationBuilderCallbacks> miscMethods,
2626
DerivationBuilderParams params,
2727
bool useSandbox)

src/libstore/unix/build/derivation-builder.cc

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ class DerivationBuilderImpl : public DerivationBuilder, public DerivationBuilder
6161
{
6262
protected:
6363

64-
Store & store;
64+
LocalStore & store;
6565

6666
std::unique_ptr<DerivationBuilderCallbacks> miscMethods;
6767

6868
public:
6969

7070
DerivationBuilderImpl(
71-
Store & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params)
71+
LocalStore & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params)
7272
: DerivationBuilderParams{std::move(params)}
7373
, store{store}
7474
, miscMethods{std::move(miscMethods)}
@@ -424,13 +424,6 @@ void handleDiffHook(
424424

425425
const Path DerivationBuilderImpl::homeDir = "/homeless-shelter";
426426

427-
static LocalStore & getLocalStore(Store & store)
428-
{
429-
auto p = dynamic_cast<LocalStore *>(&store);
430-
assert(p);
431-
return *p;
432-
}
433-
434427
void DerivationBuilderImpl::killSandbox(bool getStats)
435428
{
436429
if (buildUser) {
@@ -631,10 +624,9 @@ bool DerivationBuilderImpl::decideWhetherDiskFull()
631624
so, we don't mark this build as a permanent failure. */
632625
#if HAVE_STATVFS
633626
{
634-
auto & localStore = getLocalStore(store);
635627
uint64_t required = 8ULL * 1024 * 1024; // FIXME: make configurable
636628
struct statvfs st;
637-
if (statvfs(localStore.config->realStoreDir.get().c_str(), &st) == 0
629+
if (statvfs(store.config->realStoreDir.get().c_str(), &st) == 0
638630
&& (uint64_t) st.f_bavail * st.f_bsize < required)
639631
diskFull = true;
640632
if (statvfs(tmpDir.c_str(), &st) == 0 && (uint64_t) st.f_bavail * st.f_bsize < required)
@@ -712,7 +704,7 @@ void DerivationBuilderImpl::startBuilder()
712704
Magenta(drv.platform),
713705
concatStringsSep(", ", drvOptions.getRequiredSystemFeatures(drv)),
714706
Magenta(settings.thisSystem),
715-
concatStringsSep<StringSet>(", ", store.config.systemFeatures));
707+
concatStringsSep<StringSet>(", ", store.Store::config.systemFeatures));
716708

717709
// since aarch64-darwin has Rosetta 2, this user can actually run x86_64-darwin on their hardware - we should
718710
// tell them to run the command to install Darwin 2
@@ -724,7 +716,7 @@ void DerivationBuilderImpl::startBuilder()
724716
throw BuildError(msg);
725717
}
726718

727-
auto buildDir = getLocalStore(store).config->getBuildDir();
719+
auto buildDir = store.config->getBuildDir();
728720

729721
createDirs(buildDir);
730722

@@ -1173,7 +1165,7 @@ void DerivationBuilderImpl::startDaemon()
11731165

11741166
auto store = makeRestrictedStore(
11751167
[&] {
1176-
auto config = make_ref<LocalStore::Config>(*getLocalStore(this->store).config);
1168+
auto config = make_ref<LocalStore::Config>(*this->store.config);
11771169
config->pathInfoCacheSize = 0;
11781170
config->stateDir = "/no-such-path";
11791171
config->logDir = "/no-such-path";
@@ -1430,8 +1422,6 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs()
14301422
outputs to allow hard links between outputs. */
14311423
InodesSeen inodesSeen;
14321424

1433-
Path checkSuffix = ".check";
1434-
14351425
std::exception_ptr delayedException;
14361426

14371427
/* The paths that can be referenced are the input closures, the
@@ -1466,23 +1456,19 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs()
14661456
std::map<std::string, struct stat> outputStats;
14671457
for (auto & [outputName, _] : drv.outputs) {
14681458
auto scratchOutput = get(scratchOutputs, outputName);
1469-
if (!scratchOutput)
1470-
throw BuildError(
1471-
"builder for '%s' has no scratch output for '%s'", store.printStorePath(drvPath), outputName);
1459+
assert(scratchOutput);
14721460
auto actualPath = realPathInSandbox(store.printStorePath(*scratchOutput));
14731461

14741462
outputsToSort.insert(outputName);
14751463

14761464
/* Updated wanted info to remove the outputs we definitely don't need to register */
14771465
auto initialOutput = get(initialOutputs, outputName);
1478-
if (!initialOutput)
1479-
throw BuildError(
1480-
"builder for '%s' has no initial output for '%s'", store.printStorePath(drvPath), outputName);
1466+
assert(initialOutput);
14811467
auto & initialInfo = *initialOutput;
14821468

14831469
/* Don't register if already valid, and not checking */
1484-
initialInfo.wanted = buildMode == bmCheck || !(initialInfo.known && initialInfo.known->isValid());
1485-
if (!initialInfo.wanted) {
1470+
bool wanted = buildMode == bmCheck || !(initialInfo.known && initialInfo.known->isValid());
1471+
if (!wanted) {
14861472
outputReferencesIfUnregistered.insert_or_assign(
14871473
outputName, AlreadyRegistered{.path = initialInfo.known->path});
14881474
continue;
@@ -1839,8 +1825,6 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs()
18391825
}
18401826
}
18411827

1842-
auto & localStore = getLocalStore(store);
1843-
18441828
if (buildMode == bmCheck) {
18451829

18461830
if (!store.isValidPath(newInfo.path))
@@ -1849,7 +1833,7 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs()
18491833
if (newInfo.narHash != oldInfo.narHash) {
18501834
miscMethods->noteCheckMismatch();
18511835
if (settings.runDiffHook || settings.keepFailed) {
1852-
auto dst = store.toRealPath(finalDestPath + checkSuffix);
1836+
auto dst = store.toRealPath(finalDestPath + ".check");
18531837
deletePath(dst);
18541838
movePath(actualPath, dst);
18551839

@@ -1876,8 +1860,8 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs()
18761860
/* Since we verified the build, it's now ultimately trusted. */
18771861
if (!oldInfo.ultimate) {
18781862
oldInfo.ultimate = true;
1879-
localStore.signPathInfo(oldInfo);
1880-
localStore.registerValidPaths({{oldInfo.path, oldInfo}});
1863+
store.signPathInfo(oldInfo);
1864+
store.registerValidPaths({{oldInfo.path, oldInfo}});
18811865
}
18821866

18831867
continue;
@@ -1891,20 +1875,20 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs()
18911875
debug("unreferenced input: '%1%'", store.printStorePath(i));
18921876
}
18931877

1894-
localStore.optimisePath(actualPath, NoRepair); // FIXME: combine with scanForReferences()
1878+
store.optimisePath(actualPath, NoRepair); // FIXME: combine with scanForReferences()
18951879
miscMethods->markContentsGood(newInfo.path);
18961880

18971881
newInfo.deriver = drvPath;
18981882
newInfo.ultimate = true;
1899-
localStore.signPathInfo(newInfo);
1883+
store.signPathInfo(newInfo);
19001884

19011885
finish(newInfo.path);
19021886

19031887
/* If it's a CA path, register it right away. This is necessary if it
19041888
isn't statically known so that we can safely unlock the path before
19051889
the next iteration */
19061890
if (newInfo.ca)
1907-
localStore.registerValidPaths({{newInfo.path, newInfo}});
1891+
store.registerValidPaths({{newInfo.path, newInfo}});
19081892

19091893
infos.emplace(outputName, std::move(newInfo));
19101894
}
@@ -1925,13 +1909,11 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs()
19251909
paths referenced by each of them. If there are cycles in the
19261910
outputs, this will fail. */
19271911
{
1928-
auto & localStore = getLocalStore(store);
1929-
19301912
ValidPathInfos infos2;
19311913
for (auto & [outputName, newInfo] : infos) {
19321914
infos2.insert_or_assign(newInfo.path, newInfo);
19331915
}
1934-
localStore.registerValidPaths(infos2);
1916+
store.registerValidPaths(infos2);
19351917
}
19361918

19371919
/* In case of a fixed-output derivation hash mismatch, throw an
@@ -2164,7 +2146,7 @@ StorePath DerivationBuilderImpl::makeFallbackPath(const StorePath & path)
21642146
namespace nix {
21652147

21662148
std::unique_ptr<DerivationBuilder> makeDerivationBuilder(
2167-
Store & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params)
2149+
LocalStore & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params)
21682150
{
21692151
bool useSandbox = false;
21702152

@@ -2191,8 +2173,7 @@ std::unique_ptr<DerivationBuilder> makeDerivationBuilder(
21912173
useSandbox = params.drv.type().isSandboxed() && !params.drvOptions.noChroot;
21922174
}
21932175

2194-
auto & localStore = getLocalStore(store);
2195-
if (localStore.storeDir != localStore.config->realStoreDir.get()) {
2176+
if (store.storeDir != store.config->realStoreDir.get()) {
21962177
#ifdef __linux__
21972178
useSandbox = true;
21982179
#else

src/libstore/unix/build/linux-derivation-builder.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ struct ChrootLinuxDerivationBuilder : ChrootDerivationBuilder, LinuxDerivationBu
191191
std::optional<Path> cgroup;
192192

193193
ChrootLinuxDerivationBuilder(
194-
Store & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params)
194+
LocalStore & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params)
195195
: DerivationBuilderImpl{store, std::move(miscMethods), std::move(params)}
196196
, ChrootDerivationBuilder{store, std::move(miscMethods), std::move(params)}
197197
, LinuxDerivationBuilder{store, std::move(miscMethods), std::move(params)}
@@ -492,7 +492,7 @@ struct ChrootLinuxDerivationBuilder : ChrootDerivationBuilder, LinuxDerivationBu
492492
createDirs(chrootRootDir + "/dev/shm");
493493
createDirs(chrootRootDir + "/dev/pts");
494494
ss.push_back("/dev/full");
495-
if (store.config.systemFeatures.get().count("kvm") && pathExists("/dev/kvm"))
495+
if (store.Store::config.systemFeatures.get().count("kvm") && pathExists("/dev/kvm"))
496496
ss.push_back("/dev/kvm");
497497
ss.push_back("/dev/null");
498498
ss.push_back("/dev/random");

src/libstore/unix/include/nix/store/build/derivation-builder.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,6 @@ struct DerivationBuilder : RestrictionContext
179179
};
180180

181181
std::unique_ptr<DerivationBuilder> makeDerivationBuilder(
182-
Store & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params);
182+
LocalStore & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params);
183183

184184
} // namespace nix

0 commit comments

Comments
 (0)