mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-22 05:56:15 +02:00
nix-build: Port to c++
This was a dumb line-for-line rewrite, because nix build/nix run/etc. will replace it.
This commit is contained in:
parent
eef754813f
commit
80ebc553ec
6 changed files with 495 additions and 362 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -40,7 +40,6 @@ Makefile.config
|
||||||
/scripts/nix-prefetch-url
|
/scripts/nix-prefetch-url
|
||||||
/scripts/nix-install-package
|
/scripts/nix-install-package
|
||||||
/scripts/nix-channel
|
/scripts/nix-channel
|
||||||
/scripts/nix-build
|
|
||||||
/scripts/nix-copy-closure
|
/scripts/nix-copy-closure
|
||||||
/scripts/NixConfig.pm
|
/scripts/NixConfig.pm
|
||||||
/scripts/NixManifest.pm
|
/scripts/NixManifest.pm
|
||||||
|
@ -77,6 +76,9 @@ Makefile.config
|
||||||
# /src/download-via-ssh/
|
# /src/download-via-ssh/
|
||||||
/src/download-via-ssh/download-via-ssh
|
/src/download-via-ssh/download-via-ssh
|
||||||
|
|
||||||
|
# /src/nix-build/
|
||||||
|
/src/nix-build/nix-build
|
||||||
|
|
||||||
# /tests/
|
# /tests/
|
||||||
/tests/test-tmp
|
/tests/test-tmp
|
||||||
/tests/common.sh
|
/tests/common.sh
|
||||||
|
|
1
Makefile
1
Makefile
|
@ -12,6 +12,7 @@ makefiles = \
|
||||||
src/nix-daemon/local.mk \
|
src/nix-daemon/local.mk \
|
||||||
src/nix-collect-garbage/local.mk \
|
src/nix-collect-garbage/local.mk \
|
||||||
src/nix-prefetch-url/local.mk \
|
src/nix-prefetch-url/local.mk \
|
||||||
|
src/nix-build/local.mk \
|
||||||
perl/local.mk \
|
perl/local.mk \
|
||||||
scripts/local.mk \
|
scripts/local.mk \
|
||||||
corepkgs/local.mk \
|
corepkgs/local.mk \
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
nix_bin_scripts := \
|
nix_bin_scripts := \
|
||||||
$(d)/nix-build \
|
|
||||||
$(d)/nix-channel \
|
$(d)/nix-channel \
|
||||||
$(d)/nix-copy-closure \
|
$(d)/nix-copy-closure \
|
||||||
$(d)/nix-install-package \
|
$(d)/nix-install-package \
|
||||||
|
@ -26,6 +25,5 @@ $(eval $(call install-program-in, $(d)/build-remote.pl, $(libexecdir)/nix))
|
||||||
ifeq ($(OS), Darwin)
|
ifeq ($(OS), Darwin)
|
||||||
$(eval $(call install-program-in, $(d)/resolve-system-dependencies.pl, $(libexecdir)/nix))
|
$(eval $(call install-program-in, $(d)/resolve-system-dependencies.pl, $(libexecdir)/nix))
|
||||||
endif
|
endif
|
||||||
$(eval $(call install-symlink, nix-build, $(bindir)/nix-shell))
|
|
||||||
|
|
||||||
clean-files += $(nix_bin_scripts) $(nix_noinst_scripts)
|
clean-files += $(nix_bin_scripts) $(nix_noinst_scripts)
|
||||||
|
|
|
@ -1,359 +0,0 @@
|
||||||
#! @perl@ -w @perlFlags@
|
|
||||||
|
|
||||||
use utf8;
|
|
||||||
use strict;
|
|
||||||
use Nix::Config;
|
|
||||||
use Nix::Store;
|
|
||||||
use Nix::Utils;
|
|
||||||
use File::Basename;
|
|
||||||
use Text::ParseWords;
|
|
||||||
use Cwd;
|
|
||||||
|
|
||||||
binmode STDERR, ":encoding(utf8)";
|
|
||||||
|
|
||||||
my $dryRun = 0;
|
|
||||||
my $verbose = 0;
|
|
||||||
my $runEnv = $0 =~ /nix-shell$/;
|
|
||||||
my $pure = 0;
|
|
||||||
my $fromArgs = 0;
|
|
||||||
my $packages = 0;
|
|
||||||
my $interactive = 1;
|
|
||||||
|
|
||||||
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 $inShebang = 0;
|
|
||||||
my $script;
|
|
||||||
my @savedArgs;
|
|
||||||
|
|
||||||
my $tmpDir = mkTempDir($myName);
|
|
||||||
|
|
||||||
my $outLink = "./result";
|
|
||||||
my $drvLink = "$tmpDir/derivation";
|
|
||||||
|
|
||||||
# Ensure that the $tmpDir is deleted.
|
|
||||||
$SIG{'INT'} = sub { exit 1 };
|
|
||||||
|
|
||||||
|
|
||||||
# Heuristic to see if we're invoked as a shebang script, namely, if we
|
|
||||||
# have a single argument, it's the name of an executable file, and it
|
|
||||||
# starts with "#!".
|
|
||||||
if ($runEnv && defined $ARGV[0] && $ARGV[0] !~ /nix-shell/) {
|
|
||||||
$script = $ARGV[0];
|
|
||||||
if (-f $script && -x $script) {
|
|
||||||
open SCRIPT, "<$script" or die "$0: cannot open ‘$script’: $!\n";
|
|
||||||
my $first = <SCRIPT>;
|
|
||||||
if ($first =~ /^\#\!/) {
|
|
||||||
$inShebang = 1;
|
|
||||||
@savedArgs = @ARGV; shift @savedArgs;
|
|
||||||
@ARGV = ();
|
|
||||||
while (<SCRIPT>) {
|
|
||||||
chomp;
|
|
||||||
if (/^\#\!\s*nix-shell (.*)$/) {
|
|
||||||
push @ARGV, shellwords($1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
close SCRIPT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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" || $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" || $arg eq "-o") {
|
|
||||||
$n++;
|
|
||||||
die "$0: ‘$arg’ requires an argument\n" unless $n < scalar @ARGV;
|
|
||||||
$outLink = $ARGV[$n];
|
|
||||||
}
|
|
||||||
|
|
||||||
elsif ($arg eq "--attr" || $arg eq "-A" || $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 "--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" || $arg eq "-j" || $arg eq "--max-silent-time" || $arg eq "--cores" || $arg eq "--timeout" || $arg eq '--add-root') {
|
|
||||||
$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" || 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 "--check") {
|
|
||||||
push @buildArgs, $arg;
|
|
||||||
}
|
|
||||||
|
|
||||||
elsif ($arg eq "--run-env") { # obsolete
|
|
||||||
$runEnv = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
elsif ($arg eq "--command" || $arg eq "--run") {
|
|
||||||
$n++;
|
|
||||||
die "$0: ‘$arg’ requires an argument\n" unless $n < scalar @ARGV;
|
|
||||||
$envCommand = "$ARGV[$n]\nexit";
|
|
||||||
$interactive = 0 if $arg eq "--run";
|
|
||||||
}
|
|
||||||
|
|
||||||
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 ($arg eq "--impure") { $pure = 0; }
|
|
||||||
|
|
||||||
elsif ($arg eq "--expr" || $arg eq "-E") {
|
|
||||||
$fromArgs = 1;
|
|
||||||
push @instArgs, "--expr";
|
|
||||||
}
|
|
||||||
|
|
||||||
elsif ($arg eq "--packages" || $arg eq "-p") {
|
|
||||||
$packages = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
elsif ($inShebang && $arg eq "-i") {
|
|
||||||
$n++;
|
|
||||||
die "$0: ‘$arg’ requires an argument\n" unless $n < scalar @ARGV;
|
|
||||||
my $interpreter = $ARGV[$n];
|
|
||||||
my $execArgs = "";
|
|
||||||
|
|
||||||
sub shellEscape {
|
|
||||||
my $s = $_;
|
|
||||||
$s =~ s/'/'\\''/g;
|
|
||||||
return "'" . $s . "'";
|
|
||||||
}
|
|
||||||
|
|
||||||
# Überhack to support Perl. Perl examines the shebang and
|
|
||||||
# executes it unless it contains the string "perl" or "indir",
|
|
||||||
# or (undocumented) argv[0] does not contain "perl". Exploit
|
|
||||||
# the latter by doing "exec -a".
|
|
||||||
if ($interpreter =~ /perl/) {
|
|
||||||
$execArgs = "-a PERL";
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($interpreter =~ /ruby/) {
|
|
||||||
# Hack for Ruby. Ruby also examines the shebang. It tries to
|
|
||||||
# read the shebang to understand which packages to read from. Since
|
|
||||||
# this is handled via nix-shell -p, we wrap our ruby script execution
|
|
||||||
# in ruby -e 'load' which ignores the shebangs.
|
|
||||||
$envCommand = "exec $execArgs $interpreter -e 'load(\"$script\")' -- ${\(join ' ', (map shellEscape, @savedArgs))}";
|
|
||||||
} else {
|
|
||||||
$envCommand = "exec $execArgs $interpreter $script ${\(join ' ', (map shellEscape, @savedArgs))}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
elsif (substr($arg, 0, 1) eq "-") {
|
|
||||||
push @buildArgs, $arg;
|
|
||||||
}
|
|
||||||
|
|
||||||
elsif ($arg eq "-Q" || $arg eq "--no-build-output") {
|
|
||||||
push @buildArgs, $arg;
|
|
||||||
push @instArgs, $arg;
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
push @exprs, $arg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
die "$0: ‘-p’ and ‘-E’ are mutually exclusive\n" if $packages && $fromArgs;
|
|
||||||
|
|
||||||
if ($packages) {
|
|
||||||
push @instArgs, "--expr";
|
|
||||||
@exprs = (
|
|
||||||
'with import <nixpkgs> { }; runCommand "shell" { buildInputs = [ '
|
|
||||||
. (join " ", map { "($_)" } @exprs) . ']; } ""');
|
|
||||||
} elsif (!$fromArgs) {
|
|
||||||
@exprs = ("shell.nix") if scalar @exprs == 0 && $runEnv && -e "shell.nix";
|
|
||||||
@exprs = ("default.nix") if scalar @exprs == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
$ENV{'IN_NIX_SHELL'} = 1 if $runEnv;
|
|
||||||
|
|
||||||
|
|
||||||
foreach my $expr (@exprs) {
|
|
||||||
|
|
||||||
# Instantiate.
|
|
||||||
my @drvPaths;
|
|
||||||
if ($expr !~ /^\/.*\.drv$/) {
|
|
||||||
# If we're in a #! script, interpret filenames relative to the
|
|
||||||
# script.
|
|
||||||
$expr = dirname(Cwd::abs_path($script)) . "/" . $expr
|
|
||||||
if $inShebang && !$packages && $expr !~ /^\//;
|
|
||||||
|
|
||||||
# !!! 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 = (split '!',$drvPath)[0];
|
|
||||||
$drvPath = readlink $drvPath or die "cannot read symlink ‘$drvPath’" if -l $drvPath;
|
|
||||||
my $drv = derivationFromPath($drvPath);
|
|
||||||
|
|
||||||
# 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", "--no-gc-warning", @buildArgs, @inputDrvs, @{$drv->{inputSrcs}}) == 0
|
|
||||||
or die "$0: failed to build all dependencies\n";
|
|
||||||
|
|
||||||
# Set the environment.
|
|
||||||
my $tmp = $ENV{"TMPDIR"} // $ENV{"XDG_RUNTIME_DIR"} // "/tmp";
|
|
||||||
if ($pure) {
|
|
||||||
foreach my $name (keys %ENV) {
|
|
||||||
next if grep { $_ eq $name } ("HOME", "USER", "LOGNAME", "DISPLAY", "PATH", "TERM", "IN_NIX_SHELL", "TZ", "PAGER", "NIX_BUILD_SHELL");
|
|
||||||
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'} = $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,
|
|
||||||
"rm -rf '$tmpDir'; " .
|
|
||||||
'unset BASH_ENV; ' .
|
|
||||||
'[ -n "$PS1" ] && [ -e ~/.bashrc ] && source ~/.bashrc; ' .
|
|
||||||
($pure ? '' : 'p=$PATH; ' ) .
|
|
||||||
'dontAddDisableDepTrack=1; ' .
|
|
||||||
'[ -e $stdenv/setup ] && source $stdenv/setup; ' .
|
|
||||||
($pure ? '' : 'PATH=$PATH:$p; unset p; ') .
|
|
||||||
'set +e; ' .
|
|
||||||
'[ -n "$PS1" ] && PS1="\n\[\033[1;32m\][nix-shell:\w]$\[\033[0m\] "; ' .
|
|
||||||
'if [ "$(type -t runHook)" = function ]; then runHook shellHook; fi; ' .
|
|
||||||
'unset NIX_ENFORCE_PURITY; ' .
|
|
||||||
'unset NIX_INDENT_MAKE; ' .
|
|
||||||
'shopt -u nullglob; ' .
|
|
||||||
'unset TZ; ' . (defined $ENV{'TZ'} ? "export TZ='${ENV{'TZ'}}'; " : '') .
|
|
||||||
$envCommand);
|
|
||||||
$ENV{BASH_ENV} = $rcfile;
|
|
||||||
my @args = ($ENV{NIX_BUILD_SHELL} // "bash");
|
|
||||||
push @args, "--rcfile" if $interactive;
|
|
||||||
push @args, $rcfile;
|
|
||||||
exec @args;
|
|
||||||
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 ($? >> 8 || 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
next if $dryRun;
|
|
||||||
|
|
||||||
foreach my $outPath (@outPaths) {
|
|
||||||
my $target = readlink $outPath or die "cannot read symlink ‘$outPath’";
|
|
||||||
print "$target\n";
|
|
||||||
}
|
|
||||||
}
|
|
9
src/nix-build/local.mk
Normal file
9
src/nix-build/local.mk
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
programs += nix-build
|
||||||
|
|
||||||
|
nix-build_DIR := $(d)
|
||||||
|
|
||||||
|
nix-build_SOURCES := $(d)/nix-build.cc
|
||||||
|
|
||||||
|
nix-build_LIBS = libmain libstore libutil libformat
|
||||||
|
|
||||||
|
$(eval $(call install-symlink, nix-build, $(bindir)/nix-shell))
|
482
src/nix-build/nix-build.cc
Executable file
482
src/nix-build/nix-build.cc
Executable file
|
@ -0,0 +1,482 @@
|
||||||
|
#include <cstring>
|
||||||
|
#include <regex>
|
||||||
|
#include "util.hh"
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "shared.hh"
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include "store-api.hh"
|
||||||
|
#include "globals.hh"
|
||||||
|
#include "derivations.hh"
|
||||||
|
|
||||||
|
using namespace nix;
|
||||||
|
using std::stringstream;
|
||||||
|
|
||||||
|
extern char ** environ;
|
||||||
|
|
||||||
|
std::vector<string> shellwords(const string & s)
|
||||||
|
{
|
||||||
|
auto whitespace = std::regex("^(\\s+).*");
|
||||||
|
auto begin = s.cbegin();
|
||||||
|
auto res = std::vector<string>{};
|
||||||
|
auto cur = stringstream{};
|
||||||
|
enum state {
|
||||||
|
sBegin,
|
||||||
|
sQuote
|
||||||
|
};
|
||||||
|
state st = sBegin;
|
||||||
|
auto it = begin;
|
||||||
|
for (; it != s.cend(); ++it) {
|
||||||
|
if (st == sBegin) {
|
||||||
|
auto match = std::smatch{};
|
||||||
|
if (regex_search(it, s.cend(), match, whitespace)) {
|
||||||
|
cur << string(begin, it);
|
||||||
|
res.push_back(cur.str());
|
||||||
|
cur = stringstream{};
|
||||||
|
it = match[1].second;
|
||||||
|
begin = it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch (*it) {
|
||||||
|
case '"':
|
||||||
|
cur << string(begin, it);
|
||||||
|
begin = it + 1;
|
||||||
|
st = st == sBegin ? sQuote : sBegin;
|
||||||
|
break;
|
||||||
|
case '\\':
|
||||||
|
/* perl shellwords mostly just treats the next char as part of the string with no special processing */
|
||||||
|
cur << string(begin, it);
|
||||||
|
begin = ++it;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cur << string(begin, it);
|
||||||
|
auto last = cur.str();
|
||||||
|
if (!last.empty()) {
|
||||||
|
res.push_back(std::move(last));
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char ** argv)
|
||||||
|
{
|
||||||
|
return handleExceptions(argv[0], [&]() {
|
||||||
|
initNix();
|
||||||
|
auto store = openStore();
|
||||||
|
auto dryRun = false;
|
||||||
|
auto verbose = false;
|
||||||
|
auto runEnv = std::regex_search(argv[0], std::regex("nix-shell$"));
|
||||||
|
auto pure = false;
|
||||||
|
auto fromArgs = false;
|
||||||
|
auto packages = false;
|
||||||
|
auto interactive = true;
|
||||||
|
|
||||||
|
auto instArgs = Strings{};
|
||||||
|
auto buildArgs = Strings{};
|
||||||
|
auto exprs = Strings{};
|
||||||
|
|
||||||
|
auto shell = getEnv("SHELL", "/bin/sh");
|
||||||
|
auto envCommand = string{}; // interactive shell
|
||||||
|
auto envExclude = Strings{};
|
||||||
|
|
||||||
|
auto myName = runEnv ? "nix-shell" : "nix-build";
|
||||||
|
|
||||||
|
auto inShebang = false;
|
||||||
|
auto script = string{};
|
||||||
|
auto savedArgs = std::vector<string>{};
|
||||||
|
|
||||||
|
auto tmpDir = AutoDelete{createTempDir("", myName)};
|
||||||
|
|
||||||
|
auto outLink = string("./result");
|
||||||
|
auto drvLink = (Path) tmpDir + "/derivation";
|
||||||
|
|
||||||
|
auto args = std::vector<string>{};
|
||||||
|
for (int i = 1; i < argc; ++i)
|
||||||
|
args.push_back(argv[i]);
|
||||||
|
// Heuristic to see if we're invoked as a shebang script, namely, if we
|
||||||
|
// have a single argument, it's the name of an executable file, and it
|
||||||
|
// starts with "#!".
|
||||||
|
if (runEnv && argc > 1 && !std::regex_search(argv[1], std::regex("nix-shell"))) {
|
||||||
|
script = argv[1];
|
||||||
|
if (access(script.c_str(), F_OK) == 0 && access(script.c_str(), X_OK) == 0) {
|
||||||
|
auto SCRIPT = std::ifstream(script);
|
||||||
|
string first;
|
||||||
|
std::getline(SCRIPT, first);
|
||||||
|
if (std::regex_search(first, std::regex("^#!"))) {
|
||||||
|
inShebang = true;
|
||||||
|
for (int i = 2; i < argc - 1; ++i)
|
||||||
|
savedArgs.push_back(argv[i]);
|
||||||
|
args = std::vector<string>{};
|
||||||
|
for (string line; std::getline(SCRIPT, line);) {
|
||||||
|
line = chomp(line);
|
||||||
|
std::smatch match;
|
||||||
|
if (std::regex_match(line, match, std::regex("^#!\\s*nix-shell (.*)$")))
|
||||||
|
for (const auto & word : shellwords(match[1].str()))
|
||||||
|
args.push_back(word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto n = decltype(args)::size_type{0}; n < args.size(); ++n) {
|
||||||
|
auto arg = args[n];
|
||||||
|
|
||||||
|
if (arg == "--help") {
|
||||||
|
deletePath(tmpDir);
|
||||||
|
tmpDir.cancel();
|
||||||
|
execlp("man", "man", myName, NULL);
|
||||||
|
throw SysError("executing man");
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--version") {
|
||||||
|
std::cout << myName << " (Nix) " << nixVersion << '\n';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--add-drv-link") {
|
||||||
|
drvLink = "./derivation";
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--no-out-link" || arg == "--no-link") {
|
||||||
|
outLink = (Path) tmpDir + "/result";
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--drv-link") {
|
||||||
|
n++;
|
||||||
|
if (n >= args.size()) {
|
||||||
|
throw UsageError("--drv-link requires an argument");
|
||||||
|
}
|
||||||
|
drvLink = args[n];
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--out-link" || arg == "-o") {
|
||||||
|
n++;
|
||||||
|
if (n >= args.size()) {
|
||||||
|
throw UsageError(format("%1% requires an argument") % arg);
|
||||||
|
}
|
||||||
|
outLink = args[n];
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--attr" || arg == "-A" || arg == "-I") {
|
||||||
|
n++;
|
||||||
|
if (n >= args.size()) {
|
||||||
|
throw UsageError(format("%1% requires an argument") % arg);
|
||||||
|
}
|
||||||
|
instArgs.push_back(arg);
|
||||||
|
instArgs.push_back(args[n]);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--arg" || arg == "--argstr") {
|
||||||
|
if (n + 2 >= args.size()) {
|
||||||
|
throw UsageError(format("%1% requires two arguments") % arg);
|
||||||
|
}
|
||||||
|
instArgs.push_back(arg);
|
||||||
|
instArgs.push_back(args[n + 1]);
|
||||||
|
instArgs.push_back(args[n + 2]);
|
||||||
|
n += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--option") {
|
||||||
|
if (n + 2 >= args.size()) {
|
||||||
|
throw UsageError(format("%1% requires two arguments") % arg);
|
||||||
|
}
|
||||||
|
instArgs.push_back(arg);
|
||||||
|
instArgs.push_back(args[n + 1]);
|
||||||
|
instArgs.push_back(args[n + 2]);
|
||||||
|
buildArgs.push_back(arg);
|
||||||
|
buildArgs.push_back(args[n + 1]);
|
||||||
|
buildArgs.push_back(args[n + 2]);
|
||||||
|
n += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--max-jobs" || arg == "-j" || arg == "--max-silent-time" || arg == "--cores" || arg == "--timeout" || arg == "--add-root") {
|
||||||
|
n++;
|
||||||
|
if (n >= args.size()) {
|
||||||
|
throw UsageError(format("%1% requires an argument") % arg);
|
||||||
|
}
|
||||||
|
buildArgs.push_back(arg);
|
||||||
|
buildArgs.push_back(args[n]);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--dry-run") {
|
||||||
|
buildArgs.push_back("--dry-run");
|
||||||
|
dryRun = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--show-trace") {
|
||||||
|
instArgs.push_back(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "-") {
|
||||||
|
exprs = Strings{"-"};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--verbose" || (arg.size() >= 2 && arg.substr(0, 2) == "-v")) {
|
||||||
|
buildArgs.push_back(arg);
|
||||||
|
instArgs.push_back(arg);
|
||||||
|
verbose = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--quiet" || arg == "--repair") {
|
||||||
|
buildArgs.push_back(arg);
|
||||||
|
instArgs.push_back(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--check") {
|
||||||
|
buildArgs.push_back(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--run-env") { // obsolete
|
||||||
|
runEnv = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--command" || arg == "--run") {
|
||||||
|
n++;
|
||||||
|
if (n >= args.size()) {
|
||||||
|
throw UsageError(format("%1% requires an argument") % arg);
|
||||||
|
}
|
||||||
|
envCommand = args[n] + "\nexit";
|
||||||
|
if (arg == "--run")
|
||||||
|
interactive = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--exclude") {
|
||||||
|
n++;
|
||||||
|
if (n >= args.size()) {
|
||||||
|
throw UsageError(format("%1% requires an argument") % arg);
|
||||||
|
}
|
||||||
|
envExclude.push_back(args[n]);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--pure") { pure = true; }
|
||||||
|
else if (arg == "--impure") { pure = false; }
|
||||||
|
|
||||||
|
else if (arg == "--expr" || arg == "-E") {
|
||||||
|
fromArgs = true;
|
||||||
|
instArgs.push_back("--expr");
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "--packages" || arg == "-p") {
|
||||||
|
packages = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (inShebang && arg == "-i") {
|
||||||
|
n++;
|
||||||
|
if (n >= args.size()) {
|
||||||
|
throw UsageError(format("%1% requires an argument") % arg);
|
||||||
|
}
|
||||||
|
auto interpreter = args[n];
|
||||||
|
auto execArgs = "";
|
||||||
|
|
||||||
|
auto shellEscape = [](const string & s) {
|
||||||
|
return "'" + std::regex_replace(s, std::regex("'"), "'\\''") + "'";
|
||||||
|
};
|
||||||
|
|
||||||
|
// Überhack to support Perl. Perl examines the shebang and
|
||||||
|
// executes it unless it contains the string "perl" or "indir",
|
||||||
|
// or (undocumented) argv[0] does not contain "perl". Exploit
|
||||||
|
// the latter by doing "exec -a".
|
||||||
|
if (std::regex_search(interpreter, std::regex("perl"))) {
|
||||||
|
execArgs = "-a PERL";
|
||||||
|
}
|
||||||
|
|
||||||
|
auto joined = stringstream{};
|
||||||
|
for (const auto & i : savedArgs)
|
||||||
|
joined << shellEscape(i) << ' ';
|
||||||
|
|
||||||
|
if (std::regex_search(interpreter, std::regex("ruby"))) {
|
||||||
|
// Hack for Ruby. Ruby also examines the shebang. It tries to
|
||||||
|
// read the shebang to understand which packages to read from. Since
|
||||||
|
// this is handled via nix-shell -p, we wrap our ruby script execution
|
||||||
|
// in ruby -e 'load' which ignores the shebangs.
|
||||||
|
|
||||||
|
envCommand = (format("exec %1% %2% -e 'load(\"%3%\") -- %4%") % execArgs % interpreter % script % joined.str()).str();
|
||||||
|
} else {
|
||||||
|
envCommand = (format("exec %1% %2% %3% %4%") % execArgs % interpreter % script % joined.str()).str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (!arg.empty() && arg[0] == '-') {
|
||||||
|
buildArgs.push_back(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (arg == "-Q" || arg == "--no-build-output") {
|
||||||
|
buildArgs.push_back(arg);
|
||||||
|
instArgs.push_back(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
exprs.push_back(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (packages && fromArgs) {
|
||||||
|
throw UsageError("‘-p’ and ‘-E’ are mutually exclusive");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (packages) {
|
||||||
|
instArgs.push_back("--expr");
|
||||||
|
auto joined = stringstream{};
|
||||||
|
joined << "with import <nixpkgs> { }; runCommand \"shell\" { buildInputs = [ ";
|
||||||
|
for (const auto & i : exprs)
|
||||||
|
joined << '(' << i << ") ";
|
||||||
|
joined << "]; } \"\"";
|
||||||
|
exprs = Strings{joined.str()};
|
||||||
|
} else if (!fromArgs) {
|
||||||
|
if (exprs.empty() && runEnv && access("shell.nix", F_OK) == 0)
|
||||||
|
exprs.push_back("shell.nix");
|
||||||
|
if (exprs.empty())
|
||||||
|
exprs.push_back("default.nix");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (runEnv)
|
||||||
|
setenv("IN_NIX_SHELL", "1", 1);
|
||||||
|
|
||||||
|
for (auto & expr : exprs) {
|
||||||
|
// Instantiate.
|
||||||
|
auto drvPaths = std::vector<string>{};
|
||||||
|
if (!std::regex_match(expr, std::regex("^/.*\\.drv$"))) {
|
||||||
|
// If we're in a #! script, interpret filenames relative to the
|
||||||
|
// script.
|
||||||
|
if (inShebang && !packages)
|
||||||
|
expr = absPath(expr, dirOf(script));
|
||||||
|
|
||||||
|
auto instantiateArgs = Strings{"--add-root", drvLink, "--indirect"};
|
||||||
|
for (const auto & arg : instArgs)
|
||||||
|
instantiateArgs.push_back(arg);
|
||||||
|
instantiateArgs.push_back(expr);
|
||||||
|
auto instOutput = runProgram(settings.nixBinDir + "/nix-instantiate", false, instantiateArgs);
|
||||||
|
drvPaths = tokenizeString<std::vector<string>>(instOutput);
|
||||||
|
} else {
|
||||||
|
drvPaths.push_back(expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (runEnv) {
|
||||||
|
if (drvPaths.size() != 1)
|
||||||
|
throw UsageError("a single derivation is required");
|
||||||
|
auto drvPath = drvPaths[0];
|
||||||
|
drvPath = drvPath.substr(0, drvPath.find_first_of('!'));
|
||||||
|
if (isLink(drvPath))
|
||||||
|
drvPath = readLink(drvPath);
|
||||||
|
auto drv = store->derivationFromPath(drvPath);
|
||||||
|
|
||||||
|
// Build or fetch all dependencies of the derivation.
|
||||||
|
auto nixStoreArgs = Strings{"-r", "--no-output", "--no-gc-warning"};
|
||||||
|
for (const auto & arg : buildArgs)
|
||||||
|
nixStoreArgs.push_back(arg);
|
||||||
|
for (const auto & input : drv.inputDrvs)
|
||||||
|
if (std::all_of(envExclude.cbegin(), envExclude.cend(), [&](const string & exclude) { return !std::regex_search(input.first, std::regex(exclude)); }))
|
||||||
|
nixStoreArgs.push_back(input.first);
|
||||||
|
for (const auto & src : drv.inputSrcs)
|
||||||
|
nixStoreArgs.push_back(src);
|
||||||
|
runProgram(settings.nixBinDir + "/nix-store", false, nixStoreArgs);
|
||||||
|
|
||||||
|
// Set the environment.
|
||||||
|
auto tmp = getEnv("TMPDIR", getEnv("XDG_RUNTIME_DIR", "/tmp"));
|
||||||
|
if (pure) {
|
||||||
|
auto skippedEnv = std::vector<string>{"HOME", "USER", "LOGNAME", "DISPLAY", "PATH", "TERM", "IN_NIX_SHELL", "TZ", "PAGER", "NIX_BUILD_SHELL"};
|
||||||
|
auto removed = std::vector<string>{};
|
||||||
|
for (auto i = size_t{0}; environ[i]; ++i) {
|
||||||
|
auto eq = strchr(environ[i], '=');
|
||||||
|
if (!eq)
|
||||||
|
// invalid env, just keep going
|
||||||
|
continue;
|
||||||
|
auto name = string(environ[i], eq);
|
||||||
|
if (find(skippedEnv.begin(), skippedEnv.end(), name) == skippedEnv.end())
|
||||||
|
removed.emplace_back(std::move(name));
|
||||||
|
}
|
||||||
|
for (const auto & name : removed)
|
||||||
|
unsetenv(name.c_str());
|
||||||
|
// NixOS hack: prevent /etc/bashrc from sourcing /etc/profile.
|
||||||
|
setenv("__ETC_PROFILE_SOURCED", "1", 1);
|
||||||
|
}
|
||||||
|
setenv("NIX_BUILD_TOP", tmp.c_str(), 1);
|
||||||
|
setenv("TMPDIR", tmp.c_str(), 1);
|
||||||
|
setenv("TEMPDIR", tmp.c_str(), 1);
|
||||||
|
setenv("TMP", tmp.c_str(), 1);
|
||||||
|
setenv("TEMP", tmp.c_str(), 1);
|
||||||
|
setenv("NIX_STORE", store->storeDir.c_str(), 1);
|
||||||
|
for (const auto & env : drv.env)
|
||||||
|
setenv(env.first.c_str(), env.second.c_str(), 1);
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
auto rcfile = (Path) tmpDir + "/rc";
|
||||||
|
writeFile(rcfile, (format(
|
||||||
|
"rm -rf '%1%'; "
|
||||||
|
"unset BASH_ENV; "
|
||||||
|
"[ -n \"$PS1\" ] && [ -e ~/.bashrc ] && source ~/.bashrc; "
|
||||||
|
"%2%"
|
||||||
|
"dontAddDisableDepTrack=1; "
|
||||||
|
"[ -e $stdenv/setup ] && source $stdenv/setup; "
|
||||||
|
"%3%"
|
||||||
|
"set +e; "
|
||||||
|
"[ -n \"$PS1\" ] && PS1=\"\\n\\[\\033[1;32m\\][nix-shell:\\w]$\\[\\033[0m\\] \"; "
|
||||||
|
"if [ \"$(type -t runHook)\" = function ]; then runHook shellHook; fi; "
|
||||||
|
"unset NIX_ENFORCE_PURITY; "
|
||||||
|
"unset NIX_INDENT_MAKE; "
|
||||||
|
"shopt -u nullglob; "
|
||||||
|
"unset TZ; %4%"
|
||||||
|
"%5%"
|
||||||
|
) % (Path) tmpDir % (pure ? "" : "p=$PATH") % (pure ? "" : "PATH=$PATH:$p; unset p; ") % (getenv("TZ") ? (string("export TZ='") + getenv("TZ") + "'; ") : "") % envCommand).str());
|
||||||
|
setenv("BASH_ENV", rcfile.c_str(), 1);
|
||||||
|
if (interactive)
|
||||||
|
execlp(getEnv("NIX_BUILD_SHELL", "bash").c_str(), "bash", "--rcfile", rcfile.c_str(), NULL);
|
||||||
|
else
|
||||||
|
execlp(getEnv("NIX_BUILD_SHELL", "bash").c_str(), "bash", rcfile.c_str(), NULL);
|
||||||
|
throw SysError("executing shell");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
auto prevDrvPath = string{};
|
||||||
|
auto drvPaths2 = Strings{};
|
||||||
|
for (const auto & drvPath : drvPaths) {
|
||||||
|
auto p = drvPath;
|
||||||
|
auto output = string{"out"};
|
||||||
|
std::smatch match;
|
||||||
|
if (std::regex_match(drvPath, match, std::regex("(.*)!(.*)"))) {
|
||||||
|
p = match[1].str();
|
||||||
|
output = match[2].str();
|
||||||
|
}
|
||||||
|
auto target = readLink(p);
|
||||||
|
if (verbose)
|
||||||
|
std::cerr << "derivation is " << target << '\n';
|
||||||
|
if (target == prevDrvPath) {
|
||||||
|
auto last = drvPaths2.back();
|
||||||
|
drvPaths2.pop_back();
|
||||||
|
drvPaths2.push_back(last + "," + output);
|
||||||
|
} else {
|
||||||
|
drvPaths2.push_back(target + "!" + output);
|
||||||
|
prevDrvPath = target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Build.
|
||||||
|
auto outPaths = Strings{};
|
||||||
|
auto nixStoreArgs = Strings{"--add-root", outLink, "--indirect", "-r"};
|
||||||
|
for (const auto & arg : buildArgs)
|
||||||
|
nixStoreArgs.push_back(arg);
|
||||||
|
for (const auto & path : drvPaths2)
|
||||||
|
nixStoreArgs.push_back(path);
|
||||||
|
auto nixStoreRes = runProgram(settings.nixBinDir + "/nix-store", false, nixStoreArgs);
|
||||||
|
for (const auto & outpath : tokenizeString<std::vector<string>>(nixStoreRes)) {
|
||||||
|
outPaths.push_back(chomp(outpath));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dryRun)
|
||||||
|
continue;
|
||||||
|
for (const auto & outPath : outPaths) {
|
||||||
|
auto target = readLink(outPath);
|
||||||
|
std::cout << target << '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue