1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-08 19:46:02 +01:00

Merge pull request #13565 from NixOS/prefetch-inputs

Add `nix flake prefetch-inputs` command
This commit is contained in:
Eelco Dolstra 2025-08-21 12:30:17 +02:00 committed by GitHub
commit 3826d51a65
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1,72 @@
#include "flake-command.hh"
#include "nix/fetchers/fetch-to-store.hh"
#include "nix/util/thread-pool.hh"
#include "nix/store/filetransfer.hh"
#include "nix/util/exit.hh"
#include <nlohmann/json.hpp>
using namespace nix;
using namespace nix::flake;
struct CmdFlakePrefetchInputs : FlakeCommand
{
std::string description() override
{
return "fetch the inputs of a flake";
}
std::string doc() override
{
return
#include "flake-prefetch-inputs.md"
;
}
void run(nix::ref<nix::Store> store) override
{
auto flake = lockFlake();
ThreadPool pool{fileTransferSettings.httpConnections};
struct State
{
std::set<const Node *> done;
};
Sync<State> state_;
std::atomic<size_t> nrFailed{0};
std::function<void(const Node & node)> visit;
visit = [&](const Node & node) {
if (!state_.lock()->done.insert(&node).second)
return;
if (auto lockedNode = dynamic_cast<const LockedNode *>(&node)) {
try {
Activity act(*logger, lvlInfo, actUnknown, fmt("fetching '%s'", lockedNode->lockedRef));
auto accessor = lockedNode->lockedRef.input.getAccessor(store).first;
fetchToStore(
fetchSettings, *store, accessor, FetchMode::Copy, lockedNode->lockedRef.input.getName());
} catch (Error & e) {
printError("%s", e.what());
nrFailed++;
}
}
for (auto & [inputName, input] : node.inputs) {
if (auto inputNode = std::get_if<0>(&input))
pool.enqueue(std::bind(visit, **inputNode));
}
};
pool.enqueue(std::bind(visit, *flake.lockFile.root));
pool.process();
throw Exit(nrFailed ? 1 : 0);
}
};
static auto rCmdFlakePrefetchInputs = registerCommand2<CmdFlakePrefetchInputs>({"flake", "prefetch-inputs"});

View file

@ -0,0 +1,17 @@
R""(
# Examples
* Fetch the inputs of the `hydra` flake:
```console
# nix flake prefetch-inputs github:NixOS/hydra
```
# Description
Fetch the inputs of a flake. This ensures that they are already available for any subsequent evaluation of the flake.
This operation is recursive: it will fetch not just the direct inputs of the top-level flake, but also transitive inputs.
)""

View file

@ -78,6 +78,7 @@ nix_sources = [ config_priv_h ] + files(
'env.cc',
'eval.cc',
'flake.cc',
'flake-prefetch-inputs.cc',
'formatter.cc',
'hash.cc',
'log.cc',