about summary refs log tree commit diff
path: root/nix-repl.cc
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2013-09-02T16·18+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-09-02T16·48+0000
commit0f6279d87421f19cd2c1e286163d7567f13dc77f (patch)
tree2003fff5eb94ce42050d4382c52905cd2c9c39dc /nix-repl.cc
parent3202206d1d906ea6279dadfe608ea92ea0aaf927 (diff)
Add a command :l for loading a file into scope
Example:

nix-repl> :l <nixpkgs>

nix-repl> lib.range 0 10
[ 0 1 2 3 4 5 6 7 8 9 10 ]

nix-repl> :l <nixos>

nix-repl> config.boot.kernelModules
[ "vboxdrv" "vboxnetadp" ... ]
Diffstat (limited to 'nix-repl.cc')
-rw-r--r--nix-repl.cc43
1 files changed, 33 insertions, 10 deletions
diff --git a/nix-repl.cc b/nix-repl.cc
index 9858a034d728..0b84834491e7 100644
--- a/nix-repl.cc
+++ b/nix-repl.cc
@@ -8,6 +8,7 @@
 #include "eval.hh"
 #include "eval-inline.hh"
 #include "store-api.hh"
+#include "common-opts.hh"
 
 using namespace std;
 using namespace nix;
@@ -28,7 +29,8 @@ struct NixRepl
     NixRepl();
     void mainLoop();
     void processLine(string line);
-    void addVar(const Symbol & name, Value * v);
+    void addAttrsToScope(Value & attrs);
+    void addVarToScope(const Symbol & name, Value * v);
     Expr * parseString(string s);
     void evalString(string s, Value & v);
 };
@@ -51,6 +53,15 @@ bool getLine(string & line)
 }
 
 
+string removeWhitespace(string s)
+{
+    s = chomp(s);
+    size_t n = s.find_first_not_of(" \n\r\t");
+    if (n != string::npos) s = string(s, n);
+    return s;
+}
+
+
 NixRepl::NixRepl()
     : staticEnv(false, &state.staticBaseEnv)
 {
@@ -72,12 +83,8 @@ void NixRepl::mainLoop()
         string line;
         if (!getLine(line)) break;
 
-        /* Remove preceeding whitespace. */
-        size_t n = line.find_first_not_of(" \n\r\t");
-        if (n != string::npos) line = string(line, n);
-
         try {
-            processLine(line);
+            processLine(removeWhitespace(line));
         } catch (Error & e) {
             printMsg(lvlError, e.msg());
         }
@@ -94,9 +101,17 @@ void NixRepl::processLine(string line)
     if (string(line, 0, 2) == ":a") {
         Value v;
         evalString(string(line, 2), v);
-        state.forceAttrs(v);
-        foreach (Bindings::iterator, i, *v.attrs)
-            addVar(i->name, i->value);
+        addAttrsToScope(v);
+    }
+
+    else if (string(line, 0, 2) == ":l") {
+        state.resetFileCache();
+        Path path = lookupFileArg(state, removeWhitespace(string(line, 2)));
+        Value v, v2;
+        state.evalFile(path, v);
+        Bindings bindings;
+        state.autoCallFunction(bindings, v, v2);
+        addAttrsToScope(v2);
     }
 
     else if (string(line, 0, 2) == ":t") {
@@ -118,7 +133,15 @@ void NixRepl::processLine(string line)
 }
 
 
-void NixRepl::addVar(const Symbol & name, Value * v)
+void NixRepl::addAttrsToScope(Value & attrs)
+{
+    state.forceAttrs(attrs);
+    foreach (Bindings::iterator, i, *attrs.attrs)
+        addVarToScope(i->name, i->value);
+}
+
+
+void NixRepl::addVarToScope(const Symbol & name, Value * v)
 {
     staticEnv.vars[name] = displ;
     env->values[displ++] = v;