mirror of
https://github.com/NixOS/nix.git
synced 2025-11-09 03:56:01 +01:00
Merge pull request #13565 from NixOS/prefetch-inputs
Add `nix flake prefetch-inputs` command
This commit is contained in:
commit
3826d51a65
3 changed files with 90 additions and 0 deletions
72
src/nix/flake-prefetch-inputs.cc
Normal file
72
src/nix/flake-prefetch-inputs.cc
Normal 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"});
|
||||||
17
src/nix/flake-prefetch-inputs.md
Normal file
17
src/nix/flake-prefetch-inputs.md
Normal 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.
|
||||||
|
|
||||||
|
)""
|
||||||
|
|
@ -78,6 +78,7 @@ nix_sources = [ config_priv_h ] + files(
|
||||||
'env.cc',
|
'env.cc',
|
||||||
'eval.cc',
|
'eval.cc',
|
||||||
'flake.cc',
|
'flake.cc',
|
||||||
|
'flake-prefetch-inputs.cc',
|
||||||
'formatter.cc',
|
'formatter.cc',
|
||||||
'hash.cc',
|
'hash.cc',
|
||||||
'log.cc',
|
'log.cc',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue