nix repl: load flakes from cli args

If experimental feature "flakes" is enabled, args passed to `nix repl`
will now be considered flake refs and imported using the existing
`:load-flake` machinery.

In addition, `:load-flake` now supports loading flake fragments.
This commit is contained in:
Timothy DeHerrera
2022-05-18 21:20:59 -04:00
committed by Tom Bereknyei
parent 78dc64ec1e
commit 06d57ce759
+17 -5
View File
@@ -646,11 +646,11 @@ 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.)");
auto flakeRef = parseFlakeRef(flakeRefS, absPath("."), true);
auto [flakeRef, fragment] = parseFlakeRefWithFragment(flakeRefS, absPath("."), true);
if (evalSettings.pureEval && !flakeRef.input.isLocked())
throw Error("cannot use ':load-flake' on locked flake reference '%s' (use --impure to override)", flakeRefS);
Value v;
auto v = state->allocValue();
flake::callFlake(*state,
flake::lockFlake(*state, flakeRef,
@@ -659,8 +659,17 @@ void NixRepl::loadFlake(const std::string & flakeRefS)
.useRegistries = !evalSettings.pureEval,
.allowMutable = !evalSettings.pureEval,
}),
v);
addAttrsToScope(v);
*v);
auto f = v->attrs->get(state->symbols.create(fragment));
if (f == 0) {
warn("no attribute %s, nothing loaded", fragment);
return;
};
fragment != "" ? addAttrsToScope(*f->value) : addAttrsToScope(*v);
}
@@ -689,7 +698,10 @@ void NixRepl::reloadFiles()
if (!first) notice("");
first = false;
notice("Loading '%1%'...", i);
loadFile(i);
settings.isExperimentalFeatureEnabled(Xp::Flakes)
? loadFlake(i)
: loadFile(i);
}
}