about summary refs log tree commit diff
path: root/scripts/nix-build.in
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2015-01-08T13·56+0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2015-01-08T13·56+0100
commitb76589206a1303fb2dca073f6ec01bc71fc0fab2 (patch)
tree5918f131648a8fd28c1a1eaa6ad8f954ff09da67 /scripts/nix-build.in
parenta957893b261a4438101c205e38fe8ce62b83a121 (diff)
nix-shell: Interpret filenames relative to the #!-script
So you can have a script like:

  #! /usr/bin/env nix-shell
  #! nix-shell script.nix -i python

  import prettytable

  x = prettytable.PrettyTable(["Foo", "Bar"])
  for i in range(1, 10): x.add_row([i, i**2])
  print x

with a ‘script.nix’ in the same directory:

  with import <nixpkgs> {};

  runCommand "dummy" { buildInputs = [ python pythonPackages.prettytable ]; } ""

(Of course, in this particular case, using the ‘-p’ flag is more
convenient.)
Diffstat (limited to 'scripts/nix-build.in')
-rwxr-xr-xscripts/nix-build.in7
1 files changed, 7 insertions, 0 deletions
diff --git a/scripts/nix-build.in b/scripts/nix-build.in
index b7c923f88607..12826323c3e3 100755
--- a/scripts/nix-build.in
+++ b/scripts/nix-build.in
@@ -5,6 +5,8 @@ use strict;
 use Nix::Config;
 use Nix::Store;
 use Nix::Utils;
+use File::Basename;
+use Cwd;
 
 binmode STDERR, ":encoding(utf8)";
 
@@ -219,6 +221,11 @@ 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 && $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, $_;}