From fb510a9e50b9be8ffffbde09263dd45e664006c4 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Tue, 13 May 2025 20:55:03 -0700 Subject: [PATCH] nix repl: remember :load-flake calls for :reload Fixes #8753 --- src/libcmd/repl.cc | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 8bbda1a4b..28d475415 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -61,7 +61,10 @@ struct NixRepl { size_t debugTraceIndex; + // Arguments passed to :load, saved so they can be reloaded with :reload Strings loadedFiles; + // Arguments passed to :load-flake, saved so they can be reloaded with :reload + Strings loadedFlakes; std::function getValues; const static int envSize = 32768; @@ -90,7 +93,8 @@ struct NixRepl void loadFile(const Path & path); void loadFlake(const std::string & flakeRef); void loadFiles(); - void reloadFiles(); + void loadFlakes(); + void reloadFilesAndFlakes(); void addAttrsToScope(Value & attrs); void addVarToScope(const Symbol name, Value & v); Expr * parseString(std::string s); @@ -466,7 +470,7 @@ ProcessLineResult NixRepl::processLine(std::string line) else if (command == ":r" || command == ":reload") { state->resetFileCache(); - reloadFiles(); + reloadFilesAndFlakes(); } else if (command == ":e" || command == ":edit") { @@ -501,7 +505,7 @@ ProcessLineResult NixRepl::processLine(std::string line) // Reload right after exiting the editor state->resetFileCache(); - reloadFiles(); + reloadFilesAndFlakes(); } else if (command == ":t") { @@ -716,6 +720,9 @@ void NixRepl::loadFlake(const std::string & flakeRefS) if (flakeRefS.empty()) throw Error("cannot use ':load-flake' without a path specified. (Use '.' for the current working directory.)"); + loadedFlakes.remove(flakeRefS); + loadedFlakes.push_back(flakeRefS); + std::filesystem::path cwd; try { cwd = std::filesystem::current_path(); @@ -754,11 +761,12 @@ void NixRepl::initEnv() } -void NixRepl::reloadFiles() +void NixRepl::reloadFilesAndFlakes() { initEnv(); loadFiles(); + loadFlakes(); } @@ -779,6 +787,18 @@ void NixRepl::loadFiles() } +void NixRepl::loadFlakes() +{ + Strings old = loadedFlakes; + loadedFlakes.clear(); + + for (auto & i : old) { + notice("Loading flake '%1%'...", i); + loadFlake(i); + } +} + + void NixRepl::addAttrsToScope(Value & attrs) { state->forceAttrs(attrs, [&]() { return attrs.determinePos(noPos); }, "while evaluating an attribute set to be merged in the global scope");