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 2021-12-22 15:36:08 -07:00 committed by Tom Bereknyei
parent 78dc64ec1e
commit 06d57ce759

View file

@ -646,11 +646,11 @@ void NixRepl::loadFlake(const std::string & flakeRefS)
if (flakeRefS.empty()) if (flakeRefS.empty())
throw Error("cannot use ':load-flake' without a path specified. (Use '.' for the current working directory.)"); 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()) if (evalSettings.pureEval && !flakeRef.input.isLocked())
throw Error("cannot use ':load-flake' on locked flake reference '%s' (use --impure to override)", flakeRefS); 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::callFlake(*state,
flake::lockFlake(*state, flakeRef, flake::lockFlake(*state, flakeRef,
@ -659,8 +659,17 @@ void NixRepl::loadFlake(const std::string & flakeRefS)
.useRegistries = !evalSettings.pureEval, .useRegistries = !evalSettings.pureEval,
.allowMutable = !evalSettings.pureEval, .allowMutable = !evalSettings.pureEval,
}), }),
v); *v);
addAttrsToScope(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(""); if (!first) notice("");
first = false; first = false;
notice("Loading '%1%'...", i); notice("Loading '%1%'...", i);
loadFile(i);
settings.isExperimentalFeatureEnabled(Xp::Flakes)
? loadFlake(i)
: loadFile(i);
} }
} }