mirror of
https://github.com/NixOS/nix.git
synced 2025-11-12 21:46:01 +01:00
Move FlakeCommand into a header, allow separate registration of subcommands
This allows us to start splitting up src/nix/flake.cc.
This commit is contained in:
parent
1ef1675300
commit
e04aa2b13b
5 changed files with 130 additions and 109 deletions
|
|
@ -5,6 +5,7 @@
|
||||||
#include "nix/util/canon-path.hh"
|
#include "nix/util/canon-path.hh"
|
||||||
#include "nix/main/common-args.hh"
|
#include "nix/main/common-args.hh"
|
||||||
#include "nix/expr/search-path.hh"
|
#include "nix/expr/search-path.hh"
|
||||||
|
#include "nix/expr/eval-settings.hh"
|
||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
|
|
@ -15,10 +16,8 @@ class Store;
|
||||||
namespace fetchers { struct Settings; }
|
namespace fetchers { struct Settings; }
|
||||||
|
|
||||||
class EvalState;
|
class EvalState;
|
||||||
struct EvalSettings;
|
|
||||||
struct CompatibilitySettings;
|
struct CompatibilitySettings;
|
||||||
class Bindings;
|
class Bindings;
|
||||||
struct SourcePath;
|
|
||||||
|
|
||||||
namespace flake { struct Settings; }
|
namespace flake { struct Settings; }
|
||||||
|
|
||||||
|
|
|
||||||
27
src/nix/flake-command.hh
Normal file
27
src/nix/flake-command.hh
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "nix/cmd/command.hh"
|
||||||
|
#include "nix/cmd/installable-flake.hh"
|
||||||
|
#include "nix/flake/flake.hh"
|
||||||
|
|
||||||
|
namespace nix {
|
||||||
|
|
||||||
|
using namespace nix::flake;
|
||||||
|
|
||||||
|
class FlakeCommand : virtual Args, public MixFlakeOptions
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
std::string flakeUrl = ".";
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
FlakeCommand();
|
||||||
|
|
||||||
|
FlakeRef getFlakeRef();
|
||||||
|
|
||||||
|
LockedFlake lockFlake();
|
||||||
|
|
||||||
|
std::vector<FlakeRef> getFlakeRefsForCompletion() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
62
src/nix/flake-prefetch-inputs.cc
Normal file
62
src/nix/flake-prefetch-inputs.cc
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
#include "flake-command.hh"
|
||||||
|
#include "nix/fetchers/fetch-to-store.hh"
|
||||||
|
#include "nix/util/thread-pool.hh"
|
||||||
|
#include "nix/store/filetransfer.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::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)) {
|
||||||
|
Activity act(*logger, lvlInfo, actUnknown, fmt("fetching '%s'", lockedNode->lockedRef));
|
||||||
|
auto accessor = lockedNode->lockedRef.input.getAccessor(store).first;
|
||||||
|
if (!evalSettings.lazyTrees)
|
||||||
|
fetchToStore(*store, accessor, FetchMode::Copy, lockedNode->lockedRef.input.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static auto rCmdFlakePrefetchInputs = registerCommand2<CmdFlakePrefetchInputs>({"flake", "prefetch-inputs"});
|
||||||
146
src/nix/flake.cc
146
src/nix/flake.cc
|
|
@ -1,11 +1,9 @@
|
||||||
#include "nix/cmd/command.hh"
|
#include "flake-command.hh"
|
||||||
#include "nix/cmd/installable-flake.hh"
|
|
||||||
#include "nix/main/common-args.hh"
|
#include "nix/main/common-args.hh"
|
||||||
#include "nix/main/shared.hh"
|
#include "nix/main/shared.hh"
|
||||||
#include "nix/expr/eval.hh"
|
#include "nix/expr/eval.hh"
|
||||||
#include "nix/expr/eval-inline.hh"
|
#include "nix/expr/eval-inline.hh"
|
||||||
#include "nix/expr/eval-settings.hh"
|
#include "nix/expr/eval-settings.hh"
|
||||||
#include "nix/flake/flake.hh"
|
|
||||||
#include "nix/expr/get-drvs.hh"
|
#include "nix/expr/get-drvs.hh"
|
||||||
#include "nix/util/signals.hh"
|
#include "nix/util/signals.hh"
|
||||||
#include "nix/store/store-open.hh"
|
#include "nix/store/store-open.hh"
|
||||||
|
|
@ -19,8 +17,6 @@
|
||||||
#include "nix/util/users.hh"
|
#include "nix/util/users.hh"
|
||||||
#include "nix/fetchers/fetch-to-store.hh"
|
#include "nix/fetchers/fetch-to-store.hh"
|
||||||
#include "nix/store/local-fs-store.hh"
|
#include "nix/store/local-fs-store.hh"
|
||||||
#include "nix/util/thread-pool.hh"
|
|
||||||
#include "nix/store/filetransfer.hh"
|
|
||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
@ -35,43 +31,36 @@ using namespace nix::flake;
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
struct CmdFlakeUpdate;
|
struct CmdFlakeUpdate;
|
||||||
class FlakeCommand : virtual Args, public MixFlakeOptions
|
|
||||||
|
FlakeCommand::FlakeCommand()
|
||||||
{
|
{
|
||||||
protected:
|
expectArgs({
|
||||||
std::string flakeUrl = ".";
|
.label = "flake-url",
|
||||||
|
.optional = true,
|
||||||
|
.handler = {&flakeUrl},
|
||||||
|
.completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) {
|
||||||
|
completeFlakeRef(completions, getStore(), prefix);
|
||||||
|
}}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
FlakeRef FlakeCommand::getFlakeRef()
|
||||||
|
{
|
||||||
|
return parseFlakeRef(fetchSettings, flakeUrl, std::filesystem::current_path().string()); //FIXME
|
||||||
|
}
|
||||||
|
|
||||||
FlakeCommand()
|
LockedFlake FlakeCommand::lockFlake()
|
||||||
{
|
{
|
||||||
expectArgs({
|
return flake::lockFlake(flakeSettings, *getEvalState(), getFlakeRef(), lockFlags);
|
||||||
.label = "flake-url",
|
}
|
||||||
.optional = true,
|
|
||||||
.handler = {&flakeUrl},
|
|
||||||
.completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) {
|
|
||||||
completeFlakeRef(completions, getStore(), prefix);
|
|
||||||
}}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
FlakeRef getFlakeRef()
|
std::vector<FlakeRef> FlakeCommand::getFlakeRefsForCompletion()
|
||||||
{
|
{
|
||||||
return parseFlakeRef(fetchSettings, flakeUrl, std::filesystem::current_path().string()); //FIXME
|
return {
|
||||||
}
|
// Like getFlakeRef but with expandTilde calld first
|
||||||
|
parseFlakeRef(fetchSettings, expandTilde(flakeUrl), std::filesystem::current_path().string())
|
||||||
LockedFlake lockFlake()
|
};
|
||||||
{
|
}
|
||||||
return flake::lockFlake(flakeSettings, *getEvalState(), getFlakeRef(), lockFlags);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<FlakeRef> getFlakeRefsForCompletion() override
|
|
||||||
{
|
|
||||||
return {
|
|
||||||
// Like getFlakeRef but with expandTilde calld first
|
|
||||||
parseFlakeRef(fetchSettings, expandTilde(flakeUrl), std::filesystem::current_path().string())
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct CmdFlakeUpdate : FlakeCommand
|
struct CmdFlakeUpdate : FlakeCommand
|
||||||
{
|
{
|
||||||
|
|
@ -1142,59 +1131,6 @@ struct CmdFlakeArchive : FlakeCommand, MixJSON, MixDryRun
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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::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)) {
|
|
||||||
Activity act(*logger, lvlInfo, actUnknown,
|
|
||||||
fmt("fetching '%s'", lockedNode->lockedRef));
|
|
||||||
auto accessor = lockedNode->lockedRef.input.getAccessor(store).first;
|
|
||||||
if (!evalSettings.lazyTrees)
|
|
||||||
fetchToStore(*store, accessor, FetchMode::Copy, lockedNode->lockedRef.input.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct CmdFlakeShow : FlakeCommand, MixJSON
|
struct CmdFlakeShow : FlakeCommand, MixJSON
|
||||||
{
|
{
|
||||||
bool showLegacy = false;
|
bool showLegacy = false;
|
||||||
|
|
@ -1586,22 +1522,7 @@ struct CmdFlakePrefetch : FlakeCommand, MixJSON
|
||||||
struct CmdFlake : NixMultiCommand
|
struct CmdFlake : NixMultiCommand
|
||||||
{
|
{
|
||||||
CmdFlake()
|
CmdFlake()
|
||||||
: NixMultiCommand(
|
: NixMultiCommand("flake", RegisterCommand::getCommandsFor({"flake"}))
|
||||||
"flake",
|
|
||||||
{
|
|
||||||
{"update", []() { return make_ref<CmdFlakeUpdate>(); }},
|
|
||||||
{"lock", []() { return make_ref<CmdFlakeLock>(); }},
|
|
||||||
{"metadata", []() { return make_ref<CmdFlakeMetadata>(); }},
|
|
||||||
{"info", []() { return make_ref<CmdFlakeInfo>(); }},
|
|
||||||
{"check", []() { return make_ref<CmdFlakeCheck>(); }},
|
|
||||||
{"init", []() { return make_ref<CmdFlakeInit>(); }},
|
|
||||||
{"new", []() { return make_ref<CmdFlakeNew>(); }},
|
|
||||||
{"clone", []() { return make_ref<CmdFlakeClone>(); }},
|
|
||||||
{"archive", []() { return make_ref<CmdFlakeArchive>(); }},
|
|
||||||
{"prefetch-inputs", []() { return make_ref<CmdFlakePrefetchInputs>(); }},
|
|
||||||
{"show", []() { return make_ref<CmdFlakeShow>(); }},
|
|
||||||
{"prefetch", []() { return make_ref<CmdFlakePrefetch>(); }},
|
|
||||||
})
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1619,3 +1540,14 @@ struct CmdFlake : NixMultiCommand
|
||||||
};
|
};
|
||||||
|
|
||||||
static auto rCmdFlake = registerCommand<CmdFlake>("flake");
|
static auto rCmdFlake = registerCommand<CmdFlake>("flake");
|
||||||
|
static auto rCmdFlakeArchive = registerCommand2<CmdFlakeArchive>({"flake", "archive"});
|
||||||
|
static auto rCmdFlakeCheck = registerCommand2<CmdFlakeCheck>({"flake", "check"});
|
||||||
|
static auto rCmdFlakeClone = registerCommand2<CmdFlakeClone>({"flake", "clone"});
|
||||||
|
static auto rCmdFlakeInfo = registerCommand2<CmdFlakeInfo>({"flake", "info"});
|
||||||
|
static auto rCmdFlakeInit = registerCommand2<CmdFlakeInit>({"flake", "init"});
|
||||||
|
static auto rCmdFlakeLock = registerCommand2<CmdFlakeLock>({"flake", "lock"});
|
||||||
|
static auto rCmdFlakeMetadata = registerCommand2<CmdFlakeMetadata>({"flake", "metadata"});
|
||||||
|
static auto rCmdFlakeNew = registerCommand2<CmdFlakeNew>({"flake", "new"});
|
||||||
|
static auto rCmdFlakePrefetch = registerCommand2<CmdFlakePrefetch>({"flake", "prefetch"});
|
||||||
|
static auto rCmdFlakeShow = registerCommand2<CmdFlakeShow>({"flake", "show"});
|
||||||
|
static auto rCmdFlakeUpdate = registerCommand2<CmdFlakeUpdate>({"flake", "update"});
|
||||||
|
|
|
||||||
|
|
@ -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