|
20 | 20 | #include "nix/fetchers/fetch-to-store.hh"
|
21 | 21 | #include "nix/store/local-fs-store.hh"
|
22 | 22 | #include "nix/expr/parallel-eval.hh"
|
| 23 | +#include "nix/util/thread-pool.hh" |
| 24 | +#include "nix/store/filetransfer.hh" |
23 | 25 |
|
24 | 26 | #include <filesystem>
|
25 | 27 | #include <nlohmann/json.hpp>
|
@@ -1148,6 +1150,59 @@ struct CmdFlakeArchive : FlakeCommand, MixJSON, MixDryRun
|
1148 | 1150 | }
|
1149 | 1151 | };
|
1150 | 1152 |
|
| 1153 | +struct CmdFlakePrefetchInputs : FlakeCommand |
| 1154 | +{ |
| 1155 | + std::string description() override |
| 1156 | + { |
| 1157 | + return "fetch the inputs of a flake"; |
| 1158 | + } |
| 1159 | + |
| 1160 | + std::string doc() override |
| 1161 | + { |
| 1162 | + return |
| 1163 | + #include "flake-prefetch-inputs.md" |
| 1164 | + ; |
| 1165 | + } |
| 1166 | + |
| 1167 | + void run(nix::ref<nix::Store> store) override |
| 1168 | + { |
| 1169 | + auto flake = lockFlake(); |
| 1170 | + |
| 1171 | + ThreadPool pool{fileTransferSettings.httpConnections}; |
| 1172 | + |
| 1173 | + struct State |
| 1174 | + { |
| 1175 | + std::set<const Node *> done; |
| 1176 | + }; |
| 1177 | + |
| 1178 | + Sync<State> state_; |
| 1179 | + |
| 1180 | + std::function<void(const Node & node)> visit; |
| 1181 | + visit = [&](const Node & node) |
| 1182 | + { |
| 1183 | + if (!state_.lock()->done.insert(&node).second) |
| 1184 | + return; |
| 1185 | + |
| 1186 | + if (auto lockedNode = dynamic_cast<const LockedNode *>(&node)) { |
| 1187 | + Activity act(*logger, lvlInfo, actUnknown, |
| 1188 | + fmt("fetching '%s'", lockedNode->lockedRef)); |
| 1189 | + auto accessor = lockedNode->lockedRef.input.getAccessor(store).first; |
| 1190 | + if (!evalSettings.lazyTrees) |
| 1191 | + fetchToStore(*store, accessor, FetchMode::Copy, lockedNode->lockedRef.input.getName()); |
| 1192 | + } |
| 1193 | + |
| 1194 | + for (auto & [inputName, input] : node.inputs) { |
| 1195 | + if (auto inputNode = std::get_if<0>(&input)) |
| 1196 | + pool.enqueue(std::bind(visit, **inputNode)); |
| 1197 | + } |
| 1198 | + }; |
| 1199 | + |
| 1200 | + pool.enqueue(std::bind(visit, *flake.lockFile.root)); |
| 1201 | + |
| 1202 | + pool.process(); |
| 1203 | + } |
| 1204 | +}; |
| 1205 | + |
1151 | 1206 | struct CmdFlakeShow : FlakeCommand, MixJSON
|
1152 | 1207 | {
|
1153 | 1208 | bool showLegacy = false;
|
@@ -1503,6 +1558,7 @@ struct CmdFlake : NixMultiCommand
|
1503 | 1558 | {"new", []() { return make_ref<CmdFlakeNew>(); }},
|
1504 | 1559 | {"clone", []() { return make_ref<CmdFlakeClone>(); }},
|
1505 | 1560 | {"archive", []() { return make_ref<CmdFlakeArchive>(); }},
|
| 1561 | + {"prefetch-inputs", []() { return make_ref<CmdFlakePrefetchInputs>(); }}, |
1506 | 1562 | {"show", []() { return make_ref<CmdFlakeShow>(); }},
|
1507 | 1563 | {"prefetch", []() { return make_ref<CmdFlakePrefetch>(); }},
|
1508 | 1564 | })
|
|
0 commit comments