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

use strict;


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

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
  --no-out-link: do not create the `result' symlink
  --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 = 1;
    }

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

    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 $prefix = "";
    $prefix = ".nix-build-tmp-" if !$addDrvLink;

    my $drvPaths = `@bindir@/nix-instantiate --add-root ./${prefix}derivation --indirect @instArgs "$expr"`;
    my @drvPaths = split ' ', $drvPaths;

    foreach my $drvPath (@drvPaths) {
        my $target = readlink $drvPath;
        print STDERR "store derivation is $target\n";
    }

    # Build.
    $prefix = "";
    $prefix = ".nix-build-tmp-" if !$addOutLink;

    my $outPaths = `@bindir@/nix-store --add-root ./${prefix}result --indirect -rv @buildArgs @drvPaths`;
    my @outPaths = split ' ', $outPaths;
    
    foreach my $outPath (@outPaths) {
        my $target = readlink $outPath;
        print "$target\n";
    }

}