nix-shell: Add --packages flag

This allows you to easily set up a build environment containing the
specified packages from Nixpkgs.  For example:

  $ nix-shell -p sqlite xorg.libX11 hello

will start a shell in which the given packages are present.
This commit is contained in:
Eelco Dolstra 2014-02-19 17:08:01 +01:00
parent a897b58373
commit 36b90e72d7
2 changed files with 41 additions and 3 deletions

View file

@ -18,7 +18,6 @@
<refsynopsisdiv> <refsynopsisdiv>
<cmdsynopsis> <cmdsynopsis>
<command>nix-shell</command> <command>nix-shell</command>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="opt-common-syn.xml#xmlns(db=http://docbook.org/ns/docbook)xpointer(/db:nop/*)" />
<arg><option>--arg</option> <replaceable>name</replaceable> <replaceable>value</replaceable></arg> <arg><option>--arg</option> <replaceable>name</replaceable> <replaceable>value</replaceable></arg>
<arg><option>--argstr</option> <replaceable>name</replaceable> <replaceable>value</replaceable></arg> <arg><option>--argstr</option> <replaceable>name</replaceable> <replaceable>value</replaceable></arg>
<arg> <arg>
@ -31,7 +30,16 @@
<arg><option>--command</option> <replaceable>cmd</replaceable></arg> <arg><option>--command</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='plain'>
<group>
<arg choice='plain'><option>--packages</option></arg>
<arg choice='plain'><option>-p</option></arg>
</group>
<replaceable>packages</replaceable>
</group>
<arg><replaceable>path</replaceable></arg> <arg><replaceable>path</replaceable></arg>
</group>
</cmdsynopsis> </cmdsynopsis>
</refsynopsisdiv> </refsynopsisdiv>
@ -114,6 +122,18 @@ also <xref linkend="sec-common-options" />.</phrase></para>
</varlistentry> </varlistentry>
<varlistentry><term><option>--packages</option> / <option>-p</option></term>
<listitem><para>Set up an environment in which the specified
packages are present. The command line arguments are interpreted
as attribute names inside the Nix Packages collection. Thus,
<literal>nix-shell -p libjpeg openjdk</literal> will start a shell
in which the packages denoted by the attribute names
<varname>libjpeg</varname> and <varname>openjdk</varname> are
present.</para></listitem>
</varlistentry>
</variablelist> </variablelist>
<para>The following common options are supported:</para> <para>The following common options are supported:</para>
@ -155,6 +175,14 @@ the following starts a shell containing the packages
$ nix-shell -E 'with import &lt;nixpkgs> { }; runCommand "dummy" { buildInputs = [ sqlite xorg.libX11 ]; } ""' $ nix-shell -E 'with import &lt;nixpkgs> { }; runCommand "dummy" { buildInputs = [ sqlite xorg.libX11 ]; } ""'
</screen> </screen>
A shorter way to do the same is:
<screen>
$ nix-shell -p sqlite xorg.libX11
[nix-shell]$ echo $NIX_LDFLAGS
… -L/nix/store/j1zg5v…-sqlite-3.8.0.2/lib -L/nix/store/0gmcz9…-libX11-1.6.1/lib …
</screen>
</para> </para>
</refsection> </refsection>

View file

@ -12,6 +12,7 @@ my $verbose = 0;
my $runEnv = $0 =~ /nix-shell$/; my $runEnv = $0 =~ /nix-shell$/;
my $pure = 0; my $pure = 0;
my $fromArgs = 0; my $fromArgs = 0;
my $packages = 0;
my @instArgs = (); my @instArgs = ();
my @buildArgs = (); my @buildArgs = ();
@ -150,6 +151,10 @@ for (my $n = 0; $n < scalar @ARGV; $n++) {
push @instArgs, "--expr"; push @instArgs, "--expr";
} }
elsif ($arg eq "--packages" || $arg eq "-p") {
$packages = 1;
}
elsif (substr($arg, 0, 1) eq "-") { elsif (substr($arg, 0, 1) eq "-") {
push @buildArgs, $arg; push @buildArgs, $arg;
} }
@ -159,7 +164,12 @@ for (my $n = 0; $n < scalar @ARGV; $n++) {
} }
} }
if (!$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 = ("shell.nix") if scalar @exprs == 0 && $runEnv && -e "shell.nix";
@exprs = ("default.nix") if scalar @exprs == 0; @exprs = ("default.nix") if scalar @exprs == 0;
} }