Repository navigation
Expand file tree
/
Copy pathbundle.cc
More file actions
129 lines (104 loc) · 3.92 KB
/
Copy pathbundle.cc
File metadata and controls
129 lines (104 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "nix/cmd/installable-flake.hh"
#include "nix/cmd/command-installable-value.hh"
#include "nix/main/common-args.hh"
#include "nix/main/shared.hh"
#include "nix/store/store-api.hh"
#include "nix/store/local-fs-store.hh"
#include "nix/expr/eval-inline.hh"
#include "nix/store/globals.hh"
namespace nix::fs {
using namespace std::filesystem;
}
using namespace nix;
struct CmdBundle : InstallableValueCommand
{
std::string bundler = "github:NixOS/bundlers";
std::optional<Path> outLink;
CmdBundle()
{
addFlag({
.longName = "bundler",
.description = fmt("Use a custom bundler instead of the default (`%s`).", bundler),
.labels = {"flake-url"},
.handler = {&bundler},
.completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) {
completeFlakeRef(completions, 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,
});
}
std::string description() override
{
return "bundle an application so that it works outside of the Nix store";
}
std::string doc() override
{
return
#include "bundle.md"
;
}
Category category() override
{
return catSecondary;
}
StringSet getRoles() override
{
return {"nix-run"};
}
void run(ref<Store> store, ref<InstallableValue> installable) override
{
auto evalState = getEvalState();
auto val = installable->toValue(*evalState).first;
auto [bundlerFlakeRef, bundlerName, extendedOutputsSpec] = parseFlakeRefWithFragmentAndExtendedOutputsSpec(
fetchSettings, bundler, std::filesystem::current_path().string());
const flake::LockFlags lockFlags{.writeLockFile = false};
InstallableFlake bundler{
this,
evalState,
std::move(bundlerFlakeRef),
bundlerName,
std::move(extendedOutputsSpec),
{"nix-bundler"},
lockFlags,
getDefaultFlakeSchemas()};
auto vRes = evalState->allocValue();
evalState->callFunction(*bundler.toValue(*evalState).first, *val, *vRes, noPos);
if (!evalState->isDerivation(*vRes))
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
auto attr1 = vRes->attrs()->get(evalState->s.drvPath);
if (!attr1)
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
NixStringContext context2;
auto drvPath = evalState->coerceToStorePath(attr1->pos, *attr1->value, context2, "");
evalState->waitForAllPaths();
drvPath.requireDerivation();
auto attr2 = vRes->attrs()->get(evalState->s.outPath);
if (!attr2)
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
auto outPath = evalState->coerceToStorePath(attr2->pos, *attr2->value, context2, "");
evalState->waitForAllPaths();
store->buildPaths({
DerivedPath::Built{
.drvPath = makeConstantStorePathRef(drvPath),
.outputs = OutputsSpec::All{},
},
});
if (!outLink) {
auto * attr = vRes->attrs()->get(evalState->s.name);
if (!attr)
throw Error("attribute 'name' missing");
outLink = evalState->forceStringNoCtx(*attr->value, attr->pos, "");
}
// TODO: will crash if not a localFSStore?
store.dynamic_pointer_cast<LocalFSStore>()->addPermRoot(outPath, absPath(*outLink));
}
};
static auto r2 = registerCommand<CmdBundle>("bundle");