2016-08-09 10:24:19 +00:00
|
|
|
#include <cstring>
|
2016-09-20 13:10:51 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2016-08-09 10:24:19 +00:00
|
|
|
#include <regex>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
2016-09-20 13:10:51 +00:00
|
|
|
|
2016-08-09 10:24:19 +00:00
|
|
|
#include "store-api.hh"
|
|
|
|
#include "globals.hh"
|
|
|
|
#include "derivations.hh"
|
2016-09-20 13:10:51 +00:00
|
|
|
#include "affinity.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
#include "shared.hh"
|
2017-07-20 11:32:01 +00:00
|
|
|
#include "eval.hh"
|
2017-07-25 13:09:06 +00:00
|
|
|
#include "eval-inline.hh"
|
2017-07-20 11:32:01 +00:00
|
|
|
#include "get-drvs.hh"
|
2017-10-24 10:45:11 +00:00
|
|
|
#include "common-eval-args.hh"
|
2017-07-25 13:09:06 +00:00
|
|
|
#include "attr-path.hh"
|
2018-10-26 09:35:46 +00:00
|
|
|
#include "legacy.hh"
|
2016-08-09 10:24:19 +00:00
|
|
|
|
|
|
|
using namespace nix;
|
2017-05-03 13:01:15 +00:00
|
|
|
using namespace std::string_literals;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2016-10-19 13:21:18 +00:00
|
|
|
extern char * * environ;
|
|
|
|
|
2016-08-31 14:08:00 +00:00
|
|
|
/* Recreate the effect of the perl shellwords function, breaking up a
|
|
|
|
* string into arguments like a shell word, including escapes
|
|
|
|
*/
|
2016-08-09 10:24:19 +00:00
|
|
|
std::vector<string> shellwords(const string & s)
|
|
|
|
{
|
2016-09-12 10:06:13 +00:00
|
|
|
std::regex whitespace("^(\\s+).*");
|
2016-08-09 10:24:19 +00:00
|
|
|
auto begin = s.cbegin();
|
2016-09-12 10:06:13 +00:00
|
|
|
std::vector<string> res;
|
2016-09-12 11:22:23 +00:00
|
|
|
std::string cur;
|
2016-08-09 10:24:19 +00:00
|
|
|
enum state {
|
|
|
|
sBegin,
|
|
|
|
sQuote
|
|
|
|
};
|
|
|
|
state st = sBegin;
|
|
|
|
auto it = begin;
|
|
|
|
for (; it != s.cend(); ++it) {
|
|
|
|
if (st == sBegin) {
|
2016-09-12 10:06:13 +00:00
|
|
|
std::smatch match;
|
2016-08-09 10:24:19 +00:00
|
|
|
if (regex_search(it, s.cend(), match, whitespace)) {
|
2016-09-12 11:22:23 +00:00
|
|
|
cur.append(begin, it);
|
|
|
|
res.push_back(cur);
|
|
|
|
cur.clear();
|
2016-08-09 10:24:19 +00:00
|
|
|
it = match[1].second;
|
|
|
|
begin = it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (*it) {
|
|
|
|
case '"':
|
2016-09-12 11:22:23 +00:00
|
|
|
cur.append(begin, it);
|
2016-08-09 10:24:19 +00:00
|
|
|
begin = it + 1;
|
|
|
|
st = st == sBegin ? sQuote : sBegin;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
/* perl shellwords mostly just treats the next char as part of the string with no special processing */
|
2016-09-12 11:22:23 +00:00
|
|
|
cur.append(begin, it);
|
2016-08-09 10:24:19 +00:00
|
|
|
begin = ++it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 11:22:23 +00:00
|
|
|
cur.append(begin, it);
|
|
|
|
if (!cur.empty()) res.push_back(cur);
|
2016-08-09 10:24:19 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-10-26 09:35:46 +00:00
|
|
|
static void _main(int argc, char * * argv)
|
2016-09-21 14:21:47 +00:00
|
|
|
{
|
2017-07-25 13:09:06 +00:00
|
|
|
auto dryRun = false;
|
|
|
|
auto runEnv = std::regex_search(argv[0], std::regex("nix-shell$"));
|
|
|
|
auto pure = false;
|
|
|
|
auto fromArgs = false;
|
|
|
|
auto packages = false;
|
|
|
|
// Same condition as bash uses for interactive shells
|
|
|
|
auto interactive = isatty(STDIN_FILENO) && isatty(STDERR_FILENO);
|
|
|
|
Strings attrPaths;
|
|
|
|
Strings left;
|
|
|
|
RepairFlag repair = NoRepair;
|
|
|
|
Path gcRoot;
|
|
|
|
BuildMode buildMode = bmNormal;
|
|
|
|
bool readStdin = false;
|
|
|
|
|
|
|
|
std::string envCommand; // interactive shell
|
|
|
|
Strings envExclude;
|
|
|
|
|
|
|
|
auto myName = runEnv ? "nix-shell" : "nix-build";
|
|
|
|
|
|
|
|
auto inShebang = false;
|
|
|
|
std::string script;
|
|
|
|
std::vector<string> savedArgs;
|
|
|
|
|
|
|
|
AutoDelete tmpDir(createTempDir("", myName));
|
|
|
|
|
|
|
|
std::string outLink = "./result";
|
|
|
|
|
2018-08-02 01:28:49 +00:00
|
|
|
// List of environment variables kept for --pure
|
|
|
|
std::set<string> keepVars{"HOME", "USER", "LOGNAME", "DISPLAY", "PATH", "TERM", "IN_NIX_SHELL", "TZ", "PAGER", "NIX_BUILD_SHELL", "SHLVL"};
|
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
Strings args;
|
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
args.push_back(argv[i]);
|
|
|
|
|
2017-09-18 11:28:00 +00:00
|
|
|
// Heuristic to see if we're invoked as a shebang script, namely,
|
|
|
|
// if we have at least one argument, it's the name of an
|
|
|
|
// executable file, and it starts with "#!".
|
2017-07-25 13:09:06 +00:00
|
|
|
if (runEnv && argc > 1 && !std::regex_search(argv[1], std::regex("nix-shell"))) {
|
|
|
|
script = argv[1];
|
2017-09-18 11:28:00 +00:00
|
|
|
try {
|
2017-07-25 13:09:06 +00:00
|
|
|
auto lines = tokenizeString<Strings>(readFile(script), "\n");
|
|
|
|
if (std::regex_search(lines.front(), std::regex("^#!"))) {
|
|
|
|
lines.pop_front();
|
|
|
|
inShebang = true;
|
|
|
|
for (int i = 2; i < argc; ++i)
|
|
|
|
savedArgs.push_back(argv[i]);
|
|
|
|
args.clear();
|
|
|
|
for (auto line : lines) {
|
|
|
|
line = chomp(line);
|
|
|
|
std::smatch match;
|
|
|
|
if (std::regex_match(line, match, std::regex("^#!\\s*nix-shell (.*)$")))
|
|
|
|
for (const auto & word : shellwords(match[1].str()))
|
|
|
|
args.push_back(word);
|
2016-08-09 10:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-18 11:28:00 +00:00
|
|
|
} catch (SysError &) { }
|
2017-07-25 13:09:06 +00:00
|
|
|
}
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-10-24 10:45:11 +00:00
|
|
|
struct MyArgs : LegacyArgs, MixEvalArgs
|
|
|
|
{
|
|
|
|
using LegacyArgs::LegacyArgs;
|
|
|
|
};
|
|
|
|
|
|
|
|
MyArgs myArgs(myName, [&](Strings::iterator & arg, const Strings::iterator & end) {
|
2017-07-25 13:09:06 +00:00
|
|
|
if (*arg == "--help") {
|
|
|
|
deletePath(tmpDir);
|
|
|
|
showManPage(myName);
|
|
|
|
}
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--version")
|
|
|
|
printVersion(myName);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2018-02-01 16:09:56 +00:00
|
|
|
else if (*arg == "--add-drv-link" || *arg == "--indirect")
|
2017-07-25 13:09:06 +00:00
|
|
|
; // obsolete
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--no-out-link" || *arg == "--no-link")
|
|
|
|
outLink = (Path) tmpDir + "/result";
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--attr" || *arg == "-A")
|
|
|
|
attrPaths.push_back(getArg(*arg, arg, end));
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--drv-link")
|
|
|
|
getArg(*arg, arg, end); // obsolete
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--out-link" || *arg == "-o")
|
|
|
|
outLink = getArg(*arg, arg, end);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--add-root")
|
|
|
|
gcRoot = getArg(*arg, arg, end);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--dry-run")
|
|
|
|
dryRun = true;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--repair") {
|
|
|
|
repair = Repair;
|
|
|
|
buildMode = bmRepair;
|
|
|
|
}
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--run-env") // obsolete
|
|
|
|
runEnv = true;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--command" || *arg == "--run") {
|
|
|
|
if (*arg == "--run")
|
|
|
|
interactive = false;
|
|
|
|
envCommand = getArg(*arg, arg, end) + "\nexit";
|
|
|
|
}
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--check")
|
|
|
|
buildMode = bmCheck;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--exclude")
|
|
|
|
envExclude.push_back(getArg(*arg, arg, end));
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--expr" || *arg == "-E")
|
|
|
|
fromArgs = true;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--pure") pure = true;
|
|
|
|
else if (*arg == "--impure") pure = false;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "--packages" || *arg == "-p")
|
|
|
|
packages = true;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (inShebang && *arg == "-i") {
|
|
|
|
auto interpreter = getArg(*arg, arg, end);
|
|
|
|
interactive = false;
|
|
|
|
auto execArgs = "";
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
// Überhack to support Perl. Perl examines the shebang and
|
|
|
|
// executes it unless it contains the string "perl" or "indir",
|
|
|
|
// or (undocumented) argv[0] does not contain "perl". Exploit
|
|
|
|
// the latter by doing "exec -a".
|
|
|
|
if (std::regex_search(interpreter, std::regex("perl")))
|
|
|
|
execArgs = "-a PERL";
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
std::ostringstream joined;
|
|
|
|
for (const auto & i : savedArgs)
|
|
|
|
joined << shellEscape(i) << ' ';
|
|
|
|
|
|
|
|
if (std::regex_search(interpreter, std::regex("ruby"))) {
|
|
|
|
// Hack for Ruby. Ruby also examines the shebang. It tries to
|
|
|
|
// read the shebang to understand which packages to read from. Since
|
|
|
|
// this is handled via nix-shell -p, we wrap our ruby script execution
|
|
|
|
// in ruby -e 'load' which ignores the shebangs.
|
2018-04-08 02:38:45 +00:00
|
|
|
envCommand = (format("exec %1% %2% -e 'load(\"%3%\")' -- %4%") % execArgs % interpreter % script % joined.str()).str();
|
2017-07-25 13:09:06 +00:00
|
|
|
} else {
|
|
|
|
envCommand = (format("exec %1% %2% %3% %4%") % execArgs % interpreter % script % joined.str()).str();
|
2016-08-09 10:24:19 +00:00
|
|
|
}
|
2017-07-25 13:09:06 +00:00
|
|
|
}
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2018-08-02 01:28:49 +00:00
|
|
|
else if (*arg == "--keep")
|
|
|
|
keepVars.insert(getArg(*arg, arg, end));
|
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg == "-")
|
|
|
|
readStdin = true;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else if (*arg != "" && arg->at(0) == '-')
|
|
|
|
return false;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
else
|
|
|
|
left.push_back(*arg);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
return true;
|
|
|
|
});
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-10-24 10:45:11 +00:00
|
|
|
myArgs.parseCmdline(args);
|
|
|
|
|
2018-02-08 16:26:18 +00:00
|
|
|
initPlugins();
|
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
if (packages && fromArgs)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw UsageError("'-p' and '-E' are mutually exclusive");
|
2017-07-25 13:09:06 +00:00
|
|
|
|
|
|
|
auto store = openStore();
|
|
|
|
|
2018-06-12 15:26:36 +00:00
|
|
|
auto state = std::make_unique<EvalState>(myArgs.searchPath, store);
|
|
|
|
state->repair = repair;
|
2017-07-25 13:09:06 +00:00
|
|
|
|
2018-06-12 15:26:36 +00:00
|
|
|
Bindings & autoArgs = *myArgs.getAutoArgs(*state);
|
2017-07-25 13:09:06 +00:00
|
|
|
|
|
|
|
if (packages) {
|
|
|
|
std::ostringstream joined;
|
|
|
|
joined << "with import <nixpkgs> { }; (pkgs.runCommandCC or pkgs.runCommand) \"shell\" { buildInputs = [ ";
|
|
|
|
for (const auto & i : left)
|
|
|
|
joined << '(' << i << ") ";
|
|
|
|
joined << "]; } \"\"";
|
|
|
|
fromArgs = true;
|
|
|
|
left = {joined.str()};
|
|
|
|
} else if (!fromArgs) {
|
|
|
|
if (left.empty() && runEnv && pathExists("shell.nix"))
|
|
|
|
left = {"shell.nix"};
|
|
|
|
if (left.empty())
|
|
|
|
left = {"default.nix"};
|
|
|
|
}
|
2017-07-20 11:32:01 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
if (runEnv)
|
|
|
|
setenv("IN_NIX_SHELL", pure ? "pure" : "impure", 1);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-11-24 17:07:29 +00:00
|
|
|
DrvInfos drvs;
|
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
/* Parse the expressions. */
|
|
|
|
std::vector<Expr *> exprs;
|
|
|
|
|
|
|
|
if (readStdin)
|
2018-06-12 15:26:36 +00:00
|
|
|
exprs = {state->parseStdin()};
|
2017-07-25 13:09:06 +00:00
|
|
|
else
|
|
|
|
for (auto i : left) {
|
|
|
|
if (fromArgs)
|
2018-06-12 15:26:36 +00:00
|
|
|
exprs.push_back(state->parseExprFromString(i, absPath(".")));
|
2019-05-08 12:18:57 +00:00
|
|
|
else {
|
|
|
|
auto absolute = i;
|
|
|
|
try {
|
|
|
|
absolute = canonPath(absPath(i), true);
|
|
|
|
} catch (Error e) {};
|
|
|
|
if (store->isStorePath(absolute) && std::regex_match(absolute, std::regex(".*\\.drv(!.*)?")))
|
2018-06-12 15:26:36 +00:00
|
|
|
drvs.push_back(DrvInfo(*state, store, absolute));
|
2017-07-25 13:09:06 +00:00
|
|
|
else
|
|
|
|
/* If we're in a #! script, interpret filenames
|
|
|
|
relative to the script. */
|
2018-06-12 15:26:36 +00:00
|
|
|
exprs.push_back(state->parseExprFromFile(resolveExprPath(state->checkSourcePath(lookupFileArg(*state,
|
2018-01-16 17:50:38 +00:00
|
|
|
inShebang && !packages ? absPath(i, absPath(dirOf(script))) : i)))));
|
2019-05-08 12:18:57 +00:00
|
|
|
}
|
2016-08-09 10:24:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
/* Evaluate them into derivations. */
|
|
|
|
if (attrPaths.empty()) attrPaths = {""};
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
for (auto e : exprs) {
|
|
|
|
Value vRoot;
|
2018-06-12 15:26:36 +00:00
|
|
|
state->eval(e, vRoot);
|
2017-07-03 09:54:30 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
for (auto & i : attrPaths) {
|
2018-06-12 15:26:36 +00:00
|
|
|
Value & v(*findAlongAttrPath(*state, i, autoArgs, vRoot));
|
|
|
|
state->forceValue(v);
|
|
|
|
getDerivations(*state, v, "", autoArgs, drvs, false);
|
2017-07-25 13:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 13:39:08 +00:00
|
|
|
|
2018-09-01 01:03:32 +00:00
|
|
|
state->printStats();
|
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
auto buildPaths = [&](const PathSet & paths) {
|
|
|
|
/* Note: we do this even when !printMissing to efficiently
|
|
|
|
fetch binary cache data. */
|
|
|
|
unsigned long long downloadSize, narSize;
|
|
|
|
PathSet willBuild, willSubstitute, unknown;
|
|
|
|
store->queryMissing(paths,
|
|
|
|
willBuild, willSubstitute, unknown, downloadSize, narSize);
|
2016-09-20 13:39:08 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
if (settings.printMissing)
|
|
|
|
printMissing(ref<Store>(store), willBuild, willSubstitute, unknown, downloadSize, narSize);
|
2016-09-20 13:39:08 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
if (!dryRun)
|
|
|
|
store->buildPaths(paths, buildMode);
|
|
|
|
};
|
2017-02-24 16:25:00 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
if (runEnv) {
|
|
|
|
if (drvs.size() != 1)
|
|
|
|
throw UsageError("nix-shell requires a single derivation");
|
2017-01-06 19:30:14 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
auto & drvInfo = drvs.front();
|
|
|
|
auto drv = store->derivationFromPath(drvInfo.queryDrvPath());
|
2017-01-06 19:30:14 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
PathSet pathsToBuild;
|
2017-02-01 12:00:21 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
/* Figure out what bash shell to use. If $NIX_BUILD_SHELL
|
|
|
|
is not set, then build bashInteractive from
|
|
|
|
<nixpkgs>. */
|
|
|
|
auto shell = getEnv("NIX_BUILD_SHELL", "");
|
2016-09-20 13:39:08 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
if (shell == "") {
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2016-09-21 14:21:47 +00:00
|
|
|
try {
|
2018-06-12 15:26:36 +00:00
|
|
|
auto expr = state->parseExprFromString("(import <nixpkgs> {}).bashInteractive", absPath("."));
|
2017-07-25 13:09:06 +00:00
|
|
|
|
|
|
|
Value v;
|
2018-06-12 15:26:36 +00:00
|
|
|
state->eval(expr, v);
|
2017-07-25 13:09:06 +00:00
|
|
|
|
2018-06-12 15:26:36 +00:00
|
|
|
auto drv = getDerivation(*state, v, false);
|
2017-07-25 13:09:06 +00:00
|
|
|
if (!drv)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error("the 'bashInteractive' attribute in <nixpkgs> did not evaluate to a derivation");
|
2017-07-25 13:09:06 +00:00
|
|
|
|
|
|
|
pathsToBuild.insert(drv->queryDrvPath());
|
|
|
|
|
|
|
|
shell = drv->queryOutPath() + "/bin/bash";
|
|
|
|
|
|
|
|
} catch (Error & e) {
|
|
|
|
printError("warning: %s; will use bash from your environment", e.what());
|
|
|
|
shell = "bash";
|
2016-09-21 14:21:47 +00:00
|
|
|
}
|
2017-07-25 13:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build or fetch all dependencies of the derivation.
|
|
|
|
for (const auto & input : drv.inputDrvs)
|
|
|
|
if (std::all_of(envExclude.cbegin(), envExclude.cend(), [&](const string & exclude) { return !std::regex_search(input.first, std::regex(exclude)); }))
|
2018-05-07 12:58:25 +00:00
|
|
|
pathsToBuild.insert(makeDrvPathWithOutputs(input.first, input.second));
|
2017-07-25 13:09:06 +00:00
|
|
|
for (const auto & src : drv.inputSrcs)
|
|
|
|
pathsToBuild.insert(src);
|
2016-09-21 14:21:47 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
buildPaths(pathsToBuild);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
if (dryRun) return;
|
2016-09-12 10:06:13 +00:00
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
// Set the environment.
|
|
|
|
auto env = getEnv();
|
|
|
|
|
|
|
|
auto tmp = getEnv("TMPDIR", getEnv("XDG_RUNTIME_DIR", "/tmp"));
|
|
|
|
|
|
|
|
if (pure) {
|
|
|
|
decltype(env) newEnv;
|
|
|
|
for (auto & i : env)
|
|
|
|
if (keepVars.count(i.first))
|
|
|
|
newEnv.emplace(i);
|
|
|
|
env = newEnv;
|
|
|
|
// NixOS hack: prevent /etc/bashrc from sourcing /etc/profile.
|
|
|
|
env["__ETC_PROFILE_SOURCED"] = "1";
|
2016-08-09 10:24:19 +00:00
|
|
|
}
|
2017-07-25 13:09:06 +00:00
|
|
|
|
|
|
|
env["NIX_BUILD_TOP"] = env["TMPDIR"] = env["TEMPDIR"] = env["TMP"] = env["TEMP"] = tmp;
|
|
|
|
env["NIX_STORE"] = store->storeDir;
|
|
|
|
env["NIX_BUILD_CORES"] = std::to_string(settings.buildCores);
|
|
|
|
|
|
|
|
auto passAsFile = tokenizeString<StringSet>(get(drv.env, "passAsFile", ""));
|
|
|
|
|
|
|
|
bool keepTmp = false;
|
|
|
|
int fileNr = 0;
|
|
|
|
|
|
|
|
for (auto & var : drv.env)
|
|
|
|
if (passAsFile.count(var.first)) {
|
|
|
|
keepTmp = true;
|
|
|
|
string fn = ".attr-" + std::to_string(fileNr++);
|
|
|
|
Path p = (Path) tmpDir + "/" + fn;
|
|
|
|
writeFile(p, var.second);
|
|
|
|
env[var.first + "Path"] = p;
|
|
|
|
} else
|
|
|
|
env[var.first] = var.second;
|
|
|
|
|
2019-02-05 09:49:19 +00:00
|
|
|
restoreAffinity();
|
|
|
|
|
2017-07-25 13:09:06 +00:00
|
|
|
/* Run a shell using the derivation's environment. For
|
|
|
|
convenience, source $stdenv/setup to setup additional
|
|
|
|
environment variables and shell functions. Also don't
|
|
|
|
lose the current $PATH directories. */
|
|
|
|
auto rcfile = (Path) tmpDir + "/rc";
|
|
|
|
writeFile(rcfile, fmt(
|
|
|
|
(keepTmp ? "" : "rm -rf '%1%'; "s) +
|
|
|
|
"[ -n \"$PS1\" ] && [ -e ~/.bashrc ] && source ~/.bashrc; "
|
|
|
|
"%2%"
|
|
|
|
"dontAddDisableDepTrack=1; "
|
|
|
|
"[ -e $stdenv/setup ] && source $stdenv/setup; "
|
|
|
|
"%3%"
|
2018-09-27 16:40:19 +00:00
|
|
|
"PATH=\"%4%:$PATH\"; "
|
|
|
|
"SHELL=%5%; "
|
2017-07-25 13:09:06 +00:00
|
|
|
"set +e; "
|
|
|
|
R"s([ -n "$PS1" ] && PS1='\n\[\033[1;32m\][nix-shell:\w]\$\[\033[0m\] '; )s"
|
|
|
|
"if [ \"$(type -t runHook)\" = function ]; then runHook shellHook; fi; "
|
|
|
|
"unset NIX_ENFORCE_PURITY; "
|
|
|
|
"shopt -u nullglob; "
|
2018-09-27 16:40:19 +00:00
|
|
|
"unset TZ; %6%"
|
|
|
|
"%7%",
|
2017-07-25 13:09:06 +00:00
|
|
|
(Path) tmpDir,
|
|
|
|
(pure ? "" : "p=$PATH; "),
|
|
|
|
(pure ? "" : "PATH=$PATH:$p; unset p; "),
|
2018-09-27 16:40:19 +00:00
|
|
|
dirOf(shell),
|
|
|
|
shell,
|
2017-07-25 13:09:06 +00:00
|
|
|
(getenv("TZ") ? (string("export TZ='") + getenv("TZ") + "'; ") : ""),
|
|
|
|
envCommand));
|
|
|
|
|
|
|
|
Strings envStrs;
|
|
|
|
for (auto & i : env)
|
|
|
|
envStrs.push_back(i.first + "=" + i.second);
|
|
|
|
|
|
|
|
auto args = interactive
|
|
|
|
? Strings{"bash", "--rcfile", rcfile}
|
|
|
|
: Strings{"bash", rcfile};
|
|
|
|
|
|
|
|
auto envPtrs = stringsToCharPtrs(envStrs);
|
|
|
|
|
|
|
|
environ = envPtrs.data();
|
|
|
|
|
|
|
|
auto argPtrs = stringsToCharPtrs(args);
|
|
|
|
|
|
|
|
restoreSignals();
|
|
|
|
|
|
|
|
execvp(shell.c_str(), argPtrs.data());
|
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
throw SysError("executing shell '%s'", shell);
|
2017-07-25 13:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
PathSet pathsToBuild;
|
|
|
|
|
|
|
|
std::map<Path, Path> drvPrefixes;
|
|
|
|
std::map<Path, Path> resultSymlinks;
|
|
|
|
std::vector<Path> outPaths;
|
|
|
|
|
|
|
|
for (auto & drvInfo : drvs) {
|
|
|
|
auto drvPath = drvInfo.queryDrvPath();
|
2017-07-28 13:03:59 +00:00
|
|
|
auto outPath = drvInfo.queryOutPath();
|
2017-07-25 13:09:06 +00:00
|
|
|
|
|
|
|
auto outputName = drvInfo.queryOutputName();
|
|
|
|
if (outputName == "")
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error("derivation '%s' lacks an 'outputName' attribute", drvPath);
|
2017-07-25 13:09:06 +00:00
|
|
|
|
2017-07-28 13:03:59 +00:00
|
|
|
pathsToBuild.insert(drvPath + "!" + outputName);
|
2017-07-25 13:09:06 +00:00
|
|
|
|
|
|
|
std::string drvPrefix;
|
|
|
|
auto i = drvPrefixes.find(drvPath);
|
|
|
|
if (i != drvPrefixes.end())
|
|
|
|
drvPrefix = i->second;
|
|
|
|
else {
|
|
|
|
drvPrefix = outLink;
|
|
|
|
if (drvPrefixes.size())
|
|
|
|
drvPrefix += fmt("-%d", drvPrefixes.size() + 1);
|
|
|
|
drvPrefixes[drvPath] = drvPrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string symlink = drvPrefix;
|
|
|
|
if (outputName != "out") symlink += "-" + outputName;
|
|
|
|
|
2017-07-28 13:03:59 +00:00
|
|
|
resultSymlinks[symlink] = outPath;
|
|
|
|
outPaths.push_back(outPath);
|
2017-07-25 13:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buildPaths(pathsToBuild);
|
|
|
|
|
|
|
|
if (dryRun) return;
|
|
|
|
|
|
|
|
for (auto & symlink : resultSymlinks)
|
|
|
|
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>())
|
|
|
|
store2->addPermRoot(symlink.second, absPath(symlink.first), true);
|
|
|
|
|
|
|
|
for (auto & path : outPaths)
|
|
|
|
std::cout << path << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 09:35:46 +00:00
|
|
|
static RegisterLegacyCommand s1("nix-build", _main);
|
|
|
|
static RegisterLegacyCommand s2("nix-shell", _main);
|