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