Skip to content

Commit 1626e65

Browse files
committed
Move FlakeCommand into a header, allow separate registration of subcommands
This allows us to start splitting up src/nix/flake.cc.
1 parent 9a52b2f commit 1626e65

File tree

5 files changed

+140
-109
lines changed

5 files changed

+140
-109
lines changed

src/libcmd/include/nix/cmd/common-eval-args.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "nix/util/canon-path.hh"
66
#include "nix/main/common-args.hh"
77
#include "nix/expr/search-path.hh"
8+
#include "nix/expr/eval-settings.hh"
89

910
#include <filesystem>
1011

@@ -15,10 +16,8 @@ class Store;
1516
namespace fetchers { struct Settings; }
1617

1718
class EvalState;
18-
struct EvalSettings;
1919
struct CompatibilitySettings;
2020
class Bindings;
21-
struct SourcePath;
2221

2322
namespace flake { struct Settings; }
2423

src/nix/flake-command.hh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "nix/cmd/command.hh"
4+
#include "nix/cmd/installable-flake.hh"
5+
#include "nix/flake/flake.hh"
6+
7+
namespace nix {
8+
9+
using namespace nix::flake;
10+
11+
class FlakeCommand : virtual Args, public MixFlakeOptions
12+
{
13+
protected:
14+
std::string flakeUrl = ".";
15+
16+
public:
17+
18+
FlakeCommand();
19+
20+
FlakeRef getFlakeRef();
21+
22+
LockedFlake lockFlake();
23+
24+
std::vector<FlakeRef> getFlakeRefsForCompletion() override;
25+
};
26+
27+
}

src/nix/flake-prefetch-inputs.cc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "flake-command.hh"
2+
#include "nix/fetchers/fetch-to-store.hh"
3+
#include "nix/util/thread-pool.hh"
4+
#include "nix/store/filetransfer.hh"
5+
#include "nix/util/exit.hh"
6+
7+
#include <nlohmann/json.hpp>
8+
9+
using namespace nix;
10+
using namespace nix::flake;
11+
12+
struct CmdFlakePrefetchInputs : FlakeCommand
13+
{
14+
std::string description() override
15+
{
16+
return "fetch the inputs of a flake";
17+
}
18+
19+
std::string doc() override
20+
{
21+
return
22+
#include "flake-prefetch-inputs.md"
23+
;
24+
}
25+
26+
void run(nix::ref<nix::Store> store) override
27+
{
28+
auto flake = lockFlake();
29+
30+
ThreadPool pool{fileTransferSettings.httpConnections};
31+
32+
struct State
33+
{
34+
std::set<const Node *> done;
35+
};
36+
37+
Sync<State> state_;
38+
39+
std::atomic<size_t> nrFailed{0};
40+
41+
std::function<void(const Node & node)> visit;
42+
visit = [&](const Node & node) {
43+
if (!state_.lock()->done.insert(&node).second)
44+
return;
45+
46+
if (auto lockedNode = dynamic_cast<const LockedNode *>(&node)) {
47+
try {
48+
Activity act(*logger, lvlInfo, actUnknown, fmt("fetching '%s'", lockedNode->lockedRef));
49+
auto accessor = lockedNode->lockedRef.input.getAccessor(store).first;
50+
if (!evalSettings.lazyTrees)
51+
fetchToStore(*store, accessor, FetchMode::Copy, lockedNode->lockedRef.input.getName());
52+
} catch (Error & e) {
53+
printError("%s", e.what());
54+
nrFailed++;
55+
}
56+
}
57+
58+
for (auto & [inputName, input] : node.inputs) {
59+
if (auto inputNode = std::get_if<0>(&input))
60+
pool.enqueue(std::bind(visit, **inputNode));
61+
}
62+
};
63+
64+
pool.enqueue(std::bind(visit, *flake.lockFile.root));
65+
66+
pool.process();
67+
68+
throw Exit(nrFailed ? 1 : 0);
69+
}
70+
};
71+
72+
static auto rCmdFlakePrefetchInputs = registerCommand2<CmdFlakePrefetchInputs>({"flake", "prefetch-inputs"});

src/nix/flake.cc

Lines changed: 39 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#include "nix/cmd/command.hh"
2-
#include "nix/cmd/installable-flake.hh"
1+
#include "flake-command.hh"
32
#include "nix/main/common-args.hh"
43
#include "nix/main/shared.hh"
54
#include "nix/expr/eval.hh"
65
#include "nix/expr/eval-inline.hh"
76
#include "nix/expr/eval-settings.hh"
8-
#include "nix/flake/flake.hh"
97
#include "nix/expr/get-drvs.hh"
108
#include "nix/util/signals.hh"
119
#include "nix/store/store-open.hh"
@@ -20,8 +18,6 @@
2018
#include "nix/fetchers/fetch-to-store.hh"
2119
#include "nix/store/local-fs-store.hh"
2220
#include "nix/expr/parallel-eval.hh"
23-
#include "nix/util/thread-pool.hh"
24-
#include "nix/store/filetransfer.hh"
2521

2622
#include <filesystem>
2723
#include <nlohmann/json.hpp>
@@ -36,43 +32,36 @@ using namespace nix::flake;
3632
using json = nlohmann::json;
3733

3834
struct CmdFlakeUpdate;
39-
class FlakeCommand : virtual Args, public MixFlakeOptions
40-
{
41-
protected:
42-
std::string flakeUrl = ".";
43-
44-
public:
4535

46-
FlakeCommand()
47-
{
48-
expectArgs({
49-
.label = "flake-url",
50-
.optional = true,
51-
.handler = {&flakeUrl},
52-
.completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) {
53-
completeFlakeRef(completions, getStore(), prefix);
54-
}}
55-
});
56-
}
36+
FlakeCommand::FlakeCommand()
37+
{
38+
expectArgs({
39+
.label = "flake-url",
40+
.optional = true,
41+
.handler = {&flakeUrl},
42+
.completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) {
43+
completeFlakeRef(completions, getStore(), prefix);
44+
}}
45+
});
46+
}
5747

58-
FlakeRef getFlakeRef()
59-
{
60-
return parseFlakeRef(fetchSettings, flakeUrl, std::filesystem::current_path().string()); //FIXME
61-
}
48+
FlakeRef FlakeCommand::getFlakeRef()
49+
{
50+
return parseFlakeRef(fetchSettings, flakeUrl, std::filesystem::current_path().string()); //FIXME
51+
}
6252

63-
LockedFlake lockFlake()
64-
{
65-
return flake::lockFlake(flakeSettings, *getEvalState(), getFlakeRef(), lockFlags);
66-
}
53+
LockedFlake FlakeCommand::lockFlake()
54+
{
55+
return flake::lockFlake(flakeSettings, *getEvalState(), getFlakeRef(), lockFlags);
56+
}
6757

68-
std::vector<FlakeRef> getFlakeRefsForCompletion() override
69-
{
70-
return {
71-
// Like getFlakeRef but with expandTilde calld first
72-
parseFlakeRef(fetchSettings, expandTilde(flakeUrl), std::filesystem::current_path().string())
73-
};
74-
}
75-
};
58+
std::vector<FlakeRef> FlakeCommand::getFlakeRefsForCompletion()
59+
{
60+
return {
61+
// Like getFlakeRef but with expandTilde calld first
62+
parseFlakeRef(fetchSettings, expandTilde(flakeUrl), std::filesystem::current_path().string())
63+
};
64+
}
7665

7766
struct CmdFlakeUpdate : FlakeCommand
7867
{
@@ -1150,59 +1139,6 @@ struct CmdFlakeArchive : FlakeCommand, MixJSON, MixDryRun
11501139
}
11511140
};
11521141

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-
12061142
struct CmdFlakeShow : FlakeCommand, MixJSON
12071143
{
12081144
bool showLegacy = false;
@@ -1546,22 +1482,7 @@ struct CmdFlakePrefetch : FlakeCommand, MixJSON
15461482
struct CmdFlake : NixMultiCommand
15471483
{
15481484
CmdFlake()
1549-
: NixMultiCommand(
1550-
"flake",
1551-
{
1552-
{"update", []() { return make_ref<CmdFlakeUpdate>(); }},
1553-
{"lock", []() { return make_ref<CmdFlakeLock>(); }},
1554-
{"metadata", []() { return make_ref<CmdFlakeMetadata>(); }},
1555-
{"info", []() { return make_ref<CmdFlakeInfo>(); }},
1556-
{"check", []() { return make_ref<CmdFlakeCheck>(); }},
1557-
{"init", []() { return make_ref<CmdFlakeInit>(); }},
1558-
{"new", []() { return make_ref<CmdFlakeNew>(); }},
1559-
{"clone", []() { return make_ref<CmdFlakeClone>(); }},
1560-
{"archive", []() { return make_ref<CmdFlakeArchive>(); }},
1561-
{"prefetch-inputs", []() { return make_ref<CmdFlakePrefetchInputs>(); }},
1562-
{"show", []() { return make_ref<CmdFlakeShow>(); }},
1563-
{"prefetch", []() { return make_ref<CmdFlakePrefetch>(); }},
1564-
})
1485+
: NixMultiCommand("flake", RegisterCommand::getCommandsFor({"flake"}))
15651486
{
15661487
}
15671488

@@ -1579,3 +1500,14 @@ struct CmdFlake : NixMultiCommand
15791500
};
15801501

15811502
static auto rCmdFlake = registerCommand<CmdFlake>("flake");
1503+
static auto rCmdFlakeArchive = registerCommand2<CmdFlakeArchive>({"flake", "archive"});
1504+
static auto rCmdFlakeCheck = registerCommand2<CmdFlakeCheck>({"flake", "check"});
1505+
static auto rCmdFlakeClone = registerCommand2<CmdFlakeClone>({"flake", "clone"});
1506+
static auto rCmdFlakeInfo = registerCommand2<CmdFlakeInfo>({"flake", "info"});
1507+
static auto rCmdFlakeInit = registerCommand2<CmdFlakeInit>({"flake", "init"});
1508+
static auto rCmdFlakeLock = registerCommand2<CmdFlakeLock>({"flake", "lock"});
1509+
static auto rCmdFlakeMetadata = registerCommand2<CmdFlakeMetadata>({"flake", "metadata"});
1510+
static auto rCmdFlakeNew = registerCommand2<CmdFlakeNew>({"flake", "new"});
1511+
static auto rCmdFlakePrefetch = registerCommand2<CmdFlakePrefetch>({"flake", "prefetch"});
1512+
static auto rCmdFlakeShow = registerCommand2<CmdFlakeShow>({"flake", "show"});
1513+
static auto rCmdFlakeUpdate = registerCommand2<CmdFlakeUpdate>({"flake", "update"});

src/nix/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ nix_sources = [config_priv_h] + files(
7878
'env.cc',
7979
'eval.cc',
8080
'flake.cc',
81+
'flake-prefetch-inputs.cc',
8182
'formatter.cc',
8283
'hash.cc',
8384
'log.cc',

0 commit comments

Comments
 (0)