Resstore 'nix flake archive'

This commit is contained in:
Eelco Dolstra 2022-08-04 14:47:41 +02:00
parent 5ef9008bf3
commit b449825e91
2 changed files with 23 additions and 26 deletions

View file

@ -15,11 +15,10 @@ R""(
# nix flake archive dwarffs # nix flake archive dwarffs
``` ```
* Print the store paths of the flake sources of NixOps without * Copy and print the store paths of the flake sources of NixOps:
fetching them:
```console ```console
# nix flake archive --json --dry-run nixops # nix flake archive --json nixops
``` ```
# Description # Description

View file

@ -877,8 +877,7 @@ struct CmdFlakeClone : FlakeCommand
} }
}; };
#if 0 struct CmdFlakeArchive : FlakeCommand, MixJSON
struct CmdFlakeArchive : FlakeCommand, MixJSON, MixDryRun
{ {
std::string dstUri; std::string dstUri;
@ -906,45 +905,44 @@ struct CmdFlakeArchive : FlakeCommand, MixJSON, MixDryRun
void run(nix::ref<nix::Store> store) override void run(nix::ref<nix::Store> store) override
{ {
auto dstStore = store;
if (!dstUri.empty())
dstStore = openStore(dstUri);
auto flake = lockFlake(); auto flake = lockFlake();
auto jsonRoot = json ? std::optional<JSONObject>(std::cout) : std::nullopt; auto jsonRoot = json ? std::optional<JSONObject>(std::cout) : std::nullopt;
StorePathSet sources; {
Activity act(*logger, lvlChatty, actUnknown, fmt("archiving root"));
sources.insert(flake.flake.sourceInfo->storePath); auto storePath = flake.flake.lockedRef.input.fetchToStore(dstStore).first;
if (jsonRoot) if (jsonRoot)
jsonRoot->attr("path", store->printStorePath(flake.flake.sourceInfo->storePath)); jsonRoot->attr("path", store->printStorePath(storePath));
}
// FIXME: use graph output, handle cycles. // FIXME: use graph output, handle cycles.
std::function<void(const Node & node, std::optional<JSONObject> & jsonObj)> traverse; std::function<void(const Node & node, const InputPath & parentPath, std::optional<JSONObject> & jsonObj)> traverse;
traverse = [&](const Node & node, std::optional<JSONObject> & jsonObj) traverse = [&](const Node & node, const InputPath & parentPath, std::optional<JSONObject> & jsonObj)
{ {
auto jsonObj2 = jsonObj ? jsonObj->object("inputs") : std::optional<JSONObject>(); auto jsonObj2 = jsonObj ? jsonObj->object("inputs") : std::optional<JSONObject>();
for (auto & [inputName, input] : node.inputs) { for (auto & [inputName, input] : node.inputs) {
if (auto inputNode = std::get_if<0>(&input)) { if (auto inputNode = std::get_if<0>(&input)) {
auto inputPath = parentPath;
inputPath.push_back(inputName);
Activity act(*logger, lvlChatty, actUnknown,
fmt("archiving input '%s'", printInputPath(inputPath)));
auto jsonObj3 = jsonObj2 ? jsonObj2->object(inputName) : std::optional<JSONObject>(); auto jsonObj3 = jsonObj2 ? jsonObj2->object(inputName) : std::optional<JSONObject>();
auto storePath = auto storePath = (*inputNode)->lockedRef.input.fetchToStore(dstStore).first;
dryRun
? (*inputNode)->lockedRef.input.computeStorePath(*store)
: (*inputNode)->lockedRef.input.fetch(store).first.storePath;
if (jsonObj3) if (jsonObj3)
jsonObj3->attr("path", store->printStorePath(storePath)); jsonObj3->attr("path", store->printStorePath(storePath));
sources.insert(std::move(storePath)); traverse(**inputNode, inputPath, jsonObj3);
traverse(**inputNode, jsonObj3);
} }
} }
}; };
traverse(*flake.lockFile.root, jsonRoot); traverse(*flake.lockFile.root, {}, jsonRoot);
if (!dryRun && !dstUri.empty()) {
ref<Store> dstStore = dstUri.empty() ? openStore() : openStore(dstUri);
copyPaths(*store, *dstStore, sources);
}
} }
}; };
#endif
struct CmdFlakeShow : FlakeCommand, MixJSON struct CmdFlakeShow : FlakeCommand, MixJSON
{ {
@ -1200,7 +1198,7 @@ struct CmdFlake : NixMultiCommand
{"init", []() { return make_ref<CmdFlakeInit>(); }}, {"init", []() { return make_ref<CmdFlakeInit>(); }},
{"new", []() { return make_ref<CmdFlakeNew>(); }}, {"new", []() { return make_ref<CmdFlakeNew>(); }},
{"clone", []() { return make_ref<CmdFlakeClone>(); }}, {"clone", []() { return make_ref<CmdFlakeClone>(); }},
//{"archive", []() { return make_ref<CmdFlakeArchive>(); }}, {"archive", []() { return make_ref<CmdFlakeArchive>(); }},
{"show", []() { return make_ref<CmdFlakeShow>(); }}, {"show", []() { return make_ref<CmdFlakeShow>(); }},
{"prefetch", []() { return make_ref<CmdFlakePrefetch>(); }}, {"prefetch", []() { return make_ref<CmdFlakePrefetch>(); }},
}) })