* "Nix expression" -> "store expression".

* More refactoring.
This commit is contained in:
Eelco Dolstra 2003-11-18 11:22:29 +00:00
parent 9f0f020929
commit ce92d1bf14
20 changed files with 121 additions and 119 deletions

View file

@ -1,6 +1,6 @@
bin_PROGRAMS = fix-ng
fix_ng_SOURCES = fix-expr.cc parser.cc eval.cc primops.cc fix.cc
fix_ng_SOURCES = fixexpr.cc parser.cc eval.cc primops.cc fix.cc
fix_ng_LDADD = ../libmain/libmain.a ../libstore/libstore.a ../libutil/libutil.a \
../boost/format/libformat.a -L../../externals/inst/lib -ldb_cxx \
-lsglr -lATB -lconversion -lasfix2 -lmept -lATerm

View file

@ -1,5 +1,4 @@
#include "eval.hh"
#include "expr.hh"
#include "parser.hh"
#include "primops.hh"

View file

@ -3,8 +3,9 @@
#include <map>
#include "fix-expr.hh"
#include "expr.hh"
#include "aterm.hh"
#include "hash.hh"
#include "fixexpr.hh"
typedef map<Path, PathSet> DrvPaths;

View file

@ -4,7 +4,6 @@
#include "globals.hh"
#include "normalise.hh"
#include "shared.hh"
#include "expr.hh"
#include "eval.hh"

View file

@ -1,5 +1,5 @@
#include "fix-expr.hh"
#include "expr.hh"
#include "fixexpr.hh"
#include "storeexpr.hh"
ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct)

View file

@ -10,10 +10,10 @@ extern "C" {
#include <asfix2.h>
}
#include "aterm.hh"
#include "parser.hh"
#include "shared.hh"
#include "fix-expr.hh"
#include "expr.hh"
#include "fixexpr.hh"
#include "parse-table.h"

View file

@ -1,7 +1,7 @@
#ifndef __PARSER_H
#define __PARSER_H
#include "fix-expr.hh"
#include "fixexpr.hh"
Expr parseExprFromFile(Path path);

View file

@ -13,22 +13,22 @@ Expr primImport(EvalState & state, Expr arg)
}
static PathSet nixExprRootsCached(EvalState & state, const Path & nePath)
static PathSet storeExprRootsCached(EvalState & state, const Path & nePath)
{
DrvPaths::iterator i = state.drvPaths.find(nePath);
if (i != state.drvPaths.end())
return i->second;
else {
PathSet paths = nixExprRoots(nePath);
PathSet paths = storeExprRoots(nePath);
state.drvPaths[nePath] = paths;
return paths;
}
}
static Hash hashDerivation(EvalState & state, NixExpr ne)
static Hash hashDerivation(EvalState & state, StoreExpr ne)
{
if (ne.type == NixExpr::neDerivation) {
if (ne.type == StoreExpr::neDerivation) {
PathSet inputs2;
for (PathSet::iterator i = ne.derivation.inputs.begin();
i != ne.derivation.inputs.end(); i++)
@ -40,7 +40,7 @@ static Hash hashDerivation(EvalState & state, NixExpr ne)
}
ne.derivation.inputs = inputs2;
}
return hashTerm(unparseNixExpr(ne));
return hashTerm(unparseStoreExpr(ne));
}
@ -50,13 +50,13 @@ static Path copyAtom(EvalState & state, const Path & srcPath)
Path dstPath(addToStore(srcPath));
ClosureElem elem;
NixExpr ne;
ne.type = NixExpr::neClosure;
StoreExpr ne;
ne.type = StoreExpr::neClosure;
ne.closure.roots.insert(dstPath);
ne.closure.elems[dstPath] = elem;
Hash drvHash = hashDerivation(state, ne);
Path drvPath = writeTerm(unparseNixExpr(ne), "");
Path drvPath = writeTerm(unparseStoreExpr(ne), "");
state.drvHashes[drvPath] = drvHash;
printMsg(lvlChatty, format("copied `%1%' -> closure `%2%'")
@ -66,9 +66,9 @@ static Path copyAtom(EvalState & state, const Path & srcPath)
static string addInput(EvalState & state,
Path & nePath, NixExpr & ne)
Path & nePath, StoreExpr & ne)
{
PathSet paths = nixExprRootsCached(state, nePath);
PathSet paths = storeExprRootsCached(state, nePath);
if (paths.size() != 1) abort();
Path path = *(paths.begin());
ne.derivation.inputs.insert(nePath);
@ -76,7 +76,7 @@ static string addInput(EvalState & state,
}
static string processBinding(EvalState & state, Expr e, NixExpr & ne)
static string processBinding(EvalState & state, Expr e, StoreExpr & ne)
{
e = evalExpr(state, e);
@ -131,8 +131,8 @@ Expr primDerivation(EvalState & state, Expr args)
queryAllAttrs(args, attrs);
/* Build the derivation expression by processing the attributes. */
NixExpr ne;
ne.type = NixExpr::neDerivation;
StoreExpr ne;
ne.type = StoreExpr::neDerivation;
string drvName;
Path outPath;
@ -198,7 +198,7 @@ Expr primDerivation(EvalState & state, Expr args)
Hash drvHash = outHashGiven
? hashString((string) outHash + outPath)
: hashDerivation(state, ne);
Path drvPath = writeTerm(unparseNixExpr(ne), "-d-" + drvName);
Path drvPath = writeTerm(unparseStoreExpr(ne), "-d-" + drvName);
state.drvHashes[drvPath] = drvHash;
printMsg(lvlChatty, format("instantiated `%1%' -> `%2%'")

View file

@ -1,7 +1,7 @@
noinst_LIBRARIES = libstore.a
libstore_a_SOURCES = \
store.cc expr.cc normalise.cc exec.cc \
store.cc storeexpr.cc normalise.cc exec.cc \
globals.cc db.cc references.cc pathlocks.cc
AM_CXXFLAGS = -DSYSTEM=\"@host@\" -Wall \

View file

@ -18,21 +18,21 @@ static Path useSuccessor(const Path & path)
}
Path normaliseNixExpr(const Path & _nePath, PathSet pending)
Path normaliseStoreExpr(const Path & _nePath, PathSet pending)
{
startNest(nest, lvlTalkative,
format("normalising expression in `%1%'") % (string) _nePath);
format("normalising store expression in `%1%'") % (string) _nePath);
/* Try to substitute the expression by any known successors in
order to speed up the rewrite process. */
Path nePath = useSuccessor(_nePath);
/* Get the Nix expression. */
NixExpr ne = exprFromPath(nePath, pending);
/* Get the store expression. */
StoreExpr ne = storeExprFromPath(nePath, pending);
/* If this is a normal form (i.e., a closure) we are done. */
if (ne.type == NixExpr::neClosure) return nePath;
if (ne.type != NixExpr::neDerivation) abort();
if (ne.type == StoreExpr::neClosure) return nePath;
if (ne.type != StoreExpr::neDerivation) abort();
/* Otherwise, it's a derivation expression, and we have to build it to
@ -51,8 +51,8 @@ Path normaliseNixExpr(const Path & _nePath, PathSet pending)
Environment env;
/* The result. */
NixExpr nf;
nf.type = NixExpr::neClosure;
StoreExpr nf;
nf.type = StoreExpr::neClosure;
/* The outputs are referenceable paths. */
@ -78,10 +78,10 @@ Path normaliseNixExpr(const Path & _nePath, PathSet pending)
{
Path nePath2 = useSuccessor(nePath);
if (nePath != nePath2) {
NixExpr ne = exprFromPath(nePath2, pending);
StoreExpr ne = storeExprFromPath(nePath2, pending);
debug(format("skipping build of expression `%1%', someone beat us to it")
% (string) nePath);
if (ne.type != NixExpr::neClosure) abort();
if (ne.type != StoreExpr::neClosure) abort();
return nePath2;
}
}
@ -95,12 +95,12 @@ Path normaliseNixExpr(const Path & _nePath, PathSet pending)
for (PathSet::iterator i = ne.derivation.inputs.begin();
i != ne.derivation.inputs.end(); i++)
{
Path nfPath = normaliseNixExpr(*i, pending);
Path nfPath = normaliseStoreExpr(*i, pending);
realiseClosure(nfPath, pending);
/* !!! nfPath should be a root of the garbage collector while
we are building */
NixExpr ne = exprFromPath(nfPath, pending);
if (ne.type != NixExpr::neClosure) abort();
StoreExpr ne = storeExprFromPath(nfPath, pending);
if (ne.type != StoreExpr::neClosure) abort();
for (ClosureElems::iterator j = ne.closure.elems.begin();
j != ne.closure.elems.end(); j++)
{
@ -238,7 +238,7 @@ Path normaliseNixExpr(const Path & _nePath, PathSet pending)
/* Write the normal form. This does not have to occur in the
transaction below because writing terms is idem-potent. */
ATerm nfTerm = unparseNixExpr(nf);
ATerm nfTerm = unparseStoreExpr(nf);
printMsg(lvlVomit, format("normal form: %1%") % atPrint(nfTerm));
Path nfPath = writeTerm(nfTerm, "-s");
@ -264,8 +264,8 @@ void realiseClosure(const Path & nePath, PathSet pending)
{
startNest(nest, lvlDebug, format("realising closure `%1%'") % nePath);
NixExpr ne = exprFromPath(nePath, pending);
if (ne.type != NixExpr::neClosure)
StoreExpr ne = storeExprFromPath(nePath, pending);
if (ne.type != StoreExpr::neClosure)
throw Error(format("expected closure in `%1%'") % nePath);
for (ClosureElems::const_iterator i = ne.closure.elems.begin();
@ -286,7 +286,7 @@ void ensurePath(const Path & path, PathSet pending)
i != subPaths.end(); i++)
{
try {
normaliseNixExpr(*i, pending);
normaliseStoreExpr(*i, pending);
if (isValidPath(path)) return;
throw Error(format("substitute failed to produce expected output path"));
} catch (Error & e) {
@ -301,24 +301,24 @@ void ensurePath(const Path & path, PathSet pending)
}
NixExpr exprFromPath(const Path & path, PathSet pending)
StoreExpr storeExprFromPath(const Path & path, PathSet pending)
{
ensurePath(path, pending);
ATerm t = ATreadFromNamedFile(path.c_str());
if (!t) throw Error(format("cannot read aterm from `%1%'") % path);
return parseNixExpr(t);
return parseStoreExpr(t);
}
PathSet nixExprRoots(const Path & nePath)
PathSet storeExprRoots(const Path & nePath)
{
PathSet paths;
NixExpr ne = exprFromPath(nePath);
StoreExpr ne = storeExprFromPath(nePath);
if (ne.type == NixExpr::neClosure)
if (ne.type == StoreExpr::neClosure)
paths.insert(ne.closure.roots.begin(), ne.closure.roots.end());
else if (ne.type == NixExpr::neDerivation)
else if (ne.type == StoreExpr::neDerivation)
paths.insert(ne.derivation.outputs.begin(),
ne.derivation.outputs.end());
else abort();
@ -334,14 +334,14 @@ static void requisitesWorker(const Path & nePath,
if (doneSet.find(nePath) != doneSet.end()) return;
doneSet.insert(nePath);
NixExpr ne = exprFromPath(nePath);
StoreExpr ne = storeExprFromPath(nePath);
if (ne.type == NixExpr::neClosure)
if (ne.type == StoreExpr::neClosure)
for (ClosureElems::iterator i = ne.closure.elems.begin();
i != ne.closure.elems.end(); i++)
paths.insert(i->first);
else if (ne.type == NixExpr::neDerivation)
else if (ne.type == StoreExpr::neDerivation)
for (PathSet::iterator i = ne.derivation.inputs.begin();
i != ne.derivation.inputs.end(); i++)
requisitesWorker(*i,
@ -358,7 +358,7 @@ static void requisitesWorker(const Path & nePath,
}
PathSet nixExprRequisites(const Path & nePath,
PathSet storeExprRequisites(const Path & nePath,
bool includeExprs, bool includeSuccessors)
{
PathSet paths;

View file

@ -1,16 +1,16 @@
#ifndef __NORMALISE_H
#define __NORMALISE_H
#include "expr.hh"
#include "storeexpr.hh"
/* Normalise a Nix expression. That is, if the expression is a
/* Normalise a store expression. That is, if the expression is a
derivation, a path containing an equivalent closure expression is
returned. This requires that the derivation is performed, unless a
successor is known. */
Path normaliseNixExpr(const Path & nePath, PathSet pending = PathSet());
Path normaliseStoreExpr(const Path & nePath, PathSet pending = PathSet());
/* Realise a closure expression in the file system.
/* Realise a closure store expression in the file system.
The pending paths are those that are already being realised. This
prevents infinite recursion for paths realised through a substitute
@ -22,23 +22,25 @@ void realiseClosure(const Path & nePath, PathSet pending = PathSet());
realising a substitute. */
void ensurePath(const Path & path, PathSet pending = PathSet());
/* Read a Nix expression, after ensuring its existence through
/* Read a store expression, after ensuring its existence through
ensurePath(). */
NixExpr exprFromPath(const Path & path, PathSet pending = PathSet());
StoreExpr storeExprFromPath(const Path & path, PathSet pending = PathSet());
/* Get the list of root (output) paths of the given Nix expression. */
PathSet nixExprRoots(const Path & nePath);
/* Get the list of root (output) paths of the given store
expression. */
PathSet storeExprRoots(const Path & nePath);
/* Get the list of paths that are required to realise the given
/* Get the list of paths that are required to realise the given store
expression. For a derive expression, this is the union of
requisites of the inputs; for a closure expression, it is the path of
each element in the closure. If `includeExprs' is true, include the
paths of the Nix expressions themselves. If `includeSuccessors' is
true, include the requisites of successors. */
PathSet nixExprRequisites(const Path & nePath,
requisites of the inputs; for a closure expression, it is the path
of each element in the closure. If `includeExprs' is true, include
the paths of the store expressions themselves. If
`includeSuccessors' is true, include the requisites of
successors. */
PathSet storeExprRequisites(const Path & nePath,
bool includeExprs, bool includeSuccessors);
/* Return the list of the paths of all known Nix expressions whose
/* Return the list of the paths of all known store expressions whose
output paths are completely contained in the set `outputs'. */
PathSet findGenerators(const PathSet & outputs);

View file

@ -1,21 +1,8 @@
#include "expr.hh"
#include "storeexpr.hh"
#include "globals.hh"
#include "store.hh"
Error badTerm(const format & f, ATerm t)
{
char * s = ATwriteToString(t);
if (!s) throw Error("cannot print term");
if (strlen(s) > 1000) {
int len;
s = ATwriteToSharedString(t, &len);
if (!s) throw Error("cannot print term");
}
return Error(format("%1%, in `%2%'") % f.str() % (string) s);
}
Hash hashTerm(ATerm t)
{
return hashString(atPrint(t));
@ -138,14 +125,14 @@ static bool parseDerivation(ATerm t, Derivation & derivation)
}
NixExpr parseNixExpr(ATerm t)
StoreExpr parseStoreExpr(ATerm t)
{
NixExpr ne;
StoreExpr ne;
if (parseClosure(t, ne.closure))
ne.type = NixExpr::neClosure;
ne.type = StoreExpr::neClosure;
else if (parseDerivation(t, ne.derivation))
ne.type = NixExpr::neDerivation;
else throw badTerm("not a Nix expression", t);
ne.type = StoreExpr::neDerivation;
else throw badTerm("not a store expression", t);
return ne;
}
@ -200,11 +187,11 @@ static ATerm unparseDerivation(const Derivation & derivation)
}
ATerm unparseNixExpr(const NixExpr & ne)
ATerm unparseStoreExpr(const StoreExpr & ne)
{
if (ne.type == NixExpr::neClosure)
if (ne.type == StoreExpr::neClosure)
return unparseClosure(ne.closure);
else if (ne.type == NixExpr::neDerivation)
else if (ne.type == StoreExpr::neDerivation)
return unparseDerivation(ne.derivation);
else abort();
}

View file

@ -1,11 +1,11 @@
#ifndef __FSTATE_H
#define __FSTATE_H
#ifndef __STOREEXPR_H
#define __STOREEXPR_H
#include "aterm.hh"
#include "store.hh"
/* Abstract syntax of Nix expressions. */
/* Abstract syntax of store expressions. */
struct ClosureElem
{
@ -25,14 +25,14 @@ typedef map<string, string> StringPairs;
struct Derivation
{
PathSet outputs;
PathSet inputs; /* Nix expressions, not actual inputs */
PathSet inputs; /* Store expressions, not actual inputs */
string platform;
Path builder;
Strings args;
StringPairs env;
};
struct NixExpr
struct StoreExpr
{
enum { neClosure, neDerivation } type;
Closure closure;
@ -40,21 +40,17 @@ struct NixExpr
};
/* Throw an exception with an error message containing the given
aterm. */
Error badTerm(const format & f, ATerm t);
/* Hash an aterm. */
Hash hashTerm(ATerm t);
/* Write an aterm to the Nix store directory, and return its path. */
Path writeTerm(ATerm t, const string & suffix);
/* Parse a Nix expression. */
NixExpr parseNixExpr(ATerm t);
/* Parse a store expression. */
StoreExpr parseStoreExpr(ATerm t);
/* Parse a Nix expression. */
ATerm unparseNixExpr(const NixExpr & ne);
/* Parse a store expression. */
ATerm unparseStoreExpr(const StoreExpr & ne);
#endif /* !__FSTATE_H */
#endif /* !__STOREEXPR_H */

View file

@ -91,3 +91,16 @@ ATMatcher & operator >> (ATMatcher & pos, ATermList & out)
out = (ATermList) t;
return pos;
}
Error badTerm(const format & f, ATerm t)
{
char * s = ATwriteToString(t);
if (!s) throw Error("cannot print term");
if (strlen(s) > 1000) {
int len;
s = ATwriteToSharedString(t, &len);
if (!s) throw Error("cannot print term");
}
return Error(format("%1%, in `%2%'") % f.str() % (string) s);
}

View file

@ -74,4 +74,9 @@ ATMatcher & operator >> (ATMatcher & pos, const string & s);
ATMatcher & operator >> (ATMatcher & pos, ATermList & out);
/* Throw an exception with an error message containing the given
aterm. */
Error badTerm(const format & f, ATerm t);
#endif /* !__ATERM_H */

View file

@ -52,7 +52,7 @@ string pathLabel(const Path & nePath, const string & elemPath)
}
void printClosure(const Path & nePath, const NixExpr & fs)
void printClosure(const Path & nePath, const StoreExpr & fs)
{
PathSet workList(fs.closure.roots);
PathSet doneSet;
@ -100,11 +100,11 @@ void printDotGraph(const PathSet & roots)
if (doneSet.find(nePath) == doneSet.end()) {
doneSet.insert(nePath);
NixExpr ne = exprFromPath(nePath);
StoreExpr ne = storeExprFromPath(nePath);
string label, colour;
if (ne.type == NixExpr::neDerivation) {
if (ne.type == StoreExpr::neDerivation) {
for (PathSet::iterator i = ne.derivation.inputs.begin();
i != ne.derivation.inputs.end(); i++)
{
@ -119,7 +119,7 @@ void printDotGraph(const PathSet & roots)
if (i->first == "name") label = i->second;
}
else if (ne.type == NixExpr::neClosure) {
else if (ne.type == StoreExpr::neClosure) {
label = "<closure>";
colour = "#00ffff";
printClosure(nePath, ne);

View file

@ -1,7 +1,7 @@
#ifndef __DOTGRAPH_H
#define __DOTGRAPH_H
#include "expr.hh"
#include "storeexpr.hh"
void printDotGraph(const PathSet & roots);

View file

@ -2,7 +2,7 @@ nix [OPTIONS...] [ARGUMENTS...]
Operations:
--install / -i: realise a Nix expression
--realise / -r: realise a Nix expression
--delete / -d: delete paths from the Nix store
--add / -A: copy a path to the Nix store
--query / -q: query information
@ -22,7 +22,7 @@ Operations:
Query flags:
--list / -l: query the output paths (roots) of a Nix expression (default)
--requisites / -r: print all paths necessary to realise expression
--requisites / -R: print all paths necessary to realise expression
--predecessors: print predecessors of a Nix expression
--graph: print a dot graph rooted at given ids

View file

@ -27,15 +27,15 @@ static Path checkPath(const Path & arg)
}
/* Realise (or install) paths from the given Nix expressions. */
static void opInstall(Strings opFlags, Strings opArgs)
/* Realise paths from the given store expressions. */
static void opRealise(Strings opFlags, Strings opArgs)
{
if (!opFlags.empty()) throw UsageError("unknown flag");
for (Strings::iterator i = opArgs.begin();
i != opArgs.end(); i++)
{
Path nfPath = normaliseNixExpr(checkPath(*i));
Path nfPath = normaliseStoreExpr(checkPath(*i));
realiseClosure(nfPath);
cout << format("%1%\n") % (string) nfPath;
}
@ -66,7 +66,7 @@ static void opAdd(Strings opFlags, Strings opArgs)
Path maybeNormalise(const Path & ne, bool normalise)
{
return normalise ? normaliseNixExpr(ne) : ne;
return normalise ? normaliseStoreExpr(ne) : ne;
}
@ -82,7 +82,7 @@ static void opQuery(Strings opFlags, Strings opArgs)
for (Strings::iterator i = opFlags.begin();
i != opFlags.end(); i++)
if (*i == "--list" || *i == "-l") query = qList;
else if (*i == "--requisites" || *i == "-r") query = qRequisites;
else if (*i == "--requisites" || *i == "-R") query = qRequisites;
else if (*i == "--predecessors") query = qPredecessors;
else if (*i == "--graph") query = qGraph;
else if (*i == "--normalise" || *i == "-n") normalise = true;
@ -96,7 +96,7 @@ static void opQuery(Strings opFlags, Strings opArgs)
for (Strings::iterator i = opArgs.begin();
i != opArgs.end(); i++)
{
StringSet paths = nixExprRoots(
StringSet paths = storeExprRoots(
maybeNormalise(checkPath(*i), normalise));
for (StringSet::iterator j = paths.begin();
j != paths.end(); j++)
@ -110,7 +110,7 @@ static void opQuery(Strings opFlags, Strings opArgs)
for (Strings::iterator i = opArgs.begin();
i != opArgs.end(); i++)
{
StringSet paths2 = nixExprRequisites(
StringSet paths2 = storeExprRequisites(
maybeNormalise(checkPath(*i), normalise),
includeExprs, includeSuccessors);
paths.insert(paths2.begin(), paths2.end());
@ -258,8 +258,8 @@ void run(Strings args)
Operation oldOp = op;
if (arg == "--install" || arg == "-i")
op = opInstall;
if (arg == "--realise" || arg == "-r")
op = opRealise;
else if (arg == "--delete" || arg == "-d")
op = opDelete;
else if (arg == "--add" || arg == "-A")