1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-20 11:11:03 -04:00

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

View file

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

View file

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

View file

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

View file

@ -24,10 +24,34 @@ R""(
* Interact with Nixpkgs in the REPL: * Interact with Nixpkgs in the REPL:
```console ```console
# nix repl '<nixpkgs>' # nix repl --file example.nix
Loading Installable ''...
Added 3 variables.
Loading '<nixpkgs>'... # nix repl --expr '{a={b=3;c=4;};}'
Added 12428 variables. 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 nix-repl> emacs.name
"emacs-27.1" "emacs-27.1"