* nix-store, nix-instantiate: added an option `--add-root' to

immediately add the result as a permanent GC root.  This is the only
  way to prevent a race with the garbage collector.  For instance, the
  old style

    ln -s $(nix-store -r $(nix-instantiate foo.nix)) \
      /nix/var/nix/gcroots/result

  has two time windows in which the garbage collector can interfere
  (by GC'ing the derivation and the output, respectively).  On the
  other hand,

    nix-store --add-root /nix/var/nix/gcroots/result -r \
      $(nix-instantiate --add-root /nix/var/nix/gcroots/drv \
        foo.nix)

  is safe.

* nix-build: use `--add-root' to prevent GC races.
This commit is contained in:
Eelco Dolstra 2005-02-01 12:36:25 +00:00
parent a6b65fd5e1
commit dcc37c236c
11 changed files with 165 additions and 17 deletions

View file

@ -1,6 +1,6 @@
nix-instantiate [OPTIONS...] [FILES...]
`nix-instantiate' turns Nix expressions into store expressions.
`nix-instantiate' turns Nix expressions into store derivations.
The argument `-' may be specified to read a Nix expression from
standard input.
@ -14,3 +14,5 @@ Options:
--eval-only: evaluate and print resulting term; do not instantiate
--parse-only: parse and print abstract syntax tree
--add-root: add garbage collector roots for the result

View file

@ -3,6 +3,7 @@
#include "globals.hh"
#include "build.hh"
#include "gc.hh"
#include "shared.hh"
#include "eval.hh"
#include "parser.hh"
@ -26,6 +27,14 @@ static Expr evalStdin(EvalState & state, bool parseOnly)
}
static Path gcRoot;
static int rootNr = 0;
/* Print out the paths of the resulting derivation(s). If the user
specified the `--add-root' flag, we register the derivation as a
garbage collection root and print out the path of the GC root
symlink instead. */
static void printDrvPaths(EvalState & state, Expr e)
{
ATermList es;
@ -37,7 +46,13 @@ static void printDrvPaths(EvalState & state, Expr e)
if (a && evalString(state, a) == "derivation") {
a = queryAttr(e, "drvPath");
if (a) {
cout << format("%1%\n") % evalPath(state, a);
Path drvPath = evalPath(state, a);
if (gcRoot == "")
printGCWarning();
else
drvPath = addPermRoot(drvPath,
makeRootName(gcRoot, rootNr));
cout << format("%1%\n") % drvPath;
return;
}
throw Error("bad derivation");
@ -77,10 +92,10 @@ void run(Strings args)
bool evalOnly = false;
bool parseOnly = false;
for (Strings::iterator it = args.begin();
it != args.end(); )
for (Strings::iterator i = args.begin();
i != args.end(); )
{
string arg = *it++;
string arg = *i++;
if (arg == "-")
readStdin = true;
@ -92,6 +107,11 @@ void run(Strings args)
readOnlyMode = true;
parseOnly = evalOnly = true;
}
else if (arg == "--add-root") {
if (i == args.end())
throw UsageError("`--add-root requires an argument");
gcRoot = *i++;
}
else if (arg[0] == '-')
throw UsageError(format("unknown flag `%1%`") % arg);
else
@ -105,10 +125,10 @@ void run(Strings args)
printResult(state, e, evalOnly);
}
for (Strings::iterator it = files.begin();
it != files.end(); it++)
for (Strings::iterator i = files.begin();
i != files.end(); i++)
{
Expr e = evalFile(state, absPath(*it));
Expr e = evalFile(state, absPath(*i));
/* !!! parseOnly ignored */
printResult(state, e, evalOnly);
}