Add support for passing a Nix expression on the command line

Fixes #38.
This commit is contained in:
Zhaofeng Li 2022-08-17 00:38:37 -06:00
parent 9f64612215
commit 23f4dfdc24
2 changed files with 16 additions and 2 deletions

View file

@ -37,9 +37,10 @@ using namespace nlohmann;
#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; bool impure = false;
@ -97,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);
} }
}; };
@ -111,7 +117,12 @@ static MyArgs myArgs;
static Value *releaseExprTopLevelValue(EvalState &state, Bindings &autoArgs) { static Value *releaseExprTopLevelValue(EvalState &state, Bindings &autoArgs) {
Value vTop; Value vTop;
if (myArgs.fromArgs) {
Expr *e = state.parseExprFromString(myArgs.releaseExpr, absPath("."));
state.eval(e, vTop);
} else {
state.evalFile(lookupFileArg(state, myArgs.releaseExpr), vTop); state.evalFile(lookupFileArg(state, myArgs.releaseExpr), vTop);
}
auto vRoot = state.allocValue(); auto vRoot = state.allocValue();

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()])