diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-09-24T17·48+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-09-24T17·48+0000 |
commit | e47e0c2dbe808f299eb912da94640b95954703f7 (patch) | |
tree | fc95213e79072e3f4d7709529a418a01d1a02d2a | |
parent | df8873e14ad071812e27f38b69783f04dbae5f44 (diff) |
* Builtin function `getEnv' for getting environment variables.
-rw-r--r-- | src/libexpr/primops.cc | 24 | ||||
-rw-r--r-- | tests/lang.sh | 2 | ||||
-rw-r--r-- | tests/lang/eval-okay-getenv.exp | 1 | ||||
-rw-r--r-- | tests/lang/eval-okay-getenv.nix | 1 |
4 files changed, 22 insertions, 6 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 16d165634ffa..5c26233e8870 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -49,6 +49,13 @@ 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"); @@ -67,9 +74,8 @@ static Expr primImport(EvalState & state, const ATermVector & args) } } - if (path == "") - throw TypeError("`import' requires a path or derivation as its argument"); - + else throw TypeError("`import' requires a path or derivation as its argument"); + 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%'") % drvExtension); - /* !!! the name should not end in the derivation extension (.drv). - Likewise for sources. */ - /* Construct the "masked" derivation store expression, which is the final one except that in the list of outputs, the output 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. */ static Expr primMap(EvalState & state, const ATermVector & args) { @@ -811,6 +822,7 @@ void EvalState::addPrimOps() addPrimOp("abort", 1, primAbort); addPrimOp("__head", 1, primHead); addPrimOp("__tail", 1, primTail); + addPrimOp("__getEnv", 1, primGetEnv); addPrimOp("map", 2, primMap); addPrimOp("__getAttr", 2, primGetAttr); diff --git a/tests/lang.sh b/tests/lang.sh index b29cd344f3c8..2a14deeea8ed 100644 --- a/tests/lang.sh +++ b/tests/lang.sh @@ -1,5 +1,7 @@ source common.sh +export TEST_VAR=foo # for eval-okay-getenv.nix + fail=0 for i in lang/parse-fail-*.nix; do diff --git a/tests/lang/eval-okay-getenv.exp b/tests/lang/eval-okay-getenv.exp new file mode 100644 index 000000000000..dddca4459bfb --- /dev/null +++ b/tests/lang/eval-okay-getenv.exp @@ -0,0 +1 @@ +Str("foobar") diff --git a/tests/lang/eval-okay-getenv.nix b/tests/lang/eval-okay-getenv.nix new file mode 100644 index 000000000000..4cfec5f553d9 --- /dev/null +++ b/tests/lang/eval-okay-getenv.nix @@ -0,0 +1 @@ +builtins.getEnv "TEST_VAR" + (if builtins.getEnv "NO_SUCH_VAR" == "" then "bar" else "bla") |