guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

09/118: Make the Nix search path declarative


From: Ludovic Courtès
Subject: 09/118: Make the Nix search path declarative
Date: Tue, 19 May 2015 14:45:11 +0000

civodul pushed a commit to branch nix
in repository guix.

commit 62a6eeb1f3da0a5954ad2da54c454eb7fc1c6e5d
Author: Eelco Dolstra <address@hidden>
Date:   Mon May 26 17:02:22 2014 +0200

    Make the Nix search path declarative
    
    Nix search path lookups like <nixpkgs> are now desugared to ‘findFile
    nixPath <nixpkgs>’, where ‘findFile’ is a new primop. Thus you can
    override the search path simply by saying
    
      let
        nixPath = [ { prefix = "nixpkgs"; path = "/my-nixpkgs"; } ];
      in ... <nixpkgs> ...
    
    In conjunction with ‘scopedImport’ (commit
    c273c15cb13bb86420dda1e5341a4e19517532b5), the Nix search path can be
    propagated across imports, e.g.
    
      let
    
        overrides = {
          nixPath = [ ... ] ++ builtins.nixPath;
          import = fn: scopedImport overrides fn;
          scopedImport = attrs: fn: scopedImport (overrides // attrs) fn;
          builtins = builtins // overrides;
        };
    
      in scopedImport overrides ./nixos
---
 src/libexpr/common-opts.cc           |    4 +---
 src/libexpr/eval.hh                  |    5 ++++-
 src/libexpr/nixexpr.hh               |    1 +
 src/libexpr/parser.y                 |   23 +++++++++++------------
 src/libexpr/primops.cc               |   32 ++++++++++++++++++++++++++++++++
 tests/lang/eval-okay-search-path.exp |    2 +-
 tests/lang/eval-okay-search-path.nix |    1 +
 7 files changed, 51 insertions(+), 17 deletions(-)

diff --git a/src/libexpr/common-opts.cc b/src/libexpr/common-opts.cc
index 14a75f7..a3ea202 100644
--- a/src/libexpr/common-opts.cc
+++ b/src/libexpr/common-opts.cc
@@ -48,9 +48,7 @@ Path lookupFileArg(EvalState & state, string s)
 {
     if (s.size() > 2 && s.at(0) == '<' && s.at(s.size() - 1) == '>') {
         Path p = s.substr(1, s.size() - 2);
-        Path p2 = state.findFile(p);
-        if (p2 == "") throw Error(format("file `%1%' was not found in the Nix 
search path (add it using $NIX_PATH or -I)") % p);
-        return p2;
+        return state.findFile(p);
     } else
         return absPath(s);
 }
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index 200ec75..aa706cf 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -86,6 +86,9 @@ typedef std::map<Path, Path> SrcToStore;
 std::ostream & operator << (std::ostream & str, const Value & v);
 
 
+typedef list<std::pair<string, Path> > SearchPath;
+
+
 class EvalState
 {
 public:
@@ -111,7 +114,6 @@ private:
 #endif
     FileEvalCache fileEvalCache;
 
-    typedef list<std::pair<string, Path> > SearchPath;
     SearchPath searchPath;
 
 public:
@@ -137,6 +139,7 @@ public:
 
     /* Look up a file in the search path. */
     Path findFile(const string & path);
+    Path findFile(SearchPath & searchPath, const string & path);
 
     /* Evaluate an expression to normal form, storing the result in
        value `v'. */
diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh
index fbd5bad..8826ad2 100644
--- a/src/libexpr/nixexpr.hh
+++ b/src/libexpr/nixexpr.hh
@@ -142,6 +142,7 @@ struct ExprVar : Expr
     unsigned int level;
     unsigned int displ;
 
+    ExprVar(const Symbol & name) : name(name) { };
     ExprVar(const Pos & pos, const Symbol & name) : pos(pos), name(name) { };
     COMMON_METHODS
     Value * maybeThunk(EvalState & state, Env & env);
diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y
index 698e8ce..134d68d 100644
--- a/src/libexpr/parser.y
+++ b/src/libexpr/parser.y
@@ -386,17 +386,10 @@ expr_simple
   | PATH { $$ = new ExprPath(absPath($1, data->basePath)); }
   | SPATH {
       string path($1 + 1, strlen($1) - 2);
-      Path path2 = data->state.findFile(path);
-      /* The file wasn't found in the search path.  However, we can't
-         throw an error here, because the expression might never be
-         evaluated.  So return an expression that lazily calls
-         ‘throw’. */
-      $$ = path2 == ""
-          ? (Expr * ) new ExprApp(
-              new ExprBuiltin(data->symbols.create("throw")),
-              new ExprString(data->symbols.create(
-                      (format("file `%1%' was not found in the Nix search path 
(add it using $NIX_PATH or -I)") % path).str())))
-          : (Expr * ) new ExprPath(path2);
+      $$ = new ExprApp(CUR_POS,
+          new ExprApp(new ExprVar(data->symbols.create("__findFile")),
+              new ExprVar(data->symbols.create("nixPath"))),
+          new ExprString(data->symbols.create(path)));
   }
   | URI { $$ = new ExprString(data->symbols.create($1)); }
   | '(' expr ')' { $$ = $2; }
@@ -637,6 +630,12 @@ void EvalState::addToSearchPath(const string & s, bool 
warn)
 
 Path EvalState::findFile(const string & path)
 {
+    return findFile(searchPath, path);
+}
+
+
+Path EvalState::findFile(SearchPath & searchPath, const string & path)
+{
     foreach (SearchPath::iterator, i, searchPath) {
         Path res;
         if (i->first.empty())
@@ -650,7 +649,7 @@ Path EvalState::findFile(const string & path)
         }
         if (pathExists(res)) return canonPath(res);
     }
-    return "";
+    throw ThrownError(format("file `%1%' was not found in the Nix search path 
(add it using $NIX_PATH or -I)") % path);
 }
 
 
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index e492ff6..3337489 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -654,6 +654,37 @@ static void prim_readFile(EvalState & state, const Pos & 
pos, Value * * args, Va
 }
 
 
+/* Find a file in the Nix search path. Used to implement <x> paths,
+   which are desugared to ‘findFile nixPath "x"’. */
+static void prim_findFile(EvalState & state, const Pos & pos, Value * * args, 
Value & v)
+{
+    state.forceList(*args[0], pos);
+
+    SearchPath searchPath;
+
+    for (unsigned int n = 0; n < args[0]->list.length; ++n) {
+        Value & v2(*args[0]->list.elems[n]);
+        state.forceAttrs(v2, pos);
+
+        string prefix;
+        Bindings::iterator i = v2.attrs->find(state.symbols.create("prefix"));
+        if (i != v2.attrs->end())
+            prefix = state.forceStringNoCtx(*i->value, pos);
+
+        i = v2.attrs->find(state.symbols.create("path"));
+        if (i == v2.attrs->end())
+            throw EvalError(format("attribute `path' missing, at %1%") % pos);
+        PathSet context;
+        string path = state.coerceToPath(pos, *i->value, context);
+
+        searchPath.push_back(std::pair<string, Path>(prefix, path));
+    }
+
+    string path = state.forceStringNoCtx(*args[1], pos);
+    mkPath(v, state.findFile(searchPath, path).c_str());
+}
+
+
 /*************************************************************
  * Creating files
  *************************************************************/
@@ -1293,6 +1324,7 @@ void EvalState::createBaseEnv()
     addPrimOp("baseNameOf", 1, prim_baseNameOf);
     addPrimOp("dirOf", 1, prim_dirOf);
     addPrimOp("__readFile", 1, prim_readFile);
+    addPrimOp("__findFile", 2, prim_findFile);
 
     // Creating files
     addPrimOp("__toXML", 1, prim_toXML);
diff --git a/tests/lang/eval-okay-search-path.exp 
b/tests/lang/eval-okay-search-path.exp
index d0bc8c5..ad05904 100644
--- a/tests/lang/eval-okay-search-path.exp
+++ b/tests/lang/eval-okay-search-path.exp
@@ -1 +1 @@
-"abcc"
+"abcca"
diff --git a/tests/lang/eval-okay-search-path.nix 
b/tests/lang/eval-okay-search-path.nix
index 501d5f3..e0f433f 100644
--- a/tests/lang/eval-okay-search-path.nix
+++ b/tests/lang/eval-okay-search-path.nix
@@ -8,3 +8,4 @@ assert length (filter (x: x.prefix == "nix") nixPath) == 1;
 assert length (filter (x: baseNameOf x.path == "dir4") nixPath) == 1;
 
 import <a.nix> + import <b.nix> + import <c.nix> + import <dir5/c.nix>
+  + (let nixPath = [ { path = ./dir1; } { path = ./dir2; } ]; in import 
<a.nix>)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]