2020-03-30 20:14:17 +03:00
|
|
|
#include "eval.hh"
|
|
|
|
#include "command.hh"
|
|
|
|
#include "common-args.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "derivations.hh"
|
|
|
|
#include "affinity.hh"
|
|
|
|
#include "progress-bar.hh"
|
|
|
|
|
|
|
|
#include <regex>
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
struct Var
|
|
|
|
{
|
2020-04-30 15:39:26 +03:00
|
|
|
bool exported = true;
|
|
|
|
bool associative = false;
|
2020-03-30 20:14:17 +03:00
|
|
|
std::string value; // quoted string or array
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BuildEnvironment
|
|
|
|
{
|
|
|
|
std::map<std::string, Var> env;
|
|
|
|
std::string bashFunctions;
|
|
|
|
};
|
|
|
|
|
|
|
|
BuildEnvironment readEnvironment(const Path & path)
|
|
|
|
{
|
|
|
|
BuildEnvironment res;
|
|
|
|
|
|
|
|
std::set<std::string> exported;
|
|
|
|
|
|
|
|
debug("reading environment file '%s'", path);
|
|
|
|
|
|
|
|
auto file = readFile(path);
|
|
|
|
|
|
|
|
auto pos = file.cbegin();
|
|
|
|
|
|
|
|
static std::string varNameRegex =
|
|
|
|
R"re((?:[a-zA-Z_][a-zA-Z0-9_]*))re";
|
|
|
|
|
|
|
|
static std::regex declareRegex(
|
|
|
|
"^declare -x (" + varNameRegex + ")" +
|
|
|
|
R"re((?:="((?:[^"\\]|\\.)*)")?\n)re");
|
|
|
|
|
|
|
|
static std::string simpleStringRegex =
|
|
|
|
R"re((?:[a-zA-Z0-9_/:\.\-\+=]*))re";
|
|
|
|
|
|
|
|
static std::string quotedStringRegex =
|
|
|
|
R"re((?:\$?'(?:[^'\\]|\\[abeEfnrtv\\'"?])*'))re";
|
|
|
|
|
2020-04-30 15:39:26 +03:00
|
|
|
static std::string indexedArrayRegex =
|
2020-07-03 15:58:58 +03:00
|
|
|
R"re((?:\(( *\[[0-9]+\]="(?:[^"\\]|\\.)*")*\)))re";
|
2020-03-30 20:14:17 +03:00
|
|
|
|
|
|
|
static std::regex varRegex(
|
2020-04-30 15:39:26 +03:00
|
|
|
"^(" + varNameRegex + ")=(" + simpleStringRegex + "|" + quotedStringRegex + "|" + indexedArrayRegex + ")\n");
|
|
|
|
|
|
|
|
/* Note: we distinguish between an indexed and associative array
|
|
|
|
using the space before the closing parenthesis. Will
|
|
|
|
undoubtedly regret this some day. */
|
|
|
|
static std::regex assocArrayRegex(
|
|
|
|
"^(" + varNameRegex + ")=" + R"re((?:\(( *\[[^\]]+\]="(?:[^"\\]|\\.)*")* *\)))re" + "\n");
|
2020-03-30 20:14:17 +03:00
|
|
|
|
|
|
|
static std::regex functionRegex(
|
|
|
|
"^" + varNameRegex + " \\(\\) *\n");
|
|
|
|
|
|
|
|
while (pos != file.end()) {
|
|
|
|
|
|
|
|
std::smatch match;
|
|
|
|
|
|
|
|
if (std::regex_search(pos, file.cend(), match, declareRegex)) {
|
|
|
|
pos = match[0].second;
|
|
|
|
exported.insert(match[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (std::regex_search(pos, file.cend(), match, varRegex)) {
|
|
|
|
pos = match[0].second;
|
2020-04-30 15:39:26 +03:00
|
|
|
res.env.insert({match[1], Var { .exported = exported.count(match[1]) > 0, .value = match[2] }});
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (std::regex_search(pos, file.cend(), match, assocArrayRegex)) {
|
|
|
|
pos = match[0].second;
|
|
|
|
res.env.insert({match[1], Var { .associative = true, .value = match[2] }});
|
2020-03-30 20:14:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (std::regex_search(pos, file.cend(), match, functionRegex)) {
|
|
|
|
res.bashFunctions = std::string(pos, file.cend());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
else throw Error("shell environment '%s' has unexpected line '%s'",
|
|
|
|
path, file.substr(pos - file.cbegin(), 60));
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-04-30 14:05:29 +03:00
|
|
|
const static std::string getEnvSh =
|
|
|
|
#include "get-env.sh.gen.hh"
|
|
|
|
;
|
|
|
|
|
2020-03-30 20:14:17 +03:00
|
|
|
/* Given an existing derivation, return the shell environment as
|
|
|
|
initialised by stdenv's setup script. We do this by building a
|
|
|
|
modified derivation with the same dependencies and nearly the same
|
|
|
|
initial environment variables, that just writes the resulting
|
|
|
|
environment to a file and exits. */
|
2020-04-30 15:39:26 +03:00
|
|
|
StorePath getDerivationEnvironment(ref<Store> store, const StorePath & drvPath)
|
2020-03-30 20:14:17 +03:00
|
|
|
{
|
2020-04-30 15:39:26 +03:00
|
|
|
auto drv = store->derivationFromPath(drvPath);
|
|
|
|
|
2020-03-30 20:14:17 +03:00
|
|
|
auto builder = baseNameOf(drv.builder);
|
|
|
|
if (builder != "bash")
|
2020-06-04 11:57:40 +03:00
|
|
|
throw Error("'nix develop' only works on derivations that use 'bash' as their builder");
|
2020-03-30 20:14:17 +03:00
|
|
|
|
2020-04-30 14:05:29 +03:00
|
|
|
auto getEnvShPath = store->addTextToStore("get-env.sh", getEnvSh, {});
|
|
|
|
|
|
|
|
drv.args = {store->printStorePath(getEnvShPath)};
|
2020-03-30 20:14:17 +03:00
|
|
|
|
|
|
|
/* Remove derivation checks. */
|
|
|
|
drv.env.erase("allowedReferences");
|
|
|
|
drv.env.erase("allowedRequisites");
|
|
|
|
drv.env.erase("disallowedReferences");
|
|
|
|
drv.env.erase("disallowedRequisites");
|
|
|
|
|
|
|
|
/* Rehash and write the derivation. FIXME: would be nice to use
|
|
|
|
'buildDerivation', but that's privileged. */
|
2020-04-30 15:39:26 +03:00
|
|
|
auto drvName = std::string(drvPath.name());
|
|
|
|
assert(hasSuffix(drvName, ".drv"));
|
|
|
|
drvName.resize(drvName.size() - 4);
|
|
|
|
drvName += "-env";
|
2020-03-30 20:14:17 +03:00
|
|
|
for (auto & output : drv.outputs)
|
|
|
|
drv.env.erase(output.first);
|
|
|
|
drv.env["out"] = "";
|
|
|
|
drv.env["outputs"] = "out";
|
2020-04-30 14:05:29 +03:00
|
|
|
drv.inputSrcs.insert(std::move(getEnvShPath));
|
2020-03-30 20:14:17 +03:00
|
|
|
Hash h = hashDerivationModulo(*store, drv, true);
|
|
|
|
auto shellOutPath = store->makeOutputPath("out", h, drvName);
|
2020-07-09 02:11:39 +03:00
|
|
|
drv.outputs.insert_or_assign("out", DerivationOutput { .output = DerivationOutputIntensional {
|
|
|
|
.path = shellOutPath
|
|
|
|
} });
|
2020-03-30 20:14:17 +03:00
|
|
|
drv.env["out"] = store->printStorePath(shellOutPath);
|
|
|
|
auto shellDrvPath2 = writeDerivation(store, drv, drvName);
|
|
|
|
|
|
|
|
/* Build the derivation. */
|
2020-06-16 23:20:18 +03:00
|
|
|
store->buildPaths({{shellDrvPath2}});
|
2020-03-30 20:14:17 +03:00
|
|
|
|
|
|
|
assert(store->isValidPath(shellOutPath));
|
|
|
|
|
|
|
|
return shellOutPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Common : InstallableCommand, MixProfile
|
|
|
|
{
|
|
|
|
std::set<string> ignoreVars{
|
|
|
|
"BASHOPTS",
|
|
|
|
"EUID",
|
|
|
|
"HOME", // FIXME: don't ignore in pure mode?
|
|
|
|
"NIX_BUILD_TOP",
|
|
|
|
"NIX_ENFORCE_PURITY",
|
|
|
|
"NIX_LOG_FD",
|
|
|
|
"PPID",
|
|
|
|
"PWD",
|
|
|
|
"SHELLOPTS",
|
|
|
|
"SHLVL",
|
|
|
|
"SSL_CERT_FILE", // FIXME: only want to ignore /no-cert-file.crt
|
|
|
|
"TEMP",
|
|
|
|
"TEMPDIR",
|
|
|
|
"TERM",
|
|
|
|
"TMP",
|
|
|
|
"TMPDIR",
|
|
|
|
"TZ",
|
|
|
|
"UID",
|
|
|
|
};
|
|
|
|
|
|
|
|
void makeRcScript(const BuildEnvironment & buildEnvironment, std::ostream & out)
|
|
|
|
{
|
2020-04-30 15:46:51 +03:00
|
|
|
out << "unset shellHook\n";
|
|
|
|
|
2020-03-30 20:14:17 +03:00
|
|
|
out << "nix_saved_PATH=\"$PATH\"\n";
|
|
|
|
|
|
|
|
for (auto & i : buildEnvironment.env) {
|
|
|
|
if (!ignoreVars.count(i.first) && !hasPrefix(i.first, "BASH_")) {
|
2020-04-30 15:39:26 +03:00
|
|
|
if (i.second.associative)
|
|
|
|
out << fmt("declare -A %s=(%s)\n", i.first, i.second.value);
|
|
|
|
else {
|
|
|
|
out << fmt("%s=%s\n", i.first, i.second.value);
|
|
|
|
if (i.second.exported)
|
|
|
|
out << fmt("export %s\n", i.first);
|
|
|
|
}
|
2020-03-30 20:14:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out << "PATH=\"$PATH:$nix_saved_PATH\"\n";
|
|
|
|
|
|
|
|
out << buildEnvironment.bashFunctions << "\n";
|
|
|
|
|
|
|
|
// FIXME: set outputs
|
|
|
|
|
|
|
|
out << "export NIX_BUILD_TOP=\"$(mktemp -d --tmpdir nix-shell.XXXXXX)\"\n";
|
|
|
|
for (auto & i : {"TMP", "TMPDIR", "TEMP", "TEMPDIR"})
|
|
|
|
out << fmt("export %s=\"$NIX_BUILD_TOP\"\n", i);
|
|
|
|
|
|
|
|
out << "eval \"$shellHook\"\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
StorePath getShellOutPath(ref<Store> store)
|
|
|
|
{
|
|
|
|
auto path = installable->getStorePath();
|
|
|
|
if (path && hasSuffix(path->to_string(), "-env"))
|
2020-06-16 23:20:18 +03:00
|
|
|
return *path;
|
2020-03-30 20:14:17 +03:00
|
|
|
else {
|
|
|
|
auto drvs = toDerivations(store, {installable});
|
|
|
|
|
|
|
|
if (drvs.size() != 1)
|
|
|
|
throw Error("'%s' needs to evaluate to a single derivation, but it evaluated to %d derivations",
|
|
|
|
installable->what(), drvs.size());
|
|
|
|
|
|
|
|
auto & drvPath = *drvs.begin();
|
|
|
|
|
2020-04-30 15:39:26 +03:00
|
|
|
return getDerivationEnvironment(store, drvPath);
|
2020-03-30 20:14:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 20:12:54 +03:00
|
|
|
std::pair<BuildEnvironment, std::string> getBuildEnvironment(ref<Store> store)
|
2020-03-30 20:14:17 +03:00
|
|
|
{
|
|
|
|
auto shellOutPath = getShellOutPath(store);
|
|
|
|
|
2020-04-27 20:12:54 +03:00
|
|
|
auto strPath = store->printStorePath(shellOutPath);
|
|
|
|
|
2020-03-30 20:14:17 +03:00
|
|
|
updateProfile(shellOutPath);
|
|
|
|
|
2020-04-27 20:12:54 +03:00
|
|
|
return {readEnvironment(strPath), strPath};
|
2020-03-30 20:14:17 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-04 11:57:40 +03:00
|
|
|
struct CmdDevelop : Common, MixEnvironment
|
2020-03-30 20:14:17 +03:00
|
|
|
{
|
|
|
|
std::vector<std::string> command;
|
|
|
|
|
2020-06-04 11:57:40 +03:00
|
|
|
CmdDevelop()
|
2020-03-30 20:14:17 +03:00
|
|
|
{
|
2020-05-04 23:40:19 +03:00
|
|
|
addFlag({
|
|
|
|
.longName = "command",
|
|
|
|
.shortName = 'c',
|
|
|
|
.description = "command and arguments to be executed insted of an interactive shell",
|
|
|
|
.labels = {"command", "args"},
|
|
|
|
.handler = {[&](std::vector<std::string> ss) {
|
2020-03-30 20:14:17 +03:00
|
|
|
if (ss.empty()) throw UsageError("--command requires at least one argument");
|
|
|
|
command = ss;
|
2020-05-04 23:40:19 +03:00
|
|
|
}}
|
|
|
|
});
|
2020-03-30 20:14:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "run a bash shell that provides the build environment of a derivation";
|
|
|
|
}
|
|
|
|
|
|
|
|
Examples examples() override
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
Example{
|
|
|
|
"To get the build environment of GNU hello:",
|
2020-06-04 11:57:40 +03:00
|
|
|
"nix develop nixpkgs.hello"
|
2020-03-30 20:14:17 +03:00
|
|
|
},
|
|
|
|
Example{
|
|
|
|
"To store the build environment in a profile:",
|
2020-06-04 11:57:40 +03:00
|
|
|
"nix develop --profile /tmp/my-shell nixpkgs.hello"
|
2020-03-30 20:14:17 +03:00
|
|
|
},
|
|
|
|
Example{
|
|
|
|
"To use a build environment previously recorded in a profile:",
|
2020-06-04 11:57:40 +03:00
|
|
|
"nix develop /tmp/my-shell"
|
2020-03-30 20:14:17 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2020-04-27 20:12:54 +03:00
|
|
|
auto [buildEnvironment, gcroot] = getBuildEnvironment(store);
|
2020-03-30 20:14:17 +03:00
|
|
|
|
|
|
|
auto [rcFileFd, rcFilePath] = createTempFile("nix-shell");
|
|
|
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
makeRcScript(buildEnvironment, ss);
|
|
|
|
|
|
|
|
ss << fmt("rm -f '%s'\n", rcFilePath);
|
|
|
|
|
|
|
|
if (!command.empty()) {
|
|
|
|
std::vector<std::string> args;
|
|
|
|
for (auto s : command)
|
|
|
|
args.push_back(shellEscape(s));
|
|
|
|
ss << fmt("exec %s\n", concatStringsSep(" ", args));
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFull(rcFileFd.get(), ss.str());
|
|
|
|
|
|
|
|
stopProgressBar();
|
|
|
|
|
|
|
|
auto shell = getEnv("SHELL").value_or("bash");
|
|
|
|
|
|
|
|
setEnviron();
|
2020-04-27 22:18:26 +03:00
|
|
|
// prevent garbage collection until shell exits
|
2020-04-28 20:18:54 +03:00
|
|
|
setenv("NIX_GCROOT", gcroot.data(), 1);
|
2020-03-30 20:14:17 +03:00
|
|
|
|
|
|
|
auto args = Strings{std::string(baseNameOf(shell)), "--rcfile", rcFilePath};
|
|
|
|
|
|
|
|
restoreAffinity();
|
|
|
|
restoreSignals();
|
|
|
|
|
|
|
|
execvp(shell.c_str(), stringsToCharPtrs(args).data());
|
|
|
|
|
|
|
|
throw SysError("executing shell '%s'", shell);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CmdPrintDevEnv : Common
|
|
|
|
{
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "print shell code that can be sourced by bash to reproduce the build environment of a derivation";
|
|
|
|
}
|
|
|
|
|
|
|
|
Examples examples() override
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
Example{
|
|
|
|
"To apply the build environment of GNU hello to the current shell:",
|
|
|
|
". <(nix print-dev-env nixpkgs.hello)"
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:18:23 +03:00
|
|
|
Category category() override { return catUtility; }
|
|
|
|
|
2020-03-30 20:14:17 +03:00
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2020-04-27 20:12:54 +03:00
|
|
|
auto buildEnvironment = getBuildEnvironment(store).first;
|
2020-03-30 20:14:17 +03:00
|
|
|
|
|
|
|
stopProgressBar();
|
|
|
|
|
|
|
|
makeRcScript(buildEnvironment, std::cout);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static auto r1 = registerCommand<CmdPrintDevEnv>("print-dev-env");
|
2020-06-04 11:57:40 +03:00
|
|
|
static auto r2 = registerCommand<CmdDevelop>("develop");
|