Skip to content

Commit

Permalink
Fixing NixOS#7479: DerivationOutput and DerivationType
Browse files Browse the repository at this point in the history
See that issue for details on the general goal.

The `DerivationGoal::derivationType` field had a bogus initialization,
now caught, so I made it `std::optional`. I think NixOS#8829 can make it
non-optional again because it will ensure we always have the derivation
when we construct a `DerivationGoal`.
  • Loading branch information
Ericson2314 committed Aug 16, 2023
1 parent 989d7fe commit a6b36ca
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 163 deletions.
4 changes: 2 additions & 2 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1373,15 +1373,15 @@ drvName, Bindings * attrs, Value & v)
drv.env[i] = state.store->printStorePath(outPath);
drv.outputs.insert_or_assign(
i,
DerivationOutputInputAddressed {
DerivationOutput::InputAddressed {
.path = std::move(outPath),
});
}
break;
;
case DrvHash::Kind::Deferred:
for (auto & i : outputs) {
drv.outputs.insert_or_assign(i, DerivationOutputDeferred {});
drv.outputs.insert_or_assign(i, DerivationOutput::Deferred {});
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/libstore/build/derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ void DerivationGoal::inputsRealised()
[&](const DerivationType::Impure &) {
return true;
}
}, drvType.raw());
}, drvType.raw);

if (resolveDrv && !fullDrv.inputDrvs.empty()) {
experimentalFeatureSettings.require(Xp::CaDerivations);
Expand Down Expand Up @@ -996,10 +996,11 @@ void DerivationGoal::buildDone()
}

else {
assert(derivationType);
st =
dynamic_cast<NotDeterministic*>(&e) ? BuildResult::NotDeterministic :
statusOk(status) ? BuildResult::OutputRejected :
!derivationType.isSandboxed() || diskFull ? BuildResult::TransientFailure :
!derivationType->isSandboxed() || diskFull ? BuildResult::TransientFailure :
BuildResult::PermanentFailure;
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstore/build/derivation-goal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ struct DerivationGoal : public Goal
/**
* The sort of derivation we are building.
*/
DerivationType derivationType;
std::optional<DerivationType> derivationType;

typedef void (DerivationGoal::*GoalState)();
GoalState state;
Expand Down
16 changes: 8 additions & 8 deletions src/libstore/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void LocalDerivationGoal::tryLocalBuild()
else if (settings.sandboxMode == smDisabled)
useChroot = false;
else if (settings.sandboxMode == smRelaxed)
useChroot = derivationType.isSandboxed() && !noChroot;
useChroot = derivationType->isSandboxed() && !noChroot;
}

auto & localStore = getLocalStore();
Expand Down Expand Up @@ -689,7 +689,7 @@ void LocalDerivationGoal::startBuilder()
"nogroup:x:65534:\n", sandboxGid()));

/* Create /etc/hosts with localhost entry. */
if (derivationType.isSandboxed())
if (derivationType->isSandboxed())
writeFile(chrootRootDir + "/etc/hosts", "127.0.0.1 localhost\n::1 localhost\n");

/* Make the closure of the inputs available in the chroot,
Expand Down Expand Up @@ -893,7 +893,7 @@ void LocalDerivationGoal::startBuilder()
us.
*/

if (derivationType.isSandboxed())
if (derivationType->isSandboxed())
privateNetwork = true;

userNamespaceSync.create();
Expand Down Expand Up @@ -1121,7 +1121,7 @@ void LocalDerivationGoal::initEnv()
derivation, tell the builder, so that for instance `fetchurl'
can skip checking the output. On older Nixes, this environment
variable won't be set, so `fetchurl' will do the check. */
if (derivationType.isFixed()) env["NIX_OUTPUT_CHECKED"] = "1";
if (derivationType->isFixed()) env["NIX_OUTPUT_CHECKED"] = "1";

/* *Only* if this is a fixed-output derivation, propagate the
values of the environment variables specified in the
Expand All @@ -1132,7 +1132,7 @@ void LocalDerivationGoal::initEnv()
to the builder is generally impure, but the output of
fixed-output derivations is by definition pure (since we
already know the cryptographic hash of the output). */
if (!derivationType.isSandboxed()) {
if (!derivationType->isSandboxed()) {
for (auto & i : parsedDrv->getStringsAttr("impureEnvVars").value_or(Strings()))
env[i] = getEnv(i).value_or("");
}
Expand Down Expand Up @@ -1797,7 +1797,7 @@ void LocalDerivationGoal::runChild()
/* Fixed-output derivations typically need to access the
network, so give them access to /etc/resolv.conf and so
on. */
if (!derivationType.isSandboxed()) {
if (!derivationType->isSandboxed()) {
// Only use nss functions to resolve hosts and
// services. Don’t use it for anything else that may
// be configured for this system. This limits the
Expand Down Expand Up @@ -2048,7 +2048,7 @@ void LocalDerivationGoal::runChild()
#include "sandbox-defaults.sb"
;

if (!derivationType.isSandboxed())
if (!derivationType->isSandboxed())
sandboxProfile +=
#include "sandbox-network.sb"
;
Expand Down Expand Up @@ -2599,7 +2599,7 @@ SingleDrvOutputs LocalDerivationGoal::registerOutputs()
});
},

}, output->raw());
}, output->raw);

/* FIXME: set proper permissions in restorePath() so
we don't have to do another traversal. */
Expand Down
36 changes: 18 additions & 18 deletions src/libstore/derivations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ std::optional<StorePath> DerivationOutput::path(const Store & store, std::string
[](const DerivationOutput::Impure &) -> std::optional<StorePath> {
return std::nullopt;
},
}, raw());
}, raw);
}


Expand Down Expand Up @@ -60,7 +60,7 @@ bool DerivationType::isCA() const
[](const Impure &) {
return true;
},
}, raw());
}, raw);
}

bool DerivationType::isFixed() const
Expand All @@ -75,7 +75,7 @@ bool DerivationType::isFixed() const
[](const Impure &) {
return false;
},
}, raw());
}, raw);
}

bool DerivationType::hasKnownOutputPaths() const
Expand All @@ -90,7 +90,7 @@ bool DerivationType::hasKnownOutputPaths() const
[](const Impure &) {
return false;
},
}, raw());
}, raw);
}


Expand All @@ -106,7 +106,7 @@ bool DerivationType::isSandboxed() const
[](const Impure &) {
return false;
},
}, raw());
}, raw);
}


Expand All @@ -122,7 +122,7 @@ bool DerivationType::isPure() const
[](const Impure &) {
return false;
},
}, raw());
}, raw);
}


Expand Down Expand Up @@ -408,13 +408,13 @@ std::string Derivation::unparse(const Store & store, bool maskOutputs,
s += ','; printUnquotedString(s, "");
s += ','; printUnquotedString(s, "");
},
[&](const DerivationOutputImpure & doi) {
[&](const DerivationOutput::Impure & doi) {
// FIXME
s += ','; printUnquotedString(s, "");
s += ','; printUnquotedString(s, doi.method.renderPrefix() + printHashType(doi.hashType));
s += ','; printUnquotedString(s, "impure");
}
}, i.second.raw());
}, i.second.raw);
s += ')';
}

Expand Down Expand Up @@ -509,7 +509,7 @@ DerivationType BasicDerivation::type() const
[&](const DerivationOutput::Impure &) {
impureOutputs.insert(i.first);
},
}, i.second.raw());
}, i.second.raw);
}

if (inputAddressedOutputs.empty()
Expand Down Expand Up @@ -626,7 +626,7 @@ DrvHash hashDerivationModulo(Store & store, const Derivation & drv, bool maskOut
if (type.isFixed()) {
std::map<std::string, Hash> outputHashes;
for (const auto & i : drv.outputs) {
auto & dof = std::get<DerivationOutput::CAFixed>(i.second.raw());
auto & dof = std::get<DerivationOutput::CAFixed>(i.second.raw);
auto hash = hashString(htSHA256, "fixed:out:"
+ dof.ca.printMethodAlgo() + ":"
+ dof.ca.hash.to_string(Base16, false) + ":"
Expand Down Expand Up @@ -663,7 +663,7 @@ DrvHash hashDerivationModulo(Store & store, const Derivation & drv, bool maskOut
[](const DerivationType::Impure &) -> DrvHash::Kind {
assert(false);
}
}, drv.type().raw());
}, drv.type().raw);

std::map<std::string, StringSet> inputs2;
for (auto & [drvPath, inputOutputs0] : drv.inputDrvs) {
Expand Down Expand Up @@ -720,10 +720,10 @@ StringSet BasicDerivation::outputNames() const
DerivationOutputsAndOptPaths BasicDerivation::outputsAndOptPaths(const Store & store) const
{
DerivationOutputsAndOptPaths outsAndOptPaths;
for (auto output : outputs)
for (auto & [outputName, output] : outputs)
outsAndOptPaths.insert(std::make_pair(
output.first,
std::make_pair(output.second, output.second.path(store, name, output.first))
outputName,
std::make_pair(output.raw, output.path(store, name, outputName))
)
);
return outsAndOptPaths;
Expand Down Expand Up @@ -798,7 +798,7 @@ void writeDerivation(Sink & out, const Store & store, const BasicDerivation & dr
<< (doi.method.renderPrefix() + printHashType(doi.hashType))
<< "impure";
},
}, i.second.raw());
}, i.second.raw);
}
WorkerProto::write(store,
WorkerProto::WriteConn { .to = out },
Expand Down Expand Up @@ -840,7 +840,7 @@ static void rewriteDerivation(Store & store, BasicDerivation & drv, const String

auto hashModulo = hashDerivationModulo(store, Derivation(drv), true);
for (auto & [outputName, output] : drv.outputs) {
if (std::holds_alternative<DerivationOutput::Deferred>(output.raw())) {
if (std::holds_alternative<DerivationOutput::Deferred>(output.raw)) {
auto h = get(hashModulo.hashes, outputName);
if (!h)
throw Error("derivation '%s' output '%s' has no hash (derivations.cc/rewriteDerivation)",
Expand Down Expand Up @@ -955,7 +955,7 @@ void Derivation::checkInvariants(Store & store, const StorePath & drvPath) const
[&](const DerivationOutput::Impure &) {
/* Nothing to check */
},
}, i.second.raw());
}, i.second.raw);
}
}

Expand Down Expand Up @@ -984,7 +984,7 @@ nlohmann::json DerivationOutput::toJSON(
res["hashAlgo"] = doi.method.renderPrefix() + printHashType(doi.hashType);
res["impure"] = true;
},
}, raw());
}, raw);
return res;
}

Expand Down
Loading

0 comments on commit a6b36ca

Please sign in to comment.