Refactor: rename runEnv -> isNixShell

This commit is contained in:
Robert Hensing 2024-07-06 20:01:46 +02:00
parent 896eb7a44b
commit 13181356fc

View file

@ -93,7 +93,7 @@ static std::vector<std::string> shellwords(const std::string & s)
static void main_nix_build(int argc, char * * argv) static void main_nix_build(int argc, char * * argv)
{ {
auto dryRun = false; auto dryRun = false;
auto runEnv = std::regex_search(argv[0], std::regex("nix-shell$")); auto isNixShell = std::regex_search(argv[0], std::regex("nix-shell$"));
auto pure = false; auto pure = false;
auto fromArgs = false; auto fromArgs = false;
auto packages = false; auto packages = false;
@ -107,7 +107,7 @@ static void main_nix_build(int argc, char * * argv)
std::string envCommand; // interactive shell std::string envCommand; // interactive shell
Strings envExclude; Strings envExclude;
auto myName = runEnv ? "nix-shell" : "nix-build"; auto myName = isNixShell ? "nix-shell" : "nix-build";
auto inShebang = false; auto inShebang = false;
std::string script; std::string script;
@ -132,7 +132,7 @@ static void main_nix_build(int argc, char * * argv)
// Heuristic to see if we're invoked as a shebang script, namely, // 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 // if we have at least one argument, it's the name of an
// executable file, and it starts with "#!". // executable file, and it starts with "#!".
if (runEnv && argc > 1) { if (isNixShell && argc > 1) {
script = argv[1]; script = argv[1];
try { try {
auto lines = tokenizeString<Strings>(readFile(script), "\n"); auto lines = tokenizeString<Strings>(readFile(script), "\n");
@ -186,9 +186,9 @@ static void main_nix_build(int argc, char * * argv)
dryRun = true; dryRun = true;
else if (*arg == "--run-env") // obsolete else if (*arg == "--run-env") // obsolete
runEnv = true; isNixShell = true;
else if (runEnv && (*arg == "--command" || *arg == "--run")) { else if (isNixShell && (*arg == "--command" || *arg == "--run")) {
if (*arg == "--run") if (*arg == "--run")
interactive = false; interactive = false;
envCommand = getArg(*arg, arg, end) + "\nexit"; envCommand = getArg(*arg, arg, end) + "\nexit";
@ -206,7 +206,7 @@ static void main_nix_build(int argc, char * * argv)
else if (*arg == "--pure") pure = true; else if (*arg == "--pure") pure = true;
else if (*arg == "--impure") pure = false; else if (*arg == "--impure") pure = false;
else if (runEnv && (*arg == "--packages" || *arg == "-p")) else if (isNixShell && (*arg == "--packages" || *arg == "-p"))
packages = true; packages = true;
else if (inShebang && *arg == "-i") { else if (inShebang && *arg == "-i") {
@ -266,7 +266,7 @@ static void main_nix_build(int argc, char * * argv)
auto autoArgs = myArgs.getAutoArgs(*state); auto autoArgs = myArgs.getAutoArgs(*state);
auto autoArgsWithInNixShell = autoArgs; auto autoArgsWithInNixShell = autoArgs;
if (runEnv) { if (isNixShell) {
auto newArgs = state->buildBindings(autoArgsWithInNixShell->size() + 1); auto newArgs = state->buildBindings(autoArgsWithInNixShell->size() + 1);
newArgs.alloc("inNixShell").mkBool(true); newArgs.alloc("inNixShell").mkBool(true);
for (auto & i : *autoArgs) newArgs.insert(i); for (auto & i : *autoArgs) newArgs.insert(i);
@ -282,13 +282,13 @@ static void main_nix_build(int argc, char * * argv)
fromArgs = true; fromArgs = true;
left = {joined.str()}; left = {joined.str()};
} else if (!fromArgs) { } else if (!fromArgs) {
if (left.empty() && runEnv && pathExists("shell.nix")) if (left.empty() && isNixShell && pathExists("shell.nix"))
left = {"shell.nix"}; left = {"shell.nix"};
if (left.empty()) if (left.empty())
left = {"default.nix"}; left = {"default.nix"};
} }
if (runEnv) if (isNixShell)
setEnv("IN_NIX_SHELL", pure ? "pure" : "impure"); setEnv("IN_NIX_SHELL", pure ? "pure" : "impure");
PackageInfos drvs; PackageInfos drvs;
@ -330,7 +330,7 @@ static void main_nix_build(int argc, char * * argv)
std::function<bool(const Value & v)> takesNixShellAttr; std::function<bool(const Value & v)> takesNixShellAttr;
takesNixShellAttr = [&](const Value & v) { takesNixShellAttr = [&](const Value & v) {
if (!runEnv) { if (!isNixShell) {
return false; return false;
} }
bool add = false; bool add = false;
@ -381,7 +381,7 @@ static void main_nix_build(int argc, char * * argv)
store->buildPaths(paths, buildMode, evalStore); store->buildPaths(paths, buildMode, evalStore);
}; };
if (runEnv) { if (isNixShell) {
if (drvs.size() != 1) if (drvs.size() != 1)
throw UsageError("nix-shell requires a single derivation"); throw UsageError("nix-shell requires a single derivation");