* Builtin function `getEnv' for getting environment variables.

This commit is contained in:
Eelco Dolstra 2006-09-24 17:48:41 +00:00
parent df8873e14a
commit e47e0c2dbe
4 changed files with 22 additions and 6 deletions

View file

@ -49,6 +49,13 @@ static Expr primImport(EvalState & state, const ATermVector & args)
if (matchPath(arg, arg2)) if (matchPath(arg, arg2))
path = aterm2String(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)) { else if (matchAttrs(arg, es)) {
Expr a = queryAttr(arg, "type"); Expr a = queryAttr(arg, "type");
@ -67,8 +74,7 @@ static Expr primImport(EvalState & state, const ATermVector & args)
} }
} }
if (path == "") else throw TypeError("`import' requires a path or derivation as its argument");
throw TypeError("`import' requires a path or derivation as its argument");
return evalFile(state, path); return evalFile(state, path);
} }
@ -374,9 +380,6 @@ static Expr primDerivationStrict(EvalState & state, const ATermVector & args)
throw EvalError(format("derivation names are not allowed to end in `%1%'") throw EvalError(format("derivation names are not allowed to end in `%1%'")
% drvExtension); % drvExtension);
/* !!! the name should not end in the derivation extension (.drv).
Likewise for sources. */
/* Construct the "masked" derivation store expression, which is /* Construct the "masked" derivation store expression, which is
the final one except that in the list of outputs, the output the final one except that in the list of outputs, the output
paths are empty, and the corresponding environment variables paths are empty, and the corresponding environment variables
@ -702,6 +705,14 @@ static Expr primTail(EvalState & state, const ATermVector & args)
} }
/* Return an environment variable. Use with care. */
static Expr primGetEnv(EvalState & state, const ATermVector & args)
{
string name = evalString(state, args[0]);
return makeStr(toATerm(getEnv(name)));
}
/* Apply a function to every element of a list. */ /* Apply a function to every element of a list. */
static Expr primMap(EvalState & state, const ATermVector & args) static Expr primMap(EvalState & state, const ATermVector & args)
{ {
@ -811,6 +822,7 @@ void EvalState::addPrimOps()
addPrimOp("abort", 1, primAbort); addPrimOp("abort", 1, primAbort);
addPrimOp("__head", 1, primHead); addPrimOp("__head", 1, primHead);
addPrimOp("__tail", 1, primTail); addPrimOp("__tail", 1, primTail);
addPrimOp("__getEnv", 1, primGetEnv);
addPrimOp("map", 2, primMap); addPrimOp("map", 2, primMap);
addPrimOp("__getAttr", 2, primGetAttr); addPrimOp("__getAttr", 2, primGetAttr);

View file

@ -1,5 +1,7 @@
source common.sh source common.sh
export TEST_VAR=foo # for eval-okay-getenv.nix
fail=0 fail=0
for i in lang/parse-fail-*.nix; do for i in lang/parse-fail-*.nix; do

View file

@ -0,0 +1 @@
Str("foobar")

View file

@ -0,0 +1 @@
builtins.getEnv "TEST_VAR" + (if builtins.getEnv "NO_SUCH_VAR" == "" then "bar" else "bla")