2012-01-05 20:33:46 +00:00
|
|
|
|
#! @perl@ -w @perlFlags@
|
2006-03-03 14:15:02 +00:00
|
|
|
|
|
|
|
|
|
use strict;
|
2012-01-03 00:47:27 +00:00
|
|
|
|
use Nix::Config;
|
2012-03-19 03:14:21 +00:00
|
|
|
|
use Nix::Store;
|
2013-07-11 12:32:22 +00:00
|
|
|
|
use Nix::Utils;
|
2012-01-13 23:35:07 +00:00
|
|
|
|
use File::Temp qw(tempdir);
|
2008-11-20 15:44:59 +00:00
|
|
|
|
|
2006-03-03 14:15:02 +00:00
|
|
|
|
|
2008-08-04 13:46:01 +00:00
|
|
|
|
my $dryRun = 0;
|
2009-12-09 18:08:28 +00:00
|
|
|
|
my $verbose = 0;
|
2012-03-19 03:14:21 +00:00
|
|
|
|
my $runEnv = 0;
|
2013-07-19 09:23:32 +00:00
|
|
|
|
my $pure = 0;
|
2008-08-04 13:46:01 +00:00
|
|
|
|
|
2006-03-03 14:15:02 +00:00
|
|
|
|
my @instArgs = ();
|
|
|
|
|
my @buildArgs = ();
|
|
|
|
|
my @exprs = ();
|
|
|
|
|
|
2012-04-10 11:52:37 +00:00
|
|
|
|
my $shell = $ENV{SHELL} || "/bin/sh";
|
2013-07-11 12:32:22 +00:00
|
|
|
|
my $envCommand = ""; # interactive shell
|
2012-03-27 09:40:47 +00:00
|
|
|
|
my @envExclude = ();
|
2012-03-27 09:16:43 +00:00
|
|
|
|
|
2006-03-03 14:15:02 +00:00
|
|
|
|
|
2012-11-26 16:57:14 +00:00
|
|
|
|
my $tmpDir = tempdir("nix-build.XXXXXX", CLEANUP => 1, TMPDIR => 1)
|
2012-01-13 23:35:07 +00:00
|
|
|
|
or die "cannot create a temporary directory";
|
2006-03-03 14:15:02 +00:00
|
|
|
|
|
2012-01-13 23:35:07 +00:00
|
|
|
|
my $outLink = "./result";
|
|
|
|
|
my $drvLink = "$tmpDir/derivation";
|
2006-03-03 14:15:02 +00:00
|
|
|
|
|
2012-01-13 23:35:07 +00:00
|
|
|
|
# Ensure that the $tmpDir is deleted.
|
|
|
|
|
$SIG{'INT'} = sub { exit 1 };
|
2006-03-03 14:15:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (my $n = 0; $n < scalar @ARGV; $n++) {
|
|
|
|
|
my $arg = $ARGV[$n];
|
|
|
|
|
|
|
|
|
|
if ($arg eq "--help") {
|
2012-10-03 20:37:06 +00:00
|
|
|
|
exec "man nix-build" or die;
|
2006-03-03 14:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-11 22:07:41 +00:00
|
|
|
|
elsif ($arg eq "--version") {
|
|
|
|
|
print "nix-build (Nix) $Nix::Config::version\n";
|
|
|
|
|
exit 0;
|
|
|
|
|
}
|
2012-10-03 20:37:06 +00:00
|
|
|
|
|
2006-03-03 14:15:02 +00:00
|
|
|
|
elsif ($arg eq "--add-drv-link") {
|
2012-01-13 23:35:07 +00:00
|
|
|
|
$drvLink = "./derivation";
|
2006-03-03 14:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elsif ($arg eq "--no-out-link" or $arg eq "--no-link") {
|
2012-01-13 23:35:07 +00:00
|
|
|
|
$outLink = "$tmpDir/result";
|
2006-03-03 14:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-03-14 16:35:01 +00:00
|
|
|
|
elsif ($arg eq "--drv-link") {
|
|
|
|
|
$n++;
|
|
|
|
|
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
|
|
|
|
|
$drvLink = $ARGV[$n];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elsif ($arg eq "--out-link" or $arg eq "-o") {
|
|
|
|
|
$n++;
|
|
|
|
|
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
|
|
|
|
|
$outLink = $ARGV[$n];
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-06 16:05:24 +00:00
|
|
|
|
elsif ($arg eq "--attr" or $arg eq "-A" or $arg eq "-I") {
|
2006-03-03 14:15:02 +00:00
|
|
|
|
$n++;
|
2009-03-18 13:15:55 +00:00
|
|
|
|
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
|
2011-08-06 16:05:24 +00:00
|
|
|
|
push @instArgs, ($arg, $ARGV[$n]);
|
2006-03-03 14:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-15 16:52:40 +00:00
|
|
|
|
elsif ($arg eq "--arg" || $arg eq "--argstr") {
|
|
|
|
|
die "$0: `$arg' requires two arguments\n" unless $n + 2 < scalar @ARGV;
|
|
|
|
|
push @instArgs, ($arg, $ARGV[$n + 1], $ARGV[$n + 2]);
|
2006-07-28 16:03:28 +00:00
|
|
|
|
$n += 2;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-18 13:15:55 +00:00
|
|
|
|
elsif ($arg eq "--log-type") {
|
|
|
|
|
$n++;
|
|
|
|
|
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
|
|
|
|
|
push @instArgs, ($arg, $ARGV[$n]);
|
|
|
|
|
push @buildArgs, ($arg, $ARGV[$n]);
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-27 12:09:30 +00:00
|
|
|
|
elsif ($arg eq "--option") {
|
|
|
|
|
die "$0: `$arg' requires two arguments\n" unless $n + 2 < scalar @ARGV;
|
|
|
|
|
push @instArgs, ($arg, $ARGV[$n + 1], $ARGV[$n + 2]);
|
|
|
|
|
push @buildArgs, ($arg, $ARGV[$n + 1], $ARGV[$n + 2]);
|
|
|
|
|
$n += 2;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-23 15:16:01 +00:00
|
|
|
|
elsif ($arg eq "--max-jobs" or $arg eq "-j" or $arg eq "--max-silent-time" or $arg eq "--log-type" or $arg eq "--cores" or $arg eq "--timeout") {
|
2006-12-08 15:44:00 +00:00
|
|
|
|
$n++;
|
|
|
|
|
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
|
|
|
|
|
push @buildArgs, ($arg, $ARGV[$n]);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-04 13:46:01 +00:00
|
|
|
|
elsif ($arg eq "--dry-run") {
|
|
|
|
|
push @buildArgs, "--dry-run";
|
|
|
|
|
$dryRun = 1;
|
|
|
|
|
}
|
2012-08-24 20:58:11 +00:00
|
|
|
|
|
2009-07-15 09:10:38 +00:00
|
|
|
|
elsif ($arg eq "--show-trace") {
|
|
|
|
|
push @instArgs, $arg;
|
|
|
|
|
}
|
2012-07-23 21:11:12 +00:00
|
|
|
|
|
|
|
|
|
elsif ($arg eq "-") {
|
|
|
|
|
@exprs = ("-");
|
|
|
|
|
}
|
2012-08-24 20:58:11 +00:00
|
|
|
|
|
2009-12-09 18:08:28 +00:00
|
|
|
|
elsif ($arg eq "--verbose" or substr($arg, 0, 2) eq "-v") {
|
|
|
|
|
push @buildArgs, $arg;
|
|
|
|
|
push @instArgs, $arg;
|
|
|
|
|
$verbose = 1;
|
|
|
|
|
}
|
2012-08-24 20:58:11 +00:00
|
|
|
|
|
2012-10-03 19:14:02 +00:00
|
|
|
|
elsif ($arg eq "--quiet" || $arg eq "--repair") {
|
2010-08-30 14:53:03 +00:00
|
|
|
|
push @buildArgs, $arg;
|
|
|
|
|
push @instArgs, $arg;
|
|
|
|
|
}
|
2012-08-24 20:58:11 +00:00
|
|
|
|
|
2012-03-19 03:14:21 +00:00
|
|
|
|
elsif ($arg eq "--run-env") {
|
|
|
|
|
$runEnv = 1;
|
|
|
|
|
}
|
2012-08-24 20:58:11 +00:00
|
|
|
|
|
2012-03-27 09:16:43 +00:00
|
|
|
|
elsif ($arg eq "--command") {
|
|
|
|
|
$n++;
|
|
|
|
|
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
|
2013-07-11 12:32:22 +00:00
|
|
|
|
$envCommand = "$ARGV[$n]\nexit $!";
|
2012-03-27 09:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-27 09:40:47 +00:00
|
|
|
|
elsif ($arg eq "--exclude") {
|
|
|
|
|
$n++;
|
|
|
|
|
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
|
|
|
|
|
push @envExclude, $ARGV[$n];
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-19 09:23:32 +00:00
|
|
|
|
elsif ($arg eq "--pure") {
|
|
|
|
|
$pure = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-03 14:15:02 +00:00
|
|
|
|
elsif (substr($arg, 0, 1) eq "-") {
|
|
|
|
|
push @buildArgs, $arg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
push @exprs, $arg;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@exprs = ("./default.nix") if scalar @exprs == 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach my $expr (@exprs) {
|
|
|
|
|
|
|
|
|
|
# Instantiate.
|
2006-07-28 16:03:28 +00:00
|
|
|
|
my @drvPaths;
|
2006-08-04 11:51:46 +00:00
|
|
|
|
# !!! would prefer the perl 5.8.0 pipe open feature here.
|
2012-01-03 00:47:27 +00:00
|
|
|
|
my $pid = open(DRVPATHS, "-|") || exec "$Nix::Config::binDir/nix-instantiate", "--add-root", $drvLink, "--indirect", @instArgs, $expr;
|
2006-07-28 16:03:28 +00:00
|
|
|
|
while (<DRVPATHS>) {chomp; push @drvPaths, $_;}
|
2009-06-10 11:30:34 +00:00
|
|
|
|
if (!close DRVPATHS) {
|
|
|
|
|
die "nix-instantiate killed by signal " . ($? & 127) . "\n" if ($? & 127);
|
|
|
|
|
exit 1;
|
|
|
|
|
}
|
2006-03-03 14:15:02 +00:00
|
|
|
|
|
2012-03-19 03:14:21 +00:00
|
|
|
|
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.
|
2012-03-27 09:40:47 +00:00
|
|
|
|
my @inputDrvs = grep { my $x = $_; (grep { $x =~ $_ } @envExclude) == 0 } @{$drv->{inputDrvs}};
|
|
|
|
|
system("$Nix::Config::binDir/nix-store -r @buildArgs @inputDrvs @{$drv->{inputSrcs}} > /dev/null") == 0
|
2012-03-19 03:14:21 +00:00
|
|
|
|
or die "$0: failed to build all dependencies\n";
|
|
|
|
|
|
|
|
|
|
# Set the environment.
|
2013-07-19 09:23:32 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2012-03-19 03:14:21 +00:00
|
|
|
|
$ENV{'NIX_BUILD_TOP'} = $ENV{'TMPDIR'} || "/tmp";
|
2013-07-11 12:32:22 +00:00
|
|
|
|
$ENV{$_} = $drv->{env}->{$_} foreach keys %{$drv->{env}};
|
2012-03-19 03:14:21 +00:00
|
|
|
|
|
|
|
|
|
# Run a shell using the derivation's environment. For
|
|
|
|
|
# convenience, source $stdenv/setup to setup additional
|
2013-07-11 12:32:22 +00:00
|
|
|
|
# environment variables and shell functions. Also don't lose
|
|
|
|
|
# the current $PATH directories.
|
|
|
|
|
my $rcfile = "$tmpDir/rc";
|
|
|
|
|
writeFile(
|
|
|
|
|
$rcfile,
|
|
|
|
|
'[ -e ~/.bashrc ] && source ~/.bashrc; ' .
|
2013-07-19 09:23:32 +00:00
|
|
|
|
($pure ? '' : 'p=$PATH; ' ).
|
2013-07-11 12:32:22 +00:00
|
|
|
|
'[ -e $stdenv/setup ] && source $stdenv/setup; ' .
|
2013-07-19 09:23:32 +00:00
|
|
|
|
($pure ? '' : 'PATH=$PATH:$p; ') .
|
2013-07-11 12:32:22 +00:00
|
|
|
|
'set +e; ' .
|
|
|
|
|
'PS1="\n\[\033[1;32m\][nix-build:\w]$\[\033[0m\] "; ' .
|
|
|
|
|
$envCommand);
|
2013-07-11 12:40:29 +00:00
|
|
|
|
exec($ENV{NIX_BUILD_SHELL} // "bash", "--rcfile", $rcfile);
|
2012-03-19 03:14:21 +00:00
|
|
|
|
die;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-26 14:39:10 +00:00
|
|
|
|
# Ugly hackery to make "nix-build -A foo.all" produce symlinks
|
|
|
|
|
# ./result, ./result-dev, and so on, rather than ./result,
|
|
|
|
|
# ./result-2-dev, and so on. This combines multiple derivation
|
|
|
|
|
# paths into one "/nix/store/drv-path!out1,out2,..." argument.
|
|
|
|
|
my $prevDrvPath = "";
|
|
|
|
|
my @drvPaths2;
|
2006-03-03 14:15:02 +00:00
|
|
|
|
foreach my $drvPath (@drvPaths) {
|
2012-11-26 14:39:10 +00:00
|
|
|
|
my $p = $drvPath; my $output = "out";
|
|
|
|
|
if ($drvPath =~ /(.*)!(.*)/) {
|
|
|
|
|
$p = $1; $output = $2;
|
|
|
|
|
} else {
|
|
|
|
|
$p = $drvPath;
|
|
|
|
|
}
|
|
|
|
|
my $target = readlink $p or die "cannot read symlink `$p'";
|
2009-12-09 18:08:28 +00:00
|
|
|
|
print STDERR "derivation is $target\n" if $verbose;
|
2012-11-26 14:39:10 +00:00
|
|
|
|
if ($target eq $prevDrvPath) {
|
|
|
|
|
push @drvPaths2, (pop @drvPaths2) . "," . $output;
|
|
|
|
|
} else {
|
|
|
|
|
push @drvPaths2, $target . "!" . $output;
|
|
|
|
|
$prevDrvPath = $target;
|
|
|
|
|
}
|
2006-03-03 14:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Build.
|
2006-10-19 17:30:09 +00:00
|
|
|
|
my @outPaths;
|
2012-01-03 00:47:27 +00:00
|
|
|
|
$pid = open(OUTPATHS, "-|") || exec "$Nix::Config::binDir/nix-store", "--add-root", $outLink, "--indirect", "-r",
|
2012-11-26 14:39:10 +00:00
|
|
|
|
@buildArgs, @drvPaths2;
|
2006-10-19 17:30:09 +00:00
|
|
|
|
while (<OUTPATHS>) {chomp; push @outPaths, $_;}
|
2009-06-10 11:30:34 +00:00
|
|
|
|
if (!close OUTPATHS) {
|
|
|
|
|
die "nix-store killed by signal " . ($? & 127) . "\n" if ($? & 127);
|
|
|
|
|
exit 1;
|
|
|
|
|
}
|
2006-10-19 17:30:09 +00:00
|
|
|
|
|
2008-08-04 13:46:01 +00:00
|
|
|
|
next if $dryRun;
|
|
|
|
|
|
2006-03-03 14:15:02 +00:00
|
|
|
|
foreach my $outPath (@outPaths) {
|
2007-06-11 11:36:22 +00:00
|
|
|
|
my $target = readlink $outPath or die "cannot read symlink `$outPath'";
|
2006-03-03 14:15:02 +00:00
|
|
|
|
print "$target\n";
|
|
|
|
|
}
|
|
|
|
|
}
|