From a8edf185a9e1677088c8c30acc9d281c8350bca7 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 26 May 2014 14:55:47 +0200 Subject: [PATCH] =?UTF-8?q?Add=20constant=20=E2=80=98nixPath=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It contains the Nix expression search path as a list of { prefix, path } sets, e.g. [ { path = "/nix/var/nix/profiles/per-user/root/channels/nixos"; prefix = ""; } { path = "/etc/nixos/configuration.nix"; prefix = "nixos-config"; } { path = "/home/eelco/Dev/nix/inst/share/nix/corepkgs"; prefix = "nix"; } ] --- src/libexpr/primops.cc | 11 +++++++++++ tests/lang.sh | 2 +- tests/lang/eval-okay-search-path.nix | 9 ++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 533ae3768..e492ff683 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1352,6 +1352,17 @@ void EvalState::createBaseEnv() evalFile(path, v); addConstant("derivation", v); + /* Add a value containing the current Nix expression search path. */ + mkList(v, searchPath.size()); + int n = 0; + for (auto & i : searchPath) { + Value * v2 = v.list.elems[n++] = allocValue(); + mkAttrs(*v2, 2); + mkString(*allocAttr(*v2, symbols.create("path")), i.second); + mkString(*allocAttr(*v2, symbols.create("prefix")), i.first); + } + addConstant("nixPath", v); + /* Now that we've added all primops, sort the `builtins' set, because attribute lookups expect it to be sorted. */ baseEnv.values[0]->attrs->sort(); diff --git a/tests/lang.sh b/tests/lang.sh index bb3b9ca77..7157a68c5 100644 --- a/tests/lang.sh +++ b/tests/lang.sh @@ -46,7 +46,7 @@ for i in lang/eval-okay-*.nix; do if test -e lang/$i.flags; then flags=$(cat lang/$i.flags) fi - if ! NIX_PATH=lang/dir3:lang/dir4_PATH nix-instantiate $flags --eval --strict lang/$i.nix > lang/$i.out; then + if ! NIX_PATH=lang/dir3:lang/dir4 nix-instantiate $flags --eval --strict lang/$i.nix > lang/$i.out; then echo "FAIL: $i should evaluate" fail=1 elif ! diff lang/$i.out lang/$i.exp; then diff --git a/tests/lang/eval-okay-search-path.nix b/tests/lang/eval-okay-search-path.nix index 9b7115023..c9ea768a4 100644 --- a/tests/lang/eval-okay-search-path.nix +++ b/tests/lang/eval-okay-search-path.nix @@ -1,3 +1,10 @@ -assert builtins.pathExists ; +with import ./lib.nix; +with builtins; + +assert pathExists ; + +assert length nixPath == 3; +assert length (filter (x: x.prefix == "nix") nixPath) == 1; +assert length (filter (x: baseNameOf x.path == "dir4") nixPath) == 1; import + import + import + import