Add a command "nix-build --run-env" to reproduce the environment of a derivation

This command builds or fetches all dependencies of the given
derivation, then starts a shell with the environment variables from
the derivation.  This shell also sources $stdenv/setup to initialise
the environment further.

The current directory is not changed.  Thus this is a convenient way
to reproduce a build environment in an existing working tree.

Existing environment variables are left untouched (unless the
derivation overrides them).  As a special hack, the original value of
$PATH is appended to the $PATH produced by $stdenv/setup.

Example session:

$ nix-build --run-env '<nixpkgs>' -A xterm
(the dependencies of xterm are built/fetched...)
$ tar xf $src
$ ./configure
$ make
$ emacs
(... hack source ...)
$ make
$ ./xterm
This commit is contained in:
Eelco Dolstra 2012-03-19 04:14:21 +01:00
parent ea402a255f
commit 7f38087f35
3 changed files with 72 additions and 0 deletions

View file

@ -16,6 +16,7 @@ our @EXPORT = qw(
topoSortPaths computeFSClosure followLinksToStorePath exportPaths topoSortPaths computeFSClosure followLinksToStorePath exportPaths
hashPath hashFile hashString hashPath hashFile hashString
addToStore makeFixedOutputPath addToStore makeFixedOutputPath
derivationFromPath
); );
our $VERSION = '0.15'; our $VERSION = '0.15';

View file

@ -216,3 +216,45 @@ SV * makeFixedOutputPath(int recursive, char * algo, char * hash, char * name)
} catch (Error & e) { } catch (Error & e) {
croak(e.what()); croak(e.what());
} }
SV * derivationFromPath(char * drvPath)
PREINIT:
HV *hash;
CODE:
try {
doInit();
Derivation drv = derivationFromPath(*store, drvPath);
hash = newHV();
/* TODO: handle drv.outputs */
AV * inputDrvs = newAV();
for (DerivationInputs::iterator i = drv.inputDrvs.begin(); i != drv.inputDrvs.end(); ++i)
av_push(inputDrvs, newSVpv(i->first.c_str(), 0)); // !!! ignores i->second
hv_stores(hash, "inputDrvs", newRV((SV *) inputDrvs));
AV * inputSrcs = newAV();
for (PathSet::iterator i = drv.inputSrcs.begin(); i != drv.inputSrcs.end(); ++i)
av_push(inputSrcs, newSVpv(i->c_str(), 0));
hv_stores(hash, "inputSrcs", newRV((SV *) inputSrcs));
hv_stores(hash, "platform", newSVpv(drv.platform.c_str(), 0));
hv_stores(hash, "builder", newSVpv(drv.builder.c_str(), 0));
AV * args = newAV();
for (Strings::iterator i = drv.args.begin(); i != drv.args.end(); ++i)
av_push(args, newSVpv(i->c_str(), 0));
hv_stores(hash, "args", newRV((SV *) args));
HV * env = newHV();
for (StringPairs::iterator i = drv.env.begin(); i != drv.env.end(); ++i)
hv_store(env, i->first.c_str(), i->first.size(), newSVpv(i->second.c_str(), 0), 0);
hv_stores(hash, "env", newRV((SV *) env));
RETVAL = newRV_noinc((SV *)hash);
} catch (Error & e) {
croak(e.what());
}
OUTPUT:
RETVAL

View file

@ -2,11 +2,13 @@
use strict; use strict;
use Nix::Config; use Nix::Config;
use Nix::Store;
use File::Temp qw(tempdir); use File::Temp qw(tempdir);
my $dryRun = 0; my $dryRun = 0;
my $verbose = 0; my $verbose = 0;
my $runEnv = 0;
my @instArgs = (); my @instArgs = ();
my @buildArgs = (); my @buildArgs = ();
@ -119,6 +121,10 @@ EOF
push @instArgs, $arg; push @instArgs, $arg;
} }
elsif ($arg eq "--run-env") {
$runEnv = 1;
}
elsif (substr($arg, 0, 1) eq "-") { elsif (substr($arg, 0, 1) eq "-") {
push @buildArgs, $arg; push @buildArgs, $arg;
} }
@ -143,6 +149,29 @@ foreach my $expr (@exprs) {
exit 1; exit 1;
} }
if ($runEnv) {
die "$0: --run-env requires a single derivation\n" if scalar @drvPaths != 1;
my $drvPath = readlink $drvPaths[0] or die "cannot read symlink `$drvPaths[0]'";
my $drv = derivationFromPath($drvPath);
# Build or fetch all dependencies of the derivation.
system("$Nix::Config::binDir/nix-store -r @buildArgs @{$drv->{inputDrvs}} @{$drv->{inputSrcs}} > /dev/null") == 0
or die "$0: failed to build all dependencies\n";
# Set the environment.
$ENV{'NIX_BUILD_TOP'} = $ENV{'TMPDIR'} || "/tmp";
foreach (keys %{$drv->{env}}) {
$ENV{$_} = $drv->{env}->{$_};
}
# Run a shell using the derivation's environment. For
# convenience, source $stdenv/setup to setup additional
# environment variables. Also don't lose the current $PATH
# directories.
exec($ENV{SHELL}, "-c", "p=\$PATH; source \$stdenv/setup; PATH=\$PATH:\$p; exec $ENV{SHELL}");
die;
}
foreach my $drvPath (@drvPaths) { foreach my $drvPath (@drvPaths) {
my $target = readlink $drvPath or die "cannot read symlink `$drvPath'"; my $target = readlink $drvPath or die "cannot read symlink `$drvPath'";
print STDERR "derivation is $target\n" if $verbose; print STDERR "derivation is $target\n" if $verbose;