mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-22 22:16:16 +02:00
Merge pull request #2323 from samueldr/feature/selective-impurity
Allows selectively adding environment variables to pure shells.
This commit is contained in:
commit
122e1a61f8
3 changed files with 21 additions and 1 deletions
|
@ -32,6 +32,7 @@
|
||||||
<arg><option>--run</option> <replaceable>cmd</replaceable></arg>
|
<arg><option>--run</option> <replaceable>cmd</replaceable></arg>
|
||||||
<arg><option>--exclude</option> <replaceable>regexp</replaceable></arg>
|
<arg><option>--exclude</option> <replaceable>regexp</replaceable></arg>
|
||||||
<arg><option>--pure</option></arg>
|
<arg><option>--pure</option></arg>
|
||||||
|
<arg><option>--keep</option> <replaceable>name</replaceable></arg>
|
||||||
<group choice='req'>
|
<group choice='req'>
|
||||||
<arg choice='plain'>
|
<arg choice='plain'>
|
||||||
<group choice='req'>
|
<group choice='req'>
|
||||||
|
@ -165,6 +166,13 @@ also <xref linkend="sec-common-options" />.</phrase></para>
|
||||||
|
|
||||||
</listitem></varlistentry>
|
</listitem></varlistentry>
|
||||||
|
|
||||||
|
<varlistentry><term><option>--keep</option> <replaceable>name</replaceable></term>
|
||||||
|
|
||||||
|
<listitem><para>When a <option>--pure</option> shell is started,
|
||||||
|
keep the listed environment variables.</para></listitem>
|
||||||
|
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
</variablelist>
|
</variablelist>
|
||||||
|
|
||||||
<para>The following common options are supported:</para>
|
<para>The following common options are supported:</para>
|
||||||
|
|
|
@ -98,6 +98,9 @@ void mainWrapped(int argc, char * * argv)
|
||||||
|
|
||||||
std::string outLink = "./result";
|
std::string outLink = "./result";
|
||||||
|
|
||||||
|
// List of environment variables kept for --pure
|
||||||
|
std::set<string> keepVars{"HOME", "USER", "LOGNAME", "DISPLAY", "PATH", "TERM", "IN_NIX_SHELL", "TZ", "PAGER", "NIX_BUILD_SHELL", "SHLVL"};
|
||||||
|
|
||||||
Strings args;
|
Strings args;
|
||||||
for (int i = 1; i < argc; ++i)
|
for (int i = 1; i < argc; ++i)
|
||||||
args.push_back(argv[i]);
|
args.push_back(argv[i]);
|
||||||
|
@ -217,6 +220,9 @@ void mainWrapped(int argc, char * * argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (*arg == "--keep")
|
||||||
|
keepVars.insert(getArg(*arg, arg, end));
|
||||||
|
|
||||||
else if (*arg == "-")
|
else if (*arg == "-")
|
||||||
readStdin = true;
|
readStdin = true;
|
||||||
|
|
||||||
|
@ -367,7 +373,6 @@ void mainWrapped(int argc, char * * argv)
|
||||||
auto tmp = getEnv("TMPDIR", getEnv("XDG_RUNTIME_DIR", "/tmp"));
|
auto tmp = getEnv("TMPDIR", getEnv("XDG_RUNTIME_DIR", "/tmp"));
|
||||||
|
|
||||||
if (pure) {
|
if (pure) {
|
||||||
std::set<string> keepVars{"HOME", "USER", "LOGNAME", "DISPLAY", "PATH", "TERM", "IN_NIX_SHELL", "TZ", "PAGER", "NIX_BUILD_SHELL", "SHLVL"};
|
|
||||||
decltype(env) newEnv;
|
decltype(env) newEnv;
|
||||||
for (auto & i : env)
|
for (auto & i : env)
|
||||||
if (keepVars.count(i.first))
|
if (keepVars.count(i.first))
|
||||||
|
|
|
@ -4,12 +4,19 @@ clearStore
|
||||||
|
|
||||||
# Test nix-shell -A
|
# Test nix-shell -A
|
||||||
export IMPURE_VAR=foo
|
export IMPURE_VAR=foo
|
||||||
|
export SELECTED_IMPURE_VAR=baz
|
||||||
export NIX_BUILD_SHELL=$SHELL
|
export NIX_BUILD_SHELL=$SHELL
|
||||||
output=$(nix-shell --pure shell.nix -A shellDrv --run \
|
output=$(nix-shell --pure shell.nix -A shellDrv --run \
|
||||||
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"')
|
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"')
|
||||||
|
|
||||||
[ "$output" = " - foo - bar" ]
|
[ "$output" = " - foo - bar" ]
|
||||||
|
|
||||||
|
# Test --keep
|
||||||
|
output=$(nix-shell --pure --keep SELECTED_IMPURE_VAR shell.nix -A shellDrv --run \
|
||||||
|
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX - $SELECTED_IMPURE_VAR"')
|
||||||
|
|
||||||
|
[ "$output" = " - foo - bar - baz" ]
|
||||||
|
|
||||||
# Test nix-shell on a .drv
|
# Test nix-shell on a .drv
|
||||||
[[ $(nix-shell --pure $(nix-instantiate shell.nix -A shellDrv) --run \
|
[[ $(nix-shell --pure $(nix-instantiate shell.nix -A shellDrv) --run \
|
||||||
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"') = " - foo - bar" ]]
|
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"') = " - foo - bar" ]]
|
||||||
|
|
Loading…
Reference in a new issue