Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: NixOS/nix
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 49a53c1d3f77
Choose a base ref
...
head repository: NixOS/nix
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 122e1a61f8de
Choose a head ref
  • 2 commits
  • 3 files changed
  • 2 contributors

Commits on Aug 2, 2018

  1. Allows selectively adding environment variables to pure shells.

    Includes documentation and test.
    samueldr committed Aug 2, 2018
    Copy the full SHA
    438e025 View commit details

Commits on Aug 3, 2018

  1. Merge pull request #2323 from samueldr/feature/selective-impurity

    Allows selectively adding environment variables to pure shells.
    edolstra authored Aug 3, 2018
    Copy the full SHA
    122e1a6 View commit details
Showing with 21 additions and 1 deletion.
  1. +8 −0 doc/manual/command-ref/nix-shell.xml
  2. +6 −1 src/nix-build/nix-build.cc
  3. +7 −0 tests/nix-shell.sh
8 changes: 8 additions & 0 deletions doc/manual/command-ref/nix-shell.xml
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@
<arg><option>--run</option> <replaceable>cmd</replaceable></arg>
<arg><option>--exclude</option> <replaceable>regexp</replaceable></arg>
<arg><option>--pure</option></arg>
<arg><option>--keep</option> <replaceable>name</replaceable></arg>
<group choice='req'>
<arg choice='plain'>
<group choice='req'>
@@ -165,6 +166,13 @@ also <xref linkend="sec-common-options" />.</phrase></para>

</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>

<para>The following common options are supported:</para>
7 changes: 6 additions & 1 deletion src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
@@ -98,6 +98,9 @@ void mainWrapped(int argc, char * * argv)

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;
for (int i = 1; i < argc; ++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 == "-")
readStdin = true;

@@ -367,7 +373,6 @@ void mainWrapped(int argc, char * * argv)
auto tmp = getEnv("TMPDIR", getEnv("XDG_RUNTIME_DIR", "/tmp"));

if (pure) {
std::set<string> keepVars{"HOME", "USER", "LOGNAME", "DISPLAY", "PATH", "TERM", "IN_NIX_SHELL", "TZ", "PAGER", "NIX_BUILD_SHELL", "SHLVL"};
decltype(env) newEnv;
for (auto & i : env)
if (keepVars.count(i.first))
7 changes: 7 additions & 0 deletions tests/nix-shell.sh
Original file line number Diff line number Diff line change
@@ -4,12 +4,19 @@ clearStore

# Test nix-shell -A
export IMPURE_VAR=foo
export SELECTED_IMPURE_VAR=baz
export NIX_BUILD_SHELL=$SHELL
output=$(nix-shell --pure shell.nix -A shellDrv --run \
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"')

[ "$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
[[ $(nix-shell --pure $(nix-instantiate shell.nix -A shellDrv) --run \
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"') = " - foo - bar" ]]