Skip to content

Commit

Permalink
Merge pull request #7594 from nrdxp/paths-from-stdin
Browse files Browse the repository at this point in the history
feat: read installable paths from stdin
  • Loading branch information
thufschmitt authored Mar 2, 2023
2 parents 3a2b3af + 1f394d2 commit 8730d30
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions doc/manual/src/command-ref/nix-store.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ have an effect.
created by sequentially numbering symlinks beyond the first one
(e.g., `foo`, `foo-2`, `foo-3`, and so on).

- <span id="opt-stdin">[`--stdin`](#opt-stdin)</span>

Read *paths…* from the standard input.
Useful for chaining nix-store commands.

# Operation `--realise`

## Synopsis
Expand Down
6 changes: 5 additions & 1 deletion doc/manual/src/release-notes/rl-next.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release X.Y (202?-??-??)

* Commands which take installables on the command line can now read them from the standard input if
passed the `--stdin` flag. This is primarily useful when you have a large amount of paths which
exceed the OS arg limit.

* The special handling of an [installable](../command-ref/new-cli/nix.md#installables) with `.drv` suffix being interpreted as all of the given [store derivation](../glossary.md#gloss-store-derivation)'s output paths is removed, and instead taken as the literal store path that it represents.

The new `^` syntax for store paths introduced in Nix 2.13 allows explicitly referencing output paths of a derivation.
Expand All @@ -15,4 +19,4 @@
```shell-session
$ nix path-info /nix/store/gzaflydcr6sb3567hap9q6srzx8ggdgg-glibc-2.33-78.drv^*
```
provides information about each of its outputs.
provides information about each of its outputs.
2 changes: 2 additions & 0 deletions src/libcmd/command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ struct InstallablesCommand : virtual Args, SourceExprCommand

virtual bool useDefaultInstallables() { return true; }

bool readFromStdIn;

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

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

InstallablesCommand::InstallablesCommand()
{

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

expectArgs({
.label = "installables",
.handler = {&_installables},
Expand All @@ -710,10 +717,18 @@ void InstallablesCommand::prepare()

Installables InstallablesCommand::load()
{
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);
}

Expand Down
10 changes: 10 additions & 0 deletions src/nix-store/nix-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@ static int main_nix_store(int argc, char * * argv)
{
Strings opFlags, opArgs;
Operation op = 0;
bool readFromStdIn;

parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) {
Operation oldOp = op;
Expand Down Expand Up @@ -1078,6 +1079,8 @@ static int main_nix_store(int argc, char * * argv)
op = opGenerateBinaryCacheKey;
else if (*arg == "--add-root")
gcRoot = absPath(getArg(*arg, arg, end));
else if (*arg == "--stdin" && !isatty(STDIN_FILENO))
readFromStdIn = true;
else if (*arg == "--indirect")
;
else if (*arg == "--no-output")
Expand All @@ -1090,6 +1093,13 @@ static int main_nix_store(int argc, char * * argv)
else
opArgs.push_back(*arg);

if (readFromStdIn && op != opImport && op != opRestore && op != opServe) {
std::string word;
while (std::cin >> word) {
opArgs.emplace_back(std::move(word));
};
}

if (oldOp && oldOp != op)
throw UsageError("only one operation may be specified");

Expand Down

0 comments on commit 8730d30

Please sign in to comment.