2020-03-30 17:14:17 +00:00
|
|
|
#include "eval.hh"
|
|
|
|
#include "command.hh"
|
|
|
|
#include "common-args.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
2021-03-02 03:50:41 +00:00
|
|
|
#include "path-with-outputs.hh"
|
2020-03-30 17:14:17 +00:00
|
|
|
#include "derivations.hh"
|
|
|
|
#include "affinity.hh"
|
|
|
|
#include "progress-bar.hh"
|
2021-07-27 12:23:24 +00:00
|
|
|
#include "run.hh"
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-08-19 20:42:13 +00:00
|
|
|
#include <memory>
|
2021-07-08 22:47:57 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2020-03-30 17:14:17 +00:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2020-10-26 13:24:25 +00:00
|
|
|
struct DevelopSettings : Config
|
|
|
|
{
|
|
|
|
Setting<std::string> bashPrompt{this, "", "bash-prompt",
|
|
|
|
"The bash prompt (`PS1`) in `nix develop` shells."};
|
|
|
|
|
|
|
|
Setting<std::string> bashPromptSuffix{this, "", "bash-prompt-suffix",
|
|
|
|
"Suffix appended to the `PS1` environment variable in `nix develop` shells."};
|
|
|
|
};
|
|
|
|
|
|
|
|
static DevelopSettings developSettings;
|
|
|
|
|
|
|
|
static GlobalConfig::Register rDevelopSettings(&developSettings);
|
|
|
|
|
2020-03-30 17:14:17 +00:00
|
|
|
struct BuildEnvironment
|
|
|
|
{
|
2021-07-08 22:47:57 +00:00
|
|
|
struct String
|
|
|
|
{
|
|
|
|
bool exported;
|
|
|
|
std::string value;
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
bool operator == (const String & other) const
|
|
|
|
{
|
|
|
|
return exported == other.exported && value == other.value;
|
|
|
|
}
|
2021-07-08 22:47:57 +00:00
|
|
|
};
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
using Array = std::vector<std::string>;
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
using Associative = std::map<std::string, std::string>;
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
using Value = std::variant<String, Array, Associative>;
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
std::map<std::string, Value> vars;
|
|
|
|
std::map<std::string, std::string> bashFunctions;
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
static BuildEnvironment fromJSON(std::string_view in)
|
2021-07-08 22:47:57 +00:00
|
|
|
{
|
|
|
|
BuildEnvironment res;
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
std::set<std::string> exported;
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
auto json = nlohmann::json::parse(in);
|
2020-10-21 15:54:21 +00:00
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
for (auto & [name, info] : json["variables"].items()) {
|
|
|
|
std::string type = info["type"];
|
|
|
|
if (type == "var" || type == "exported")
|
|
|
|
res.vars.insert({name, BuildEnvironment::String { .exported = type == "exported", .value = info["value"] }});
|
|
|
|
else if (type == "array")
|
|
|
|
res.vars.insert({name, (Array) info["value"]});
|
|
|
|
else if (type == "associative")
|
|
|
|
res.vars.insert({name, (Associative) info["value"]});
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
for (auto & [name, def] : json["bashFunctions"].items()) {
|
|
|
|
res.bashFunctions.insert({name, def});
|
2020-04-30 12:39:26 +00:00
|
|
|
}
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
return res;
|
|
|
|
}
|
2020-10-21 15:54:21 +00:00
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
std::string toJSON() const
|
|
|
|
{
|
|
|
|
auto res = nlohmann::json::object();
|
|
|
|
|
|
|
|
auto vars2 = nlohmann::json::object();
|
|
|
|
for (auto & [name, value] : vars) {
|
|
|
|
auto info = nlohmann::json::object();
|
|
|
|
if (auto str = std::get_if<String>(&value)) {
|
|
|
|
info["type"] = str->exported ? "exported" : "var";
|
|
|
|
info["value"] = str->value;
|
|
|
|
}
|
|
|
|
else if (auto arr = std::get_if<Array>(&value)) {
|
|
|
|
info["type"] = "array";
|
|
|
|
info["value"] = *arr;
|
|
|
|
}
|
|
|
|
else if (auto arr = std::get_if<Associative>(&value)) {
|
|
|
|
info["type"] = "associative";
|
|
|
|
info["value"] = *arr;
|
|
|
|
}
|
|
|
|
vars2[name] = std::move(info);
|
|
|
|
}
|
|
|
|
res["variables"] = std::move(vars2);
|
2020-04-30 12:39:26 +00:00
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
res["bashFunctions"] = bashFunctions;
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
auto json = res.dump();
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
assert(BuildEnvironment::fromJSON(json) == *this);
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
return json;
|
|
|
|
}
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
void toBash(std::ostream & out, const std::set<std::string> & ignoreVars) const
|
|
|
|
{
|
|
|
|
for (auto & [name, value] : vars) {
|
2021-07-08 23:02:47 +00:00
|
|
|
if (!ignoreVars.count(name)) {
|
2021-07-08 22:47:57 +00:00
|
|
|
if (auto str = std::get_if<String>(&value)) {
|
|
|
|
out << fmt("%s=%s\n", name, shellEscape(str->value));
|
|
|
|
if (str->exported)
|
|
|
|
out << fmt("export %s\n", name);
|
|
|
|
}
|
|
|
|
else if (auto arr = std::get_if<Array>(&value)) {
|
|
|
|
out << "declare -a " << name << "=(";
|
|
|
|
for (auto & s : *arr)
|
|
|
|
out << shellEscape(s) << " ";
|
|
|
|
out << ")\n";
|
|
|
|
}
|
|
|
|
else if (auto arr = std::get_if<Associative>(&value)) {
|
|
|
|
out << "declare -A " << name << "=(";
|
|
|
|
for (auto & [n, v] : *arr)
|
|
|
|
out << "[" << shellEscape(n) << "]=" << shellEscape(v) << " ";
|
|
|
|
out << ")\n";
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
for (auto & [name, def] : bashFunctions) {
|
|
|
|
out << name << " ()\n{\n" << def << "}\n";
|
2020-04-30 12:39:26 +00:00
|
|
|
}
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
2020-04-30 12:39:26 +00:00
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
static std::string getString(const Value & value)
|
|
|
|
{
|
|
|
|
if (auto str = std::get_if<String>(&value))
|
|
|
|
return str->value;
|
|
|
|
else
|
|
|
|
throw Error("bash variable is not a string");
|
|
|
|
}
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
static Array getStrings(const Value & value)
|
|
|
|
{
|
|
|
|
if (auto str = std::get_if<String>(&value))
|
|
|
|
return tokenizeString<Array>(str->value);
|
|
|
|
else if (auto arr = std::get_if<Array>(&value)) {
|
|
|
|
return *arr;
|
2021-07-12 13:46:41 +00:00
|
|
|
} else if (auto assoc = std::get_if<Associative>(&value)) {
|
|
|
|
Array assocKeys;
|
|
|
|
std::for_each(assoc->begin(), assoc->end(), [&](auto & n) { assocKeys.push_back(n.first); });
|
|
|
|
return assocKeys;
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
2021-07-08 22:47:57 +00:00
|
|
|
else
|
|
|
|
throw Error("bash variable is not a string or array");
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
bool operator == (const BuildEnvironment & other) const
|
|
|
|
{
|
|
|
|
return vars == other.vars && bashFunctions == other.bashFunctions;
|
|
|
|
}
|
2021-07-08 22:47:57 +00:00
|
|
|
};
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2020-04-30 11:05:29 +00:00
|
|
|
const static std::string getEnvSh =
|
|
|
|
#include "get-env.sh.gen.hh"
|
|
|
|
;
|
|
|
|
|
2020-03-30 17:14:17 +00: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. */
|
2021-07-19 13:43:08 +00:00
|
|
|
static StorePath getDerivationEnvironment(ref<Store> store, ref<Store> evalStore, const StorePath & drvPath)
|
2020-03-30 17:14:17 +00:00
|
|
|
{
|
2021-07-19 13:43:08 +00:00
|
|
|
auto drv = evalStore->derivationFromPath(drvPath);
|
2020-04-30 12:39:26 +00:00
|
|
|
|
2020-03-30 17:14:17 +00:00
|
|
|
auto builder = baseNameOf(drv.builder);
|
|
|
|
if (builder != "bash")
|
2020-06-04 08:57:40 +00:00
|
|
|
throw Error("'nix develop' only works on derivations that use 'bash' as their builder");
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-19 13:43:08 +00:00
|
|
|
auto getEnvShPath = evalStore->addTextToStore("get-env.sh", getEnvSh, {});
|
2020-04-30 11:05:29 +00:00
|
|
|
|
|
|
|
drv.args = {store->printStorePath(getEnvShPath)};
|
2020-03-30 17:14:17 +00: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-08-09 20:32:35 +00:00
|
|
|
drv.name += "-env";
|
2020-04-30 11:05:29 +00:00
|
|
|
drv.inputSrcs.insert(std::move(getEnvShPath));
|
2021-10-25 13:53:01 +00:00
|
|
|
if (settings.isExperimentalFeatureEnabled(Xp::CaDerivations)) {
|
2021-06-11 11:31:19 +00:00
|
|
|
for (auto & output : drv.outputs) {
|
|
|
|
output.second = {
|
|
|
|
.output = DerivationOutputDeferred{},
|
|
|
|
};
|
2021-06-21 13:52:01 +00:00
|
|
|
drv.env[output.first] = hashPlaceholder(output.first);
|
2021-06-11 11:31:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (auto & output : drv.outputs) {
|
|
|
|
output.second = { .output = DerivationOutputInputAddressed { .path = StorePath::dummy } };
|
|
|
|
drv.env[output.first] = "";
|
|
|
|
}
|
2021-07-19 13:43:08 +00:00
|
|
|
Hash h = std::get<0>(hashDerivationModulo(*evalStore, drv, true));
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-06-11 11:31:19 +00:00
|
|
|
for (auto & output : drv.outputs) {
|
|
|
|
auto outPath = store->makeOutputPath(output.first, h, drv.name);
|
|
|
|
output.second = { .output = DerivationOutputInputAddressed { .path = outPath } };
|
|
|
|
drv.env[output.first] = store->printStorePath(outPath);
|
|
|
|
}
|
2020-08-28 16:16:03 +00:00
|
|
|
}
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-19 13:43:08 +00:00
|
|
|
auto shellDrvPath = writeDerivation(*evalStore, drv);
|
2020-03-30 17:14:17 +00:00
|
|
|
|
|
|
|
/* Build the derivation. */
|
2021-07-19 13:43:08 +00:00
|
|
|
store->buildPaths({DerivedPath::Built{shellDrvPath}}, bmNormal, evalStore);
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-07-19 13:43:08 +00:00
|
|
|
for (auto & [_0, optPath] : evalStore->queryPartialDerivationOutputMap(shellDrvPath)) {
|
2020-08-28 19:59:14 +00:00
|
|
|
assert(optPath);
|
|
|
|
auto & outPath = *optPath;
|
2020-08-28 16:16:03 +00:00
|
|
|
assert(store->isValidPath(outPath));
|
|
|
|
auto outPathS = store->toRealPath(outPath);
|
|
|
|
if (lstat(outPathS).st_size)
|
|
|
|
return outPath;
|
|
|
|
}
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2020-08-28 16:16:03 +00:00
|
|
|
throw Error("get-env.sh failed to produce an environment");
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Common : InstallableCommand, MixProfile
|
|
|
|
{
|
2021-07-08 22:47:57 +00:00
|
|
|
std::set<std::string> ignoreVars{
|
2020-03-30 17:14:17 +00:00
|
|
|
"BASHOPTS",
|
|
|
|
"HOME", // FIXME: don't ignore in pure mode?
|
|
|
|
"NIX_BUILD_TOP",
|
|
|
|
"NIX_ENFORCE_PURITY",
|
|
|
|
"NIX_LOG_FD",
|
2021-06-15 10:06:01 +00:00
|
|
|
"NIX_REMOTE",
|
2020-03-30 17:14:17 +00:00
|
|
|
"PPID",
|
|
|
|
"SHELLOPTS",
|
|
|
|
"SSL_CERT_FILE", // FIXME: only want to ignore /no-cert-file.crt
|
|
|
|
"TEMP",
|
|
|
|
"TEMPDIR",
|
|
|
|
"TERM",
|
|
|
|
"TMP",
|
|
|
|
"TMPDIR",
|
|
|
|
"TZ",
|
|
|
|
"UID",
|
|
|
|
};
|
|
|
|
|
2020-10-18 19:06:36 +00:00
|
|
|
std::vector<std::pair<std::string, std::string>> redirects;
|
|
|
|
|
|
|
|
Common()
|
|
|
|
{
|
|
|
|
addFlag({
|
|
|
|
.longName = "redirect",
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = "Redirect a store path to a mutable location.",
|
2020-10-18 19:06:36 +00:00
|
|
|
.labels = {"installable", "outputs-dir"},
|
|
|
|
.handler = {[&](std::string installable, std::string outputsDir) {
|
|
|
|
redirects.push_back({installable, outputsDir});
|
|
|
|
}}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-28 16:16:03 +00:00
|
|
|
std::string makeRcScript(
|
2020-10-18 19:06:36 +00:00
|
|
|
ref<Store> store,
|
2020-08-28 16:16:03 +00:00
|
|
|
const BuildEnvironment & buildEnvironment,
|
|
|
|
const Path & outputsDir = absPath(".") + "/outputs")
|
2020-03-30 17:14:17 +00:00
|
|
|
{
|
2020-08-28 16:16:03 +00:00
|
|
|
std::ostringstream out;
|
|
|
|
|
2020-04-30 12:46:51 +00:00
|
|
|
out << "unset shellHook\n";
|
|
|
|
|
2020-03-30 17:14:17 +00:00
|
|
|
out << "nix_saved_PATH=\"$PATH\"\n";
|
|
|
|
|
2021-07-08 22:47:57 +00:00
|
|
|
buildEnvironment.toBash(out, ignoreVars);
|
2020-03-30 17:14:17 +00:00
|
|
|
|
|
|
|
out << "PATH=\"$PATH:$nix_saved_PATH\"\n";
|
|
|
|
|
2021-01-29 03:55:18 +00:00
|
|
|
out << "export NIX_BUILD_TOP=\"$(mktemp -d -t nix-shell.XXXXXX)\"\n";
|
2020-03-30 17:14:17 +00:00
|
|
|
for (auto & i : {"TMP", "TMPDIR", "TEMP", "TEMPDIR"})
|
|
|
|
out << fmt("export %s=\"$NIX_BUILD_TOP\"\n", i);
|
|
|
|
|
|
|
|
out << "eval \"$shellHook\"\n";
|
2020-08-28 16:16:03 +00:00
|
|
|
|
2020-10-18 19:06:36 +00:00
|
|
|
auto script = out.str();
|
|
|
|
|
2020-08-28 16:16:03 +00:00
|
|
|
/* Substitute occurrences of output paths. */
|
2021-07-08 22:47:57 +00:00
|
|
|
auto outputs = buildEnvironment.vars.find("outputs");
|
|
|
|
assert(outputs != buildEnvironment.vars.end());
|
2020-08-28 16:16:03 +00:00
|
|
|
|
|
|
|
// FIXME: properly unquote 'outputs'.
|
|
|
|
StringMap rewrites;
|
2021-07-08 22:47:57 +00:00
|
|
|
for (auto & outputName : BuildEnvironment::getStrings(outputs->second)) {
|
|
|
|
auto from = buildEnvironment.vars.find(outputName);
|
|
|
|
assert(from != buildEnvironment.vars.end());
|
2020-08-28 16:16:03 +00:00
|
|
|
// FIXME: unquote
|
2021-07-08 22:47:57 +00:00
|
|
|
rewrites.insert({BuildEnvironment::getString(from->second), outputsDir + "/" + outputName});
|
2020-08-28 16:16:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 19:06:36 +00:00
|
|
|
/* Substitute redirects. */
|
2020-10-19 10:03:15 +00:00
|
|
|
for (auto & [installable_, dir_] : redirects) {
|
|
|
|
auto dir = absPath(dir_);
|
|
|
|
auto installable = parseInstallable(store, installable_);
|
2021-05-17 06:45:08 +00:00
|
|
|
auto builtPaths = toStorePaths(
|
2021-07-16 14:04:47 +00:00
|
|
|
getEvalStore(), store, Realise::Nothing, OperateOn::Output, {installable});
|
2021-05-17 06:45:08 +00:00
|
|
|
for (auto & path: builtPaths) {
|
2020-10-18 19:06:36 +00:00
|
|
|
auto from = store->printStorePath(path);
|
|
|
|
if (script.find(from) == std::string::npos)
|
|
|
|
warn("'%s' (path '%s') is not used by this build environment", installable->what(), from);
|
|
|
|
else {
|
|
|
|
printInfo("redirecting '%s' to '%s'", from, dir);
|
|
|
|
rewrites.insert({from, dir});
|
|
|
|
}
|
2021-05-17 06:45:08 +00:00
|
|
|
}
|
2020-10-18 19:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rewriteStrings(script, rewrites);
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 19:10:13 +00:00
|
|
|
Strings getDefaultFlakeAttrPaths() override
|
|
|
|
{
|
Support non-x86_64-linux system types in flakes
A command like
$ nix run nixpkgs#hello
will now build the attribute 'packages.${system}.hello' rather than
'packages.hello'. Note that this does mean that the flake needs to
export an attribute for every system type it supports, and you can't
build on unsupported systems. So 'packages' typically looks like this:
packages = nixpkgs.lib.genAttrs ["x86_64-linux" "i686-linux"] (system: {
hello = ...;
});
The 'checks', 'defaultPackage', 'devShell', 'apps' and 'defaultApp'
outputs similarly are now attrsets that map system types to
derivations/apps. 'nix flake check' checks that the derivations for
all platforms evaluate correctly, but only builds the derivations in
'checks.${system}'.
Fixes #2861. (That issue also talks about access to ~/.config/nixpkgs
and --arg, but I think it's reasonable to say that flakes shouldn't
support those.)
The alternative to attribute selection is to pass the system type as
an argument to the flake's 'outputs' function, e.g. 'outputs = { self,
nixpkgs, system }: ...'. However, that approach would be at odds with
hermetic evaluation and make it impossible to enumerate the packages
provided by a flake.
2019-10-15 15:52:10 +00:00
|
|
|
return {"devShell." + settings.thisSystem.get(), "defaultPackage." + settings.thisSystem.get()};
|
2019-05-02 19:10:13 +00:00
|
|
|
}
|
2021-07-13 09:44:19 +00:00
|
|
|
Strings getDefaultFlakeAttrPathPrefixes() override
|
|
|
|
{
|
|
|
|
auto res = SourceExprCommand::getDefaultFlakeAttrPathPrefixes();
|
2021-08-20 23:24:03 +00:00
|
|
|
res.emplace_front("devShells." + settings.thisSystem.get() + ".");
|
2021-07-13 09:44:19 +00:00
|
|
|
return res;
|
|
|
|
}
|
2019-07-12 14:16:27 +00:00
|
|
|
|
2020-03-30 17:14:17 +00:00
|
|
|
StorePath getShellOutPath(ref<Store> store)
|
|
|
|
{
|
|
|
|
auto path = installable->getStorePath();
|
|
|
|
if (path && hasSuffix(path->to_string(), "-env"))
|
2020-06-16 20:20:18 +00:00
|
|
|
return *path;
|
2020-03-30 17:14:17 +00: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();
|
|
|
|
|
2021-07-19 13:43:08 +00:00
|
|
|
return getDerivationEnvironment(store, getEvalStore(), drvPath);
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 17:12:54 +00:00
|
|
|
std::pair<BuildEnvironment, std::string> getBuildEnvironment(ref<Store> store)
|
2020-03-30 17:14:17 +00:00
|
|
|
{
|
|
|
|
auto shellOutPath = getShellOutPath(store);
|
|
|
|
|
2020-04-27 17:12:54 +00:00
|
|
|
auto strPath = store->printStorePath(shellOutPath);
|
|
|
|
|
2020-03-30 17:14:17 +00:00
|
|
|
updateProfile(shellOutPath);
|
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
debug("reading environment file '%s'", strPath);
|
|
|
|
|
2021-07-19 13:43:08 +00:00
|
|
|
return {BuildEnvironment::fromJSON(readFile(store->toRealPath(shellOutPath))), strPath};
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-04 08:57:40 +00:00
|
|
|
struct CmdDevelop : Common, MixEnvironment
|
2020-03-30 17:14:17 +00:00
|
|
|
{
|
|
|
|
std::vector<std::string> command;
|
2020-08-28 17:24:29 +00:00
|
|
|
std::optional<std::string> phase;
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2020-06-04 08:57:40 +00:00
|
|
|
CmdDevelop()
|
2020-03-30 17:14:17 +00:00
|
|
|
{
|
2020-05-04 20:40:19 +00:00
|
|
|
addFlag({
|
|
|
|
.longName = "command",
|
|
|
|
.shortName = 'c',
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = "Instead of starting an interactive shell, start the specified command and arguments.",
|
2020-05-04 20:40:19 +00:00
|
|
|
.labels = {"command", "args"},
|
|
|
|
.handler = {[&](std::vector<std::string> ss) {
|
2020-03-30 17:14:17 +00:00
|
|
|
if (ss.empty()) throw UsageError("--command requires at least one argument");
|
|
|
|
command = ss;
|
2020-05-04 20:40:19 +00:00
|
|
|
}}
|
|
|
|
});
|
2020-08-28 17:24:29 +00:00
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "phase",
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = "The stdenv phase to run (e.g. `build` or `configure`).",
|
2020-08-28 17:24:29 +00:00
|
|
|
.labels = {"phase-name"},
|
|
|
|
.handler = {&phase},
|
|
|
|
});
|
|
|
|
|
2021-10-08 22:19:50 +00:00
|
|
|
addFlag({
|
|
|
|
.longName = "unpack",
|
|
|
|
.description = "Run the `unpack` phase.",
|
|
|
|
.handler = {&phase, {"unpack"}},
|
|
|
|
});
|
|
|
|
|
2020-08-28 17:24:29 +00:00
|
|
|
addFlag({
|
|
|
|
.longName = "configure",
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = "Run the `configure` phase.",
|
2020-08-28 17:24:29 +00:00
|
|
|
.handler = {&phase, {"configure"}},
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "build",
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = "Run the `build` phase.",
|
2020-08-28 17:24:29 +00:00
|
|
|
.handler = {&phase, {"build"}},
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "check",
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = "Run the `check` phase.",
|
2020-08-28 17:24:29 +00:00
|
|
|
.handler = {&phase, {"check"}},
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "install",
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = "Run the `install` phase.",
|
2020-08-28 17:24:29 +00:00
|
|
|
.handler = {&phase, {"install"}},
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "installcheck",
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = "Run the `installcheck` phase.",
|
2020-08-28 17:24:29 +00:00
|
|
|
.handler = {&phase, {"installCheck"}},
|
|
|
|
});
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "run a bash shell that provides the build environment of a derivation";
|
|
|
|
}
|
|
|
|
|
2020-12-08 20:52:22 +00:00
|
|
|
std::string doc() override
|
2020-03-30 17:14:17 +00:00
|
|
|
{
|
2020-12-08 20:52:22 +00:00
|
|
|
return
|
|
|
|
#include "develop.md"
|
|
|
|
;
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2020-04-27 17:12:54 +00:00
|
|
|
auto [buildEnvironment, gcroot] = getBuildEnvironment(store);
|
2020-03-30 17:14:17 +00:00
|
|
|
|
|
|
|
auto [rcFileFd, rcFilePath] = createTempFile("nix-shell");
|
|
|
|
|
2020-10-18 19:06:36 +00:00
|
|
|
auto script = makeRcScript(store, buildEnvironment);
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2020-08-28 17:24:29 +00:00
|
|
|
if (verbosity >= lvlDebug)
|
|
|
|
script += "set -x\n";
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-05-17 20:07:00 +00:00
|
|
|
script += fmt("command rm -f '%s'\n", rcFilePath);
|
2020-08-28 17:24:29 +00:00
|
|
|
|
|
|
|
if (phase) {
|
|
|
|
if (!command.empty())
|
|
|
|
throw UsageError("you cannot use both '--command' and '--phase'");
|
|
|
|
// FIXME: foundMakefile is set by buildPhase, need to get
|
|
|
|
// rid of that.
|
|
|
|
script += fmt("foundMakefile=1\n");
|
|
|
|
script += fmt("runHook %1%Phase\n", *phase);
|
|
|
|
}
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2020-08-28 17:24:29 +00:00
|
|
|
else if (!command.empty()) {
|
2020-03-30 17:14:17 +00:00
|
|
|
std::vector<std::string> args;
|
|
|
|
for (auto s : command)
|
|
|
|
args.push_back(shellEscape(s));
|
2020-08-28 16:16:03 +00:00
|
|
|
script += fmt("exec %s\n", concatStringsSep(" ", args));
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:02:00 +00:00
|
|
|
else {
|
2021-05-17 20:07:00 +00:00
|
|
|
script = "[ -n \"$PS1\" ] && [ -e ~/.bashrc ] && source ~/.bashrc;\n" + script;
|
2020-10-26 13:24:25 +00:00
|
|
|
if (developSettings.bashPrompt != "")
|
|
|
|
script += fmt("[ -n \"$PS1\" ] && PS1=%s;\n", shellEscape(developSettings.bashPrompt));
|
|
|
|
if (developSettings.bashPromptSuffix != "")
|
|
|
|
script += fmt("[ -n \"$PS1\" ] && PS1+=%s;\n", shellEscape(developSettings.bashPromptSuffix));
|
2020-10-09 20:02:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 16:16:03 +00:00
|
|
|
writeFull(rcFileFd.get(), script);
|
2020-03-30 17:14:17 +00:00
|
|
|
|
|
|
|
setEnviron();
|
2020-04-27 19:18:26 +00:00
|
|
|
// prevent garbage collection until shell exits
|
2020-04-28 17:18:54 +00:00
|
|
|
setenv("NIX_GCROOT", gcroot.data(), 1);
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2020-07-06 15:08:54 +00:00
|
|
|
Path shell = "bash";
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto state = getEvalState();
|
|
|
|
|
2021-06-29 08:13:42 +00:00
|
|
|
auto nixpkgsLockFlags = lockFlags;
|
|
|
|
nixpkgsLockFlags.inputOverrides = {};
|
|
|
|
nixpkgsLockFlags.inputUpdates = {};
|
|
|
|
|
2020-07-06 15:08:54 +00:00
|
|
|
auto bashInstallable = std::make_shared<InstallableFlake>(
|
2021-02-17 16:32:10 +00:00
|
|
|
this,
|
2020-07-06 15:08:54 +00:00
|
|
|
state,
|
2020-09-08 12:50:23 +00:00
|
|
|
installable->nixpkgsFlakeRef(),
|
2020-07-06 15:08:54 +00:00
|
|
|
Strings{"bashInteractive"},
|
|
|
|
Strings{"legacyPackages." + settings.thisSystem.get() + "."},
|
2021-06-29 08:13:42 +00:00
|
|
|
nixpkgsLockFlags);
|
2020-07-06 15:08:54 +00:00
|
|
|
|
2021-07-16 14:04:47 +00:00
|
|
|
shell = store->printStorePath(
|
|
|
|
toStorePath(getEvalStore(), store, Realise::Outputs, OperateOn::Output, bashInstallable)) + "/bin/bash";
|
2020-07-06 15:08:54 +00:00
|
|
|
} catch (Error &) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:00:22 +00:00
|
|
|
// If running a phase or single command, don't want an interactive shell running after
|
2020-10-20 16:48:07 +00:00
|
|
|
// Ctrl-C, so don't pass --rcfile
|
2020-11-07 14:00:22 +00:00
|
|
|
auto args = phase || !command.empty() ? Strings{std::string(baseNameOf(shell)), rcFilePath}
|
2020-10-20 16:48:07 +00:00
|
|
|
: Strings{std::string(baseNameOf(shell)), "--rcfile", rcFilePath};
|
2020-03-30 17:14:17 +00:00
|
|
|
|
2021-08-19 20:42:13 +00:00
|
|
|
// Need to chdir since phases assume in flake directory
|
|
|
|
if (phase) {
|
|
|
|
// chdir if installable is a flake of type git+file or path
|
|
|
|
auto installableFlake = std::dynamic_pointer_cast<InstallableFlake>(installable);
|
|
|
|
if (installableFlake) {
|
|
|
|
auto sourcePath = installableFlake->getLockedFlake()->flake.resolvedRef.input.getSourcePath();
|
|
|
|
if (sourcePath) {
|
|
|
|
if (chdir(sourcePath->c_str()) == -1) {
|
|
|
|
throw SysError("chdir to '%s' failed", *sourcePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 12:23:24 +00:00
|
|
|
runProgramInStore(store, shell, args);
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
struct CmdPrintDevEnv : Common, MixJSON
|
2020-03-30 17:14:17 +00:00
|
|
|
{
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "print shell code that can be sourced by bash to reproduce the build environment of a derivation";
|
|
|
|
}
|
|
|
|
|
2020-12-08 20:52:22 +00:00
|
|
|
std::string doc() override
|
2020-03-30 17:14:17 +00:00
|
|
|
{
|
2020-12-08 20:52:22 +00:00
|
|
|
return
|
|
|
|
#include "print-dev-env.md"
|
|
|
|
;
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 13:18:23 +00:00
|
|
|
Category category() override { return catUtility; }
|
|
|
|
|
2020-03-30 17:14:17 +00:00
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2020-04-27 17:12:54 +00:00
|
|
|
auto buildEnvironment = getBuildEnvironment(store).first;
|
2020-03-30 17:14:17 +00:00
|
|
|
|
|
|
|
stopProgressBar();
|
|
|
|
|
2021-07-09 10:10:48 +00:00
|
|
|
logger->writeToStdout(
|
|
|
|
json
|
|
|
|
? buildEnvironment.toJSON()
|
|
|
|
: makeRcScript(store, buildEnvironment));
|
2020-03-30 17:14:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static auto rCmdPrintDevEnv = registerCommand<CmdPrintDevEnv>("print-dev-env");
|
|
|
|
static auto rCmdDevelop = registerCommand<CmdDevelop>("develop");
|