mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-23 14:36:16 +02:00
Merge branch 'run-environment' of https://github.com/mkenigs/nix into flakes
This commit is contained in:
commit
0456a4ec65
4 changed files with 64 additions and 51 deletions
|
@ -142,4 +142,48 @@ MixDefaultProfile::MixDefaultProfile()
|
|||
profile = getDefaultProfile();
|
||||
}
|
||||
|
||||
MixEnvironment::MixEnvironment() : ignoreEnvironment(false) {
|
||||
mkFlag()
|
||||
.longName("ignore-environment")
|
||||
.shortName('i')
|
||||
.description("clear the entire environment (except those specified with --keep)")
|
||||
.set(&ignoreEnvironment, true);
|
||||
|
||||
mkFlag()
|
||||
.longName("keep")
|
||||
.shortName('k')
|
||||
.description("keep specified environment variable")
|
||||
.arity(1)
|
||||
.labels({"name"})
|
||||
.handler([&](std::vector<std::string> ss) { keep.insert(ss.front()); });
|
||||
|
||||
mkFlag()
|
||||
.longName("unset")
|
||||
.shortName('u')
|
||||
.description("unset specified environment variable")
|
||||
.arity(1)
|
||||
.labels({"name"})
|
||||
.handler([&](std::vector<std::string> ss) { unset.insert(ss.front()); });
|
||||
}
|
||||
|
||||
void MixEnvironment::setEnviron() {
|
||||
if (ignoreEnvironment) {
|
||||
if (!unset.empty())
|
||||
throw UsageError("--unset does not make sense with --ignore-environment");
|
||||
|
||||
for (const auto & var : keep) {
|
||||
auto val = getenv(var.c_str());
|
||||
if (val) stringsEnv.emplace_back(fmt("%s=%s", var.c_str(), val));
|
||||
}
|
||||
vectorEnv = stringsToCharPtrs(stringsEnv);
|
||||
environ = vectorEnv.data();
|
||||
} else {
|
||||
if (!keep.empty())
|
||||
throw UsageError("--keep does not make sense without --ignore-environment");
|
||||
|
||||
for (const auto & var : unset)
|
||||
unsetenv(var.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -200,4 +200,17 @@ struct MixDefaultProfile : MixProfile
|
|||
MixDefaultProfile();
|
||||
};
|
||||
|
||||
}
|
||||
struct MixEnvironment : virtual Args {
|
||||
|
||||
StringSet keep, unset;
|
||||
Strings stringsEnv;
|
||||
std::vector<char*> vectorEnv;
|
||||
bool ignoreEnvironment;
|
||||
|
||||
MixEnvironment();
|
||||
|
||||
/* Modify global environ based on ignoreEnvironment, keep, and unset. It's expected that exec will be called before this class goes out of scope, otherwise environ will become invalid. */
|
||||
void setEnviron();
|
||||
};
|
||||
|
||||
}
|
|
@ -57,11 +57,9 @@ struct RunCommon : virtual Command
|
|||
}
|
||||
};
|
||||
|
||||
struct CmdRun : InstallablesCommand, RunCommon
|
||||
struct CmdRun : InstallablesCommand, RunCommon, MixEnvironment
|
||||
{
|
||||
std::vector<std::string> command = { "bash" };
|
||||
StringSet keep, unset;
|
||||
bool ignoreEnvironment = false;
|
||||
|
||||
CmdRun()
|
||||
{
|
||||
|
@ -75,28 +73,6 @@ struct CmdRun : InstallablesCommand, RunCommon
|
|||
if (ss.empty()) throw UsageError("--command requires at least one argument");
|
||||
command = ss;
|
||||
});
|
||||
|
||||
mkFlag()
|
||||
.longName("ignore-environment")
|
||||
.shortName('i')
|
||||
.description("clear the entire environment (except those specified with --keep)")
|
||||
.set(&ignoreEnvironment, true);
|
||||
|
||||
mkFlag()
|
||||
.longName("keep")
|
||||
.shortName('k')
|
||||
.description("keep specified environment variable")
|
||||
.arity(1)
|
||||
.labels({"name"})
|
||||
.handler([&](std::vector<std::string> ss) { keep.insert(ss.front()); });
|
||||
|
||||
mkFlag()
|
||||
.longName("unset")
|
||||
.shortName('u')
|
||||
.description("unset specified environment variable")
|
||||
.arity(1)
|
||||
.labels({"name"})
|
||||
.handler([&](std::vector<std::string> ss) { unset.insert(ss.front()); });
|
||||
}
|
||||
|
||||
std::string description() override
|
||||
|
@ -132,35 +108,13 @@ struct CmdRun : InstallablesCommand, RunCommon
|
|||
|
||||
auto accessor = store->getFSAccessor();
|
||||
|
||||
if (ignoreEnvironment) {
|
||||
|
||||
if (!unset.empty())
|
||||
throw UsageError("--unset does not make sense with --ignore-environment");
|
||||
|
||||
std::map<std::string, std::string> kept;
|
||||
for (auto & var : keep) {
|
||||
auto s = getenv(var.c_str());
|
||||
if (s) kept[var] = s;
|
||||
}
|
||||
|
||||
clearEnv();
|
||||
|
||||
for (auto & var : kept)
|
||||
setenv(var.first.c_str(), var.second.c_str(), 1);
|
||||
|
||||
} else {
|
||||
|
||||
if (!keep.empty())
|
||||
throw UsageError("--keep does not make sense without --ignore-environment");
|
||||
|
||||
for (auto & var : unset)
|
||||
unsetenv(var.c_str());
|
||||
}
|
||||
|
||||
std::unordered_set<Path> done;
|
||||
std::queue<Path> todo;
|
||||
for (auto & path : outPaths) todo.push(path);
|
||||
|
||||
setEnviron();
|
||||
|
||||
auto unixPath = tokenizeString<Strings>(getEnv("PATH"), ":");
|
||||
|
||||
while (!todo.empty()) {
|
||||
|
|
|
@ -231,7 +231,7 @@ struct Common : InstallableCommand, MixProfile
|
|||
}
|
||||
};
|
||||
|
||||
struct CmdDevShell : Common
|
||||
struct CmdDevShell : Common, MixEnvironment
|
||||
{
|
||||
std::string description() override
|
||||
{
|
||||
|
@ -277,6 +277,8 @@ struct CmdDevShell : Common
|
|||
|
||||
auto shell = getEnv("SHELL", "bash");
|
||||
|
||||
setEnviron();
|
||||
|
||||
auto args = Strings{baseNameOf(shell), "--rcfile", rcFilePath};
|
||||
|
||||
restoreAffinity();
|
||||
|
|
Loading…
Reference in a new issue