2020-07-30 16:33:22 +00:00
|
|
|
#include "command.hh"
|
|
|
|
#include "common-args.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#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
|
|
|
{
|
2020-07-30 20:03:57 +00:00
|
|
|
std::string bundler = "github:matthewbauer/nix-bundle";
|
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",
|
|
|
|
.description = "use custom 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',
|
|
|
|
.description = "path of the symlink to the build result",
|
|
|
|
.labels = {"path"},
|
|
|
|
.handler = {&outLink},
|
|
|
|
.completer = completePath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Examples examples() override
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
Example{
|
2020-07-30 20:03:57 +00:00
|
|
|
"To bundle Hello:",
|
|
|
|
"nix bundle hello"
|
2020-07-30 16:33:22 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-31 15:30:12 +00:00
|
|
|
Category category() override { return catSecondary; }
|
|
|
|
|
2020-07-30 16:33:22 +00: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();
|
|
|
|
|
|
|
|
auto app = installable->toApp(*evalState);
|
|
|
|
store->buildPaths(app.context);
|
|
|
|
|
2020-07-30 20:03:57 +00:00
|
|
|
auto [bundlerFlakeRef, bundlerName] = parseFlakeRefWithFragment(bundler, absPath("."));
|
2020-07-30 16:33:22 +00:00
|
|
|
const flake::LockFlags lockFlags{ .writeLockFile = false };
|
2020-07-30 20:03:57 +00:00
|
|
|
auto bundler = InstallableFlake(
|
|
|
|
evalState, std::move(bundlerFlakeRef),
|
2020-07-30 20:37:05 +00:00
|
|
|
Strings{bundlerName == "" ? "defaultBundler" : bundlerName},
|
|
|
|
Strings({"bundlers."}), lockFlags);
|
2020-07-30 16:33:22 +00:00
|
|
|
|
|
|
|
Value * arg = evalState->allocValue();
|
2020-07-30 20:37:05 +00:00
|
|
|
evalState->mkAttrs(*arg, 2);
|
2020-07-30 16:33:22 +00: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 20:37:05 +00:00
|
|
|
mkString(*evalState->allocAttr(*arg, evalState->symbols.create("system")), settings.thisSystem.get());
|
|
|
|
|
|
|
|
arg->attrs->sort();
|
|
|
|
|
2020-07-30 16:33:22 +00:00
|
|
|
auto vRes = evalState->allocValue();
|
2020-07-30 20:03:57 +00:00
|
|
|
evalState->callFunction(*bundler.toValue(*evalState).first, *arg, *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-07-30 20:18:48 +00:00
|
|
|
auto attr1 = vRes->attrs->find(evalState->sDrvPath);
|
|
|
|
if (!attr1)
|
|
|
|
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
|
2020-07-30 16:33:22 +00:00
|
|
|
|
|
|
|
PathSet context2;
|
2020-07-30 20:18:48 +00:00
|
|
|
StorePath drvPath = store->parseStorePath(evalState->coerceToPath(*attr1->pos, *attr1->value, context2));
|
2020-07-30 16:33:22 +00:00
|
|
|
|
2020-07-30 20:18:48 +00:00
|
|
|
auto attr2 = vRes->attrs->find(evalState->sOutPath);
|
|
|
|
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
|
|
|
|
2020-07-30 20:18:48 +00:00
|
|
|
StorePath outPath = store->parseStorePath(evalState->coerceToPath(*attr2->pos, *attr2->value, context2));
|
2020-07-30 16:33:22 +00:00
|
|
|
|
|
|
|
store->buildPaths({{drvPath}});
|
|
|
|
|
|
|
|
auto outPathS = store->printStorePath(outPath);
|
|
|
|
|
|
|
|
auto info = store->queryPathInfo(outPath);
|
|
|
|
if (!info->references.empty())
|
2020-07-30 20:03:57 +00:00
|
|
|
throw Error("'%s' has references; a bundler must not leave any references", outPathS);
|
2020-07-30 16:33:22 +00:00
|
|
|
|
2020-07-30 20:18:48 +00:00
|
|
|
if (!outLink)
|
2020-07-30 16:33:22 +00:00
|
|
|
outLink = baseNameOf(app.program);
|
|
|
|
|
2020-07-30 20:18:48 +00:00
|
|
|
store.dynamic_pointer_cast<LocalFSStore>()->addPermRoot(outPath, absPath(*outLink), true);
|
2020-07-30 16:33:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-30 20:03:57 +00:00
|
|
|
static auto r2 = registerCommand<CmdBundle>("bundle");
|