Repository navigation
Expand file tree
/
Copy pathmain.cc
More file actions
605 lines (510 loc) · 18 KB
/
Copy pathmain.cc
File metadata and controls
605 lines (510 loc) · 18 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#include "nix/util/args/root.hh"
#include "nix/util/current-process.hh"
#include "nix/cmd/command.hh"
#include "nix/main/common-args.hh"
#include "nix/expr/eval.hh"
#include "nix/expr/eval-settings.hh"
#include "nix/store/globals.hh"
#include "nix/cmd/legacy.hh"
#include "nix/main/shared.hh"
#include "nix/store/store-open.hh"
#include "nix/store/store-registration.hh"
#include "nix/store/filetransfer.hh"
#include "nix/util/finally.hh"
#include "nix/main/loggers.hh"
#include "nix/cmd/markdown.hh"
#include "nix/util/memory-source-accessor.hh"
#include "nix/util/terminal.hh"
#include "nix/util/users.hh"
#include "nix/cmd/network-proxy.hh"
#include "nix/expr/eval-cache.hh"
#include "nix/flake/flake.hh"
#include "nix/flake/settings.hh"
#include "nix/util/json-utils.hh"
#include "self-exe.hh"
#include "crash-handler.hh"
#include "cli-config-private.hh"
#include <sys/types.h>
#include <regex>
#include <nlohmann/json.hpp>
#ifndef _WIN32
# include <sys/socket.h>
# include <ifaddrs.h>
# include <netdb.h>
# include <netinet/in.h>
#endif
#ifdef __linux__
# include "nix/util/linux-namespaces.hh"
#endif
#ifndef _WIN32
extern std::string chrootHelperName;
void chrootHelper(int argc, char ** argv);
#endif
#include "nix/util/strings.hh"
namespace nix {
/* Check if we have a non-loopback/link-local network interface. */
static bool haveInternet()
{
#ifndef _WIN32
struct ifaddrs * addrs;
if (getifaddrs(&addrs))
return true;
Finally free([&]() { freeifaddrs(addrs); });
for (auto i = addrs; i; i = i->ifa_next) {
if (!i->ifa_addr)
continue;
if (i->ifa_addr->sa_family == AF_INET) {
if (ntohl(((sockaddr_in *) i->ifa_addr)->sin_addr.s_addr) != INADDR_LOOPBACK) {
return true;
}
} else if (i->ifa_addr->sa_family == AF_INET6) {
if (!IN6_IS_ADDR_LOOPBACK(&((sockaddr_in6 *) i->ifa_addr)->sin6_addr)
&& !IN6_IS_ADDR_LINKLOCAL(&((sockaddr_in6 *) i->ifa_addr)->sin6_addr))
return true;
}
}
if (haveNetworkProxyConnection())
return true;
return false;
#else
// TODO implement on Windows
return true;
#endif
}
static void disableNet()
{
// FIXME: should check for command line overrides only.
if (!settings.useSubstitutes.overridden)
// FIXME: should not disable local substituters (like file:///).
settings.useSubstitutes = false;
if (!settings.tarballTtl.overridden)
settings.tarballTtl = std::numeric_limits<unsigned int>::max();
if (!settings.ttlNarInfoCacheMeta.overridden)
settings.ttlNarInfoCacheMeta = std::numeric_limits<unsigned int>::max();
if (!fileTransferSettings.tries.overridden)
fileTransferSettings.tries = 0;
if (!fileTransferSettings.connectTimeout.overridden)
fileTransferSettings.connectTimeout = 1;
}
std::string programPath;
struct NixArgs : virtual MultiCommand, virtual MixCommonArgs, virtual RootArgs
{
bool useNet = true;
bool refresh = false;
bool helpRequested = false;
bool showVersion = false;
NixArgs()
: MultiCommand("", RegisterCommand::getCommandsFor({}))
, MixCommonArgs("nix")
{
categories.clear();
categories[catHelp] = "Help commands";
categories[Command::catDefault] = "Main commands";
categories[catSecondary] = "Infrequently used commands";
categories[catUtility] = "Utility/scripting commands";
categories[catNixInstallation] = "Commands for upgrading or troubleshooting your Nix installation";
addFlag({
.longName = "help",
.description = "Show usage information.",
.category = miscCategory,
.handler = {[this]() { this->helpRequested = true; }},
});
addFlag({
.longName = "print-build-logs",
.shortName = 'L',
.description = "Print full build logs on standard error.",
.category = loggingCategory,
.handler = {[&]() { logger->setPrintBuildLogs(true); }},
});
addFlag({
.longName = "version",
.description = "Show version information.",
.category = miscCategory,
.handler = {[&]() { showVersion = true; }},
});
addFlag({
.longName = "offline",
.aliases = {"no-net"}, // FIXME: remove
.description = "Disable substituters and consider all previously downloaded files up-to-date.",
.category = miscCategory,
.handler = {[&]() { useNet = false; }},
});
addFlag({
.longName = "refresh",
.description = "Consider all previously downloaded files out-of-date.",
.category = miscCategory,
.handler = {[&]() { refresh = true; }},
});
aliases = {
{"add-to-store", {AliasStatus::Deprecated, {"store", "add-path"}}},
{"cat-nar", {AliasStatus::Deprecated, {"nar", "cat"}}},
{"cat-store", {AliasStatus::Deprecated, {"store", "cat"}}},
{"copy-sigs", {AliasStatus::Deprecated, {"store", "copy-sigs"}}},
{"dev-shell", {AliasStatus::Deprecated, {"develop"}}},
{"diff-closures", {AliasStatus::Deprecated, {"store", "diff-closures"}}},
{"dump-path", {AliasStatus::Deprecated, {"store", "dump-path"}}},
{"hash-file", {AliasStatus::Deprecated, {"hash", "file"}}},
{"hash-path", {AliasStatus::Deprecated, {"hash", "path"}}},
{"ls-nar", {AliasStatus::Deprecated, {"nar", "ls"}}},
{"ls-store", {AliasStatus::Deprecated, {"store", "ls"}}},
{"make-content-addressable", {AliasStatus::Deprecated, {"store", "make-content-addressed"}}},
{"optimise-store", {AliasStatus::Deprecated, {"store", "optimise"}}},
{"ping-store", {AliasStatus::Deprecated, {"store", "info"}}},
{"sign-paths", {AliasStatus::Deprecated, {"store", "sign"}}},
{"shell", {AliasStatus::AcceptedShorthand, {"env", "shell"}}},
{"show-derivation", {AliasStatus::Deprecated, {"derivation", "show"}}},
{"show-config", {AliasStatus::Deprecated, {"config", "show"}}},
{"to-base16", {AliasStatus::Deprecated, {"hash", "to-base16"}}},
{"to-base32", {AliasStatus::Deprecated, {"hash", "to-base32"}}},
{"to-base64", {AliasStatus::Deprecated, {"hash", "to-base64"}}},
{"verify", {AliasStatus::Deprecated, {"store", "verify"}}},
{"doctor", {AliasStatus::Deprecated, {"config", "check"}}},
};
};
std::string description() override
{
return "a tool for reproducible and declarative configuration management";
}
std::string doc() override
{
return
#include "nix.md"
;
}
// Plugins may add new subcommands.
void pluginsInited() override
{
commands = RegisterCommand::getCommandsFor({});
}
std::string dumpCli()
{
using nlohmann::json;
auto res = json::object();
res["args"] = toJSON();
{
auto & stores = res["stores"] = json::object();
for (auto & [storeName, implem] : Implementations::registered()) {
auto & j = stores[storeName];
j["doc"] = implem.doc;
j["uri-schemes"] = implem.uriSchemes;
j["settings"] = implem.getConfig()->toJSON();
j["experimentalFeature"] = implem.experimentalFeature;
}
}
{
auto & fetchers = res["fetchers"] = json::object();
for (const auto & [schemeName, scheme] : fetchers::getAllInputSchemes()) {
auto & s = fetchers[schemeName] = json::object();
s["description"] = scheme->schemeDescription();
auto & attrs = s["allowedAttrs"] = json::object();
for (auto & [fieldName, field] : scheme->allowedAttrs()) {
auto & f = attrs[fieldName] = json::object();
f["type"] = field.type;
f["required"] = field.required;
f["doc"] = stripIndentation(field.doc);
}
}
};
return res.dump();
}
};
/* Render the help for the specified subcommand to stdout using
lowdown. */
static void showHelp(std::vector<std::string> subcommand, NixArgs & toplevel)
{
// Check for aliases if subcommand has exactly one element
if (subcommand.size() == 1) {
auto alias = toplevel.aliases.find(subcommand[0]);
if (alias != toplevel.aliases.end()) {
subcommand = alias->second.replacement;
}
}
auto mdName = subcommand.empty() ? "nix" : fmt("nix3-%s", concatStringsSep("-", subcommand));
evalSettings.restrictEval = true;
evalSettings.pureEval = true;
EvalState state({}, openStore("dummy://"), fetchSettings, evalSettings);
auto vGenerateManpage = state.allocValue();
state.eval(
state.parseExprFromString(
#include "generate-manpage.nix.gen.hh"
, state.rootPath(CanonPath::root)),
*vGenerateManpage);
state.corepkgsFS->addFile(
CanonPath("utils.nix"),
#include "utils.nix.gen.hh"
);
state.corepkgsFS->addFile(
CanonPath("/generate-settings.nix"),
#include "generate-settings.nix.gen.hh"
);
state.corepkgsFS->addFile(
CanonPath("/generate-store-info.nix"),
#include "generate-store-info.nix.gen.hh"
);
auto vDump = state.allocValue();
vDump->mkString(toplevel.dumpCli(), state.mem);
auto vRes = state.allocValue();
Value * args[]{&state.getBuiltin("false"), vDump};
state.callFunction(*vGenerateManpage, args, *vRes, noPos);
auto attr = vRes->attrs()->get(state.symbols.create(mdName + ".md"));
if (!attr)
throw UsageError("Nix has no subcommand '%s'", concatStringsSep("", subcommand));
auto markdown = state.forceString(*attr->value, noPos, "while evaluating the lowdown help text");
RunPager pager;
std::cout << renderMarkdownToTerminal(markdown) << "\n";
}
static NixArgs & getNixArgs(Command & cmd)
{
return dynamic_cast<NixArgs &>(cmd.getRoot());
}
struct CmdHelp : Command
{
std::vector<std::string> subcommand;
CmdHelp()
{
expectArgs({
.label = "subcommand",
.handler = {&subcommand},
});
}
std::string description() override
{
return "show help about `nix` or a particular subcommand";
}
std::string doc() override
{
return
#include "help.md"
;
}
Category category() override
{
return catHelp;
}
void run() override
{
assert(parent);
MultiCommand * toplevel = parent;
while (toplevel->parent)
toplevel = toplevel->parent;
showHelp(subcommand, getNixArgs(*this));
}
};
static auto rCmdHelp = registerCommand<CmdHelp>("help");
struct CmdHelpStores : Command
{
std::string description() override
{
return "show help about store types and their settings";
}
std::string doc() override
{
return
#include "help-stores.md.gen.hh"
;
}
Category category() override
{
return catHelp;
}
void run() override
{
showHelp({"help-stores"}, getNixArgs(*this));
}
};
static auto rCmdHelpStores = registerCommand<CmdHelpStores>("help-stores");
void mainWrapped(int argc, char ** argv)
{
savedArgv = argv;
registerCrashHandler();
/* The chroot helper needs to be run before any threads have been
started. */
#ifndef _WIN32
if (argc > 0 && argv[0] == chrootHelperName) {
chrootHelper(argc, argv);
return;
}
#endif
initNix();
initGC();
flakeSettings.configureEvalSettings(evalSettings);
/* Set the build hook location
For builds we perform a self-invocation, so Nix has to be
self-aware. That is, it has to know where it is installed. We
don't think it's sentient.
*/
settings.buildHook.setDefault(
Strings{
getNixBin({}).string(),
"__build-remote",
});
#ifdef __linux__
if (isRootUser()) {
try {
saveMount
8000
Namespace();
if (unshare(CLONE_NEWNS) == -1)
throw SysError("setting up a private mount namespace");
} catch (Error & e) {
}
}
#endif
programPath = argv[0];
auto programName = std::string(baseNameOf(programPath));
auto extensionPos = programName.find_last_of(".");
if (extensionPos != std::string::npos)
programName.erase(extensionPos);
if (argc > 1 && std::string_view(argv[1]) == "__build-remote") {
programName = "build-remote";
argv++;
argc--;
}
{
auto legacy = RegisterLegacyCommand::commands()[programName];
if (legacy)
return legacy(argc, argv);
}
evalSettings.pureEval = true;
setLogFormat("bar");
settings.verboseBuild = false;
// If on a terminal, progress will be displayed via progress bars etc. (thus verbosity=notice)
if (nix::isTTY()) {
verbosity = lvlNotice;
} else {
verbosity = lvlInfo;
}
NixArgs args;
if (argc == 2 && std::string(argv[1]) == "__dump-cli") {
logger->cout(args.dumpCli());
return;
}
if (argc == 2 && std::string(argv[1]) == "__dump-language") {
experimentalFeatureSettings.experimentalFeatures = {
Xp::FetchClosure,
Xp::DynamicDerivations,
Xp::FetchTree,
};
evalSettings.pureEval = false;
EvalState state({}, openStore("dummy://"), fetchSettings, evalSettings);
auto builtinsJson = nlohmann::json::object();
for (auto & builtinPtr : state.getBuiltins().attrs()->lexicographicOrder(state.symbols)) {
auto & builtin = *builtinPtr;
auto b = nlohmann::json::object();
if (!builtin.value->isPrimOp())
continue;
auto primOp = builtin.value->primOp();
if (!primOp->doc)
continue;
b["args"] = primOp->args;
b["doc"] = trim(stripIndentation(*primOp->doc));
if (primOp->experimentalFeature)
b["experimental-feature"] = primOp->experimentalFeature;
builtinsJson.emplace(state.symbols[builtin.name], std::move(b));
}
for (auto & [name, info] : state.constantInfos) {
auto b = nlohmann::json::object();
if (!info.doc)
continue;
b["doc"] = trim(stripIndentation(info.doc));
b["type"] = showType(info.type, false);
if (info.impureOnly)
b["impure-only"] = true;
builtinsJson[name] = std::move(b);
}
logger->cout("%s", builtinsJson);
return;
}
if (argc == 2 && std::string(argv[1]) == "__dump-xp-features") {
logger->cout(documentExperimentalFeatures().dump());
return;
}
Finally printCompletions([&]() {
if (args.completions) {
switch (args.completions->type) {
case Completions::Type::Normal:
logger->cout("normal");
break;
case Completions::Type::Filenames:
logger->cout("filenames");
break;
case Completions::Type::Attrs:
logger->cout("attrs");
break;
}
for (auto & s : args.completions->completions)
logger->cout(s.completion + "\t" + trim(s.description));
}
});
if (getEnv("NIX_GET_COMPLETIONS"))
/* Avoid fetching stuff during tab completion. We have to this
early because we haven't checked `haveInternet()` yet
(below). */
disableNet();
try {
auto isNixCommand = std::regex_search(programName, std::regex("nix$"));
auto allowShebang = isNixCommand && argc > 1;
args.parseCmdline(argvToStrings(argc, argv), allowShebang);
} catch (UsageError &) {
if (!args.helpRequested && !args.completions)
throw;
}
applyJSONLogger();
printTalkative("Nix %s", version());
if (args.helpRequested) {
std::vector<std::string> subcommand;
MultiCommand * command = &args;
while (command)
7083
{
if (command && command->command) {
subcommand.push_back(command->command->first);
command = dynamic_cast<MultiCommand *>(&*command->command->second);
} else
break;
}
showHelp(subcommand, args);
return;
}
if (args.completions)
return;
if (args.showVersion) {
printVersion(programName);
return;
}
if (!args.command)
throw UsageError("no subcommand specified");
experimentalFeatureSettings.require(args.command->second->experimentalFeature());
if (args.useNet && !haveInternet()) {
warn("you don't have Internet access; disabling some network-dependent features");
args.useNet = false;
}
if (!args.useNet)
disableNet();
if (args.refresh) {
settings.tarballTtl = 0;
settings.ttlNegativeNarInfoCache = 0;
settings.ttlPositiveNarInfoCache = 0;
settings.ttlNarInfoCacheMeta = 0;
}
if (args.command->second->forceImpureByDefault() && !evalSettings.pureEval.overridden) {
evalSettings.pureEval = false;
}
try {
args.command->second->run();
} catch (eval_cache::CachedEvalError & e) {
/* Evaluate the original attribute that resulted in this
cached error so that we can show the original error to the
user. */
e.force();
}
}
} // namespace nix
int main(int argc, char ** argv)
{
using namespace nix;
// The CLI has a more detailed version than the libraries; see nixVersion.
nixVersion = NIX_CLI_VERSION;
#ifndef _WIN32
// Increase the default stack size for the evaluator and for
// libstdc++'s std::regex.
setStackSize(evalStackSize);
#endif
return handleExceptions(argv[0], [&]() { mainWrapped(argc, argv); });
}