-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cc
More file actions
155 lines (136 loc) · 5.73 KB
/
Copy pathapp.cc
File metadata and controls
155 lines (136 loc) · 5.73 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "nix/cmd/installables.hh"
#include "nix/cmd/installable-derived-path.hh"
#include "nix/cmd/installable-value.hh"
#include "nix/store/store-api.hh"
#include "nix/expr/eval-inline.hh"
#include "nix/expr/eval-cache.hh"
#include "nix/store/names.hh"
#include "nix/cmd/command.hh"
#include "nix/store/derivations.hh"
#include "nix/store/downstream-placeholder.hh"
namespace nix {
/**
* Return the rewrites that are needed to resolve a string whose context is
* included in `dependencies`.
*/
StringPairs resolveRewrites(Store & store, const std::vector<BuiltPathWithResult> & dependencies)
{
StringPairs res;
if (!experimentalFeatureSettings.isEnabled(Xp::CaDerivations)) {
return res;
}
for (auto & dep : dependencies) {
auto drvDep = std::get_if<BuiltPathBuilt>(&dep.path);
if (!drvDep) {
continue;
}
for (const auto & [outputName, outputPath] : drvDep->outputs) {
res.emplace(
DownstreamPlaceholder::fromSingleDerivedPathBuilt(
SingleDerivedPath::Built{
.drvPath = make_ref<SingleDerivedPath>(drvDep->drvPath->discardOutputPath()),
.output = outputName,
})
.render(),
store.printStorePath(outputPath));
}
}
return res;
}
/**
* Resolve the given string assuming the given context.
*/
std::string
resolveString(Store & store, const std::string & toResolve, const std::vector<BuiltPathWithResult> & dependencies)
{
auto rewrites = resolveRewrites(store, dependencies);
return rewriteStrings(toResolve, rewrites);
}
UnresolvedApp InstallableValue::toApp(EvalState & state)
{
auto cursor = getCursor(state);
auto attrPath = cursor->getAttrPath();
auto type = cursor->getAttr("type")->getString();
std::string expectedType =
!attrPath.empty() && (state.symbols[attrPath[0]] == "apps" || state.symbols[attrPath[0]] == "defaultApp")
? "app"
: "derivation";
if (type != expectedType)
throw Error("attribute '%s' should have type '%s'", cursor->getAttrPathStr(), expectedType);
if (type == "app") {
auto [program, context] = cursor->getAttr("program")->getStringWithContext();
std::vector<DerivedPath> context2;
for (auto & c : context) {
context2.emplace_back(
std::visit(
overloaded{
[&](const NixStringContextElem::DrvDeep & d) -> DerivedPath {
state.waitForPath(d.drvPath);
/* We want all outputs of the drv */
return DerivedPath::Built{
.drvPath = makeConstantStorePathRef(d.drvPath),
.outputs = OutputsSpec::All{},
};
},
[&](const NixStringContextElem::Built & b) -> DerivedPath {
state.waitForPath(*b.drvPath);
return DerivedPath::Built{
.drvPath = b.drvPath,
.outputs = OutputsSpec::Names{b.output},
};
},
[&](const NixStringContextElem::Opaque & o) -> DerivedPath {
return DerivedPath::Opaque{
.path = state.devirtualize(o.path),
};
},
[&](const NixStringContextElem::Path & p) -> DerivedPath {
throw Error("'program' attribute of an 'app' output cannot have no context");
},
},
c.raw));
}
return UnresolvedApp{App{
.context = std::move(context2),
.program = state.devirtualize(program, context),
}};
}
else if (type == "derivation") {
auto drvPath = cursor->forceDerivation();
auto outPath = cursor->getAttr(state.s.outPath)->getString();
auto outputName = cursor->getAttr(state.s.outputName)->getString();
auto name = cursor->getAttr(state.s.name)->getString();
auto aPname = cursor->maybeGetAttr("pname");
auto aMeta = cursor->maybeGetAttr(state.s.meta);
auto aMainProgram = aMeta ? aMeta->maybeGetAttr("mainProgram") : nullptr;
auto mainProgram = aMainProgram ? aMainProgram->getString() : aPname ? aPname->getString() : DrvName(name).name;
auto program = outPath + "/bin/" + mainProgram;
return UnresolvedApp{App{
.context = {DerivedPath::Built{
.drvPath = makeConstantStorePathRef(drvPath),
.outputs = OutputsSpec::Names{outputName},
}},
.program = program,
}};
}
else
throw Error("attribute '%s' has unsupported type '%s'", cursor->getAttrPathStr(), type);
}
std::vector<BuiltPathWithResult> UnresolvedApp::build(ref<Store> evalStore, ref<Store> store)
{
Installables installableContext;
for (auto & ctxElt : unresolved.context)
installableContext.push_back(make_ref<InstallableDerivedPath>(store, DerivedPath{ctxElt}));
return Installable::build(evalStore, store, Realise::Outputs, installableContext);
}
// FIXME: move to libcmd
App UnresolvedApp::resolve(ref<Store> evalStore, ref<Store> store)
{
auto res = unresolved;
auto builtContext = build(evalStore, store);
res.program = resolveString(*store, unresolved.program.string(), builtContext);
if (!store->isInStore(res.program.string()))
throw Error("app program '%s' is not in the Nix store", res.program.string());
return res;
}
} // namespace nix