Skip to content

Commit

Permalink
nix-store: implement --query --uncached
Browse files Browse the repository at this point in the history
Resolves NixOS#3946 by providing an efficient means of querying cache status
of the passed paths.

For every path passed, it will be returned on standard output only if it
is not available in any of the configured substituters.
  • Loading branch information
nrdxp committed Dec 29, 2022
1 parent 3dbf9b5 commit 5e3e6fe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
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 @@ -281,7 +281,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` | `--uncached` | `-U`}
[`--use-output`] [`-u`] [`--force-realise`] [`-f`]
*paths…*

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

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

## Examples

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

Show uncached 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
34 changes: 32 additions & 2 deletions src/nix-store/nix-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "graphml.hh"
#include "legacy.hh"
#include "path-with-outputs.hh"
#include "filetransfer.hh"
#include "thread-pool.hh"

#include <iostream>
#include <algorithm>
Expand All @@ -24,6 +26,8 @@
#include <sys/stat.h>
#include <fcntl.h>

#include <nlohmann/json.hpp>


namespace nix_store {

Expand Down Expand Up @@ -276,7 +280,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, qUncached };
QueryType query = qDefault;
bool useOutput = false;
bool includeOutputs = false;
Expand All @@ -300,6 +304,7 @@ static void opQuery(Strings opFlags, Strings opArgs)
}
else if (i == "--hash") query = qHash;
else if (i == "--size") query = qSize;
else if (i == "--uncached" || i == "-U") query = qUncached;
else if (i == "--tree") query = qTree;
else if (i == "--graph") query = qGraph;
else if (i == "--graphml") query = qGraphML;
Expand Down Expand Up @@ -364,6 +369,32 @@ static void opQuery(Strings opFlags, Strings opArgs)
}
break;

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

ThreadPool pool(fileTransferSettings.httpConnections);

std::function<void(StorePath)> queryPath;
queryPath = [&](const StorePath & req) {
SubstitutablePathInfos infos;
store->querySubstitutablePathInfos({{req, std::nullopt}}, infos);

if (infos.empty()) {
cout << store->printStorePath(req) << '\n';
}
};

for (auto & i : opArgs) {
auto path = store->followLinksToStorePath(i);

pool.enqueue(std::bind(queryPath, path));
}

pool.process();

break;
}

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


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

0 comments on commit 5e3e6fe

Please sign in to comment.