1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
#include <lix/config.h> // IWYU pragma: keep
// doesn't exist on macOS
// IWYU pragma: no_include <bits/types/struct_rusage.h>
#include <lix/libexpr/attr-path.hh>
#include <lix/libstore/local-fs-store.hh>
#include <lix/libcmd/installable-flake.hh>
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
#include <lix/libexpr/attr-set.hh>
#include <lix/libutil/canon-path.hh>
#include <lix/libcmd/common-eval-args.hh>
#include <lix/libutil/current-process.hh>
#include <lix/libutil/error.hh>
#include <lix/libexpr/eval-cache.hh>
#include <lix/libexpr/eval-inline.hh>
#include <lix/libexpr/eval.hh>
#include <lix/libexpr/flake/flakeref.hh>
#include <lix/libexpr/get-drvs.hh>
#include <lix/libutil/input-accessor.hh>
#include <lix/libutil/json.hh>
#include <lix/libutil/logging.hh>
#include <lix/libexpr/nixexpr.hh>
#include <lix/libutil/ref.hh>
#include <lix/libstore/store-api.hh>
#include <lix/libexpr/symbol-table.hh>
#include <lix/libutil/types.hh>
#include <lix/libexpr/value.hh>
#include <lix/libutil/terminal.hh>
#include <exception>
#include <numeric>
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <utility>
#include "worker.hh"
#include "drv.hh"
#include "buffered-io.hh"
#include "eval-args.hh"
static nix::Value releaseExprTopLevelValue(nix::EvalState &state,
nix::Bindings &autoArgs,
MyArgs &args) {
nix::Value vTop = [&]() {
if (args.fromArgs) {
nix::Expr &e = state.ctx.parseExprFromString(
args.releaseExpr, nix::CanonPath::fromCwd());
return state.eval(e);
} else {
return state.evalFile(
state.aio
.blockOn(nix::lookupFileArg(state.ctx, args.releaseExpr))
.unwrap());
}
}();
return state.autoCallFunction(autoArgs, vTop, {});
}
static std::string attrPathJoin(nix::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;
});
}
static std::optional<Constituents>
readConstituents(const nix::Value *v, nix::box_ptr<nix::EvalState> &state,
nix::ref<nix::eval_cache::CachingEvaluator> &evaluator) {
auto a = v->attrs()->get(state->ctx.symbols.create("_hydraAggregate"));
if (a && state->forceBool(a->value, a->pos,
"while evaluating the "
"`_hydraAggregate` attribute")) {
std::vector<std::string> constituents;
std::vector<std::string> namedConstituents;
auto a = v->attrs()->get(state->ctx.symbols.create("constituents"));
if (!a)
state->ctx.errors
.make<nix::EvalError>("derivation must have a ‘constituents’ "
"attribute")
.debugThrow(nix::always_progresses); // we can't have a debugger here
nix::NixStringContext context;
state->coerceToString(a->pos, a->value, context,
"while evaluating the `constituents` attribute",
nix::StringCoercionMode::ToString, false);
for (auto &c : context)
std::visit(nix::overloaded{
[&](const nix::NixStringContextElem::Built &b) {
constituents.push_back(
b.drvPath.to_string(*evaluator->store));
},
[&](const nix::NixStringContextElem::Opaque &) {},
[&](const nix::NixStringContextElem::DrvDeep &) {},
},
c.raw);
state->forceList(a->value, a->pos,
"while evaluating the "
"`constituents` attribute");
for (unsigned int n = 0; n < a->value.listSize(); ++n) {
auto v = a->value.listElems()[n];
state->forceValue(v, nix::noPos);
if (v.type() == nix::nString)
namedConstituents.emplace_back(v.str());
}
return Constituents(constituents, namedConstituents);
}
return std::nullopt;
}
void worker(nix::AutoCloseFD &to, nix::AutoCloseFD &from, MyArgs &args)
try {
// Increase the default stack size for the evaluator and for
// libstdc++'s std::regex.
nix::ensureStackSizeAtLeast(64 * 1024 * 1024);
#if HAVE_BOEHMGC
// We are doing the garbage collection by killing forks.
GC_disable();
// There is some memory usage overhead on top of the GC heap size.
// A simple model using fixed and proportional overhead already gives reasonable results.
// The parameters have been determined experimentally using evaluation of nixpkgs.
size_t maxGcHeapSize = std::ldexp(std::max(0.0, 0.9 * (static_cast<double>(args.maxMemorySize) - 150.0)), 20);
if (GC_get_heap_size() > maxGcHeapSize) {
throw nix::Error("The heap is too large. Increase %s or reduce %s.",
"--max-memory-size", "GC_INITIAL_HEAP_SIZE");
}
#endif
auto & aio = args.aio();
auto evalStore = aio.blockOn(args.evalStoreUrl
? nix::openStore(*args.evalStoreUrl)
: nix::openStore());
auto evaluator =
nix::make_ref<nix::eval_cache::CachingEvaluator>(
aio, args.searchPath, evalStore);
nix::Bindings &autoArgs = *args.getAutoArgs(*evaluator);
nix::Value vRoot = [&]() {
auto state = evaluator->begin(aio);
if (args.flake) {
auto [flakeRef, fragment, outputSpec] =
nix::parseFlakeRefWithFragmentAndExtendedOutputsSpec(
args.releaseExpr, nix::absPath("."));
nix::InstallableFlake flake{
{}, evaluator, std::move(flakeRef), fragment, outputSpec,
{}, {}, args.lockFlags};
return flake.toValue(*state).first;
} else {
return releaseExprTopLevelValue(*state, autoArgs, args);
}
}();
LineReader fromReader(from.release());
auto state = evaluator->begin(aio);
while (true) {
/* Wait for the collector to send us a job name. */
if (tryWriteLine(to.get(), "next") < 0) {
return; // main process died
}
auto s = fromReader.readLine();
if (s == "exit") {
break;
}
if (!s.starts_with("do ")) {
std::cerr << "worker error: received invalid command '" << s << "'\n";
abort();
}
auto path = nix::json::parse(s.substr(3));
auto attrPathS = attrPathJoin(path);
/* Evaluate it and send info back to the collector. */
nix::JSON reply =
nix::JSON{{"attr", attrPathS}, {"attrPath", path}};
try {
auto vTmp =
nix::findAlongAttrPath(*state, attrPathS, autoArgs, vRoot)
.first;
nix::Value v = state->autoCallFunction(autoArgs, vTmp, {});
if (v.type() == nix::nAttrs) {
if (auto drvInfo = nix::getDerivation(*state, v, false)) {
std::optional<Constituents> maybeConstituents;
if (args.constituents) {
maybeConstituents =
readConstituents(&v, state, evaluator);
}
auto drv = Drv(attrPathS, *state, *drvInfo, args,
maybeConstituents);
reply.update(drv);
/* Register the derivation as a GC root. !!! This
registers roots for jobs that we may have already
done. */
register_gc_root(args.gcRootsDir, drv.drvPath, evaluator->store, aio);
} else {
auto attrs = nix::JSON::array();
bool recurse =
args.forceRecurse ||
path.size() == 0; // Dont require `recurseForDerivations
// = true;` for top-level attrset
for (auto &i :
v.attrs()->lexicographicOrder(evaluator->symbols)) {
const std::string_view name = evaluator->symbols[i->name];
attrs.emplace_back(name);
if (name == "recurseForDerivations" &&
!args.forceRecurse) {
auto attrv = v.attrs()->get(
evaluator->symbols.sym_recurseForDerivations);
recurse = state->forceBool(
attrv->value, attrv->pos,
"while evaluating recurseForDerivations");
}
}
if (recurse)
reply["attrs"] = std::move(attrs);
else
reply["attrs"] = nix::JSON::array();
}
} else {
// We ignore everything that cannot be build
reply["attrs"] = nix::JSON::array();
}
} catch (nix::EvalError &e) {
auto err = e.info();
std::ostringstream oss;
nix::showErrorInfo(oss, err, nix::loggerSettings.showTrace.get());
auto msg = oss.str();
// Transmits the error we got from the previous evaluation
// in the JSON output.
reply["error"] = nix::filterANSIEscapes(msg, true);
// Don't forget to print it into the STDERR log, this is
// what's shown in the Hydra UI.
std::cerr << msg << "\n";
} catch (const nix::Interrupted &) {
throw;
} catch ( // NOLINT(lix-foreign-exceptions)
const std::exception &e) { // FIXME: for some reason the catch block
// above, doesn't trigger on macOS (?)
auto msg = e.what();
reply["error"] = nix::filterANSIEscapes(msg, true);
std::cerr << msg << "\n";
}
if (tryWriteLine(to.get(), reply.dump()) < 0) {
return; // main process died
}
// If the memory limit is exceeded, exit.
// The collector will start a new process.
#if HAVE_BOEHMGC
if (GC_get_heap_size() > maxGcHeapSize) {
break;
}
#else
struct rusage r;
getrusage(RUSAGE_SELF, &r);
if ((size_t)r.ru_maxrss > args.maxMemorySize * 1024) {
break;
}
#endif
}
if (tryWriteLine(to.get(), "restart") < 0) {
return; // main process died
};
} catch (const nix::Interrupted &) {
// The coordinator should get the interrupt too, so it doesn't need to be logged.
} catch (nix::Error &e) {
nix::JSON err;
auto msg = e.msg();
err["error"] = nix::filterANSIEscapes(msg, true);
printError("%1%", nix::Uncolored(msg));
if (tryWriteLine(to.get(), err.dump()) < 0) {
return; // main process died
};
// Don't forget to print it into the STDERR log, this is
// what's shown in the Hydra UI.
if (tryWriteLine(to.get(), "restart") < 0) {
return; // main process died
}
}
|