From 765a3a20cb3be150498b73d1a2a5f7eb282ccc09 Mon Sep 17 00:00:00 2001 From: regnat Date: Mon, 19 Jul 2021 17:47:03 +0200 Subject: [PATCH] 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 --- src/nix/repl.cc | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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 Open the derivation in $EDITOR\n" << " :i Build derivation, then install result into current profile\n" << " :l Load Nix expression and add it to scope\n" + << " :lf Load Nix flake and add it to scope\n" << " :p 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() {