diff --git a/src/eval-args.cc b/src/eval-args.cc new file mode 100644 index 0000000..c0a8723 --- /dev/null +++ b/src/eval-args.cc @@ -0,0 +1,93 @@ +#include "eval-args.hh" + +MyArgs::MyArgs() : MixCommonArgs("nix-eval-jobs") { + addFlag({ + .longName = "help", + .description = "show usage information", + .handler = {[&]() { + printf("USAGE: nix-eval-jobs [options] expr\n\n"); + for (const auto &[name, flag] : longFlags) { + if (hiddenCategories.count(flag->category)) { + continue; + } + printf(" --%-20s %s\n", name.c_str(), + flag->description.c_str()); + } + ::exit(0); + }}, + }); + + addFlag({.longName = "impure", + .description = "allow impure expressions", + .handler = {&impure, true}}); + + addFlag({.longName = "force-recurse", + .description = "force recursion (don't respect recurseIntoAttrs)", + .handler = {&forceRecurse, true}}); + + addFlag({.longName = "gc-roots-dir", + .description = "garbage collector roots directory", + .labels = {"path"}, + .handler = {&gcRootsDir}}); + + addFlag( + {.longName = "workers", + .description = "number of evaluate workers", + .labels = {"workers"}, + .handler = {[=, this](std::string s) { nrWorkers = std::stoi(s); }}}); + + addFlag({.longName = "max-memory-size", + .description = "maximum evaluation memory size in megabyte " + "(4GiB per worker by default)", + .labels = {"size"}, + .handler = { + [=, this](std::string s) { maxMemorySize = std::stoi(s); }}}); + + addFlag({.longName = "flake", + .description = "build a flake", + .handler = {&flake, true}}); + + addFlag({.longName = "meta", + .description = "include derivation meta field in output", + .handler = {&meta, true}}); + + addFlag({.longName = "check-cache-status", + .description = + "Check if the derivations are present locally or in " + "any configured substituters (i.e. binary cache). The " + "information " + "will be exposed in the `isCached` field of the JSON output.", + .handler = {&checkCacheStatus, true}}); + + addFlag( + {.longName = "show-trace", + .description = "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}}); + + // usually in MixFlakeOptions + addFlag({ + .longName = "override-input", + .description = + "Override a specific flake input (e.g. `dwarffs/nixpkgs`).", + .category = category, + .labels = {"input-path", "flake-url"}, + .handler = {[&](std::string inputPath, std::string flakeRef) { + // overriden inputs are unlocked + lockFlags.allowUnlocked = true; + lockFlags.inputOverrides.insert_or_assign( + nix::flake::parseInputPath(inputPath), + nix::parseFlakeRef(flakeRef, nix::absPath("."), true)); + }}, + }); + + expectArg("expr", &releaseExpr); +} + +void MyArgs::parseArgs(char** argv, int argc) { + parseCmdline(nix::argvToStrings(argc, argv), 0); +} diff --git a/src/eval-args.hh b/src/eval-args.hh new file mode 100644 index 0000000..cd5a9aa --- /dev/null +++ b/src/eval-args.hh @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include +#include + +class MyArgs : virtual public nix::MixEvalArgs, + virtual public nix::MixCommonArgs, + virtual nix::RootArgs { + public: + std::string releaseExpr; + nix::Path gcRootsDir; + bool flake = false; + bool fromArgs = false; + bool meta = false; + bool showTrace = false; + bool impure = false; + bool forceRecurse = false; + bool checkCacheStatus = false; + size_t nrWorkers = 1; + size_t maxMemorySize = 4096; + + // usually in MixFlakeOptions + nix::flake::LockFlags lockFlags = {.updateLockFile = false, + .writeLockFile = false, + .useRegistries = false, + .allowUnlocked = false}; + MyArgs(); + + void parseArgs(char** argv, int argc); +}; diff --git a/src/meson.build b/src/meson.build index f8fb528..d2a145c 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,5 +1,6 @@ src = [ 'nix-eval-jobs.cc', + 'eval-args.cc' ] executable('nix-eval-jobs', src, diff --git a/src/nix-eval-jobs.cc b/src/nix-eval-jobs.cc index 27be870..271a16e 100644 --- a/src/nix-eval-jobs.cc +++ b/src/nix-eval-jobs.cc @@ -1,36 +1,29 @@ + #include -#include #include -#include -#include +#include #include -#include -#include -#include -#include -#include -#include -#include +#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + +#include +#include #include +#include #include +#include #include - +#include +#include #include - -#include +#include +#include #include #include +#include "eval-args.hh" + #include using namespace nix; @@ -42,122 +35,6 @@ using namespace nlohmann; #elif __clang__ #pragma clang diagnostic ignored "-Wnon-virtual-dtor" #endif -struct MyArgs : virtual MixEvalArgs, virtual MixCommonArgs, virtual RootArgs { - std::string releaseExpr; - Path gcRootsDir; - bool flake = false; - bool fromArgs = false; - bool meta = false; - bool showTrace = false; - bool impure = false; - bool forceRecurse = false; - bool checkCacheStatus = false; - size_t nrWorkers = 1; - size_t maxMemorySize = 4096; - - // usually in MixFlakeOptions - flake::LockFlags lockFlags = {.updateLockFile = false, - .writeLockFile = false, - .useRegistries = false, - .allowUnlocked = false}; - - MyArgs() : MixCommonArgs("nix-eval-jobs") { - addFlag({ - .longName = "help", - .description = "show usage information", - .handler = {[&]() { - printf("USAGE: nix-eval-jobs [options] expr\n\n"); - for (const auto &[name, flag] : longFlags) { - if (hiddenCategories.count(flag->category)) { - continue; - } - printf(" --%-20s %s\n", name.c_str(), - flag->description.c_str()); - } - ::exit(0); - }}, - }); - - addFlag({.longName = "impure", - .description = "allow impure expressions", - .handler = {&impure, true}}); - - addFlag( - {.longName = "force-recurse", - .description = "force recursion (don't respect recurseIntoAttrs)", - .handler = {&forceRecurse, true}}); - - addFlag({.longName = "gc-roots-dir", - .description = "garbage collector roots directory", - .labels = {"path"}, - .handler = {&gcRootsDir}}); - - addFlag({.longName = "workers", - .description = "number of evaluate workers", - .labels = {"workers"}, - .handler = { - [=, this](std::string s) { nrWorkers = std::stoi(s); }}}); - - addFlag({.longName = "max-memory-size", - .description = "maximum evaluation memory size in megabyte " - "(4GiB per worker by default)", - .labels = {"size"}, - .handler = {[=, this](std::string s) { - maxMemorySize = std::stoi(s); - }}}); - - addFlag({.longName = "flake", - .description = "build a flake", - .handler = {&flake, true}}); - - addFlag({.longName = "meta", - .description = "include derivation meta field in output", - .handler = {&meta, true}}); - - addFlag( - {.longName = "check-cache-status", - .description = - "Check if the derivations are present locally or in " - "any configured substituters (i.e. binary cache). The " - "information " - "will be exposed in the `isCached` field of the JSON output.", - .handler = {&checkCacheStatus, true}}); - - addFlag({.longName = "show-trace", - .description = - "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}}); - - // usually in MixFlakeOptions - addFlag({ - .longName = "override-input", - .description = - "Override a specific flake input (e.g. `dwarffs/nixpkgs`).", - .category = category, - .labels = {"input-path", "flake-url"}, - .handler = {[&](std::string inputPath, std::string flakeRef) { - // overriden inputs are unlocked - lockFlags.allowUnlocked = true; - lockFlags.inputOverrides.insert_or_assign( - flake::parseInputPath(inputPath), - parseFlakeRef(flakeRef, absPath("."), true)); - }}, - }); - - expectArg("expr", &releaseExpr); - } -}; -#ifdef __GNUC__ -#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" -#elif __clang__ -#pragma clang diagnostic ignored "-Wnon-virtual-dtor" -#endif - static MyArgs myArgs; static Value *releaseExprTopLevelValue(EvalState &state, Bindings &autoArgs) { @@ -679,7 +556,7 @@ int main(int argc, char **argv) { initNix(); initGC(); - myArgs.parseCmdline(argvToStrings(argc, argv), 0); + myArgs.parseArgs(argv, argc); /* FIXME: The build hook in conjunction with import-from-derivation is * causing "unexpected EOF" during eval */