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;
|
2022-04-26 05:11:17 +00:00
|
|
|
using namespace nlohmann;
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-21 17:02:05 +00:00
|
|
|
// Safe to ignore - the args will be static.
|
2022-04-25 15:53:17 +00:00
|
|
|
#ifdef __GNUC__
|
2022-04-21 17:02:05 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
2022-04-25 15:53:17 +00:00
|
|
|
#elif __clang__
|
2022-04-21 17:02:05 +00:00
|
|
|
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
|
2022-04-25 15:53:17 +00:00
|
|
|
#endif
|
2022-04-26 09:15:56 +00:00
|
|
|
struct MyArgs : MixEvalArgs, MixCommonArgs {
|
2022-08-17 06:38:37 +00:00
|
|
|
std::string releaseExpr;
|
2021-03-21 17:37:15 +00:00
|
|
|
Path gcRootsDir;
|
2020-11-29 14:33:55 +00:00
|
|
|
bool flake = false;
|
2022-08-17 06:38:37 +00:00
|
|
|
bool fromArgs = false;
|
2022-01-06 00:19:15 +00:00
|
|
|
bool meta = false;
|
2022-01-07 07:20:41 +00:00
|
|
|
bool showTrace = false;
|
2022-08-17 06:38:37 +00:00
|
|
|
bool impure = false;
|
2020-11-29 14:33:55 +00:00
|
|
|
size_t nrWorkers = 1;
|
|
|
|
size_t maxMemorySize = 4096;
|
|
|
|
|
2022-04-26 09:15:56 +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");
|
2022-04-26 09:15:56 +00:00
|
|
|
for (const auto &[name, flag] : longFlags) {
|
2021-03-14 22:16:57 +00:00
|
|
|
if (hiddenCategories.count(flag->category)) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-26 09:15:56 +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
|
|
|
});
|
|
|
|
|
2022-08-17 06:38:37 +00:00
|
|
|
addFlag({.longName = "impure",
|
|
|
|
.description = "allow impure expressions",
|
|
|
|
.handler = {&impure, true}});
|
2021-03-21 18:05:11 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
addFlag({.longName = "gc-roots-dir",
|
|
|
|
.description = "garbage collector roots directory",
|
|
|
|
.labels = {"path"},
|
|
|
|
.handler = {&gcRootsDir}});
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
addFlag(
|
|
|
|
{.longName = "workers",
|
|
|
|
.description = "number of evaluate workers",
|
|
|
|
.labels = {"workers"},
|
|
|
|
.handler = {[=](std::string s) { nrWorkers = std::stoi(s); }}});
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
addFlag({.longName = "max-memory-size",
|
|
|
|
.description = "maximum evaluation memory size",
|
|
|
|
.labels = {"size"},
|
|
|
|
.handler = {
|
|
|
|
[=](std::string s) { maxMemorySize = std::stoi(s); }}});
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
addFlag({.longName = "flake",
|
|
|
|
.description = "build a flake",
|
|
|
|
.handler = {&flake, true}});
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
addFlag({.longName = "meta",
|
|
|
|
.description = "include derivation meta field in output",
|
|
|
|
.handler = {&meta, true}});
|
2022-01-06 00:19:15 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
addFlag({.longName = "show-trace",
|
|
|
|
.description =
|
|
|
|
"print out a stack trace in case of evaluation errors",
|
|
|
|
.handler = {&showTrace, true}});
|
2022-01-07 07:20:41 +00:00
|
|
|
|
2022-08-17 06:38:37 +00:00
|
|
|
addFlag({.longName = "expr",
|
|
|
|
.shortName = 'E',
|
|
|
|
.description = "treat the argument as a Nix expression",
|
|
|
|
.handler = {&fromArgs, true}});
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
expectArg("expr", &releaseExpr);
|
|
|
|
}
|
|
|
|
};
|
2022-04-25 15:53:17 +00:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
|
|
|
#elif __clang__
|
|
|
|
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
|
|
|
|
#endif
|
2020-11-29 14:33:55 +00:00
|
|
|
|
|
|
|
static MyArgs myArgs;
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
static Value *releaseExprTopLevelValue(EvalState &state, Bindings &autoArgs) {
|
2020-11-29 14:33:55 +00:00
|
|
|
Value vTop;
|
|
|
|
|
2022-08-17 06:38:37 +00:00
|
|
|
if (myArgs.fromArgs) {
|
|
|
|
Expr *e = state.parseExprFromString(myArgs.releaseExpr, absPath("."));
|
|
|
|
state.eval(e, vTop);
|
|
|
|
} else {
|
|
|
|
state.evalFile(lookupFileArg(state, myArgs.releaseExpr), vTop);
|
|
|
|
}
|
2022-04-21 18:34:49 +00:00
|
|
|
|
|
|
|
auto vRoot = state.allocValue();
|
|
|
|
|
|
|
|
state.autoCallFunction(autoArgs, vTop, *vRoot);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-21 18:34:49 +00:00
|
|
|
return vRoot;
|
|
|
|
}
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
static Value *flakeTopLevelValue(EvalState &state, Bindings &autoArgs) {
|
2022-04-21 16:15:20 +00:00
|
|
|
using namespace flake;
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
auto [flakeRef, fragment] =
|
|
|
|
parseFlakeRefWithFragment(myArgs.releaseExpr, absPath("."));
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-21 16:15:20 +00:00
|
|
|
auto vFlake = state.allocValue();
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-21 16:15:20 +00:00
|
|
|
auto lockedFlake = lockFlake(state, flakeRef,
|
2022-04-26 09:15:56 +00:00
|
|
|
LockFlags{
|
|
|
|
.updateLockFile = false,
|
|
|
|
.useRegistries = false,
|
|
|
|
.allowMutable = false,
|
|
|
|
});
|
2021-08-26 23:48:11 +00:00
|
|
|
|
2022-04-21 16:15:20 +00:00
|
|
|
callFlake(state, lockedFlake, *vFlake);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-21 16:15:20 +00:00
|
|
|
auto vOutputs = vFlake->attrs->get(state.symbols.create("outputs"))->value;
|
|
|
|
state.forceValue(*vOutputs, noPos);
|
|
|
|
auto vTop = *vOutputs;
|
|
|
|
|
|
|
|
if (fragment.length() > 0) {
|
2022-04-26 09:15:56 +00:00
|
|
|
Bindings &bindings(*state.allocBindings(0));
|
2022-04-21 16:15:20 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
auto vRoot = state.allocValue();
|
|
|
|
state.autoCallFunction(autoArgs, vTop, *vRoot);
|
|
|
|
|
2022-04-21 16:15:20 +00:00
|
|
|
return vRoot;
|
|
|
|
}
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
Value *topLevelValue(EvalState &state, Bindings &autoArgs) {
|
|
|
|
return myArgs.flake ? flakeTopLevelValue(state, autoArgs)
|
|
|
|
: releaseExprTopLevelValue(state, autoArgs);
|
2022-04-21 18:41:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 22:41:51 +00:00
|
|
|
/* The fields of a derivation that are printed in json form */
|
|
|
|
struct Drv {
|
|
|
|
std::string name;
|
|
|
|
std::string system;
|
|
|
|
std::string drvPath;
|
|
|
|
std::map<std::string, std::string> outputs;
|
|
|
|
std::optional<nlohmann::json> meta;
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
Drv(EvalState &state, DrvInfo &drvInfo) {
|
2022-04-13 22:41:51 +00:00
|
|
|
if (drvInfo.querySystem() == "unknown")
|
|
|
|
throw EvalError("derivation must have a 'system' attribute");
|
|
|
|
|
|
|
|
auto localStore = state.store.dynamic_pointer_cast<LocalFSStore>();
|
|
|
|
|
|
|
|
for (auto out : drvInfo.queryOutputs(true)) {
|
|
|
|
if (out.second)
|
|
|
|
outputs[out.first] = localStore->printStorePath(*out.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (myArgs.meta) {
|
|
|
|
nlohmann::json meta_;
|
2022-04-26 09:15:56 +00:00
|
|
|
for (auto &name : drvInfo.queryMetaNames()) {
|
2022-04-13 22:41:51 +00:00
|
|
|
PathSet context;
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
auto metaValue = drvInfo.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);
|
|
|
|
|
|
|
|
meta_[name] = nlohmann::json::parse(ss.str());
|
|
|
|
}
|
|
|
|
meta = meta_;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = drvInfo.queryName();
|
|
|
|
system = drvInfo.querySystem();
|
|
|
|
drvPath = localStore->printStorePath(drvInfo.requireDrvPath());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
static void to_json(nlohmann::json &json, const Drv &drv) {
|
2022-04-13 22:41:51 +00:00
|
|
|
json = nlohmann::json{
|
2022-04-26 09:15:56 +00:00
|
|
|
{"name", drv.name},
|
|
|
|
{"system", drv.system},
|
|
|
|
{"drvPath", drv.drvPath},
|
|
|
|
{"outputs", drv.outputs},
|
2022-04-13 22:41:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (drv.meta.has_value())
|
|
|
|
json["meta"] = drv.meta.value();
|
|
|
|
}
|
|
|
|
|
2022-04-26 05:11:17 +00:00
|
|
|
std::string attrPathJoin(json input) {
|
|
|
|
return std::accumulate(input.begin(), input.end(), std::string(),
|
|
|
|
[](std::string ss, std::string s) {
|
|
|
|
// Escape token if containing dots
|
|
|
|
if (s.find(".") != std::string::npos) {
|
|
|
|
s = "\"" + s + "\"";
|
|
|
|
}
|
|
|
|
return ss.empty() ? s : ss + "." + s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
static void worker(EvalState &state, Bindings &autoArgs, AutoCloseFD &to,
|
|
|
|
AutoCloseFD &from) {
|
2022-04-21 18:41:31 +00:00
|
|
|
auto vRoot = topLevelValue(state, autoArgs);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
|
|
|
while (true) {
|
2022-04-21 04:01:02 +00:00
|
|
|
/* Wait for the collector to send us a job name. */
|
2020-11-29 14:33:55 +00:00
|
|
|
writeLine(to.get(), "next");
|
|
|
|
|
|
|
|
auto s = readLine(from.get());
|
2022-04-26 09:15:56 +00:00
|
|
|
if (s == "exit")
|
|
|
|
break;
|
|
|
|
if (!hasPrefix(s, "do "))
|
|
|
|
abort();
|
2022-04-26 05:11:17 +00:00
|
|
|
auto path = json::parse(s.substr(3));
|
|
|
|
auto attrPathS = attrPathJoin(path);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 05:11:17 +00:00
|
|
|
debug("worker process %d at '%s'", getpid(), path);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-21 04:01:02 +00:00
|
|
|
/* Evaluate it and send info back to the collector. */
|
2022-04-26 09:15:56 +00:00
|
|
|
json reply = json{{"attr", attrPathS}, {"attrPath", path}};
|
2020-11-29 14:33:55 +00:00
|
|
|
try {
|
2022-04-26 09:15:56 +00:00
|
|
|
auto vTmp =
|
|
|
|
findAlongAttrPath(state, attrPathS, autoArgs, *vRoot).first;
|
2020-11-29 14:33:55 +00:00
|
|
|
|
|
|
|
auto v = state.allocValue();
|
|
|
|
state.autoCallFunction(autoArgs, *vTmp, *v);
|
|
|
|
|
2022-04-13 22:41:51 +00:00
|
|
|
if (auto drvInfo = getDerivation(state, *v, false)) {
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-13 22:41:51 +00:00
|
|
|
auto drv = Drv(state, *drvInfo);
|
2022-04-21 18:25:40 +00:00
|
|
|
reply.update(drv);
|
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. */
|
2022-04-21 17:46:38 +00:00
|
|
|
if (myArgs.gcRootsDir != "") {
|
2022-04-26 09:15:56 +00:00
|
|
|
Path root = myArgs.gcRootsDir + "/" +
|
|
|
|
std::string(baseNameOf(drv.drvPath));
|
2022-04-21 18:22:29 +00:00
|
|
|
if (!pathExists(root)) {
|
2022-04-26 09:15:56 +00:00
|
|
|
auto localStore =
|
|
|
|
state.store.dynamic_pointer_cast<LocalFSStore>();
|
|
|
|
auto storePath =
|
|
|
|
localStore->parseStorePath(drv.drvPath);
|
2021-03-21 18:05:11 +00:00
|
|
|
localStore->addPermRoot(storePath, root);
|
2022-04-21 18:22:29 +00:00
|
|
|
}
|
2021-03-21 18:05:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
else if (v->type() == nAttrs) {
|
2020-11-29 14:33:55 +00:00
|
|
|
auto attrs = nlohmann::json::array();
|
2022-04-26 09:15:56 +00:00
|
|
|
bool recurse =
|
|
|
|
path.size() == 0; // Dont require `recurseForDerivations =
|
|
|
|
// true;` for top-level attrset
|
2022-04-25 09:23:06 +00:00
|
|
|
|
2022-05-07 07:01:41 +00:00
|
|
|
for (auto &i : v->attrs->lexicographicOrder(state.symbols)) {
|
|
|
|
const std::string &name = state.symbols[i->name];
|
2020-11-29 14:33:55 +00:00
|
|
|
attrs.push_back(name);
|
2022-04-25 09:23:06 +00:00
|
|
|
|
|
|
|
if (name == "recurseForDerivations") {
|
2022-04-26 09:15:56 +00:00
|
|
|
auto attrv =
|
|
|
|
v->attrs->get(state.sRecurseForDerivations);
|
2022-05-07 07:01:41 +00:00
|
|
|
recurse = state.forceBool(*attrv->value, attrv->pos);
|
2022-04-25 09:23:06 +00:00
|
|
|
}
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
2022-04-25 09:23:06 +00:00
|
|
|
if (recurse)
|
2022-04-26 09:15:56 +00:00
|
|
|
reply["attrs"] = std::move(attrs);
|
2022-04-25 09:23:06 +00:00
|
|
|
else
|
2022-04-26 09:15:56 +00:00
|
|
|
reply["attrs"] = nlohmann::json::array();
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 22:16:57 +00:00
|
|
|
else if (v->type() == nNull)
|
2020-11-29 14:33:55 +00:00
|
|
|
;
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
else
|
|
|
|
throw TypeError("attribute '%s' is %s, which is not supported",
|
|
|
|
path, showType(*v));
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
} 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());
|
|
|
|
|
2022-04-21 04:01:02 +00:00
|
|
|
/* If our RSS exceeds the maximum, exit. The collector will
|
2020-11-29 14:33:55 +00:00
|
|
|
start a new process. */
|
|
|
|
struct rusage r;
|
|
|
|
getrusage(RUSAGE_SELF, &r);
|
2022-04-26 09:15:56 +00:00
|
|
|
if ((size_t)r.ru_maxrss > myArgs.maxMemorySize * 1024)
|
|
|
|
break;
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeLine(to.get(), "restart");
|
|
|
|
}
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
typedef std::function<void(EvalState &state, Bindings &autoArgs,
|
|
|
|
AutoCloseFD &to, AutoCloseFD &from)>
|
2022-04-15 06:28:19 +00:00
|
|
|
Processor;
|
|
|
|
|
|
|
|
/* Auto-cleanup of fork's process and fds. */
|
|
|
|
struct Proc {
|
|
|
|
AutoCloseFD to, from;
|
|
|
|
Pid pid;
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
Proc(const Processor &proc) {
|
2022-04-15 06:28:19 +00:00
|
|
|
Pipe toPipe, fromPipe;
|
|
|
|
toPipe.create();
|
|
|
|
fromPipe.create();
|
|
|
|
auto p = startProcess(
|
|
|
|
[&,
|
|
|
|
to{std::make_shared<AutoCloseFD>(std::move(fromPipe.writeSide))},
|
2022-04-26 09:15:56 +00:00
|
|
|
from{
|
|
|
|
std::make_shared<AutoCloseFD>(std::move(toPipe.readSide))}]() {
|
2022-04-15 06:28:19 +00:00
|
|
|
debug("created worker process %d", getpid());
|
|
|
|
try {
|
|
|
|
EvalState state(myArgs.searchPath, openStore());
|
2022-04-26 09:15:56 +00:00
|
|
|
Bindings &autoArgs = *myArgs.getAutoArgs(state);
|
2022-04-15 06:28:19 +00:00
|
|
|
proc(state, autoArgs, *to, *from);
|
2022-04-26 09:15:56 +00:00
|
|
|
} catch (Error &e) {
|
2022-04-15 06:28:19 +00:00
|
|
|
nlohmann::json err;
|
|
|
|
auto msg = e.msg();
|
|
|
|
err["error"] = filterANSIEscapes(msg, true);
|
|
|
|
printError(msg);
|
|
|
|
writeLine(to->get(), err.dump());
|
|
|
|
// Don't forget to print it into the STDERR log, this is
|
|
|
|
// what's shown in the Hydra UI.
|
|
|
|
writeLine(to->get(), "restart");
|
|
|
|
}
|
|
|
|
},
|
2022-04-26 09:15:56 +00:00
|
|
|
ProcessOptions{.allowVfork = false});
|
2022-04-15 06:28:19 +00:00
|
|
|
|
|
|
|
to = std::move(toPipe.writeSide);
|
|
|
|
from = std::move(fromPipe.readSide);
|
|
|
|
pid = p;
|
|
|
|
}
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
~Proc() {}
|
2022-04-15 06:28:19 +00:00
|
|
|
};
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
struct State {
|
|
|
|
std::set<json> todo = json::array({json::array()});
|
|
|
|
std::set<json> active;
|
|
|
|
std::exception_ptr exc;
|
2022-04-21 04:01:02 +00:00
|
|
|
};
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
std::function<void()> collector(Sync<State> &state_,
|
|
|
|
std::condition_variable &wakeup) {
|
2022-04-21 04:01:02 +00:00
|
|
|
return [&]() {
|
|
|
|
try {
|
|
|
|
std::optional<std::unique_ptr<Proc>> proc_;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
auto proc = proc_.has_value() ? std::move(proc_.value())
|
|
|
|
: std::make_unique<Proc>(worker);
|
2022-04-21 04:01:02 +00:00
|
|
|
|
|
|
|
/* Check whether the existing worker process is still there. */
|
|
|
|
auto s = readLine(proc->from.get());
|
|
|
|
if (s == "restart") {
|
|
|
|
proc_ = std::nullopt;
|
|
|
|
continue;
|
|
|
|
} else if (s != "next") {
|
2022-04-26 05:11:17 +00:00
|
|
|
auto json = json::parse(s);
|
2022-04-26 09:15:56 +00:00
|
|
|
throw Error("worker error: %s", (std::string)json["error"]);
|
2022-04-21 04:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for a job name to become available. */
|
2022-04-26 05:11:17 +00:00
|
|
|
json attrPath;
|
2022-04-21 04:01:02 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
checkInterrupt();
|
|
|
|
auto state(state_.lock());
|
2022-04-26 09:15:56 +00:00
|
|
|
if ((state->todo.empty() && state->active.empty()) ||
|
|
|
|
state->exc) {
|
2022-04-21 04:01:02 +00:00
|
|
|
writeLine(proc->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. */
|
2022-04-26 05:11:17 +00:00
|
|
|
writeLine(proc->to.get(), "do " + attrPath.dump());
|
2022-04-21 04:01:02 +00:00
|
|
|
|
|
|
|
/* Wait for the response. */
|
|
|
|
auto respString = readLine(proc->from.get());
|
2022-04-26 05:11:17 +00:00
|
|
|
auto response = json::parse(respString);
|
2022-04-21 04:01:02 +00:00
|
|
|
|
|
|
|
/* Handle the response. */
|
2022-04-26 05:11:17 +00:00
|
|
|
std::vector<json> newAttrs;
|
2022-04-21 04:01:02 +00:00
|
|
|
if (response.find("attrs") != response.end()) {
|
2022-04-26 09:15:56 +00:00
|
|
|
for (auto &i : response["attrs"]) {
|
|
|
|
json newAttr = json(response["attrPath"]);
|
|
|
|
newAttr.emplace_back(i);
|
|
|
|
newAttrs.push_back(newAttr);
|
2022-04-21 04:01:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto state(state_.lock());
|
|
|
|
std::cout << respString << "\n" << std::flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
proc_ = std::move(proc);
|
|
|
|
|
|
|
|
/* Add newly discovered job names to the queue. */
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->active.erase(attrPath);
|
2022-04-26 05:11:17 +00:00
|
|
|
for (auto p : newAttrs) {
|
|
|
|
state->todo.insert(p);
|
|
|
|
}
|
2022-04-21 04:01:02 +00:00
|
|
|
wakeup.notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->exc = std::current_exception();
|
|
|
|
wakeup.notify_all();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
int main(int argc, char **argv) {
|
2020-11-29 14:33:55 +00:00
|
|
|
/* Prevent undeclared dependencies in the evaluation via
|
|
|
|
$NIX_PATH. */
|
|
|
|
unsetenv("NIX_PATH");
|
|
|
|
|
2022-04-21 16:31:53 +00:00
|
|
|
/* We are doing the garbage collection by killing forks */
|
|
|
|
setenv("GC_DONT_GC", "1", 1);
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
return handleExceptions(argv[0], [&]() {
|
|
|
|
initNix();
|
|
|
|
initGC();
|
|
|
|
|
|
|
|
myArgs.parseCmdline(argvToStrings(argc, argv));
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
/* FIXME: The build hook in conjunction with import-from-derivation is
|
|
|
|
* causing "unexpected EOF" during eval */
|
2020-11-29 14:33:55 +00:00
|
|
|
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. */
|
2022-08-17 06:38:37 +00:00
|
|
|
if (myArgs.impure) {
|
|
|
|
evalSettings.pureEval = false;
|
|
|
|
} else if (myArgs.flake) {
|
|
|
|
evalSettings.pureEval = true;
|
|
|
|
}
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
if (myArgs.releaseExpr == "")
|
|
|
|
throw UsageError("no expression specified");
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +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
|
|
|
Sync<State> state_;
|
|
|
|
|
2022-04-21 04:01:02 +00:00
|
|
|
/* Start a collector thread per worker process. */
|
2020-11-29 14:33:55 +00:00
|
|
|
std::vector<std::thread> threads;
|
2022-04-21 04:01:02 +00:00
|
|
|
std::condition_variable wakeup;
|
2020-11-29 14:33:55 +00:00
|
|
|
for (size_t i = 0; i < myArgs.nrWorkers; i++)
|
2022-04-21 04:01:02 +00:00
|
|
|
threads.emplace_back(std::thread(collector(state_, wakeup)));
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
for (auto &thread : threads)
|
2020-11-29 14:33:55 +00:00
|
|
|
thread.join();
|
|
|
|
|
|
|
|
auto state(state_.lock());
|
|
|
|
|
|
|
|
if (state->exc)
|
|
|
|
std::rethrow_exception(state->exc);
|
|
|
|
});
|
|
|
|
}
|