2012-01-05 22:33:46 +02:00
|
|
|
|
#! @perl@ -w @perlFlags@
|
2006-03-03 16:15:02 +02:00
|
|
|
|
|
|
|
|
|
use strict;
|
2012-01-03 02:47:27 +02:00
|
|
|
|
use Nix::Config;
|
2012-03-19 05:14:21 +02:00
|
|
|
|
use Nix::Store;
|
2012-01-14 01:35:07 +02:00
|
|
|
|
use File::Temp qw(tempdir);
|
2008-11-20 17:44:59 +02:00
|
|
|
|
|
2006-03-03 16:15:02 +02:00
|
|
|
|
|
2008-08-04 16:46:01 +03:00
|
|
|
|
my $dryRun = 0;
|
2009-12-09 20:08:28 +02:00
|
|
|
|
my $verbose = 0;
|
2012-03-19 05:14:21 +02:00
|
|
|
|
my $runEnv = 0;
|
2008-08-04 16:46:01 +03:00
|
|
|
|
|
2006-03-03 16:15:02 +02:00
|
|
|
|
my @instArgs = ();
|
|
|
|
|
my @buildArgs = ();
|
|
|
|
|
my @exprs = ();
|
|
|
|
|
|
2012-04-10 14:52:37 +03:00
|
|
|
|
my $shell = $ENV{SHELL} || "/bin/sh";
|
|
|
|
|
my $envCommand = "p=\$PATH; source \$stdenv/setup; PATH=\$PATH:\$p; exec $shell";
|
2012-03-27 12:40:47 +03:00
|
|
|
|
my @envExclude = ();
|
2012-03-27 12:16:43 +03:00
|
|
|
|
|
2006-03-03 16:15:02 +02:00
|
|
|
|
|
2012-01-14 01:35:07 +02:00
|
|
|
|
my $tmpDir = tempdir("nix-build.XXXXXX", CLEANUP => 1, TMPDIR => 1)
|
|
|
|
|
or die "cannot create a temporary directory";
|
2006-03-03 16:15:02 +02:00
|
|
|
|
|
2012-01-14 01:35:07 +02:00
|
|
|
|
my $outLink = "./result";
|
|
|
|
|
my $drvLink = "$tmpDir/derivation";
|
2006-03-03 16:15:02 +02:00
|
|
|
|
|
2012-01-14 01:35:07 +02:00
|
|
|
|
# Ensure that the $tmpDir is deleted.
|
|
|
|
|
$SIG{'INT'} = sub { exit 1 };
|
2006-03-03 16:15:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (my $n = 0; $n < scalar @ARGV; $n++) {
|
|
|
|
|
my $arg = $ARGV[$n];
|
|
|
|
|
|
|
|
|
|
if ($arg eq "--help") {
|
2006-03-03 16:25:07 +02:00
|
|
|
|
print STDERR <<EOF;
|
|
|
|
|
Usage: nix-build [OPTION]... [FILE]...
|
|
|
|
|
|
|
|
|
|
`nix-build' builds the given Nix expressions (which
|
|
|
|
|
default to ./default.nix if none are given). A symlink called
|
|
|
|
|
`result' is placed in the current directory.
|
|
|
|
|
|
|
|
|
|
Flags:
|
|
|
|
|
--add-drv-link: create a symlink `derivation' to the store derivation
|
2006-03-14 18:35:01 +02:00
|
|
|
|
--drv-link NAME: create symlink NAME instead of `derivation'
|
2006-03-03 16:25:07 +02:00
|
|
|
|
--no-out-link: do not create the `result' symlink
|
2006-03-14 18:35:01 +02:00
|
|
|
|
--out-link / -o NAME: create symlink NAME instead of `result'
|
2012-03-27 12:59:36 +03:00
|
|
|
|
--attr / -A ATTR: select a specific attribute from the Nix expression
|
|
|
|
|
|
|
|
|
|
--run-env: build dependencies of the specified derivation, then start a
|
|
|
|
|
shell with the environment of the derivation
|
|
|
|
|
--command: command to run with `--run-env'
|
|
|
|
|
--exclude: regexp specifying dependencies to be excluded by `--run-env'
|
2006-03-03 16:25:07 +02:00
|
|
|
|
|
|
|
|
|
Any additional flags are passed to `nix-store'.
|
|
|
|
|
EOF
|
2006-03-03 16:15:02 +02:00
|
|
|
|
exit 0;
|
2006-03-14 18:35:01 +02:00
|
|
|
|
# '` hack
|
2006-03-03 16:15:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-12 01:07:41 +03:00
|
|
|
|
elsif ($arg eq "--version") {
|
|
|
|
|
print "nix-build (Nix) $Nix::Config::version\n";
|
|
|
|
|
exit 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-03 16:15:02 +02:00
|
|
|
|
elsif ($arg eq "--add-drv-link") {
|
2012-01-14 01:35:07 +02:00
|
|
|
|
$drvLink = "./derivation";
|
2006-03-03 16:15:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elsif ($arg eq "--no-out-link" or $arg eq "--no-link") {
|
2012-01-14 01:35:07 +02:00
|
|
|
|
$outLink = "$tmpDir/result";
|
2006-03-03 16:15:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2006-03-14 18:35:01 +02: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 19:05:24 +03:00
|
|
|
|
elsif ($arg eq "--attr" or $arg eq "-A" or $arg eq "-I") {
|
2006-03-03 16:15:02 +02:00
|
|
|
|
$n++;
|
2009-03-18 15:15:55 +02:00
|
|
|
|
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
|
2011-08-06 19:05:24 +03:00
|
|
|
|
push @instArgs, ($arg, $ARGV[$n]);
|
2006-03-03 16:15:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-15 18:52:40 +02: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 19:03:28 +03:00
|
|
|
|
$n += 2;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-18 15:15:55 +02: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 14:09:30 +02: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;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-11 18:28:02 +03: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") {
|
2006-12-08 17:44:00 +02:00
|
|
|
|
$n++;
|
|
|
|
|
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
|
|
|
|
|
push @buildArgs, ($arg, $ARGV[$n]);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-04 16:46:01 +03:00
|
|
|
|
elsif ($arg eq "--dry-run") {
|
|
|
|
|
push @buildArgs, "--dry-run";
|
|
|
|
|
$dryRun = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-15 12:10:38 +03:00
|
|
|
|
elsif ($arg eq "--show-trace") {
|
|
|
|
|
push @instArgs, $arg;
|
|
|
|
|
}
|
2012-07-24 00:11:12 +03:00
|
|
|
|
|
|
|
|
|
elsif ($arg eq "-") {
|
|
|
|
|
@exprs = ("-");
|
|
|
|
|
}
|
2009-07-15 12:10:38 +03:00
|
|
|
|
|
2009-12-09 20:08:28 +02:00
|
|
|
|
elsif ($arg eq "--verbose" or substr($arg, 0, 2) eq "-v") {
|
|
|
|
|
push @buildArgs, $arg;
|
|
|
|
|
push @instArgs, $arg;
|
|
|
|
|
$verbose = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-30 17:53:03 +03:00
|
|
|
|
elsif ($arg eq "--quiet") {
|
|
|
|
|
push @buildArgs, $arg;
|
|
|
|
|
push @instArgs, $arg;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-19 05:14:21 +02:00
|
|
|
|
elsif ($arg eq "--run-env") {
|
|
|
|
|
$runEnv = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-27 12:16:43 +03:00
|
|
|
|
elsif ($arg eq "--command") {
|
|
|
|
|
$n++;
|
|
|
|
|
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
|
|
|
|
|
$envCommand = $ARGV[$n];
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-27 12:40:47 +03:00
|
|
|
|
elsif ($arg eq "--exclude") {
|
|
|
|
|
$n++;
|
|
|
|
|
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
|
|
|
|
|
push @envExclude, $ARGV[$n];
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-03 16:15:02 +02: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 19:03:28 +03:00
|
|
|
|
my @drvPaths;
|
2006-08-04 14:51:46 +03:00
|
|
|
|
# !!! would prefer the perl 5.8.0 pipe open feature here.
|
2012-01-03 02:47:27 +02:00
|
|
|
|
my $pid = open(DRVPATHS, "-|") || exec "$Nix::Config::binDir/nix-instantiate", "--add-root", $drvLink, "--indirect", @instArgs, $expr;
|
2006-07-28 19:03:28 +03:00
|
|
|
|
while (<DRVPATHS>) {chomp; push @drvPaths, $_;}
|
2009-06-10 14:30:34 +03:00
|
|
|
|
if (!close DRVPATHS) {
|
|
|
|
|
die "nix-instantiate killed by signal " . ($? & 127) . "\n" if ($? & 127);
|
|
|
|
|
exit 1;
|
|
|
|
|
}
|
2006-03-03 16:15:02 +02:00
|
|
|
|
|
2012-03-19 05:14:21 +02: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 12:40:47 +03: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 05:14:21 +02:00
|
|
|
|
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.
|
2012-03-27 12:16:43 +03:00
|
|
|
|
exec($ENV{SHELL}, "-c", $envCommand);
|
2012-03-19 05:14:21 +02:00
|
|
|
|
die;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-03 16:15:02 +02:00
|
|
|
|
foreach my $drvPath (@drvPaths) {
|
2007-06-11 14:36:22 +03:00
|
|
|
|
my $target = readlink $drvPath or die "cannot read symlink `$drvPath'";
|
2009-12-09 20:08:28 +02:00
|
|
|
|
print STDERR "derivation is $target\n" if $verbose;
|
2006-03-03 16:15:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Build.
|
2006-10-19 20:30:09 +03:00
|
|
|
|
my @outPaths;
|
2012-01-03 02:47:27 +02:00
|
|
|
|
$pid = open(OUTPATHS, "-|") || exec "$Nix::Config::binDir/nix-store", "--add-root", $outLink, "--indirect", "-r",
|
2006-10-19 20:30:09 +03:00
|
|
|
|
@buildArgs, @drvPaths;
|
|
|
|
|
while (<OUTPATHS>) {chomp; push @outPaths, $_;}
|
2009-06-10 14:30:34 +03:00
|
|
|
|
if (!close OUTPATHS) {
|
|
|
|
|
die "nix-store killed by signal " . ($? & 127) . "\n" if ($? & 127);
|
|
|
|
|
exit 1;
|
|
|
|
|
}
|
2006-10-19 20:30:09 +03:00
|
|
|
|
|
2008-08-04 16:46:01 +03:00
|
|
|
|
next if $dryRun;
|
|
|
|
|
|
2006-03-03 16:15:02 +02:00
|
|
|
|
foreach my $outPath (@outPaths) {
|
2007-06-11 14:36:22 +03:00
|
|
|
|
my $target = readlink $outPath or die "cannot read symlink `$outPath'";
|
2006-03-03 16:15:02 +02:00
|
|
|
|
print "$target\n";
|
|
|
|
|
}
|
|
|
|
|
}
|