diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 5c26233e88..96ce4f71e6 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -49,13 +49,6 @@ static Expr primImport(EvalState & state, const ATermVector & args) if (matchPath(arg, arg2)) path = aterm2String(arg2); - else if (matchStr(arg, arg2)) { - path = aterm2String(arg2); - if (path == "" || path[0] != '/') - throw EvalError("`import' requires an absolute path name"); - path = canonPath(path); - } - else if (matchAttrs(arg, es)) { Expr a = queryAttr(arg, "type"); @@ -80,6 +73,17 @@ static Expr primImport(EvalState & state, const ATermVector & args) } +static Expr primPathExists(EvalState & state, const ATermVector & args) +{ + Expr arg = evalExpr(state, args[0]), arg2; + + if (!matchPath(arg, arg2)) + throw TypeError("`pathExists' requires a path as its argument"); + + return makeBool(pathExists(aterm2String(arg2))); +} + + static void flattenList(EvalState & state, Expr e, ATermList & result) { ATermList es; @@ -476,6 +480,19 @@ static Expr primToString(EvalState & state, const ATermVector & args) } +/* Convert the argument to a path. */ +static Expr primToPath(EvalState & state, const ATermVector & args) +{ + Expr e = evalExpr(state, args[0]); + ATerm t = coerceToString(e); + if (!t) throw TypeError(format("cannot coerce %1% to a path in `toPath'") % showType(e)); + Path path = aterm2String(t); + if (path == "" || path[0] != '/') + throw EvalError("string doesn't represent an absolute path in `toPath'"); + return makePath(toATerm(canonPath(path))); +} + + /* Convert the argument (which can be any Nix expression) to an XML representation returned in a string. Not all Nix expressions can be sensibly or completely represented (e.g., functions). */ @@ -809,11 +826,13 @@ void EvalState::addPrimOps() addPrimOp("__currentTime", 0, primCurrentTime); addPrimOp("import", 1, primImport); + addPrimOp("__pathExists", 1, primPathExists); addPrimOp("derivation!", 1, primDerivationStrict); addPrimOp("derivation", 1, primDerivationLazy); addPrimOp("baseNameOf", 1, primBaseNameOf); addPrimOp("dirOf", 1, primDirOf); addPrimOp("toString", 1, primToString); + addPrimOp("__toPath", 1, primToPath); addPrimOp("__toXML", 1, primToXML); addPrimOp("__toFile", 1, primToFile); addPrimOp("isNull", 1, primIsNull); diff --git a/tests/lang/eval-okay-pathexists.exp b/tests/lang/eval-okay-pathexists.exp new file mode 100644 index 0000000000..2015847b65 --- /dev/null +++ b/tests/lang/eval-okay-pathexists.exp @@ -0,0 +1 @@ +Bool(True) diff --git a/tests/lang/eval-okay-pathexists.nix b/tests/lang/eval-okay-pathexists.nix new file mode 100644 index 0000000000..cabd6d9c75 --- /dev/null +++ b/tests/lang/eval-okay-pathexists.nix @@ -0,0 +1,3 @@ +builtins.pathExists (builtins.toPath ./lib.nix) +&& builtins.pathExists ./lib.nix +&& !builtins.pathExists ./bla.nix