Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix-store: implement --query --unsubstitutable #7526

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion doc/manual/src/command-ref/nix-store.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ error: cannot delete path `/nix/store/zq0h41l75vlb4z45kzgjjmsjxvcv1qk7-mesa-6.4'
{`--outputs` | `--requisites` | `-R` | `--references` |
`--referrers` | `--referrers-closure` | `--deriver` | `-d` |
`--graph` | `--tree` | `--binding` *name* | `-b` *name* | `--hash` |
`--size` | `--roots`}
`--size` | `--roots` | `--unsubstitutable` | `-U`}
[`--use-output`] [`-u`] [`--force-realise`] [`-f`]
*paths…*

Expand Down Expand Up @@ -405,6 +405,10 @@ symlink.
Prints the garbage collector roots that point, directly or
indirectly, at the store paths *paths*.

- `--unsubstitutable`; `-U`\
Outputs a list of the store paths *paths* which are not available in
any configured substituters.

## Examples

Print the closure (runtime dependencies) of the `svn` program in the
Expand Down Expand Up @@ -443,6 +447,16 @@ $ nix-store -q --tree $(nix-store -qd $(which svn))
...
```

Show unsubstitutable build & runtime dependencies:

```console
$ drv=$(nix-store -qd $(which svn))
$ nix-store -qU $(nix-store -qR --include-outputs $drv)
/nix/store/5mbglq5ldqld8sj57273aljwkfvj22mc-subversion-1.1.4
/nix/store/9lz9yc6zgmc0vlqmn2ipcpkjlmbi51vv-glibc-2.3.4
...
```

Show all paths that depend on the same OpenSSL library as `svn`:

```console
Expand Down
37 changes: 35 additions & 2 deletions src/nix-store/nix-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ static void opQuery(Strings opFlags, Strings opArgs)
enum QueryType
{ qDefault, qOutputs, qRequisites, qReferences, qReferrers
, qReferrersClosure, qDeriver, qBinding, qHash, qSize
, qTree, qGraph, qGraphML, qResolve, qRoots };
, qTree, qGraph, qGraphML, qResolve, qRoots, qUnsubstitutable };
QueryType query = qDefault;
bool useOutput = false;
bool includeOutputs = false;
Expand All @@ -300,6 +300,7 @@ static void opQuery(Strings opFlags, Strings opArgs)
}
else if (i == "--hash") query = qHash;
else if (i == "--size") query = qSize;
else if (i == "--unsubstitutable" || i == "-U") query = qUnsubstitutable;
else if (i == "--tree") query = qTree;
else if (i == "--graph") query = qGraph;
else if (i == "--graphml") query = qGraphML;
Expand Down Expand Up @@ -364,6 +365,39 @@ static void opQuery(Strings opFlags, Strings opArgs)
}
break;

case qUnsubstitutable: {
if (opArgs.empty()) throw UsageError("no paths passed");

StorePathSet storePaths;
for (auto const & str : opArgs) {
storePaths.emplace(store->followLinksToStorePath(str));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation here and below is wrong (should be 4 characters per level).

}
nrdxp marked this conversation as resolved.
Show resolved Hide resolved

StringSet pathsS;
// query each configured substituter
for (auto & sub : getDefaultSubstituters()) {
if (sub->storeDir != store->storeDir) continue;
if (!sub->wantMassQuery) continue;

auto paths = sub->queryValidPaths(StorePathSet(storePaths.begin(), storePaths.end()));
for (auto & path : paths) {
pathsS.emplace(store->printStorePath(path));
}
}

StorePathSet substitutablePaths;
for (auto & str : pathsS) {
substitutablePaths.emplace(store->followLinksToStorePath(str));
}

for ( auto & path : storePaths) {
if (substitutablePaths.find(path) == substitutablePaths.end())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (substitutablePaths.find(path) == substitutablePaths.end())
if (!substitutablePaths.count(path))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not short-circuit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what way? On an std::map it should be equally efficient.

But since we're fine with C++20 now, you can also use substitutablePaths.contains(path).

std::cout << '\n' << store->printStorePath(path);
}
std::cout << std::endl;
break;
}

case qBinding:
for (auto & i : opArgs) {
auto path = useDeriver(store->followLinksToStorePath(i));
Expand Down Expand Up @@ -680,7 +714,6 @@ static void opExport(Strings opFlags, Strings opArgs)
sink.flush();
}


static void opImport(Strings opFlags, Strings opArgs)
{
for (auto & i : opFlags)
Expand Down