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

View file

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