From c31836008e45460513188a3fbeda4416f9153a05 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 19 Feb 2014 16:30:19 +0100 Subject: [PATCH] nix-instantiate: Add a flag --expr / -E to read expressions from the command line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is basically a shortcut for ‘echo 'expr...' | nix-instantiate -’. Also supported by nix-build and nix-shell. --- doc/manual/nix-build.xml | 10 +++ doc/manual/nix-instantiate.xml | 116 ++++++++++++++----------- doc/manual/nix-shell.xml | 22 +++-- doc/manual/opt-common.xml | 64 ++++++++------ scripts/nix-build.in | 22 +++-- src/nix-instantiate/nix-instantiate.cc | 9 +- 6 files changed, 148 insertions(+), 95 deletions(-) diff --git a/doc/manual/nix-build.xml b/doc/manual/nix-build.xml index 969faf9d1..3832f5fc3 100644 --- a/doc/manual/nix-build.xml +++ b/doc/manual/nix-build.xml @@ -164,6 +164,16 @@ $ nix-build '<nixpkgs>' -A openssl.man This will create a symlink result-man. +Build a Nix expression given on the command line: + + +$ nix-build -E 'with import <nixpkgs> { }; runCommand "foo" { } "echo bar > $out"' +$ cat ./result +bar + + + + diff --git a/doc/manual/nix-instantiate.xml b/doc/manual/nix-instantiate.xml index a3d67debe..a780302e1 100644 --- a/doc/manual/nix-instantiate.xml +++ b/doc/manual/nix-instantiate.xml @@ -2,7 +2,7 @@ xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="sec-nix-instantiate"> - + nix-instantiate 1 @@ -18,7 +18,15 @@ nix-instantiate - + + + + + + + + + name value @@ -29,18 +37,14 @@ path - - - - - - - - - - - - + + + + + files + + nix-instantiate + files @@ -50,8 +54,8 @@ The command nix-instantiate generates store derivations from (high-level) -Nix expressions. It loads and evaluates the Nix expressions in each -of files (which defaults to +Nix expressions. It evaluates the Nix expressions in each of +files (which defaults to ./default.nix). Each top-level expression should evaluate to a derivation, a list of derivations, or a set of derivations. The paths of the resulting store derivations are printed @@ -61,12 +65,6 @@ on standard output. -, then a Nix expression will be read from standard input. -Most users and developers don’t need to use this command -(nix-env and nix-build perform -store derivation instantiation from Nix expressions automatically). -It is most commonly used for implementing new deployment -policies. - See also for a list of common options. @@ -86,25 +84,24 @@ policies. - - + Just parse the input files, and print their abstract syntax trees on standard output in ATerm format. - + - + - + Just parse and evaluate the input files, and print the resulting values on standard output. No instantiation of store derivations takes place. - + - + Look up the given files in Nix’s search path (as specified by the NIX_PATH environment variable). If found, print the corresponding absolute paths on standard @@ -113,9 +110,9 @@ policies. nix-instantiate --find-file nixpkgs/default.nix will print /home/alice/nixpkgs/default.nix. - + - + When used with and @@ -180,19 +177,34 @@ dr-xr-xr-x 2 eelco users 4096 1970-01-01 01:00 lib +You can also give a Nix expression on the command line: + + +$ nix-instantiate -E 'with import <nixpkgs> { }; hello' +/nix/store/j8s4zyv75a724q38cb0r87rlczaiag4y-hello-2.8.drv + + +This is equivalent to: + + +$ nix-instantiate '<nixpkgs>' -A hello + + + + Parsing and evaluating Nix expressions: -$ echo '"foo" + "bar"' | nix-instantiate --parse-only - -OpPlus(Str("foo"),Str("bar")) +$ nix-instantiate --parse-only -E '1 + 2' +1 + 2 -$ echo '"foo" + "bar"' | nix-instantiate --eval-only - -Str("foobar") +$ nix-instantiate --eval-only -E '1 + 2' +3 -$ echo '"foo" + "bar"' | nix-instantiate --eval-only --xml - +$ nix-instantiate --eval-only --xml -E '1 + 2' - + ]]> @@ -200,28 +212,28 @@ $ echo '"foo" + "bar"' | nix-instantiate --eval-only --xml - The difference between non-strict and strict evaluation: -$ echo 'rec { x = "foo"; y = x; }' | nix-instantiate --eval-only --xml - +$ nix-instantiate --eval-only --xml -E 'rec { x = "foo"; y = x; }' ... - - - - - ]]> + + + + + + ]]> ... Note that y is left unevaluated (the XML representation doesn’t attempt to show non-normal forms). -$ echo 'rec { x = "foo"; y = x; }' | nix-instantiate --eval-only --xml --strict - +$ nix-instantiate --eval-only --xml --strict -E 'rec { x = "foo"; y = x; }' ... - - - - - ]]> + + + + + + ]]> ... @@ -236,6 +248,6 @@ $ echo 'rec { x = "foo"; y = x; }' | nix-instantiate --eval-only --xml --strict - + diff --git a/doc/manual/nix-shell.xml b/doc/manual/nix-shell.xml index 4b369662e..8178cea69 100644 --- a/doc/manual/nix-shell.xml +++ b/doc/manual/nix-shell.xml @@ -61,11 +61,11 @@ the derivation attribute shellHook = '' - echo "Hello shell!" + echo "Hello shell" ''; -will cause nix-shell to print Hello shell!. +will cause nix-shell to print Hello shell. @@ -132,11 +132,11 @@ interactive shell in which to build it: $ nix-shell '<nixpkgs>' -A pan -$ unpackPhase -$ cd pan-* -$ configurePhase -$ buildPhase -$ ./pan/gui/pan +[nix-shell]$ unpackPhase +[nix-shell]$ cd pan-* +[nix-shell]$ configurePhase +[nix-shell]$ buildPhase +[nix-shell]$ ./pan/gui/pan To clear the environment first, and do some additional automatic @@ -147,6 +147,14 @@ $ nix-shell '<nixpkgs>' -A pan --pure \ --command 'export NIX_DEBUG=1; export NIX_CORES=8; return' +Nix expressions can also be given on the command line. For instance, +the following starts a shell containing the packages +sqlite and libX11: + + +$ nix-shell -E 'with import <nixpkgs> { }; runCommand "dummy" { buildInputs = [ sqlite xorg.libX11 ]; } ""' + + diff --git a/doc/manual/opt-common.xml b/doc/manual/opt-common.xml index a877b536a..f8584f4d6 100644 --- a/doc/manual/opt-common.xml +++ b/doc/manual/opt-common.xml @@ -8,15 +8,15 @@ - + Prints out a summary of the command syntax and exits. - + - + Prints out the Nix version number on standard output and exits. @@ -26,7 +26,7 @@ - + Increases the level of verbosity of diagnostic messages printed on standard error. For each Nix operation, the information printed on standard output is well-defined; any diagnostic @@ -37,18 +37,18 @@ following verbosity levels exist: - + 0 “Errors only”: only print messages explaining why the Nix invocation failed. - + 1 “Informational”: print useful messages about what Nix is doing. This is the default. - + 2 “Talkative”: print more informational messages. @@ -68,11 +68,11 @@ “Vomit”: print vast amounts of debug information. - + - + @@ -85,7 +85,7 @@ builder's standard output and error are always written to a log file in prefix/nix/var/log/nix. - + @@ -99,7 +99,7 @@ configuration setting, which itself defaults to 1. A higher value is useful on SMP systems or to exploit I/O latency. - + @@ -117,7 +117,7 @@ configuration setting, if set, or 1 otherwise. The value 0 means that the builder should use all available CPU cores in the system. - + @@ -151,7 +151,7 @@ derivation itself. Without this option, Nix stops if any build fails (except for builds of substitutes), possibly killing builds in progress (in case of parallel or distributed builds). - + @@ -188,7 +188,7 @@ resources). - + @@ -197,7 +197,7 @@ When this option is used, no attempt is made to open the Nix database. Most Nix operations do need database access, so those operations will fail. - + @@ -247,12 +247,12 @@ - - + + - + @@ -310,10 +310,10 @@ / attrPath - In nix-env, - nix-instantiate and nix-build, - allows you to select an attribute from the - top-level Nix expression being evaluated. The attribute + Select an attribute from the top-level Nix + expression being evaluated. (nix-env, + nix-instantiate, nix-build and + nix-shell only.) The attribute path attrPath is a sequence of attribute names separated by dots. For instance, given a top-level Nix expression e, the attribute path @@ -333,8 +333,18 @@ + / + + Interpret the command line arguments as a list of + Nix expressions to be parsed and evaluated, rather than as a list + of file names of Nix expressions. + (nix-instantiate, nix-build + and nix-shell only.) + + + - + Causes Nix to print out a stack trace in case of Nix expression evaluation errors. @@ -342,23 +352,23 @@ path - + Add a path to the Nix expression search path. This option may be given multiple times. See the NIX_PATH environment variable for information on the semantics of the Nix search path. Paths added through take precedence over NIX_PATH. - + name value - + Set the Nix configuration option name to value. This overrides settings in the Nix configuration file (see nix.conf5). - + diff --git a/scripts/nix-build.in b/scripts/nix-build.in index 4f59118f8..cc6ab423d 100755 --- a/scripts/nix-build.in +++ b/scripts/nix-build.in @@ -11,6 +11,7 @@ my $dryRun = 0; my $verbose = 0; my $runEnv = $0 =~ /nix-shell$/; my $pure = 0; +my $fromArgs = 0; my @instArgs = (); my @buildArgs = (); @@ -49,7 +50,7 @@ for (my $n = 0; $n < scalar @ARGV; $n++) { $drvLink = "./derivation"; } - elsif ($arg eq "--no-out-link" or $arg eq "--no-link") { + elsif ($arg eq "--no-out-link" || $arg eq "--no-link") { $outLink = "$tmpDir/result"; } @@ -59,13 +60,13 @@ for (my $n = 0; $n < scalar @ARGV; $n++) { $drvLink = $ARGV[$n]; } - elsif ($arg eq "--out-link" or $arg eq "-o") { + elsif ($arg eq "--out-link" || $arg eq "-o") { $n++; die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV; $outLink = $ARGV[$n]; } - elsif ($arg eq "--attr" or $arg eq "-A" or $arg eq "-I") { + elsif ($arg eq "--attr" || $arg eq "-A" || $arg eq "-I") { $n++; die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV; push @instArgs, ($arg, $ARGV[$n]); @@ -91,7 +92,7 @@ for (my $n = 0; $n < scalar @ARGV; $n++) { $n += 2; } - elsif ($arg eq "--max-jobs" or $arg eq "-j" or $arg eq "--max-silent-time" or $arg eq "--log-type" or $arg eq "--cores" or $arg eq "--timeout") { + elsif ($arg eq "--max-jobs" || $arg eq "-j" || $arg eq "--max-silent-time" || $arg eq "--log-type" || $arg eq "--c||es" || $arg eq "--timeout") { $n++; die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV; push @buildArgs, ($arg, $ARGV[$n]); @@ -110,7 +111,7 @@ for (my $n = 0; $n < scalar @ARGV; $n++) { @exprs = ("-"); } - elsif ($arg eq "--verbose" or substr($arg, 0, 2) eq "-v") { + elsif ($arg eq "--verbose" || substr($arg, 0, 2) eq "-v") { push @buildArgs, $arg; push @instArgs, $arg; $verbose = 1; @@ -144,6 +145,11 @@ for (my $n = 0; $n < scalar @ARGV; $n++) { elsif ($arg eq "--pure") { $pure = 1; } elsif ($arg eq "--impure") { $pure = 0; } + elsif ($arg eq "--expr" || $arg eq "-E") { + $fromArgs = 1; + push @instArgs, "--expr"; + } + elsif (substr($arg, 0, 1) eq "-") { push @buildArgs, $arg; } @@ -153,8 +159,10 @@ for (my $n = 0; $n < scalar @ARGV; $n++) { } } -@exprs = ("shell.nix") if scalar @exprs == 0 && $runEnv && -e "shell.nix"; -@exprs = ("default.nix") if scalar @exprs == 0; +if (!$fromArgs) { + @exprs = ("shell.nix") if scalar @exprs == 0 && $runEnv && -e "shell.nix"; + @exprs = ("default.nix") if scalar @exprs == 0; +} $ENV{'IN_NIX_SHELL'} = 1 if $runEnv; diff --git a/src/nix-instantiate/nix-instantiate.cc b/src/nix-instantiate/nix-instantiate.cc index e9f1284eb..941728698 100644 --- a/src/nix-instantiate/nix-instantiate.cc +++ b/src/nix-instantiate/nix-instantiate.cc @@ -89,6 +89,7 @@ void run(Strings args) EvalState state; Strings files; bool readStdin = false; + bool fromArgs = false; bool findFile = false; bool evalOnly = false; bool parseOnly = false; @@ -104,6 +105,8 @@ void run(Strings args) if (arg == "-") readStdin = true; + else if (arg == "--expr" || arg == "-E") + fromArgs = true; else if (arg == "--eval-only") evalOnly = true; else if (arg == "--read-write-mode") @@ -162,11 +165,13 @@ void run(Strings args) Expr * e = parseStdin(state); processExpr(state, attrPaths, parseOnly, strict, autoArgs, evalOnly, xmlOutput, xmlOutputSourceLocation, e); - } else if (files.empty()) + } else if (files.empty() && !fromArgs) files.push_back("./default.nix"); foreach (Strings::iterator, i, files) { - Expr * e = state.parseExprFromFile(resolveExprPath(lookupFileArg(state, *i))); + Expr * e = fromArgs + ? state.parseExprFromString(*i, absPath(".")) + : state.parseExprFromFile(resolveExprPath(lookupFileArg(state, *i))); processExpr(state, attrPaths, parseOnly, strict, autoArgs, evalOnly, xmlOutput, xmlOutputSourceLocation, e); }