aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorregnat <rg@regnat.ovh>2021-07-19 17:47:03 +0200
committerregnat <rg@regnat.ovh>2021-07-19 17:52:41 +0200
commit765a3a20cb3be150498b73d1a2a5f7eb282ccc09 (patch)
treefe2524ba1f219f1d7fb85f16d3b7b90057b0333d /src
parent58cb411db628e6e8db9c789f431c8622615f3221 (diff)
Add a `:load-flake` command to the nix repl
`:lf ref` does essentially the same thing as `:a (builtins.getFlake "ref")` (but cannonicalising `ref` if needs be, so that `:lf .` works) Fix #3803
Diffstat (limited to 'src')
-rw-r--r--src/nix/repl.cc25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/nix/repl.cc b/src/nix/repl.cc
index bf3b2c401..3ebe102cf 100644
--- a/src/nix/repl.cc
+++ b/src/nix/repl.cc
@@ -68,6 +68,7 @@ struct NixRepl
StorePath getDerivationPath(Value & v);
bool processLine(string line);
void loadFile(const Path & path);
+ void loadFlake(const std::string & flakeRef);
void initEnv();
void reloadFiles();
void addAttrsToScope(Value & attrs);
@@ -415,6 +416,7 @@ bool NixRepl::processLine(string line)
<< " :e <expr> Open the derivation in $EDITOR\n"
<< " :i <expr> Build derivation, then install result into current profile\n"
<< " :l <path> Load Nix expression and add it to scope\n"
+ << " :lf <ref> Load Nix flake and add it to scope\n"
<< " :p <expr> Evaluate and print expression recursively\n"
<< " :q Exit nix-repl\n"
<< " :r Reload all files\n"
@@ -435,6 +437,10 @@ bool NixRepl::processLine(string line)
loadFile(arg);
}
+ else if (command == ":lf" || command == ":load-flake") {
+ loadFlake(arg);
+ }
+
else if (command == ":r" || command == ":reload") {
state->resetFileCache();
reloadFiles();
@@ -576,6 +582,25 @@ void NixRepl::loadFile(const Path & path)
addAttrsToScope(v2);
}
+void NixRepl::loadFlake(const std::string & flakeRefS)
+{
+ auto flakeRef = parseFlakeRef(flakeRefS, absPath("."), true);
+ if (evalSettings.pureEval && !flakeRef.input.isImmutable())
+ throw Error("cannot use ':load-flake' on mutable flake reference '%s' (use --impure to override)", flakeRefS);
+
+ Value v;
+
+ flake::callFlake(*state,
+ flake::lockFlake(*state, flakeRef,
+ flake::LockFlags {
+ .updateLockFile = false,
+ .useRegistries = !evalSettings.pureEval,
+ .allowMutable = !evalSettings.pureEval,
+ }),
+ v);
+ addAttrsToScope(v);
+}
+
void NixRepl::initEnv()
{