repl: use installables

This commit is contained in:
Tom Bereknyei 2022-03-11 13:26:08 -05:00
parent 81567a0962
commit 5640b52834
5 changed files with 48 additions and 20 deletions

View file

@ -114,6 +114,7 @@ struct InstallablesCommand : virtual Args, SourceExprCommand
InstallablesCommand();
void prepare() override;
Installables load();
virtual bool useDefaultInstallables() { return true; }
@ -132,7 +133,6 @@ struct InstallableCommand : virtual Args, SourceExprCommand
InstallableCommand(bool supportReadOnlyMode = false);
void prepare() override;
std::shared_ptr<Installable> load();
std::optional<FlakeRef> getFlakeRefForCompletion() override
{

View file

@ -1025,11 +1025,16 @@ InstallablesCommand::InstallablesCommand()
void InstallablesCommand::prepare()
{
installables = load();
}
Installables InstallablesCommand::load() {
Installables installables;
if (_installables.empty() && useDefaultInstallables())
// FIXME: commands like "nix profile install" should not have a
// default, probably.
_installables.push_back(".");
installables = parseInstallables(getStore(), _installables);
return parseInstallables(getStore(), _installables);
}
std::optional<FlakeRef> InstallablesCommand::getFlakeRefForCompletion()
@ -1054,13 +1059,10 @@ InstallableCommand::InstallableCommand(bool supportReadOnlyMode)
}}
});
}
std::shared_ptr<Installable> InstallableCommand::load() {
return parseInstallable(getStore(), _installable);
}
void InstallableCommand::prepare()
{
installable = load();
installable = parseInstallable(getStore(), _installable);
}
}

View file

@ -131,6 +131,7 @@ struct Installable
OperateOn operateOn,
const std::vector<std::shared_ptr<Installable>> & installables);
};
typedef std::vector<std::shared_ptr<Installable>> Installables;
struct InstallableValue : Installable
{

View file

@ -43,8 +43,6 @@ extern "C" {
namespace nix {
typedef std::vector<std::shared_ptr<Installable>> Installables;
struct NixRepl
#if HAVE_BOEHMGC
: gc
@ -899,17 +897,16 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m
return str;
}
struct CmdRepl : InstallableCommand
struct CmdRepl : InstallablesCommand
{
std::vector<std::string> files;
Strings getDefaultFlakeAttrPathPrefixes()
override {
return {};
}
Strings getDefaultFlakeAttrPaths()
override {
return {""};
}
virtual bool useDefaultInstallables() {
return file.has_value() or expr.has_value();
}
CmdRepl()
{
@ -934,10 +931,14 @@ struct CmdRepl : InstallableCommand
auto state = getEvalState();
auto repl = std::make_unique<NixRepl>(searchPath, openStore(),state
,[&]()->NixRepl::AnnotatedValues{
auto installable = load();
auto installables = load();
NixRepl::AnnotatedValues values;
for (auto & installable: installables){
auto [val, pos] = installable->toValue(*state);
auto what = installable->what();
return { {val,what} };
values.push_back( {val,what} );
}
return values;
}
);
repl->autoArgs = getAutoArgs(*repl->state);

View file

@ -24,10 +24,34 @@ R""(
* Interact with Nixpkgs in the REPL:
```console
# nix repl '<nixpkgs>'
# nix repl --file example.nix
Loading Installable ''...
Added 3 variables.
Loading '<nixpkgs>'...
Added 12428 variables.
# nix repl --expr '{a={b=3;c=4;};}'
Loading Installable ''...
Added 1 variables.
# nix repl --expr '{a={b=3;c=4;};}' a
Loading Installable ''...
Added 1 variables.
# nix repl nixpkgs
Loading Installable 'flake:nixpkgs#'...
Added 5 variables.
nix-repl> legacyPackages.x86_64-linux.emacs.name
"emacs-27.1"
nix-repl> legacyPackages.x86_64-linux.emacs.name
"emacs-27.1"
nix-repl> :q
# nix repl --expr 'import <nixpkgs>{}' --impure
Loading Installable ''...
Added 12439 variables.
nix-repl> emacs.name
"emacs-27.1"