Optionally ignore null-valued derivation attributes

This allows adding attributes like

  attr = if stdenv.system == "bla" then something else null;

without changing the resulting derivation on non-<bla> platforms.

We once considered adding a special "ignore" value for this purpose,
but using null seems more elegant.
This commit is contained in:
Eelco Dolstra 2012-11-27 15:01:32 +01:00
parent 8b8ee53bc7
commit 6c98e6a5de
3 changed files with 24 additions and 11 deletions

View file

@ -139,6 +139,7 @@ EvalState::EvalState()
, sSystem(symbols.create("system"))
, sOverrides(symbols.create("__overrides"))
, sOutputName(symbols.create("outputName"))
, sIgnoreNulls(symbols.create("__ignoreNulls"))
, baseEnv(allocEnv(128))
, baseEnvDispl(0)
, staticBaseEnv(false, 0)

View file

@ -93,7 +93,7 @@ public:
SymbolTable symbols;
const Symbol sWith, sOutPath, sDrvPath, sType, sMeta, sName,
sSystem, sOverrides, sOutputName;
sSystem, sOverrides, sOutputName, sIgnoreNulls;
/* If set, force copying files to the Nix store even if they
already exist there. */

View file

@ -317,6 +317,12 @@ static void prim_derivationStrict(EvalState & state, Value * * args, Value & v)
throw;
}
/* Check whether null attributes should be ignored. */
bool ignoreNulls = false;
attr = args[0]->attrs->find(state.sIgnoreNulls);
if (attr != args[0]->attrs->end())
ignoreNulls = state.forceBool(*attr->value);
/* Build the derivation expression by processing the attributes. */
Derivation drv;
@ -329,11 +335,17 @@ static void prim_derivationStrict(EvalState & state, Value * * args, Value & v)
outputs.insert("out");
foreach (Bindings::iterator, i, *args[0]->attrs) {
if (i->name == state.sIgnoreNulls) continue;
string key = i->name;
startNest(nest, lvlVomit, format("processing attribute `%1%'") % key);
try {
if (ignoreNulls) {
state.forceValue(*i->value);
if (i->value->type == tNull) continue;
}
/* The `args' attribute is special: it supplies the
command-line arguments to the builder. */
if (key == "args") {