lix/src/nix/bundle.cc

125 lines
4 KiB
C++
Raw Normal View History

#include "command.hh"
#include "common-args.hh"
#include "shared.hh"
#include "store-api.hh"
#include "local-fs-store.hh"
#include "fs-accessor.hh"
using namespace nix;
2020-07-30 20:03:57 +00:00
struct CmdBundle : InstallableCommand
{
std::string bundler = "github:NixOS/bundlers";
2020-07-30 20:18:48 +00:00
std::optional<Path> outLink;
2020-07-30 20:03:57 +00:00
CmdBundle()
{
addFlag({
2020-07-30 20:03:57 +00:00
.longName = "bundler",
.description = fmt("Use a custom bundler instead of the default (`%s`).", bundler),
.labels = {"flake-url"},
2020-07-30 20:03:57 +00:00
.handler = {&bundler},
.completer = {[&](size_t, std::string_view prefix) {
completeFlakeRef(getStore(), prefix);
}}
});
addFlag({
.longName = "out-link",
.shortName = 'o',
.description = "Override the name of the symlink to the build result. It defaults to the base name of the app.",
.labels = {"path"},
.handler = {&outLink},
.completer = completePath
});
2020-11-21 04:28:49 +00:00
}
std::string description() override
{
2020-07-30 20:03:57 +00:00
return "bundle an application so that it works outside of the Nix store";
}
2020-12-17 10:45:59 +00:00
std::string doc() override
{
2020-12-17 10:45:59 +00:00
return
#include "bundle.md"
;
}
2020-07-31 15:30:12 +00:00
Category category() override { return catSecondary; }
// FIXME: cut&paste from CmdRun.
Strings getDefaultFlakeAttrPaths() override
{
Strings res{
"apps." + settings.thisSystem.get() + ".default",
"defaultApp." + settings.thisSystem.get()
};
for (auto & s : SourceExprCommand::getDefaultFlakeAttrPaths())
res.push_back(s);
return res;
}
Strings getDefaultFlakeAttrPathPrefixes() override
{
Strings res{"apps." + settings.thisSystem.get() + "."};
for (auto & s : SourceExprCommand::getDefaultFlakeAttrPathPrefixes())
res.push_back(s);
return res;
}
void run(ref<Store> store) override
{
auto evalState = getEvalState();
auto val = installable->toValue(*evalState).first;
auto [bundlerFlakeRef, bundlerName, extendedOutputsSpec] = parseFlakeRefWithFragmentAndExtendedOutputsSpec(bundler, absPath("."));
const flake::LockFlags lockFlags{ .writeLockFile = false };
InstallableFlake bundler{this,
evalState, std::move(bundlerFlakeRef), bundlerName, extendedOutputsSpec,
{"bundlers." + settings.thisSystem.get() + ".default",
"defaultBundler." + settings.thisSystem.get()
},
{"bundlers." + settings.thisSystem.get() + "."},
lockFlags
};
auto vRes = evalState->allocValue();
evalState->callFunction(*bundler.toValue(*evalState).first, *val, *vRes, noPos);
if (!evalState->isDerivation(*vRes))
2020-07-30 20:03:57 +00:00
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
auto attr1 = vRes->attrs->get(evalState->sDrvPath);
2020-07-30 20:18:48 +00:00
if (!attr1)
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
PathSet context2;
auto drvPath = evalState->coerceToStorePath(attr1->pos, *attr1->value, context2, "");
auto attr2 = vRes->attrs->get(evalState->sOutPath);
2020-07-30 20:18:48 +00:00
if (!attr2)
2020-07-30 20:03:57 +00:00
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
auto outPath = evalState->coerceToStorePath(attr2->pos, *attr2->value, context2, "");
2021-04-05 13:48:18 +00:00
store->buildPaths({ DerivedPath::Built { drvPath } });
auto outPathS = store->printStorePath(outPath);
if (!outLink) {
auto * attr = vRes->attrs->get(evalState->sName);
if (!attr)
throw Error("attribute 'name' missing");
outLink = evalState->forceStringNoCtx(*attr->value, attr->pos, "");
}
// TODO: will crash if not a localFSStore?
2020-09-03 09:26:36 +00:00
store.dynamic_pointer_cast<LocalFSStore>()->addPermRoot(outPath, absPath(*outLink));
}
};
2020-07-30 20:03:57 +00:00
static auto r2 = registerCommand<CmdBundle>("bundle");