Skip to content

Commit

Permalink
Add a :load-flake command to the nix repl
Browse files Browse the repository at this point in the history
`:lf ref` does essentially the same thing as
`:a (builtins.getFlake "ref")` (but cannonicalising `ref` if needs be,
so that `:lf .` works)

Fix #3803
  • Loading branch information
thufschmitt committed Jul 19, 2021
1 parent 58cb411 commit 765a3a2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/nix/repl.cc
Expand Up @@ -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);
Expand Down Expand Up @@ -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"
Expand All @@ -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();
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 765a3a2

Please sign in to comment.