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.)
This commit is contained in:
Eelco Dolstra 2015-01-08 14:56:14 +01:00
parent a957893b26
commit b76589206a

View file

@ -5,6 +5,8 @@ use strict;
use Nix::Config; use Nix::Config;
use Nix::Store; use Nix::Store;
use Nix::Utils; use Nix::Utils;
use File::Basename;
use Cwd;
binmode STDERR, ":encoding(utf8)"; binmode STDERR, ":encoding(utf8)";
@ -219,6 +221,11 @@ foreach my $expr (@exprs) {
# Instantiate. # Instantiate.
my @drvPaths; my @drvPaths;
if ($expr !~ /^\/.*\.drv$/) { 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. # !!! 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; my $pid = open(DRVPATHS, "-|") || exec "$Nix::Config::binDir/nix-instantiate", "--add-root", $drvLink, "--indirect", @instArgs, $expr;
while (<DRVPATHS>) {chomp; push @drvPaths, $_;} while (<DRVPATHS>) {chomp; push @drvPaths, $_;}