From 128538ef06aa1075b82a1c559e11f6e445514858 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 8 Jan 2015 15:14:38 +0100 Subject: [PATCH] nix-shell: Add --run flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ‘--run’ is like ‘--command’, except that it runs the command in a non-interactive shell. This is important if you do things like: $ nix-shell --command make Hitting Ctrl-C while make is running drops you into the interactive Nix shell, which is probably not what you want. So you can now do $ nix-shell --run make instead. --- doc/manual/command-ref/nix-shell.xml | 24 +++++++++++++++++++----- scripts/nix-build.in | 9 +++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/doc/manual/command-ref/nix-shell.xml b/doc/manual/command-ref/nix-shell.xml index 1f0311763..c1b172b70 100644 --- a/doc/manual/command-ref/nix-shell.xml +++ b/doc/manual/command-ref/nix-shell.xml @@ -29,6 +29,7 @@ attrPath cmd + cmd regexp @@ -92,11 +93,24 @@ also . cmd In the environment of the derivation, run the - shell command cmd instead of starting - an interactive shell. However, if you end the shell command with - return, you still get an interactive shell. - This can be useful for doing any additional - initialisation. + shell command cmd. This command is + executed in an interactive shell. (Use to + use a non-interactive shell instead.) However, a call to + exit is implicitly added to the command, so the + shell will exit after running the command. To prevent this, add + return at the end; e.g. --command + "echo Hello; return" will print Hello + and then drop you into the interactive shell. This can be useful + for doing any additional initialisation. + + + + cmd + + Like , but executes the + command in a non-interactive shell. This means (among other + things) that if you hit Ctrl-C while the command is running, the + shell exits. diff --git a/scripts/nix-build.in b/scripts/nix-build.in index 12826323c..4339899fc 100755 --- a/scripts/nix-build.in +++ b/scripts/nix-build.in @@ -16,6 +16,7 @@ my $runEnv = $0 =~ /nix-shell$/; my $pure = 0; my $fromArgs = 0; my $packages = 0; +my $interactive = 1; my @instArgs = (); my @buildArgs = (); @@ -158,10 +159,11 @@ for (my $n = 0; $n < scalar @ARGV; $n++) { $runEnv = 1; } - elsif ($arg eq "--command") { + 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") { @@ -286,7 +288,10 @@ foreach my $expr (@exprs) { 'unset TZ; ' . (defined $ENV{'TZ'} ? "export TZ='${ENV{'TZ'}}'; " : '') . $envCommand); $ENV{BASH_ENV} = $rcfile; - exec($ENV{NIX_BUILD_SHELL} // "bash", "--rcfile", $rcfile); + my @args = ($ENV{NIX_BUILD_SHELL} // "bash"); + push @args, "--rcfile" if $interactive; + push @args, $rcfile; + exec @args; die; }