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
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
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"
|
|
|
|
|
#include "get-drvs.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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 14:21:47 +00:00
|
|
|
|
static void maybePrintExecError(ExecError & e)
|
|
|
|
|
{
|
|
|
|
|
if (WIFEXITED(e.status))
|
|
|
|
|
throw Exit(WEXITSTATUS(e.status));
|
|
|
|
|
else
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-09 10:24:19 +00:00
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
|
{
|
|
|
|
|
return handleExceptions(argv[0], [&]() {
|
|
|
|
|
initNix();
|
2017-07-20 11:32:01 +00:00
|
|
|
|
initGC();
|
|
|
|
|
|
2016-08-09 10:24:19 +00:00
|
|
|
|
auto store = openStore();
|
|
|
|
|
auto dryRun = false;
|
|
|
|
|
auto verbose = false;
|
|
|
|
|
auto runEnv = std::regex_search(argv[0], std::regex("nix-shell$"));
|
|
|
|
|
auto pure = false;
|
|
|
|
|
auto fromArgs = false;
|
|
|
|
|
auto packages = false;
|
2017-01-07 17:08:28 +00:00
|
|
|
|
// Same condition as bash uses for interactive shells
|
|
|
|
|
auto interactive = isatty(STDIN_FILENO) && isatty(STDERR_FILENO);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
|
2016-09-12 10:06:13 +00:00
|
|
|
|
Strings instArgs;
|
|
|
|
|
Strings buildArgs;
|
|
|
|
|
Strings exprs;
|
2017-07-20 11:32:01 +00:00
|
|
|
|
Strings searchPath;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
|
|
|
|
|
auto shell = getEnv("SHELL", "/bin/sh");
|
2016-09-12 10:06:13 +00:00
|
|
|
|
std::string envCommand; // interactive shell
|
|
|
|
|
Strings envExclude;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
|
|
|
|
|
auto myName = runEnv ? "nix-shell" : "nix-build";
|
|
|
|
|
|
|
|
|
|
auto inShebang = false;
|
2016-09-12 10:06:13 +00:00
|
|
|
|
std::string script;
|
|
|
|
|
std::vector<string> savedArgs;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
|
2016-09-12 10:06:13 +00:00
|
|
|
|
AutoDelete tmpDir(createTempDir("", myName));
|
2016-08-09 10:24:19 +00:00
|
|
|
|
|
2016-09-12 10:06:13 +00:00
|
|
|
|
std::string outLink = "./result";
|
2016-08-09 10:24:19 +00:00
|
|
|
|
auto drvLink = (Path) tmpDir + "/derivation";
|
|
|
|
|
|
2016-09-12 10:06:13 +00:00
|
|
|
|
std::vector<string> args;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
|
args.push_back(argv[i]);
|
2017-01-03 10:40:51 +00:00
|
|
|
|
|
2016-08-09 10:24:19 +00:00
|
|
|
|
// Heuristic to see if we're invoked as a shebang script, namely, if we
|
|
|
|
|
// have a single argument, it's the name of an executable file, and it
|
|
|
|
|
// starts with "#!".
|
|
|
|
|
if (runEnv && argc > 1 && !std::regex_search(argv[1], std::regex("nix-shell"))) {
|
|
|
|
|
script = argv[1];
|
|
|
|
|
if (access(script.c_str(), F_OK) == 0 && access(script.c_str(), X_OK) == 0) {
|
2016-08-31 14:08:00 +00:00
|
|
|
|
auto lines = tokenizeString<Strings>(readFile(script), "\n");
|
|
|
|
|
if (std::regex_search(lines.front(), std::regex("^#!"))) {
|
|
|
|
|
lines.pop_front();
|
2016-08-09 10:24:19 +00:00
|
|
|
|
inShebang = true;
|
2017-01-03 10:40:51 +00:00
|
|
|
|
for (int i = 2; i < argc; ++i)
|
2016-08-09 10:24:19 +00:00
|
|
|
|
savedArgs.push_back(argv[i]);
|
2017-01-01 16:42:06 +00:00
|
|
|
|
args.clear();
|
2016-08-31 14:08:00 +00:00
|
|
|
|
for (auto line : lines) {
|
2016-08-09 10:24:19 +00:00
|
|
|
|
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-31 14:08:00 +00:00
|
|
|
|
for (size_t n = 0; n < args.size(); ++n) {
|
2016-08-09 10:24:19 +00:00
|
|
|
|
auto arg = args[n];
|
|
|
|
|
|
|
|
|
|
if (arg == "--help") {
|
|
|
|
|
deletePath(tmpDir);
|
2017-01-16 21:06:05 +00:00
|
|
|
|
showManPage(myName);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-16 21:06:05 +00:00
|
|
|
|
else if (arg == "--version")
|
|
|
|
|
printVersion(myName);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
|
|
|
|
|
else if (arg == "--add-drv-link") {
|
|
|
|
|
drvLink = "./derivation";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--no-out-link" || arg == "--no-link") {
|
|
|
|
|
outLink = (Path) tmpDir + "/result";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--drv-link") {
|
|
|
|
|
n++;
|
|
|
|
|
if (n >= args.size()) {
|
|
|
|
|
throw UsageError("--drv-link requires an argument");
|
|
|
|
|
}
|
|
|
|
|
drvLink = args[n];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--out-link" || arg == "-o") {
|
|
|
|
|
n++;
|
|
|
|
|
if (n >= args.size()) {
|
|
|
|
|
throw UsageError(format("%1% requires an argument") % arg);
|
|
|
|
|
}
|
|
|
|
|
outLink = args[n];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--attr" || arg == "-A" || arg == "-I") {
|
|
|
|
|
n++;
|
|
|
|
|
if (n >= args.size()) {
|
|
|
|
|
throw UsageError(format("%1% requires an argument") % arg);
|
|
|
|
|
}
|
|
|
|
|
instArgs.push_back(arg);
|
|
|
|
|
instArgs.push_back(args[n]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--arg" || arg == "--argstr") {
|
|
|
|
|
if (n + 2 >= args.size()) {
|
|
|
|
|
throw UsageError(format("%1% requires two arguments") % arg);
|
|
|
|
|
}
|
|
|
|
|
instArgs.push_back(arg);
|
|
|
|
|
instArgs.push_back(args[n + 1]);
|
|
|
|
|
instArgs.push_back(args[n + 2]);
|
|
|
|
|
n += 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--option") {
|
|
|
|
|
if (n + 2 >= args.size()) {
|
|
|
|
|
throw UsageError(format("%1% requires two arguments") % arg);
|
|
|
|
|
}
|
|
|
|
|
instArgs.push_back(arg);
|
|
|
|
|
instArgs.push_back(args[n + 1]);
|
|
|
|
|
instArgs.push_back(args[n + 2]);
|
|
|
|
|
buildArgs.push_back(arg);
|
|
|
|
|
buildArgs.push_back(args[n + 1]);
|
|
|
|
|
buildArgs.push_back(args[n + 2]);
|
2017-05-24 09:33:42 +00:00
|
|
|
|
settings.set(args[n + 1], args[n + 2]);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
n += 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--max-jobs" || arg == "-j" || arg == "--max-silent-time" || arg == "--cores" || arg == "--timeout" || arg == "--add-root") {
|
|
|
|
|
n++;
|
|
|
|
|
if (n >= args.size()) {
|
|
|
|
|
throw UsageError(format("%1% requires an argument") % arg);
|
|
|
|
|
}
|
|
|
|
|
buildArgs.push_back(arg);
|
|
|
|
|
buildArgs.push_back(args[n]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--dry-run") {
|
|
|
|
|
buildArgs.push_back("--dry-run");
|
|
|
|
|
dryRun = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--show-trace") {
|
|
|
|
|
instArgs.push_back(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "-") {
|
|
|
|
|
exprs = Strings{"-"};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--verbose" || (arg.size() >= 2 && arg.substr(0, 2) == "-v")) {
|
|
|
|
|
buildArgs.push_back(arg);
|
|
|
|
|
instArgs.push_back(arg);
|
|
|
|
|
verbose = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--quiet" || arg == "--repair") {
|
|
|
|
|
buildArgs.push_back(arg);
|
|
|
|
|
instArgs.push_back(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--check") {
|
|
|
|
|
buildArgs.push_back(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--run-env") { // obsolete
|
|
|
|
|
runEnv = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--command" || arg == "--run") {
|
|
|
|
|
n++;
|
|
|
|
|
if (n >= args.size()) {
|
|
|
|
|
throw UsageError(format("%1% requires an argument") % arg);
|
|
|
|
|
}
|
|
|
|
|
envCommand = args[n] + "\nexit";
|
|
|
|
|
if (arg == "--run")
|
|
|
|
|
interactive = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--exclude") {
|
|
|
|
|
n++;
|
|
|
|
|
if (n >= args.size()) {
|
|
|
|
|
throw UsageError(format("%1% requires an argument") % arg);
|
|
|
|
|
}
|
|
|
|
|
envExclude.push_back(args[n]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--pure") { pure = true; }
|
|
|
|
|
else if (arg == "--impure") { pure = false; }
|
|
|
|
|
|
|
|
|
|
else if (arg == "--expr" || arg == "-E") {
|
|
|
|
|
fromArgs = true;
|
|
|
|
|
instArgs.push_back("--expr");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "--packages" || arg == "-p") {
|
|
|
|
|
packages = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (inShebang && arg == "-i") {
|
|
|
|
|
n++;
|
|
|
|
|
if (n >= args.size()) {
|
|
|
|
|
throw UsageError(format("%1% requires an argument") % arg);
|
|
|
|
|
}
|
2017-01-03 07:59:09 +00:00
|
|
|
|
interactive = false;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
auto interpreter = args[n];
|
|
|
|
|
auto execArgs = "";
|
|
|
|
|
|
|
|
|
|
auto shellEscape = [](const string & s) {
|
|
|
|
|
return "'" + std::regex_replace(s, std::regex("'"), "'\\''") + "'";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Ü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".
|
2017-01-03 10:40:51 +00:00
|
|
|
|
if (std::regex_search(interpreter, std::regex("perl")))
|
|
|
|
|
execArgs = "-a PERL";
|
2016-08-09 10:24:19 +00:00
|
|
|
|
|
2016-09-12 10:06:13 +00:00
|
|
|
|
std::ostringstream joined;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
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.
|
|
|
|
|
envCommand = (format("exec %1% %2% -e 'load(\"%3%\") -- %4%") % execArgs % interpreter % script % joined.str()).str();
|
|
|
|
|
} else {
|
|
|
|
|
envCommand = (format("exec %1% %2% %3% %4%") % execArgs % interpreter % script % joined.str()).str();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (!arg.empty() && arg[0] == '-') {
|
|
|
|
|
buildArgs.push_back(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (arg == "-Q" || arg == "--no-build-output") {
|
|
|
|
|
buildArgs.push_back(arg);
|
|
|
|
|
instArgs.push_back(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
exprs.push_back(arg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 11:32:01 +00:00
|
|
|
|
EvalState state(searchPath, store);
|
|
|
|
|
|
2016-08-09 10:24:19 +00:00
|
|
|
|
if (packages && fromArgs) {
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw UsageError("‘-p’ and ‘-E’ are mutually exclusive");
|
2016-08-09 10:24:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (packages) {
|
|
|
|
|
instArgs.push_back("--expr");
|
2016-09-12 10:06:13 +00:00
|
|
|
|
std::ostringstream joined;
|
2017-04-24 10:04:01 +00:00
|
|
|
|
joined << "with import <nixpkgs> { }; (pkgs.runCommandCC or pkgs.runCommand) \"shell\" { buildInputs = [ ";
|
2016-08-09 10:24:19 +00:00
|
|
|
|
for (const auto & i : exprs)
|
|
|
|
|
joined << '(' << i << ") ";
|
|
|
|
|
joined << "]; } \"\"";
|
|
|
|
|
exprs = Strings{joined.str()};
|
|
|
|
|
} else if (!fromArgs) {
|
|
|
|
|
if (exprs.empty() && runEnv && access("shell.nix", F_OK) == 0)
|
|
|
|
|
exprs.push_back("shell.nix");
|
|
|
|
|
if (exprs.empty())
|
|
|
|
|
exprs.push_back("default.nix");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (runEnv)
|
2016-08-11 15:09:04 +00:00
|
|
|
|
setenv("IN_NIX_SHELL", pure ? "pure" : "impure", 1);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
|
|
|
|
|
for (auto & expr : exprs) {
|
|
|
|
|
// Instantiate.
|
2016-09-12 10:06:13 +00:00
|
|
|
|
std::vector<string> drvPaths;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
if (!std::regex_match(expr, std::regex("^/.*\\.drv$"))) {
|
|
|
|
|
// If we're in a #! script, interpret filenames relative to the
|
|
|
|
|
// script.
|
|
|
|
|
if (inShebang && !packages)
|
|
|
|
|
expr = absPath(expr, dirOf(script));
|
|
|
|
|
|
2016-09-12 10:06:13 +00:00
|
|
|
|
Strings instantiateArgs{"--add-root", drvLink, "--indirect"};
|
2016-08-09 10:24:19 +00:00
|
|
|
|
for (const auto & arg : instArgs)
|
|
|
|
|
instantiateArgs.push_back(arg);
|
|
|
|
|
instantiateArgs.push_back(expr);
|
2016-09-21 14:21:47 +00:00
|
|
|
|
try {
|
|
|
|
|
auto instOutput = runProgram(settings.nixBinDir + "/nix-instantiate", false, instantiateArgs);
|
|
|
|
|
drvPaths = tokenizeString<std::vector<string>>(instOutput);
|
|
|
|
|
} catch (ExecError & e) {
|
|
|
|
|
maybePrintExecError(e);
|
|
|
|
|
}
|
2016-08-09 10:24:19 +00:00
|
|
|
|
} else {
|
|
|
|
|
drvPaths.push_back(expr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (runEnv) {
|
|
|
|
|
if (drvPaths.size() != 1)
|
|
|
|
|
throw UsageError("a single derivation is required");
|
|
|
|
|
auto drvPath = drvPaths[0];
|
|
|
|
|
drvPath = drvPath.substr(0, drvPath.find_first_of('!'));
|
|
|
|
|
if (isLink(drvPath))
|
|
|
|
|
drvPath = readLink(drvPath);
|
|
|
|
|
auto drv = store->derivationFromPath(drvPath);
|
|
|
|
|
|
|
|
|
|
// Build or fetch all dependencies of the derivation.
|
2016-09-12 10:06:13 +00:00
|
|
|
|
Strings nixStoreArgs{"-r", "--no-output", "--no-gc-warning"};
|
2016-08-09 10:24:19 +00:00
|
|
|
|
for (const auto & arg : buildArgs)
|
|
|
|
|
nixStoreArgs.push_back(arg);
|
|
|
|
|
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)); }))
|
|
|
|
|
nixStoreArgs.push_back(input.first);
|
|
|
|
|
for (const auto & src : drv.inputSrcs)
|
|
|
|
|
nixStoreArgs.push_back(src);
|
2016-09-21 14:21:47 +00:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
runProgram(settings.nixBinDir + "/nix-store", false, nixStoreArgs);
|
|
|
|
|
} catch (ExecError & e) {
|
|
|
|
|
maybePrintExecError(e);
|
|
|
|
|
}
|
2016-08-09 10:24:19 +00:00
|
|
|
|
|
2017-07-03 09:54:30 +00:00
|
|
|
|
if (dryRun) return;
|
|
|
|
|
|
2016-08-09 10:24:19 +00:00
|
|
|
|
// Set the environment.
|
2016-09-20 13:39:08 +00:00
|
|
|
|
auto env = getEnv();
|
|
|
|
|
|
2016-08-09 10:24:19 +00:00
|
|
|
|
auto tmp = getEnv("TMPDIR", getEnv("XDG_RUNTIME_DIR", "/tmp"));
|
2016-09-20 13:39:08 +00:00
|
|
|
|
|
2016-08-09 10:24:19 +00:00
|
|
|
|
if (pure) {
|
2017-03-12 00:04:21 +00:00
|
|
|
|
std::set<string> keepVars{"HOME", "USER", "LOGNAME", "DISPLAY", "PATH", "TERM", "IN_NIX_SHELL", "TZ", "PAGER", "NIX_BUILD_SHELL", "SHLVL"};
|
2016-09-20 13:39:08 +00:00
|
|
|
|
decltype(env) newEnv;
|
|
|
|
|
for (auto & i : env)
|
|
|
|
|
if (keepVars.count(i.first))
|
|
|
|
|
newEnv.emplace(i);
|
|
|
|
|
env = newEnv;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
// NixOS hack: prevent /etc/bashrc from sourcing /etc/profile.
|
2016-09-20 13:39:08 +00:00
|
|
|
|
env["__ETC_PROFILE_SOURCED"] = "1";
|
2016-08-09 10:24:19 +00:00
|
|
|
|
}
|
2016-09-20 13:39:08 +00:00
|
|
|
|
|
|
|
|
|
env["NIX_BUILD_TOP"] = env["TMPDIR"] = env["TEMPDIR"] = env["TMP"] = env["TEMP"] = tmp;
|
|
|
|
|
env["NIX_STORE"] = store->storeDir;
|
2017-05-24 09:33:42 +00:00
|
|
|
|
env["NIX_BUILD_CORES"] = std::to_string(settings.buildCores);
|
2016-09-20 13:39:08 +00:00
|
|
|
|
|
2017-05-03 13:01:15 +00:00
|
|
|
|
auto passAsFile = tokenizeString<StringSet>(get(drv.env, "passAsFile", ""));
|
|
|
|
|
|
|
|
|
|
bool keepTmp = false;
|
|
|
|
|
int fileNr = 0;
|
|
|
|
|
|
2016-09-20 13:39:08 +00:00
|
|
|
|
for (auto & var : drv.env)
|
2017-05-03 13:01:15 +00:00
|
|
|
|
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;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
|
2016-09-20 13:10:51 +00:00
|
|
|
|
restoreAffinity();
|
|
|
|
|
|
2016-08-09 10:24:19 +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";
|
2017-01-03 10:40:51 +00:00
|
|
|
|
writeFile(rcfile, fmt(
|
2017-05-03 13:01:15 +00:00
|
|
|
|
(keepTmp ? "" : "rm -rf '%1%'; "s) +
|
2016-08-09 10:24:19 +00:00
|
|
|
|
"[ -n \"$PS1\" ] && [ -e ~/.bashrc ] && source ~/.bashrc; "
|
|
|
|
|
"%2%"
|
|
|
|
|
"dontAddDisableDepTrack=1; "
|
|
|
|
|
"[ -e $stdenv/setup ] && source $stdenv/setup; "
|
|
|
|
|
"%3%"
|
|
|
|
|
"set +e; "
|
2017-05-24 09:21:38 +00:00
|
|
|
|
R"s([ -n "$PS1" ] && PS1='\n\[\033[1;32m\][nix-shell:\w]\$\[\033[0m\] '; )s"
|
2016-08-09 10:24:19 +00:00
|
|
|
|
"if [ \"$(type -t runHook)\" = function ]; then runHook shellHook; fi; "
|
|
|
|
|
"unset NIX_ENFORCE_PURITY; "
|
|
|
|
|
"unset NIX_INDENT_MAKE; "
|
|
|
|
|
"shopt -u nullglob; "
|
|
|
|
|
"unset TZ; %4%"
|
2017-01-03 10:40:51 +00:00
|
|
|
|
"%5%",
|
|
|
|
|
(Path) tmpDir,
|
|
|
|
|
(pure ? "" : "p=$PATH; "),
|
|
|
|
|
(pure ? "" : "PATH=$PATH:$p; unset p; "),
|
|
|
|
|
(getenv("TZ") ? (string("export TZ='") + getenv("TZ") + "'; ") : ""),
|
|
|
|
|
envCommand));
|
2016-09-20 13:39:08 +00:00
|
|
|
|
|
|
|
|
|
Strings envStrs;
|
|
|
|
|
for (auto & i : env)
|
|
|
|
|
envStrs.push_back(i.first + "=" + i.second);
|
|
|
|
|
|
|
|
|
|
auto args = interactive
|
|
|
|
|
? Strings{"bash", "--rcfile", rcfile}
|
|
|
|
|
: Strings{"bash", rcfile};
|
|
|
|
|
|
2017-01-06 19:30:14 +00:00
|
|
|
|
auto envPtrs = stringsToCharPtrs(envStrs);
|
2016-10-19 13:21:18 +00:00
|
|
|
|
|
2017-07-20 11:32:01 +00:00
|
|
|
|
auto shell = getEnv("NIX_BUILD_SHELL", "");
|
|
|
|
|
|
|
|
|
|
if (shell == "") {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
auto expr = state.parseExprFromString("(import <nixpkgs> {}).bashInteractive", absPath("."));
|
|
|
|
|
|
|
|
|
|
Value v;
|
|
|
|
|
state.eval(expr, v);
|
|
|
|
|
|
|
|
|
|
auto drv = getDerivation(state, v, false);
|
|
|
|
|
if (!drv)
|
|
|
|
|
throw Error("the ‘bashInteractive’ attribute in <nixpkgs> did not evaluate to a derivation");
|
|
|
|
|
|
|
|
|
|
auto drvPath = drv->queryDrvPath();
|
|
|
|
|
|
|
|
|
|
unsigned long long downloadSize, narSize;
|
|
|
|
|
PathSet willBuild, willSubstitute, unknown;
|
|
|
|
|
store->queryMissing({drvPath},
|
|
|
|
|
willBuild, willSubstitute, unknown, downloadSize, narSize);
|
|
|
|
|
|
|
|
|
|
if (settings.printMissing)
|
|
|
|
|
printMissing(ref<Store>(store), willBuild, willSubstitute, unknown, downloadSize, narSize);
|
|
|
|
|
|
|
|
|
|
store->buildPaths({drvPath});
|
|
|
|
|
|
|
|
|
|
shell = drv->queryOutPath() + "/bin/bash";
|
|
|
|
|
if (!pathExists(shell))
|
|
|
|
|
throw Error("expected shell ‘%s’ to exist, but it doesn't", shell);
|
|
|
|
|
|
|
|
|
|
} catch (Error & e) {
|
|
|
|
|
printError("warning: %s; will use bash from your environment", e.what());
|
|
|
|
|
shell = "bash";
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-24 16:25:00 +00:00
|
|
|
|
|
2017-01-06 19:30:14 +00:00
|
|
|
|
environ = envPtrs.data();
|
|
|
|
|
|
|
|
|
|
auto argPtrs = stringsToCharPtrs(args);
|
|
|
|
|
|
2017-02-01 12:00:21 +00:00
|
|
|
|
restoreSignals();
|
|
|
|
|
|
2017-02-24 16:25:00 +00:00
|
|
|
|
execvp(shell.c_str(), argPtrs.data());
|
2016-09-20 13:39:08 +00:00
|
|
|
|
|
2017-02-24 16:25:00 +00:00
|
|
|
|
throw SysError("executing shell ‘%s’", shell);
|
2016-08-09 10:24:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ugly hackery to make "nix-build -A foo.all" produce symlinks
|
|
|
|
|
// ./result, ./result-dev, and so on, rather than ./result,
|
|
|
|
|
// ./result-2-dev, and so on. This combines multiple derivation
|
|
|
|
|
// paths into one "/nix/store/drv-path!out1,out2,..." argument.
|
2016-09-12 10:06:13 +00:00
|
|
|
|
std::string prevDrvPath;
|
|
|
|
|
Strings drvPaths2;
|
2016-08-09 10:24:19 +00:00
|
|
|
|
for (const auto & drvPath : drvPaths) {
|
|
|
|
|
auto p = drvPath;
|
2016-09-12 10:06:13 +00:00
|
|
|
|
std::string output = "out";
|
2016-08-09 10:24:19 +00:00
|
|
|
|
std::smatch match;
|
|
|
|
|
if (std::regex_match(drvPath, match, std::regex("(.*)!(.*)"))) {
|
|
|
|
|
p = match[1].str();
|
|
|
|
|
output = match[2].str();
|
|
|
|
|
}
|
|
|
|
|
auto target = readLink(p);
|
|
|
|
|
if (verbose)
|
|
|
|
|
std::cerr << "derivation is " << target << '\n';
|
|
|
|
|
if (target == prevDrvPath) {
|
|
|
|
|
auto last = drvPaths2.back();
|
|
|
|
|
drvPaths2.pop_back();
|
|
|
|
|
drvPaths2.push_back(last + "," + output);
|
|
|
|
|
} else {
|
|
|
|
|
drvPaths2.push_back(target + "!" + output);
|
|
|
|
|
prevDrvPath = target;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Build.
|
2016-09-12 10:06:13 +00:00
|
|
|
|
Strings outPaths;
|
|
|
|
|
Strings nixStoreArgs{"--add-root", outLink, "--indirect", "-r"};
|
2016-08-09 10:24:19 +00:00
|
|
|
|
for (const auto & arg : buildArgs)
|
|
|
|
|
nixStoreArgs.push_back(arg);
|
|
|
|
|
for (const auto & path : drvPaths2)
|
|
|
|
|
nixStoreArgs.push_back(path);
|
2016-09-21 14:21:47 +00:00
|
|
|
|
|
|
|
|
|
std::string nixStoreRes;
|
|
|
|
|
try {
|
|
|
|
|
nixStoreRes = runProgram(settings.nixBinDir + "/nix-store", false, nixStoreArgs);
|
|
|
|
|
} catch (ExecError & e) {
|
|
|
|
|
maybePrintExecError(e);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-12 10:06:13 +00:00
|
|
|
|
for (const auto & outpath : tokenizeString<std::vector<string>>(nixStoreRes))
|
2016-08-09 10:24:19 +00:00
|
|
|
|
outPaths.push_back(chomp(outpath));
|
|
|
|
|
|
|
|
|
|
if (dryRun)
|
|
|
|
|
continue;
|
2016-09-12 10:06:13 +00:00
|
|
|
|
|
|
|
|
|
for (const auto & outPath : outPaths)
|
|
|
|
|
std::cout << readLink(outPath) << '\n';
|
2016-08-09 10:24:19 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|