From 9f6461221530d0336d159bf5e8509c31b798ea68 Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Wed, 17 Aug 2022 00:38:37 -0600 Subject: [PATCH 1/2] Don't override evalSettings.pureEval unless necessary Other eval flags like `--pure-eval` may activate `evalSettings.pureEval`, so let's avoid touching it if we don't have to. --- src/nix-eval-jobs.cc | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/nix-eval-jobs.cc b/src/nix-eval-jobs.cc index eb97d0b..dac8532 100644 --- a/src/nix-eval-jobs.cc +++ b/src/nix-eval-jobs.cc @@ -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" @@ -44,9 +42,9 @@ struct MyArgs : MixEvalArgs, MixCommonArgs { bool flake = 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 +63,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", @@ -493,9 +489,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"); From 23f4dfdc24b06fa8d653c4f374b4a897641f0e8f Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Wed, 17 Aug 2022 00:38:37 -0600 Subject: [PATCH 2/2] Add support for passing a Nix expression on the command line Fixes #38. --- src/nix-eval-jobs.cc | 15 +++++++++++++-- tests/test_eval.py | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/nix-eval-jobs.cc b/src/nix-eval-jobs.cc index dac8532..5a335a0 100644 --- a/src/nix-eval-jobs.cc +++ b/src/nix-eval-jobs.cc @@ -37,9 +37,10 @@ using namespace nlohmann; #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; @@ -97,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); } }; @@ -111,7 +117,12 @@ static MyArgs myArgs; static Value *releaseExprTopLevelValue(EvalState &state, Bindings &autoArgs) { 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(); diff --git a/tests/test_eval.py b/tests/test_eval.py index 68dca79..8ab26b7 100644 --- a/tests/test_eval.py +++ b/tests/test_eval.py @@ -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()])