Skip to content

Commit

Permalink
feat: read installable paths from stdin
Browse files Browse the repository at this point in the history
Resolves NixOS#7437 for new `nix` commands only by adding a `--stdin` flag.

If paths are also passed on the cli they will be combined with the ones
from standard input.
  • Loading branch information
nrdxp committed Jan 12, 2023
1 parent eaa20f2 commit a1ede9c
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/libcmd/command.hh
Original file line number Diff line number Diff line change
@@ -127,6 +127,8 @@ struct InstallablesCommand : virtual Args, SourceExprCommand

virtual bool useDefaultInstallables() { return true; }

bool readFromStdIn;

std::vector<std::string> getFlakesForCompletion() override;

protected:
17 changes: 16 additions & 1 deletion src/libcmd/installables.cc
Original file line number Diff line number Diff line change
@@ -1073,6 +1073,13 @@ StorePathSet Installable::toDerivations(

InstallablesCommand::InstallablesCommand()
{

addFlag({
.longName = "stdin",
.description = "Read installables from the standard input.",
.handler = {&readFromStdIn, true}
});

expectArgs({
.label = "installables",
.handler = {&_installables},
@@ -1089,10 +1096,18 @@ void InstallablesCommand::prepare()

Installables InstallablesCommand::load() {
Installables installables;
if (_installables.empty() && useDefaultInstallables())
if (_installables.empty() && useDefaultInstallables() && !readFromStdIn)
// FIXME: commands like "nix profile install" should not have a
// default, probably.
_installables.push_back(".");

if (readFromStdIn && !isatty(STDIN_FILENO)) {
std::string word;
while (std::cin >> word) {
_installables.emplace_back(std::move(word));
}
}

return parseInstallables(getStore(), _installables);
}

0 comments on commit a1ede9c

Please sign in to comment.