2020-07-30 19:33:22 +03:00
|
|
|
#include "command.hh"
|
|
|
|
#include "common-args.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
2020-10-09 23:18:08 +03:00
|
|
|
#include "local-fs-store.hh"
|
2020-07-30 19:33:22 +03:00
|
|
|
#include "fs-accessor.hh"
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2020-07-30 23:03:57 +03:00
|
|
|
struct CmdBundle : InstallableCommand
|
2020-07-30 19:33:22 +03:00
|
|
|
{
|
2020-07-30 23:03:57 +03:00
|
|
|
std::string bundler = "github:matthewbauer/nix-bundle";
|
2020-07-30 23:18:48 +03:00
|
|
|
std::optional<Path> outLink;
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2020-07-30 23:03:57 +03:00
|
|
|
CmdBundle()
|
2020-07-30 19:33:22 +03:00
|
|
|
{
|
|
|
|
addFlag({
|
2020-07-30 23:03:57 +03:00
|
|
|
.longName = "bundler",
|
2021-01-13 15:18:04 +02:00
|
|
|
.description = fmt("Use a custom bundler instead of the default (`%s`).", bundler),
|
2020-07-30 19:33:22 +03:00
|
|
|
.labels = {"flake-url"},
|
2020-07-30 23:03:57 +03:00
|
|
|
.handler = {&bundler},
|
2020-07-30 19:33:22 +03:00
|
|
|
.completer = {[&](size_t, std::string_view prefix) {
|
|
|
|
completeFlakeRef(getStore(), prefix);
|
|
|
|
}}
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "out-link",
|
|
|
|
.shortName = 'o',
|
2021-01-13 15:18:04 +02:00
|
|
|
.description = "Override the name of the symlink to the build result. It defaults to the base name of the app.",
|
2020-07-30 19:33:22 +03:00
|
|
|
.labels = {"path"},
|
|
|
|
.handler = {&outLink},
|
|
|
|
.completer = completePath
|
|
|
|
});
|
2020-11-21 06:28:49 +02:00
|
|
|
|
2020-07-30 19:33:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
2020-07-30 23:03:57 +03:00
|
|
|
return "bundle an application so that it works outside of the Nix store";
|
2020-07-30 19:33:22 +03:00
|
|
|
}
|
|
|
|
|
2020-12-17 12:45:59 +02:00
|
|
|
std::string doc() override
|
2020-07-30 19:33:22 +03:00
|
|
|
{
|
2020-12-17 12:45:59 +02:00
|
|
|
return
|
|
|
|
#include "bundle.md"
|
|
|
|
;
|
2020-07-30 19:33:22 +03:00
|
|
|
}
|
|
|
|
|
2020-07-31 18:30:12 +03:00
|
|
|
Category category() override { return catSecondary; }
|
|
|
|
|
2020-07-30 19:33:22 +03:00
|
|
|
Strings getDefaultFlakeAttrPaths() override
|
|
|
|
{
|
|
|
|
Strings res{"defaultApp." + settings.thisSystem.get()};
|
|
|
|
for (auto & s : SourceExprCommand::getDefaultFlakeAttrPaths())
|
|
|
|
res.push_back(s);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Strings getDefaultFlakeAttrPathPrefixes() override
|
|
|
|
{
|
|
|
|
Strings res{"apps." + settings.thisSystem.get() + ".", "packages"};
|
|
|
|
for (auto & s : SourceExprCommand::getDefaultFlakeAttrPathPrefixes())
|
|
|
|
res.push_back(s);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
|
|
|
auto evalState = getEvalState();
|
|
|
|
|
2021-05-17 18:49:20 +03:00
|
|
|
auto app = installable->toApp(*evalState).resolve(store);
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2020-07-30 23:03:57 +03:00
|
|
|
auto [bundlerFlakeRef, bundlerName] = parseFlakeRefWithFragment(bundler, absPath("."));
|
2020-07-30 19:33:22 +03:00
|
|
|
const flake::LockFlags lockFlags{ .writeLockFile = false };
|
2021-02-17 18:32:10 +02:00
|
|
|
auto bundler = InstallableFlake(this,
|
2020-07-30 23:03:57 +03:00
|
|
|
evalState, std::move(bundlerFlakeRef),
|
2020-07-30 23:37:05 +03:00
|
|
|
Strings{bundlerName == "" ? "defaultBundler" : bundlerName},
|
|
|
|
Strings({"bundlers."}), lockFlags);
|
2020-07-30 19:33:22 +03:00
|
|
|
|
|
|
|
Value * arg = evalState->allocValue();
|
2020-07-30 23:37:05 +03:00
|
|
|
evalState->mkAttrs(*arg, 2);
|
2020-07-30 19:33:22 +03:00
|
|
|
|
|
|
|
PathSet context;
|
|
|
|
for (auto & i : app.context)
|
|
|
|
context.insert("=" + store->printStorePath(i.path));
|
|
|
|
mkString(*evalState->allocAttr(*arg, evalState->symbols.create("program")), app.program, context);
|
|
|
|
|
2020-07-30 23:37:05 +03:00
|
|
|
mkString(*evalState->allocAttr(*arg, evalState->symbols.create("system")), settings.thisSystem.get());
|
|
|
|
|
|
|
|
arg->attrs->sort();
|
2021-01-13 15:18:04 +02:00
|
|
|
|
2020-07-30 19:33:22 +03:00
|
|
|
auto vRes = evalState->allocValue();
|
2020-07-30 23:03:57 +03:00
|
|
|
evalState->callFunction(*bundler.toValue(*evalState).first, *arg, *vRes, noPos);
|
2020-07-30 19:33:22 +03:00
|
|
|
|
|
|
|
if (!evalState->isDerivation(*vRes))
|
2020-07-30 23:03:57 +03:00
|
|
|
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2020-09-18 14:10:42 +03:00
|
|
|
auto attr1 = vRes->attrs->get(evalState->sDrvPath);
|
2020-07-30 23:18:48 +03:00
|
|
|
if (!attr1)
|
|
|
|
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
|
2020-07-30 19:33:22 +03:00
|
|
|
|
|
|
|
PathSet context2;
|
2020-07-30 23:18:48 +03:00
|
|
|
StorePath drvPath = store->parseStorePath(evalState->coerceToPath(*attr1->pos, *attr1->value, context2));
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2020-09-18 14:10:42 +03:00
|
|
|
auto attr2 = vRes->attrs->get(evalState->sOutPath);
|
2020-07-30 23:18:48 +03:00
|
|
|
if (!attr2)
|
2020-07-30 23:03:57 +03:00
|
|
|
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2020-07-30 23:18:48 +03:00
|
|
|
StorePath outPath = store->parseStorePath(evalState->coerceToPath(*attr2->pos, *attr2->value, context2));
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2021-04-05 16:48:18 +03:00
|
|
|
store->buildPaths({ DerivedPath::Built { drvPath } });
|
2020-07-30 19:33:22 +03:00
|
|
|
|
|
|
|
auto outPathS = store->printStorePath(outPath);
|
|
|
|
|
2020-07-30 23:18:48 +03:00
|
|
|
if (!outLink)
|
2020-07-30 19:33:22 +03:00
|
|
|
outLink = baseNameOf(app.program);
|
|
|
|
|
2020-09-03 12:26:36 +03:00
|
|
|
store.dynamic_pointer_cast<LocalFSStore>()->addPermRoot(outPath, absPath(*outLink));
|
2020-07-30 19:33:22 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-30 23:03:57 +03:00
|
|
|
static auto r2 = registerCommand<CmdBundle>("bundle");
|