about summary refs log tree commit diff
path: root/scripts/nix-build.in
blob: d90b902aeb0950be630442b1c33bca2f256f919d (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
#! @perl@ -w -I@libexecdir@/nix

use strict;

my $binDir = $ENV{"NIX_BIN_DIR"} || "@bindir@";


my $addDrvLink = 0;
my $addOutLink = 1;

my $outLink;
my $drvLink;

my $dryRun = 0;

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


END {
    foreach my $fn (glob ".nix-build-tmp-*") {
        unlink $fn;
    }
}

sub intHandler {
    exit 1;
}

$SIG{'INT'} = 'intHandler';


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

    if ($arg eq "--help") {
        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
  --drv-link NAME: create symlink NAME instead of `derivation'
  --no-out-link: do not create the `result' symlink
  --out-link / -o NAME: create symlink NAME instead of `result'
  --attr ATTR: select a specific attribution from the Nix expression

Any additional flags are passed to `nix-store'.
EOF
        exit 0;
        # '` hack
    }

    elsif ($arg eq "--add-drv-link") {
        $addDrvLink = 1;
    }

    elsif ($arg eq "--no-out-link" or $arg eq "--no-link") {
        $addOutLink = 0;
    }

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

    else {
        push @exprs, $arg;
    }
}

@exprs = ("./default.nix") if scalar @exprs == 0;


if (!defined $drvLink) {
    $drvLink = "derivation";
    $drvLink = ".nix-build-tmp-" . $drvLink if !$addDrvLink;
}

if (!defined $outLink) {
    $outLink = "result";
    $outLink = ".nix-build-tmp-" . $outLink if !$addOutLink;
}


foreach my $expr (@exprs) {

    # Instantiate.
    my @drvPaths;
    # !!! would prefer the perl 5.8.0 pipe open feature here.
    my $pid = open(DRVPATHS, "-|") || exec "$binDir/nix-instantiate", "--add-root", $drvLink, "--indirect", @instArgs, $expr;
    while (<DRVPATHS>) {chomp; push @drvPaths, $_;}
    close DRVPATHS or exit 1;

    foreach my $drvPath (@drvPaths) {
        my $target = readlink $drvPath or die "cannot read symlink `$drvPath'";
        print STDERR "store derivation is $target\n";
    }

    # Build.
    my @outPaths;
    $pid = open(OUTPATHS, "-|") || exec "$binDir/nix-store", "--add-root", $outLink, "--indirect", "-rv",
        @buildArgs, @drvPaths;
    while (<OUTPATHS>) {chomp; push @outPaths, $_;}
    close OUTPATHS or exit 1;

    next if $dryRun;

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