Merge pull request #103 from zhaofengli/cli-expr

Add support for passing a Nix expression on the command line
This commit is contained in:
adisbladis 2022-08-20 18:50:34 +08:00 committed by GitHub
commit d05952ea83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 13 deletions

View file

@ -30,8 +30,6 @@
using namespace nix;
using namespace nlohmann;
typedef enum { evalAuto, evalImpure, evalPure } pureEval;
// Safe to ignore - the args will be static.
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
@ -39,14 +37,15 @@ typedef enum { evalAuto, evalImpure, evalPure } pureEval;
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
#endif
struct MyArgs : MixEvalArgs, MixCommonArgs {
Path releaseExpr;
std::string releaseExpr;
Path gcRootsDir;
bool flake = false;
bool fromArgs = false;
bool meta = false;
bool showTrace = false;
bool impure = false;
size_t nrWorkers = 1;
size_t maxMemorySize = 4096;
pureEval evalMode = evalAuto;
MyArgs() : MixCommonArgs("nix-eval-jobs") {
addFlag({
@ -65,11 +64,9 @@ struct MyArgs : MixEvalArgs, MixCommonArgs {
}},
});
addFlag({
.longName = "impure",
.description = "set evaluation mode",
.handler = {[&]() { evalMode = evalImpure; }},
});
addFlag({.longName = "impure",
.description = "allow impure expressions",
.handler = {&impure, true}});
addFlag({.longName = "gc-roots-dir",
.description = "garbage collector roots directory",
@ -101,6 +98,11 @@ struct MyArgs : MixEvalArgs, MixCommonArgs {
"print out a stack trace in case of evaluation errors",
.handler = {&showTrace, true}});
addFlag({.longName = "expr",
.shortName = 'E',
.description = "treat the argument as a Nix expression",
.handler = {&fromArgs, true}});
expectArg("expr", &releaseExpr);
}
};
@ -115,7 +117,12 @@ static MyArgs myArgs;
static Value *releaseExprTopLevelValue(EvalState &state, Bindings &autoArgs) {
Value vTop;
if (myArgs.fromArgs) {
Expr *e = state.parseExprFromString(myArgs.releaseExpr, absPath("."));
state.eval(e, vTop);
} else {
state.evalFile(lookupFileArg(state, myArgs.releaseExpr), vTop);
}
auto vRoot = state.allocValue();
@ -493,9 +500,11 @@ int main(int argc, char **argv) {
/* When building a flake, use pure evaluation (no access to
'getEnv', 'currentSystem' etc. */
evalSettings.pureEval = myArgs.evalMode == evalAuto
? myArgs.flake
: myArgs.evalMode == evalPure;
if (myArgs.impure) {
evalSettings.pureEval = false;
} else if (myArgs.flake) {
evalSettings.pureEval = true;
}
if (myArgs.releaseExpr == "")
throw UsageError("no expression specified");

View file

@ -55,3 +55,6 @@ def test_flake() -> None:
def test_expression() -> None:
common_test(["ci.nix"])
with open(TEST_ROOT.joinpath("assets/ci.nix"), "r") as ci_nix:
common_test(["-E", ci_nix.read()])