mirror of
https://github.com/NixOS/nix.git
synced 2025-11-15 06:52:43 +01:00
* It is tough to contribute to a project that doesn't use a formatter, * It is extra hard to contribute to a project which has configured the formatter, but ignores it for some files * Code formatting makes it harder to hide obscure / weird bugs by accident or on purpose, Let's rip the bandaid off? Note that PRs currently in flight should be able to be merged relatively easily by applying `clang-format` to their tip prior to merge.
51 lines
1 KiB
C++
51 lines
1 KiB
C++
#include "nix/util/util.hh"
|
|
#include "nix/util/environment-variables.hh"
|
|
|
|
extern char ** environ __attribute__((weak));
|
|
|
|
namespace nix {
|
|
|
|
std::optional<std::string> getEnv(const std::string & key)
|
|
{
|
|
char * value = getenv(key.c_str());
|
|
if (!value)
|
|
return {};
|
|
return std::string(value);
|
|
}
|
|
|
|
std::optional<std::string> getEnvNonEmpty(const std::string & key)
|
|
{
|
|
auto value = getEnv(key);
|
|
if (value == "")
|
|
return {};
|
|
return value;
|
|
}
|
|
|
|
StringMap getEnv()
|
|
{
|
|
StringMap env;
|
|
for (size_t i = 0; environ[i]; ++i) {
|
|
auto s = environ[i];
|
|
auto eq = strchr(s, '=');
|
|
if (!eq)
|
|
// invalid env, just keep going
|
|
continue;
|
|
env.emplace(std::string(s, eq), std::string(eq + 1));
|
|
}
|
|
return env;
|
|
}
|
|
|
|
void clearEnv()
|
|
{
|
|
for (auto & name : getEnv())
|
|
unsetenv(name.first.c_str());
|
|
}
|
|
|
|
void replaceEnv(const StringMap & newEnv)
|
|
{
|
|
clearEnv();
|
|
for (auto & newEnvVar : newEnv)
|
|
setEnv(newEnvVar.first.c_str(), newEnvVar.second.c_str());
|
|
}
|
|
|
|
} // namespace nix
|