libcmd: implement base for Activatables

This commit is contained in:
Max Headroom 2023-01-13 01:56:32 +01:00
parent 1c35324a97
commit 2b621c8a55
3 changed files with 145 additions and 0 deletions

View file

@ -0,0 +1,46 @@
#include "command.hh"
#include "progress-bar.hh"
using namespace nix;
ActivatableCommand::ActivatableCommand(std::string activationPackageAttrPath)
: activationPackageAttrPath(activationPackageAttrPath)
{ }
void ActivatableCommand::prepare() {
InstallableCommand::prepare();
auto installableFlake = std::dynamic_pointer_cast<InstallableFlake>(installable);
if (installableFlake) {
auto fragment = *installableFlake->attrPaths.begin();
installable = std::make_shared<InstallableFlake>(
this,
getEvalState(),
std::move(installableFlake->flakeRef),
activationPackageAttrPath == "" ? fragment : fragment + "." + activationPackageAttrPath,
installableFlake->outputsSpec,
getDefaultFlakeAttrPaths(),
getDefaultFlakeAttrPathPrefixes(),
installableFlake->lockFlags
);
}
}
StorePath ActivatableCommand::buildActivatable(nix::ref<nix::Store> store, bool dryRun) {
auto installableFlake = std::dynamic_pointer_cast<InstallableFlake>(installable);
std::vector<std::shared_ptr<Installable>> installableContext;
installableContext.emplace_back(installable);
auto buildables = Installable::build(
getEvalStore(), store,
Realise::Outputs,
installableContext, bmNormal);
BuiltPaths buildables2;
for (auto & b : buildables)
buildables2.push_back(b.path);
auto buildable = buildables2.front();
if (buildables2.size() != 1 || buildable.outPaths().size() != 1) {
throw UsageError("this command requires that the argument produces a single store path");
}
return *buildable.outPaths().begin();
}

View file

@ -0,0 +1,70 @@
#include "command.hh"
#include "local-fs-store.hh"
#include "progress-bar.hh"
#include "shared.hh"
using namespace nix;
template<class ActivatableCommand>
ActivatableBuildCommand<ActivatableCommand>::ActivatableBuildCommand()
{
addFlag({
.longName = "out-link",
.shortName = 'o',
.description = "Use *path* as prefix for the symlinks to the build results. It defaults to `result`.",
.labels = {"path"},
.handler = {&outLink},
.completer = completePath
});
addFlag({
.longName = "no-link",
.description = "Do not create symlinks to the build results.",
.handler = {&outLink, Path("")},
});
addFlag({
.longName = "print-out-paths",
.description = "Print the resulting output paths",
.handler = {&printOutputPaths, true},
});
};
template<class ActivatableCommand>
void ActivatableBuildCommand<ActivatableCommand>::run(nix::ref<Store> store)
{
auto installableFlake = std::dynamic_pointer_cast<InstallableFlake>(ActivatableCommand::installable);
if (dryRun) {
std::vector<std::shared_ptr<Installable>> installableContext;
installableContext.emplace_back(ActivatableCommand::installable);
std::vector<DerivedPath> pathsToBuild;
for (auto & i : installableContext) {
auto b = i->toDerivedPaths();
pathsToBuild.insert(pathsToBuild.end(), b.begin(), b.end());
}
printMissing(store, pathsToBuild, lvlError);
return;
}
auto out = ActivatableCommand::buildActivatable(store);
if (outLink != "") {
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>()) {
std::string symlink = outLink;
store2->addPermRoot(out, absPath(symlink));
}
}
if (printOutputPaths) {
stopProgressBar();
std::cout << store->printStorePath(out) << std::endl;
}
}
template<class ActivatableCommand>
ActivationCommand<ActivatableCommand>::ActivationCommand(std::string activationType, std::string selfCommandName)
: activationType(activationType),
selfCommandName(selfCommandName)
{ }

View file

@ -1,5 +1,6 @@
#pragma once
#include "common-args.hh"
#include "installables.hh"
#include "args.hh"
#include "common-eval-args.hh"
@ -272,6 +273,34 @@ struct MixEnvironment : virtual Args {
void setEnviron();
};
/* A command that operates on exactly one "activatable" */
struct ActivatableCommand : public InstallableCommand
{
ActivatableCommand(std::string activationPackageAttrPath);
std::string activationPackageAttrPath;
void prepare() override;
StorePath buildActivatable(nix::ref<Store> store, bool dryRun = false);
};
template<class ActivatableCommand>
struct ActivatableBuildCommand : public ActivatableCommand, public MixDryRun
{
ActivatableBuildCommand();
Path outLink = "result";
bool printOutputPaths = false;
void run(nix::ref<Store>) override;
};
/* A command that manages the activation of exactly one "activatable" */
template<class ActivatableCommand>
struct ActivationCommand : public ActivatableCommand
{
public:
ActivationCommand(std::string activationType, std::string selfCommandName);
const std::string activationType;
const std::string selfCommandName;
};
void completeFlakeRef(ref<Store> store, std::string_view prefix);
void completeFlakeRefWithFragment(