nix-build --run-env: Add a ‘--pure’ flag

This causes the environment to be (almost) cleared, thus giving a
shell that more closely resembled the actual Nix derivation.
This commit is contained in:
Eelco Dolstra 2013-07-19 11:23:32 +02:00
parent a4921b8ceb
commit dc5f2e7da6
2 changed files with 43 additions and 3 deletions

View File

@ -42,6 +42,7 @@
<option>--run-env</option> <option>--run-env</option>
<arg><option>--command</option> <replaceable>cmd</replaceable></arg> <arg><option>--command</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> </arg>
<arg choice='plain' rep='repeat'><replaceable>paths</replaceable></arg> <arg choice='plain' rep='repeat'><replaceable>paths</replaceable></arg>
</cmdsynopsis> </cmdsynopsis>
@ -147,7 +148,10 @@ also <xref linkend="sec-common-options" />.</phrase></para>
<listitem><para>In the environment of the derivation, run the <listitem><para>In the environment of the derivation, run the
shell command <replaceable>cmd</replaceable> instead of starting shell command <replaceable>cmd</replaceable> instead of starting
an interactive shell.</para></listitem> an interactive shell. However, if you end the shell command with
<literal>return</literal>, you still get an interactive shell.
This can be useful for doing any additional
initialisation.</para></listitem>
</varlistentry> </varlistentry>
@ -159,6 +163,21 @@ also <xref linkend="sec-common-options" />.</phrase></para>
</varlistentry> </varlistentry>
<varlistentry><term><option>--pure</option></term>
<listitem><para>If this flag is specified, the environment is
almost entirely cleared before the interactive shell is started,
so you get an environment that more closely corresponds to the
“real” Nix build. A few variables, in particular
<envar>HOME</envar>, <envar>USER</envar> and
<envar>DISPLAY</envar>, are retained. Note that
<filename>~/.bashrc</filename> and (depending on your Bash
installation) <filename>/etc/bashrc</filename> are still sourced,
so any variables set there will affect the interactive
shell.</para></listitem>
</varlistentry>
</variablelist> </variablelist>
</refsection> </refsection>
@ -189,6 +208,14 @@ $ buildPhase
$ ./pan/gui/pan $ ./pan/gui/pan
</screen> </screen>
To clear the environment first, and do some additional automatic
initialisation of the interactive shell:
<screen>
$ nix-build '&lt;nixpkgs>' --run-env -A pan --pure \
--command 'export NIX_DEBUG=1; export NIX_CORES=8; return'
</screen>
</para> </para>
<para>If a derivation has multiple outputs, <para>If a derivation has multiple outputs,

View File

@ -10,6 +10,7 @@ use File::Temp qw(tempdir);
my $dryRun = 0; my $dryRun = 0;
my $verbose = 0; my $verbose = 0;
my $runEnv = 0; my $runEnv = 0;
my $pure = 0;
my @instArgs = (); my @instArgs = ();
my @buildArgs = (); my @buildArgs = ();
@ -134,6 +135,10 @@ for (my $n = 0; $n < scalar @ARGV; $n++) {
push @envExclude, $ARGV[$n]; push @envExclude, $ARGV[$n];
} }
elsif ($arg eq "--pure") {
$pure = 1;
}
elsif (substr($arg, 0, 1) eq "-") { elsif (substr($arg, 0, 1) eq "-") {
push @buildArgs, $arg; push @buildArgs, $arg;
} }
@ -169,6 +174,14 @@ foreach my $expr (@exprs) {
or die "$0: failed to build all dependencies\n"; or die "$0: failed to build all dependencies\n";
# Set the environment. # Set the environment.
if ($pure) {
foreach my $name (keys %ENV) {
next if $name eq "HOME" || $name eq "USER" || $name eq "LOGNAME" || $name eq "DISPLAY" || $name eq "PATH";
delete $ENV{$name};
}
# NixOS hack: prevent /etc/bashrc from sourcing /etc/profile.
$ENV{'__ETC_PROFILE_SOURCED'} = 1;
}
$ENV{'NIX_BUILD_TOP'} = $ENV{'TMPDIR'} || "/tmp"; $ENV{'NIX_BUILD_TOP'} = $ENV{'TMPDIR'} || "/tmp";
$ENV{$_} = $drv->{env}->{$_} foreach keys %{$drv->{env}}; $ENV{$_} = $drv->{env}->{$_} foreach keys %{$drv->{env}};
@ -180,9 +193,9 @@ foreach my $expr (@exprs) {
writeFile( writeFile(
$rcfile, $rcfile,
'[ -e ~/.bashrc ] && source ~/.bashrc; ' . '[ -e ~/.bashrc ] && source ~/.bashrc; ' .
'p=$PATH; ' . ($pure ? '' : 'p=$PATH; ' ).
'[ -e $stdenv/setup ] && source $stdenv/setup; ' . '[ -e $stdenv/setup ] && source $stdenv/setup; ' .
'PATH=$PATH:$p; ' . ($pure ? '' : 'PATH=$PATH:$p; ') .
'set +e; ' . 'set +e; ' .
'PS1="\n\[\033[1;32m\][nix-build:\w]$\[\033[0m\] "; ' . 'PS1="\n\[\033[1;32m\][nix-build:\w]$\[\033[0m\] "; ' .
$envCommand); $envCommand);