Use enum and predicates rather than bitfile for derivation type

This commit is contained in:
John Ericson 2020-06-03 17:38:54 +00:00
parent 6b7f4ec4ab
commit 2500403059
3 changed files with 54 additions and 25 deletions

View file

@ -1382,7 +1382,7 @@ void DerivationGoal::inputsRealised()
/* Don't repeat fixed-output derivations since they're already /* Don't repeat fixed-output derivations since they're already
verified by their output hash.*/ verified by their output hash.*/
nrRounds = DtAxisFixed & derivationType ? 1 : settings.buildRepeat + 1; nrRounds = derivationIsFixed(derivationType) ? 1 : settings.buildRepeat + 1;
/* Okay, try to build. Note that here we don't wait for a build /* Okay, try to build. Note that here we don't wait for a build
slot to become available, since we don't need one if there is a slot to become available, since we don't need one if there is a
@ -1760,7 +1760,7 @@ void DerivationGoal::buildDone()
st = st =
dynamic_cast<NotDeterministic*>(&e) ? BuildResult::NotDeterministic : dynamic_cast<NotDeterministic*>(&e) ? BuildResult::NotDeterministic :
statusOk(status) ? BuildResult::OutputRejected : statusOk(status) ? BuildResult::OutputRejected :
DtAxisImpure & derivationType || diskFull ? BuildResult::TransientFailure : derivationIsImpure(derivationType) || diskFull ? BuildResult::TransientFailure :
BuildResult::PermanentFailure; BuildResult::PermanentFailure;
} }
@ -1966,7 +1966,7 @@ void DerivationGoal::startBuilder()
else if (settings.sandboxMode == smDisabled) else if (settings.sandboxMode == smDisabled)
useChroot = false; useChroot = false;
else if (settings.sandboxMode == smRelaxed) else if (settings.sandboxMode == smRelaxed)
useChroot = !(DtAxisImpure & derivationType) && !noChroot; useChroot = !(derivationIsImpure(derivationType)) && !noChroot;
} }
if (worker.store.storeDir != worker.store.realStoreDir) { if (worker.store.storeDir != worker.store.realStoreDir) {
@ -2132,7 +2132,7 @@ void DerivationGoal::startBuilder()
"nogroup:x:65534:\n") % sandboxGid).str()); "nogroup:x:65534:\n") % sandboxGid).str());
/* Create /etc/hosts with localhost entry. */ /* Create /etc/hosts with localhost entry. */
if (!(DtAxisImpure & derivationType)) if (!(derivationIsImpure(derivationType)))
writeFile(chrootRootDir + "/etc/hosts", "127.0.0.1 localhost\n::1 localhost\n"); writeFile(chrootRootDir + "/etc/hosts", "127.0.0.1 localhost\n::1 localhost\n");
/* Make the closure of the inputs available in the chroot, /* Make the closure of the inputs available in the chroot,
@ -2341,7 +2341,7 @@ void DerivationGoal::startBuilder()
us. us.
*/ */
if (!(DtAxisImpure & derivationType)) if (!(derivationIsImpure(derivationType)))
privateNetwork = true; privateNetwork = true;
userNamespaceSync.create(); userNamespaceSync.create();
@ -2542,7 +2542,7 @@ void DerivationGoal::initEnv()
derivation, tell the builder, so that for instance `fetchurl' derivation, tell the builder, so that for instance `fetchurl'
can skip checking the output. On older Nixes, this environment can skip checking the output. On older Nixes, this environment
variable won't be set, so `fetchurl' will do the check. */ variable won't be set, so `fetchurl' will do the check. */
if (DtAxisFixed & derivationType) env["NIX_OUTPUT_CHECKED"] = "1"; if (derivationIsFixed(derivationType)) env["NIX_OUTPUT_CHECKED"] = "1";
/* *Only* if this is a fixed-output derivation, propagate the /* *Only* if this is a fixed-output derivation, propagate the
values of the environment variables specified in the values of the environment variables specified in the
@ -2553,7 +2553,7 @@ void DerivationGoal::initEnv()
to the builder is generally impure, but the output of to the builder is generally impure, but the output of
fixed-output derivations is by definition pure (since we fixed-output derivations is by definition pure (since we
already know the cryptographic hash of the output). */ already know the cryptographic hash of the output). */
if (derivationType & DtAxisImpure) { if (derivationIsImpure(derivationType)) {
for (auto & i : parsedDrv->getStringsAttr("impureEnvVars").value_or(Strings())) for (auto & i : parsedDrv->getStringsAttr("impureEnvVars").value_or(Strings()))
env[i] = getEnv(i).value_or(""); env[i] = getEnv(i).value_or("");
} }
@ -3167,7 +3167,7 @@ void DerivationGoal::runChild()
/* Fixed-output derivations typically need to access the /* Fixed-output derivations typically need to access the
network, so give them access to /etc/resolv.conf and so network, so give them access to /etc/resolv.conf and so
on. */ on. */
if (DtAxisImpure & derivationType) { if (derivationIsImpure(derivationType)) {
ss.push_back("/etc/resolv.conf"); ss.push_back("/etc/resolv.conf");
// Only use nss functions to resolve hosts and // Only use nss functions to resolve hosts and
@ -3408,7 +3408,7 @@ void DerivationGoal::runChild()
sandboxProfile += "(import \"sandbox-defaults.sb\")\n"; sandboxProfile += "(import \"sandbox-defaults.sb\")\n";
if (DtAxisImpure & derivationType) if (derivationIsImpure(derivationType))
sandboxProfile += "(import \"sandbox-network.sb\")\n"; sandboxProfile += "(import \"sandbox-network.sb\")\n";
/* Our rwx outputs */ /* Our rwx outputs */

View file

@ -37,6 +37,32 @@ void DerivationOutput::parseHashInfo(FileIngestionMethod & recursive, Hash & has
} }
bool derivationIsCA(DerivationType dt) {
switch (dt) {
case DerivationType::Regular: return false;
case DerivationType::CAFixed: return true;
};
// Since enums can have non-variant values, but making a `default:` would
// disable exhaustiveness warnings.
abort();
}
bool derivationIsFixed(DerivationType dt) {
switch (dt) {
case DerivationType::Regular: return false;
case DerivationType::CAFixed: return true;
};
abort();
}
bool derivationIsImpure(DerivationType dt) {
switch (dt) {
case DerivationType::Regular: return false;
case DerivationType::CAFixed: return true;
};
abort();
}
BasicDerivation::BasicDerivation(const BasicDerivation & other) BasicDerivation::BasicDerivation(const BasicDerivation & other)
: platform(other.platform) : platform(other.platform)
, builder(other.builder) , builder(other.builder)
@ -344,7 +370,7 @@ DerivationType BasicDerivation::type() const
outputs.begin()->first == "out" && outputs.begin()->first == "out" &&
outputs.begin()->second.hash != "") outputs.begin()->second.hash != "")
{ {
return DtCAFixed; return DerivationType::CAFixed;
} }
auto const algo = outputs.begin()->second.hashAlgo; auto const algo = outputs.begin()->second.hashAlgo;
@ -359,7 +385,7 @@ DerivationType BasicDerivation::type() const
throw Error("Invalid mix of CA and regular outputs"); throw Error("Invalid mix of CA and regular outputs");
} }
} }
return DtRegular; return DerivationType::Regular;
} }
@ -390,7 +416,7 @@ Hash hashDerivationModulo(Store & store, const Derivation & drv, bool maskOutput
{ {
/* Return a fixed hash for fixed-output derivations. */ /* Return a fixed hash for fixed-output derivations. */
switch (drv.type()) { switch (drv.type()) {
case DtCAFixed: { case DerivationType::CAFixed: {
DerivationOutputs::const_iterator i = drv.outputs.begin(); DerivationOutputs::const_iterator i = drv.outputs.begin();
return hashString(htSHA256, "fixed:out:" return hashString(htSHA256, "fixed:out:"
+ i->second.hashAlgo + ":" + i->second.hashAlgo + ":"

View file

@ -34,21 +34,24 @@ typedef std::map<StorePath, StringSet> DerivationInputs;
typedef std::map<string, string> StringPairs; typedef std::map<string, string> StringPairs;
// Bit: enum struct DerivationType : uint8_t {
// 7: regular vs ca Regular,
// 6: floating vs fixed hash if ca, regular always floating CAFixed,
// 5: pure vs impure if ca, regular always pure
// _: Unassigned
enum DerivationTypeAxis : uint8_t {
DtAxisCA = 0b10000000,
DtAxisFixed = 0b01000000,
DtAxisImpure = 0b00100000,
};
enum DerivationType : uint8_t {
DtRegular = 0b0000000,
DtCAFixed = 0b11100000,
}; };
/* Do the outputs of the derivation have paths calculated from their content,
or from the derivation itself? */
bool derivationIsCA(DerivationType);
/* Is the content of the outputs fixed a-priori via a hash? Never true for
non-CA derivations. */
bool derivationIsFixed(DerivationType);
/* Is the derivation impure and needs to access non-deterministic resources, or
pure and can be sandboxed? Note that whether or not we actually sandbox the
derivation is controlled separately. Never true for non-CA derivations. */
bool derivationIsImpure(DerivationType);
struct BasicDerivation struct BasicDerivation
{ {
DerivationOutputs outputs; /* keyed on symbolic IDs */ DerivationOutputs outputs; /* keyed on symbolic IDs */