From a3e6415ba8cf1b8d2a1db40c06997d997eac8afc Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 12 Dec 2006 23:05:01 +0000 Subject: * New primop builtins.filterSource, which can be used to filter files from a source directory. All files for which a predicate function returns true are copied to the store. Typical example is to leave out the .svn directory: stdenv.mkDerivation { ... src = builtins.filterSource (path: baseNameOf (toString path) != ".svn") ./source-dir; # as opposed to # src = ./source-dir; } This is important because the .svn directory influences the hash in a rather unpredictable and variable way. --- src/libexpr/primops.cc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/libexpr') diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index fcf035450..84d9f5a13 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -3,6 +3,7 @@ #include "globals.hh" #include "store-api.hh" #include "util.hh" +#include "archive.hh" #include "expr-to-xml.hh" #include "nixexpr-ast.hh" @@ -726,6 +727,42 @@ static Expr primLessThan(EvalState & state, const ATermVector & args) } +struct FilterFromExpr : PathFilter +{ + EvalState & state; + Expr filter; + + FilterFromExpr(EvalState & state, Expr filter) + : state(state), filter(filter) + { + } + + bool operator () (const Path & path) + { + printMsg(lvlError, format("filter %1%") % path); + Expr call = makeCall(filter, makePath(toATerm(path))); + return evalBool(state, call); + } +}; + + +static Expr primFilterSource(EvalState & state, const ATermVector & args) +{ + PathSet context; + Path path = coerceToPath(state, args[1], context); + if (!context.empty()) + throw EvalError(format("string `%1%' cannot refer to other paths") % path); + + FilterFromExpr filter(state, args[0]); + + Path dstPath = readOnlyMode + ? computeStorePathForPath(path, false, false, "", filter).first + : store->addToStore(path, false, false, "", filter); + + return makeStr(dstPath, singleton(dstPath)); +} + + void EvalState::addPrimOps() { addPrimOp("builtins", 0, primBuiltins); @@ -762,6 +799,7 @@ void EvalState::addPrimOps() addPrimOp("__add", 2, primAdd); addPrimOp("__lessThan", 2, primLessThan); addPrimOp("__toFile", 2, primToFile); + addPrimOp("__filterSource", 2, primFilterSource); } -- cgit v1.2.3