From dc5f2e7da607bdf50bf710cbe0b5f6ff32980e19 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 19 Jul 2013 11:23:32 +0200 Subject: [PATCH] =?UTF-8?q?nix-build=20--run-env:=20Add=20a=20=E2=80=98--p?= =?UTF-8?q?ure=E2=80=99=20flag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This causes the environment to be (almost) cleared, thus giving a shell that more closely resembled the actual Nix derivation. --- doc/manual/nix-build.xml | 29 ++++++++++++++++++++++++++++- scripts/nix-build.in | 17 +++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/doc/manual/nix-build.xml b/doc/manual/nix-build.xml index 6640898b4..61b59c1e0 100644 --- a/doc/manual/nix-build.xml +++ b/doc/manual/nix-build.xml @@ -42,6 +42,7 @@ cmd regexp + paths @@ -147,7 +148,10 @@ also . In the environment of the derivation, run the shell command cmd instead of starting - an interactive shell. + an interactive shell. However, if you end the shell command with + return, you still get an interactive shell. + This can be useful for doing any additional + initialisation. @@ -159,6 +163,21 @@ also . + + + 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 + HOME, USER and + DISPLAY, are retained. Note that + ~/.bashrc and (depending on your Bash + installation) /etc/bashrc are still sourced, + so any variables set there will affect the interactive + shell. + + + @@ -189,6 +208,14 @@ $ buildPhase $ ./pan/gui/pan +To clear the environment first, and do some additional automatic +initialisation of the interactive shell: + + +$ nix-build '<nixpkgs>' --run-env -A pan --pure \ + --command 'export NIX_DEBUG=1; export NIX_CORES=8; return' + + If a derivation has multiple outputs, diff --git a/scripts/nix-build.in b/scripts/nix-build.in index fb70331bf..899882952 100755 --- a/scripts/nix-build.in +++ b/scripts/nix-build.in @@ -10,6 +10,7 @@ use File::Temp qw(tempdir); my $dryRun = 0; my $verbose = 0; my $runEnv = 0; +my $pure = 0; my @instArgs = (); my @buildArgs = (); @@ -134,6 +135,10 @@ for (my $n = 0; $n < scalar @ARGV; $n++) { push @envExclude, $ARGV[$n]; } + elsif ($arg eq "--pure") { + $pure = 1; + } + elsif (substr($arg, 0, 1) eq "-") { push @buildArgs, $arg; } @@ -169,6 +174,14 @@ foreach my $expr (@exprs) { or die "$0: failed to build all dependencies\n"; # 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{$_} = $drv->{env}->{$_} foreach keys %{$drv->{env}}; @@ -180,9 +193,9 @@ foreach my $expr (@exprs) { writeFile( $rcfile, '[ -e ~/.bashrc ] && source ~/.bashrc; ' . - 'p=$PATH; ' . + ($pure ? '' : 'p=$PATH; ' ). '[ -e $stdenv/setup ] && source $stdenv/setup; ' . - 'PATH=$PATH:$p; ' . + ($pure ? '' : 'PATH=$PATH:$p; ') . 'set +e; ' . 'PS1="\n\[\033[1;32m\][nix-build:\w]$\[\033[0m\] "; ' . $envCommand);