2020-11-29 14:33:55 +00:00
|
|
|
#include <map>
|
|
|
|
#include <iostream>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <nix/config.h>
|
2020-11-29 20:32:04 +00:00
|
|
|
#include <nix/shared.hh>
|
|
|
|
#include <nix/store-api.hh>
|
|
|
|
#include <nix/eval.hh>
|
|
|
|
#include <nix/eval-inline.hh>
|
|
|
|
#include <nix/util.hh>
|
|
|
|
#include <nix/get-drvs.hh>
|
|
|
|
#include <nix/globals.hh>
|
|
|
|
#include <nix/common-eval-args.hh>
|
|
|
|
#include <nix/flake/flakeref.hh>
|
|
|
|
#include <nix/flake/flake.hh>
|
|
|
|
#include <nix/attr-path.hh>
|
|
|
|
#include <nix/derivations.hh>
|
|
|
|
#include <nix/local-fs-store.hh>
|
2022-01-07 07:20:41 +00:00
|
|
|
#include <nix/logging.hh>
|
|
|
|
#include <nix/error.hh>
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2021-08-23 23:29:04 +00:00
|
|
|
#include <nix/value-to-json.hh>
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2021-03-21 17:39:32 +00:00
|
|
|
typedef enum { evalAuto, evalImpure, evalPure } pureEval;
|
2020-11-29 14:33:55 +00:00
|
|
|
|
|
|
|
struct MyArgs : MixEvalArgs, MixCommonArgs
|
|
|
|
{
|
|
|
|
Path releaseExpr;
|
2021-03-21 17:37:15 +00:00
|
|
|
Path gcRootsDir;
|
2020-11-29 14:33:55 +00:00
|
|
|
bool flake = false;
|
2022-01-06 00:19:15 +00:00
|
|
|
bool meta = false;
|
2022-01-07 07:20:41 +00:00
|
|
|
bool showTrace = false;
|
2020-11-29 14:33:55 +00:00
|
|
|
size_t nrWorkers = 1;
|
|
|
|
size_t maxMemorySize = 4096;
|
2021-03-21 17:39:32 +00:00
|
|
|
pureEval evalMode = evalAuto;
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2021-08-24 18:13:04 +00:00
|
|
|
MyArgs() : MixCommonArgs("nix-eval-jobs")
|
2020-11-29 14:33:55 +00:00
|
|
|
{
|
|
|
|
addFlag({
|
|
|
|
.longName = "help",
|
|
|
|
.description = "show usage information",
|
|
|
|
.handler = {[&]() {
|
2021-08-24 18:13:04 +00:00
|
|
|
printf("USAGE: nix-eval-jobs [options] expr\n\n");
|
2021-03-14 22:16:57 +00:00
|
|
|
for (const auto & [name, flag] : longFlags) {
|
|
|
|
if (hiddenCategories.count(flag->category)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-14 22:29:49 +00:00
|
|
|
printf(" --%-20s %s\n", name.c_str(), flag->description.c_str());
|
2021-03-14 22:16:57 +00:00
|
|
|
}
|
2021-03-21 17:38:56 +00:00
|
|
|
::exit(0);
|
2021-03-14 22:16:57 +00:00
|
|
|
}},
|
2020-11-29 14:33:55 +00:00
|
|
|
});
|
|
|
|
|
2021-03-21 17:39:32 +00:00
|
|
|
addFlag({
|
|
|
|
.longName = "impure",
|
|
|
|
.description = "set evaluation mode",
|
|
|
|
.handler = {[&]() {
|
|
|
|
evalMode = evalImpure;
|
|
|
|
}},
|
|
|
|
});
|
2021-03-21 18:05:11 +00:00
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
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 = {[=](std::string s) {
|
|
|
|
nrWorkers = std::stoi(s);
|
|
|
|
}}
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "max-memory-size",
|
|
|
|
.description = "maximum evaluation memory size",
|
|
|
|
.labels = {"size"},
|
|
|
|
.handler = {[=](std::string s) {
|
|
|
|
maxMemorySize = std::stoi(s);
|
|
|
|
}}
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "flake",
|
|
|
|
.description = "build a flake",
|
|
|
|
.handler = {&flake, true}
|
|
|
|
});
|
|
|
|
|
2022-01-06 00:19:15 +00:00
|
|
|
addFlag({
|
|
|
|
.longName = "meta",
|
|
|
|
.description = "include derivation meta field in output",
|
|
|
|
.handler = {&meta, true}
|
|
|
|
});
|
|
|
|
|
2022-01-07 07:20:41 +00:00
|
|
|
addFlag({
|
|
|
|
.longName = "show-trace",
|
|
|
|
.description = "print out a stack trace in case of evaluation errors",
|
|
|
|
.handler = {&showTrace, true}
|
|
|
|
});
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
expectArg("expr", &releaseExpr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static MyArgs myArgs;
|
|
|
|
|
|
|
|
static void worker(
|
|
|
|
EvalState & state,
|
|
|
|
Bindings & autoArgs,
|
|
|
|
AutoCloseFD & to,
|
2021-03-21 17:37:15 +00:00
|
|
|
AutoCloseFD & from,
|
|
|
|
const Path &gcRootsDir)
|
2020-11-29 14:33:55 +00:00
|
|
|
{
|
|
|
|
Value vTop;
|
|
|
|
|
|
|
|
if (myArgs.flake) {
|
|
|
|
using namespace flake;
|
|
|
|
|
2021-08-26 23:48:11 +00:00
|
|
|
auto [flakeRef, fragment] = parseFlakeRefWithFragment(myArgs.releaseExpr, absPath("."));
|
2020-11-29 14:33:55 +00:00
|
|
|
|
|
|
|
auto vFlake = state.allocValue();
|
|
|
|
|
|
|
|
auto lockedFlake = lockFlake(state, flakeRef,
|
|
|
|
LockFlags {
|
|
|
|
.updateLockFile = false,
|
|
|
|
.useRegistries = false,
|
|
|
|
.allowMutable = false,
|
|
|
|
});
|
|
|
|
|
|
|
|
callFlake(state, lockedFlake, *vFlake);
|
|
|
|
|
|
|
|
auto vOutputs = vFlake->attrs->get(state.symbols.create("outputs"))->value;
|
|
|
|
state.forceValue(*vOutputs);
|
2021-08-26 23:48:11 +00:00
|
|
|
vTop = *vOutputs;
|
|
|
|
|
|
|
|
if (fragment.length() > 0) {
|
|
|
|
Bindings & bindings(*state.allocBindings(0));
|
|
|
|
auto [nTop, pos] = findAlongAttrPath(state, fragment, bindings, vTop);
|
|
|
|
if (!nTop)
|
|
|
|
throw Error("error: attribute '%s' missing", nTop);
|
|
|
|
vTop = *nTop;
|
|
|
|
}
|
2020-11-29 14:33:55 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
state.evalFile(lookupFileArg(state, myArgs.releaseExpr), vTop);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto vRoot = state.allocValue();
|
|
|
|
state.autoCallFunction(autoArgs, vTop, *vRoot);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
/* Wait for the master to send us a job name. */
|
|
|
|
writeLine(to.get(), "next");
|
|
|
|
|
|
|
|
auto s = readLine(from.get());
|
|
|
|
if (s == "exit") break;
|
|
|
|
if (!hasPrefix(s, "do ")) abort();
|
|
|
|
std::string attrPath(s, 3);
|
|
|
|
|
|
|
|
debug("worker process %d at '%s'", getpid(), attrPath);
|
|
|
|
|
|
|
|
/* Evaluate it and send info back to the master. */
|
|
|
|
nlohmann::json reply;
|
2021-08-25 17:43:35 +00:00
|
|
|
reply["attr"] = attrPath;
|
2020-11-29 14:33:55 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
auto vTmp = findAlongAttrPath(state, attrPath, autoArgs, *vRoot).first;
|
|
|
|
|
|
|
|
auto v = state.allocValue();
|
|
|
|
state.autoCallFunction(autoArgs, *vTmp, *v);
|
|
|
|
|
|
|
|
if (auto drv = getDerivation(state, *v, false)) {
|
|
|
|
|
|
|
|
if (drv->querySystem() == "unknown")
|
|
|
|
throw EvalError("derivation must have a 'system' attribute");
|
|
|
|
|
|
|
|
auto drvPath = drv->queryDrvPath();
|
2021-12-31 11:00:04 +00:00
|
|
|
auto localStore = state.store.dynamic_pointer_cast<LocalFSStore>();
|
|
|
|
auto storePath = localStore->parseStorePath(drvPath);
|
2022-01-04 08:43:06 +00:00
|
|
|
auto outputs = drv->queryOutputs(false);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2021-08-26 21:56:23 +00:00
|
|
|
reply["name"] = drv->queryName();
|
|
|
|
reply["system"] = drv->querySystem();
|
|
|
|
reply["drvPath"] = drvPath;
|
2022-01-04 08:43:06 +00:00
|
|
|
for (auto out : outputs){
|
|
|
|
reply["outputs"][out.first] = out.second;
|
|
|
|
}
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-01-06 00:19:15 +00:00
|
|
|
if (myArgs.meta) {
|
|
|
|
nlohmann::json meta;
|
|
|
|
for (auto & name : drv->queryMetaNames()) {
|
|
|
|
PathSet context;
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
auto metaValue = drv->queryMeta(name);
|
|
|
|
// Skip non-serialisable types
|
|
|
|
// TODO: Fix serialisation of derivations to store paths
|
|
|
|
if (metaValue == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
printValueAsJSON(state, true, *metaValue, noPos, ss, context);
|
|
|
|
nlohmann::json field = nlohmann::json::parse(ss.str());
|
|
|
|
meta[name] = field;
|
|
|
|
}
|
|
|
|
reply["meta"] = meta;
|
2021-08-23 23:29:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
/* Register the derivation as a GC root. !!! This
|
|
|
|
registers roots for jobs that we may have already
|
|
|
|
done. */
|
2021-12-31 11:00:04 +00:00
|
|
|
if (gcRootsDir != "") {
|
2020-11-29 14:33:55 +00:00
|
|
|
Path root = gcRootsDir + "/" + std::string(baseNameOf(drvPath));
|
|
|
|
if (!pathExists(root))
|
2021-03-21 18:05:11 +00:00
|
|
|
localStore->addPermRoot(storePath, root);
|
|
|
|
}
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 17:43:35 +00:00
|
|
|
else if (v->type() == nAttrs)
|
|
|
|
{
|
2020-11-29 14:33:55 +00:00
|
|
|
auto attrs = nlohmann::json::array();
|
|
|
|
StringSet ss;
|
|
|
|
for (auto & i : v->attrs->lexicographicOrder()) {
|
|
|
|
std::string name(i->name);
|
|
|
|
if (name.find('.') != std::string::npos || name.find(' ') != std::string::npos) {
|
|
|
|
printError("skipping job with illegal name '%s'", name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
attrs.push_back(name);
|
|
|
|
}
|
|
|
|
reply["attrs"] = std::move(attrs);
|
|
|
|
}
|
|
|
|
|
2021-03-14 22:16:57 +00:00
|
|
|
else if (v->type() == nNull)
|
2020-11-29 14:33:55 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
else throw TypeError("attribute '%s' is %s, which is not supported", attrPath, showType(*v));
|
|
|
|
|
|
|
|
} catch (EvalError & e) {
|
2022-01-07 07:20:41 +00:00
|
|
|
auto err = e.info();
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
showErrorInfo(oss, err, loggerSettings.showTrace.get());
|
|
|
|
auto msg = oss.str();
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
// Transmits the error we got from the previous evaluation
|
|
|
|
// in the JSON output.
|
2021-03-14 22:16:57 +00:00
|
|
|
reply["error"] = filterANSIEscapes(msg, true);
|
2020-11-29 14:33:55 +00:00
|
|
|
// Don't forget to print it into the STDERR log, this is
|
|
|
|
// what's shown in the Hydra UI.
|
2022-01-07 07:20:41 +00:00
|
|
|
printError(e.msg());
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeLine(to.get(), reply.dump());
|
|
|
|
|
|
|
|
/* If our RSS exceeds the maximum, exit. The master will
|
|
|
|
start a new process. */
|
|
|
|
struct rusage r;
|
|
|
|
getrusage(RUSAGE_SELF, &r);
|
|
|
|
if ((size_t) r.ru_maxrss > myArgs.maxMemorySize * 1024) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
writeLine(to.get(), "restart");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char * * argv)
|
|
|
|
{
|
|
|
|
/* Prevent undeclared dependencies in the evaluation via
|
|
|
|
$NIX_PATH. */
|
|
|
|
unsetenv("NIX_PATH");
|
|
|
|
|
|
|
|
return handleExceptions(argv[0], [&]() {
|
|
|
|
initNix();
|
|
|
|
initGC();
|
|
|
|
|
|
|
|
myArgs.parseCmdline(argvToStrings(argc, argv));
|
|
|
|
|
|
|
|
/* FIXME: The build hook in conjunction with import-from-derivation is causing "unexpected EOF" during eval */
|
|
|
|
settings.builders = "";
|
|
|
|
|
|
|
|
/* Prevent access to paths outside of the Nix search path and
|
|
|
|
to the environment. */
|
2021-03-14 22:32:36 +00:00
|
|
|
evalSettings.restrictEval = false;
|
2020-11-29 14:33:55 +00:00
|
|
|
|
|
|
|
/* When building a flake, use pure evaluation (no access to
|
|
|
|
'getEnv', 'currentSystem' etc. */
|
2021-03-21 17:39:32 +00:00
|
|
|
evalSettings.pureEval = myArgs.evalMode == evalAuto ? myArgs.flake : myArgs.evalMode == evalPure;
|
2020-11-29 14:33:55 +00:00
|
|
|
|
|
|
|
if (myArgs.releaseExpr == "") throw UsageError("no expression specified");
|
|
|
|
|
2021-03-21 17:37:15 +00:00
|
|
|
if (myArgs.gcRootsDir == "") printMsg(lvlError, "warning: `--gc-roots-dir' not specified");
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-01-07 07:20:41 +00:00
|
|
|
if (myArgs.showTrace) {
|
|
|
|
loggerSettings.showTrace.assign(true);
|
|
|
|
}
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
struct State
|
|
|
|
{
|
|
|
|
std::set<std::string> todo{""};
|
|
|
|
std::set<std::string> active;
|
|
|
|
std::exception_ptr exc;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::condition_variable wakeup;
|
|
|
|
|
|
|
|
Sync<State> state_;
|
|
|
|
|
|
|
|
/* Start a handler thread per worker process. */
|
|
|
|
auto handler = [&]()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
pid_t pid = -1;
|
|
|
|
AutoCloseFD from, to;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
/* Start a new worker process if necessary. */
|
|
|
|
if (pid == -1) {
|
|
|
|
Pipe toPipe, fromPipe;
|
|
|
|
toPipe.create();
|
|
|
|
fromPipe.create();
|
|
|
|
pid = startProcess(
|
|
|
|
[&,
|
|
|
|
to{std::make_shared<AutoCloseFD>(std::move(fromPipe.writeSide))},
|
|
|
|
from{std::make_shared<AutoCloseFD>(std::move(toPipe.readSide))}
|
|
|
|
]()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
EvalState state(myArgs.searchPath, openStore());
|
|
|
|
Bindings & autoArgs = *myArgs.getAutoArgs(state);
|
2021-03-21 17:37:15 +00:00
|
|
|
worker(state, autoArgs, *to, *from, myArgs.gcRootsDir);
|
2021-03-14 22:16:57 +00:00
|
|
|
} catch (Error & e) {
|
2020-11-29 14:33:55 +00:00
|
|
|
nlohmann::json err;
|
2021-03-14 22:16:57 +00:00
|
|
|
auto msg = e.msg();
|
|
|
|
err["error"] = filterANSIEscapes(msg, true);
|
|
|
|
printError(msg);
|
2020-11-29 14:33:55 +00:00
|
|
|
writeLine(to->get(), err.dump());
|
|
|
|
// Don't forget to print it into the STDERR log, this is
|
|
|
|
// what's shown in the Hydra UI.
|
2021-03-14 22:16:57 +00:00
|
|
|
writeLine(to->get(), "restart");
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
ProcessOptions { .allowVfork = false });
|
|
|
|
from = std::move(fromPipe.readSide);
|
|
|
|
to = std::move(toPipe.writeSide);
|
|
|
|
debug("created worker process %d", pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether the existing worker process is still there. */
|
|
|
|
auto s = readLine(from.get());
|
|
|
|
if (s == "restart") {
|
|
|
|
pid = -1;
|
|
|
|
continue;
|
|
|
|
} else if (s != "next") {
|
|
|
|
auto json = nlohmann::json::parse(s);
|
|
|
|
throw Error("worker error: %s", (std::string) json["error"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for a job name to become available. */
|
|
|
|
std::string attrPath;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
checkInterrupt();
|
|
|
|
auto state(state_.lock());
|
|
|
|
if ((state->todo.empty() && state->active.empty()) || state->exc) {
|
|
|
|
writeLine(to.get(), "exit");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!state->todo.empty()) {
|
|
|
|
attrPath = *state->todo.begin();
|
|
|
|
state->todo.erase(state->todo.begin());
|
|
|
|
state->active.insert(attrPath);
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
state.wait(wakeup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tell the worker to evaluate it. */
|
|
|
|
writeLine(to.get(), "do " + attrPath);
|
|
|
|
|
|
|
|
/* Wait for the response. */
|
2021-08-25 17:43:35 +00:00
|
|
|
auto respString = readLine(from.get());
|
|
|
|
auto response = nlohmann::json::parse(respString);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
|
|
|
/* Handle the response. */
|
|
|
|
StringSet newAttrs;
|
|
|
|
if (response.find("attrs") != response.end()) {
|
|
|
|
for (auto & i : response["attrs"]) {
|
|
|
|
auto s = (attrPath.empty() ? "" : attrPath + ".") + (std::string) i;
|
|
|
|
newAttrs.insert(s);
|
|
|
|
}
|
2021-08-25 17:43:35 +00:00
|
|
|
} else {
|
2020-11-29 14:33:55 +00:00
|
|
|
auto state(state_.lock());
|
2022-02-16 08:46:57 +00:00
|
|
|
std::cout << respString << "\n" << std::flush;
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add newly discovered job names to the queue. */
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->active.erase(attrPath);
|
|
|
|
for (auto & s : newAttrs)
|
|
|
|
state->todo.insert(s);
|
|
|
|
wakeup.notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->exc = std::current_exception();
|
|
|
|
wakeup.notify_all();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
for (size_t i = 0; i < myArgs.nrWorkers; i++)
|
|
|
|
threads.emplace_back(std::thread(handler));
|
|
|
|
|
|
|
|
for (auto & thread : threads)
|
|
|
|
thread.join();
|
|
|
|
|
|
|
|
auto state(state_.lock());
|
|
|
|
|
|
|
|
if (state->exc)
|
|
|
|
std::rethrow_exception(state->exc);
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|