nix-shell: Add --run flag

‘--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.
This commit is contained in:
Eelco Dolstra 2015-01-08 15:14:38 +01:00
parent b76589206a
commit 128538ef06
2 changed files with 26 additions and 7 deletions

View file

@ -29,6 +29,7 @@
<replaceable>attrPath</replaceable> <replaceable>attrPath</replaceable>
</arg> </arg>
<arg><option>--command</option> <replaceable>cmd</replaceable></arg> <arg><option>--command</option> <replaceable>cmd</replaceable></arg>
<arg><option>--run</option> <replaceable>cmd</replaceable></arg>
<arg><option>--exclude</option> <replaceable>regexp</replaceable></arg> <arg><option>--exclude</option> <replaceable>regexp</replaceable></arg>
<arg><option>--pure</option></arg> <arg><option>--pure</option></arg>
<group choice='req'> <group choice='req'>
@ -92,11 +93,24 @@ also <xref linkend="sec-common-options" />.</phrase></para>
<varlistentry><term><option>--command</option> <replaceable>cmd</replaceable></term> <varlistentry><term><option>--command</option> <replaceable>cmd</replaceable></term>
<listitem><para>In the environment of the derivation, run the <listitem><para>In the environment of the derivation, run the
shell command <replaceable>cmd</replaceable> instead of starting shell command <replaceable>cmd</replaceable>. This command is
an interactive shell. However, if you end the shell command with executed in an interactive shell. (Use <option>--run</option> to
<literal>return</literal>, you still get an interactive shell. use a non-interactive shell instead.) However, a call to
This can be useful for doing any additional <literal>exit</literal> is implicitly added to the command, so the
initialisation.</para></listitem> shell will exit after running the command. To prevent this, add
<literal>return</literal> at the end; e.g. <literal>--command
"echo Hello; return"</literal> will print <literal>Hello</literal>
and then drop you into the interactive shell. This can be useful
for doing any additional initialisation.</para></listitem>
</varlistentry>
<varlistentry><term><option>--run</option> <replaceable>cmd</replaceable></term>
<listitem><para>Like <option>--command</option>, 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.</para></listitem>
</varlistentry> </varlistentry>

View file

@ -16,6 +16,7 @@ my $runEnv = $0 =~ /nix-shell$/;
my $pure = 0; my $pure = 0;
my $fromArgs = 0; my $fromArgs = 0;
my $packages = 0; my $packages = 0;
my $interactive = 1;
my @instArgs = (); my @instArgs = ();
my @buildArgs = (); my @buildArgs = ();
@ -158,10 +159,11 @@ for (my $n = 0; $n < scalar @ARGV; $n++) {
$runEnv = 1; $runEnv = 1;
} }
elsif ($arg eq "--command") { elsif ($arg eq "--command" || $arg eq "--run") {
$n++; $n++;
die "$0: $arg requires an argument\n" unless $n < scalar @ARGV; die "$0: $arg requires an argument\n" unless $n < scalar @ARGV;
$envCommand = "$ARGV[$n]\nexit"; $envCommand = "$ARGV[$n]\nexit";
$interactive = 0 if $arg eq "--run";
} }
elsif ($arg eq "--exclude") { elsif ($arg eq "--exclude") {
@ -286,7 +288,10 @@ foreach my $expr (@exprs) {
'unset TZ; ' . (defined $ENV{'TZ'} ? "export TZ='${ENV{'TZ'}}'; " : '') . 'unset TZ; ' . (defined $ENV{'TZ'} ? "export TZ='${ENV{'TZ'}}'; " : '') .
$envCommand); $envCommand);
$ENV{BASH_ENV} = $rcfile; $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; die;
} }