about summary refs log tree commit diff
path: root/scripts/nix-build.in
blob: 60e8afec8ee226a5cc79570563aab9e0831bde8a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#! @perl@ -w @perlFlags@

use strict;
use Nix::Config;
use Nix::Store;
use Nix::Utils;
use File::Temp qw(tempdir);


my $dryRun = 0;
my $verbose = 0;
my $runEnv = $0 =~ /nix-shell$/;
my $pure = 0;

my @instArgs = ();
my @buildArgs = ();
my @exprs = ();

my $shell = $ENV{SHELL} || "/bin/sh";
my $envCommand = ""; # interactive shell
my @envExclude = ();

my $myName = $runEnv ? "nix-shell" : "nix-build";


my $tmpDir = tempdir("$myName.XXXXXX", CLEANUP => 1, TMPDIR => 1)
    or die "cannot create a temporary directory";

my $outLink = "./result";
my $drvLink = "$tmpDir/derivation";

# Ensure that the $tmpDir is deleted.
$SIG{'INT'} = sub { exit 1 };


for (my $n = 0; $n < scalar @ARGV; $n++) {
    my $arg = $ARGV[$n];

    if ($arg eq "--help") {
        exec "man $myName" or die;
    }

    elsif ($arg eq "--version") {
        print "$myName (Nix) $Nix::Config::version\n";
        exit 0;
    }

    elsif ($arg eq "--add-drv-link") {
        $drvLink = "./derivation";
    }

    elsif ($arg eq "--no-out-link" or $arg eq "--no-link") {
        $outLink = "$tmpDir/result";
    }

    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];
    }

    elsif ($arg eq "--attr" or $arg eq "-A" or $arg eq "-I") {
        $n++;
        die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
        push @instArgs, ($arg, $ARGV[$n]);
    }

    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]);
        $n += 2;
    }

    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]);
    }

    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;
    }

    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") {
        $n++;
        die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
        push @buildArgs, ($arg, $ARGV[$n]);
    }

    elsif ($arg eq "--dry-run") {
        push @buildArgs, "--dry-run";
        $dryRun = 1;
    }

    elsif ($arg eq "--show-trace") {
        push @instArgs, $arg;
    }

    elsif ($arg eq "-") {
        @exprs = ("-");
    }

    elsif ($arg eq "--verbose" or substr($arg, 0, 2) eq "-v") {
        push @buildArgs, $arg;
        push @instArgs, $arg;
        $verbose = 1;
    }

    elsif ($arg eq "--quiet" || $arg eq "--repair") {
        push @buildArgs, $arg;
        push @instArgs, $arg;
    }

    elsif ($arg eq "--run-env") { # obsolete
        $runEnv = 1;
    }

    elsif ($arg eq "--command") {
        $n++;
        die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
        $envCommand = "$ARGV[$n]\nexit $!";
    }

    elsif ($arg eq "--exclude") {
        $n++;
        die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
        push @envExclude, $ARGV[$n];
    }

    elsif ($arg eq "--pure") {
        $pure = 1;
    }

    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.
    my @drvPaths;
    if ($expr !~ /^\/.*\.drv$/) {
        # !!! would prefer the perl 5.8.0 pipe open feature here.
        my $pid = open(DRVPATHS, "-|") || exec "$Nix::Config::binDir/nix-instantiate", "--add-root", $drvLink, "--indirect", @instArgs, $expr;
        while (<DRVPATHS>) {chomp; push @drvPaths, $_;}
        if (!close DRVPATHS) {
            die "nix-instantiate killed by signal " . ($? & 127) . "\n" if ($? & 127);
            exit 1;
        }
    } else {
        push @drvPaths, $expr;
    }

    if ($runEnv) {
        die "$0: a single derivation is required\n" if scalar @drvPaths != 1;
        my $drvPath = $drvPaths[0];
        $drvPath = readlink $drvPath or die "cannot read symlink `$drvPath'" if -l $drvPath;
        my $drv = derivationFromPath($drvPath);

        $ENV{'IN_NIX_SHELL'} = 1;

        # Build or fetch all dependencies of the derivation.
        my @inputDrvs = grep { my $x = $_; (grep { $x =~ $_ } @envExclude) == 0 } @{$drv->{inputDrvs}};
        system("$Nix::Config::binDir/nix-store", "-r", "--no-output", @buildArgs, @inputDrvs, @{$drv->{inputSrcs}}) == 0
            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" || $name eq "TERM";
                delete $ENV{$name};
            }
            # NixOS hack: prevent /etc/bashrc from sourcing /etc/profile.
            $ENV{'__ETC_PROFILE_SOURCED'} = 1;
        }
        $ENV{'NIX_BUILD_TOP'} = $ENV{'TMPDIR'} = $ENV{'TEMPDIR'} = $ENV{'TMP'} = $ENV{'TEMP'} = $ENV{'TMPDIR'} // "/tmp";
        $ENV{'NIX_STORE'} = $Nix::Config::storeDir;
        $ENV{$_} = $drv->{env}->{$_} foreach keys %{$drv->{env}};

        # Run a shell using the derivation's environment.  For
        # convenience, source $stdenv/setup to setup additional
        # environment variables and shell functions.  Also don't lose
        # the current $PATH directories.
        my $rcfile = "$tmpDir/rc";
        writeFile(
            $rcfile,
            'unset BASH_ENV; ' .
            '[ -n "$PS1" ] && [ -e ~/.bashrc ] && source ~/.bashrc; ' .
            ($pure ? '' : 'p=$PATH; ' ) .
            'dontAddDisableDepTrack=1; ' .
            '[ -e $stdenv/setup ] && source $stdenv/setup; ' .
            ($pure ? '' : 'PATH=$PATH:$p; ') .
            'set +e; ' .
            '[ -n "$PS1" ] && PS1="\n\[\033[1;32m\][nix-shell:\w]$\[\033[0m\] "; ' .
            'unset NIX_ENFORCE_PURITY; ' .
            'shopt -u nullglob; ' .
            $envCommand);
        $ENV{BASH_ENV} = $rcfile;
        exec($ENV{NIX_BUILD_SHELL} // "bash", "--rcfile", $rcfile);
        die;
    }

    # 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;
    foreach my $drvPath (@drvPaths) {
        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'";
        print STDERR "derivation is $target\n" if $verbose;
        if ($target eq $prevDrvPath) {
            push @drvPaths2, (pop @drvPaths2) . "," . $output;
        } else {
            push @drvPaths2, $target . "!" . $output;
            $prevDrvPath = $target;
        }
    }

    # Build.
    my @outPaths;
    my $pid = open(OUTPATHS, "-|") || exec "$Nix::Config::binDir/nix-store", "--add-root", $outLink, "--indirect", "-r",
        @buildArgs, @drvPaths2;
    while (<OUTPATHS>) {chomp; push @outPaths, $_;}
    if (!close OUTPATHS) {
        die "nix-store killed by signal " . ($? & 127) . "\n" if ($? & 127);
        exit 1;
    }

    next if $dryRun;

    foreach my $outPath (@outPaths) {
        my $target = readlink $outPath or die "cannot read symlink `$outPath'";
        print "$target\n";
    }
}