2020-06-29 12:14:23 +00:00
|
|
|
#include "installables.hh"
|
2023-02-03 16:21:47 +00:00
|
|
|
#include "installable-derived-path.hh"
|
2023-02-05 17:16:17 +00:00
|
|
|
#include "installable-value.hh"
|
2020-06-29 12:14:23 +00:00
|
|
|
#include "store-api.hh"
|
|
|
|
#include "eval-inline.hh"
|
2020-06-29 17:08:50 +00:00
|
|
|
#include "eval-cache.hh"
|
|
|
|
#include "names.hh"
|
2021-05-17 07:48:51 +00:00
|
|
|
#include "command.hh"
|
2022-03-17 22:29:15 +00:00
|
|
|
#include "derivations.hh"
|
2023-05-11 22:01:41 +00:00
|
|
|
#include "downstream-placeholder.hh"
|
2020-06-29 12:14:23 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2021-05-17 07:48:51 +00:00
|
|
|
/**
|
|
|
|
* Return the rewrites that are needed to resolve a string whose context is
|
2022-04-19 18:48:13 +00:00
|
|
|
* included in `dependencies`.
|
2021-05-17 07:48:51 +00:00
|
|
|
*/
|
2022-11-21 09:49:01 +00:00
|
|
|
StringPairs resolveRewrites(
|
|
|
|
Store & store,
|
|
|
|
const std::vector<BuiltPathWithResult> & dependencies)
|
2021-05-17 07:48:51 +00:00
|
|
|
{
|
|
|
|
StringPairs res;
|
|
|
|
for (auto & dep : dependencies)
|
2022-11-21 09:49:01 +00:00
|
|
|
if (auto drvDep = std::get_if<BuiltPathBuilt>(&dep.path))
|
2021-05-17 07:48:51 +00:00
|
|
|
for (auto & [ outputName, outputPath ] : drvDep->outputs)
|
|
|
|
res.emplace(
|
2023-05-11 22:01:41 +00:00
|
|
|
DownstreamPlaceholder::unknownCaOutput(drvDep->drvPath, outputName).render(),
|
2021-05-17 07:48:51 +00:00
|
|
|
store.printStorePath(outputPath)
|
|
|
|
);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-19 18:48:13 +00:00
|
|
|
* Resolve the given string assuming the given context.
|
2021-05-17 07:48:51 +00:00
|
|
|
*/
|
2022-11-21 09:49:01 +00:00
|
|
|
std::string resolveString(
|
|
|
|
Store & store,
|
|
|
|
const std::string & toResolve,
|
|
|
|
const std::vector<BuiltPathWithResult> & dependencies)
|
2021-05-17 07:48:51 +00:00
|
|
|
{
|
|
|
|
auto rewrites = resolveRewrites(store, dependencies);
|
|
|
|
return rewriteStrings(toResolve, rewrites);
|
|
|
|
}
|
|
|
|
|
2023-02-05 17:16:17 +00:00
|
|
|
UnresolvedApp InstallableValue::toApp(EvalState & state)
|
2020-06-29 12:14:23 +00:00
|
|
|
{
|
2022-04-14 12:04:19 +00:00
|
|
|
auto cursor = getCursor(state);
|
2022-04-19 10:09:12 +00:00
|
|
|
auto attrPath = cursor->getAttrPath();
|
2020-06-29 12:14:23 +00:00
|
|
|
|
2020-06-29 17:08:50 +00:00
|
|
|
auto type = cursor->getAttr("type")->getString();
|
2020-06-29 12:14:23 +00:00
|
|
|
|
2022-10-26 08:05:27 +00:00
|
|
|
std::string expected = !attrPath.empty() &&
|
|
|
|
(state.symbols[attrPath[0]] == "apps" || state.symbols[attrPath[0]] == "defaultApp")
|
|
|
|
? "app" : "derivation";
|
2022-04-19 10:09:12 +00:00
|
|
|
if (type != expected)
|
|
|
|
throw Error("attribute '%s' should have type '%s'", cursor->getAttrPathStr(), expected);
|
|
|
|
|
2021-05-17 15:49:20 +00:00
|
|
|
if (type == "app") {
|
|
|
|
auto [program, context] = cursor->getAttr("program")->getStringWithContext();
|
2021-02-17 16:54:13 +00:00
|
|
|
|
2023-01-10 16:27:19 +00:00
|
|
|
std::vector<DerivedPath> context2;
|
2023-01-03 16:44:59 +00:00
|
|
|
for (auto & c : context) {
|
|
|
|
context2.emplace_back(std::visit(overloaded {
|
|
|
|
[&](const NixStringContextElem::DrvDeep & d) -> DerivedPath {
|
|
|
|
/* We want all outputs of the drv */
|
|
|
|
return DerivedPath::Built {
|
|
|
|
.drvPath = d.drvPath,
|
2023-01-11 23:57:18 +00:00
|
|
|
.outputs = OutputsSpec::All {},
|
2023-01-03 16:44:59 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
[&](const NixStringContextElem::Built & b) -> DerivedPath {
|
|
|
|
return DerivedPath::Built {
|
|
|
|
.drvPath = b.drvPath,
|
2023-01-11 23:57:18 +00:00
|
|
|
.outputs = OutputsSpec::Names { b.output },
|
2023-01-03 16:44:59 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
[&](const NixStringContextElem::Opaque & o) -> DerivedPath {
|
|
|
|
return DerivedPath::Opaque {
|
|
|
|
.path = o.path,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}, c.raw()));
|
2023-01-10 16:27:19 +00:00
|
|
|
}
|
2020-06-29 12:14:23 +00:00
|
|
|
|
2021-05-17 15:49:20 +00:00
|
|
|
return UnresolvedApp{App {
|
|
|
|
.context = std::move(context2),
|
|
|
|
.program = program,
|
|
|
|
}};
|
2020-06-29 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (type == "derivation") {
|
|
|
|
auto drvPath = cursor->forceDerivation();
|
2022-04-26 12:01:21 +00:00
|
|
|
auto outPath = cursor->getAttr(state.sOutPath)->getString();
|
|
|
|
auto outputName = cursor->getAttr(state.sOutputName)->getString();
|
|
|
|
auto name = cursor->getAttr(state.sName)->getString();
|
2021-10-27 18:18:30 +00:00
|
|
|
auto aPname = cursor->maybeGetAttr("pname");
|
2022-04-20 14:39:47 +00:00
|
|
|
auto aMeta = cursor->maybeGetAttr(state.sMeta);
|
2021-02-17 16:54:13 +00:00
|
|
|
auto aMainProgram = aMeta ? aMeta->maybeGetAttr("mainProgram") : nullptr;
|
|
|
|
auto mainProgram =
|
|
|
|
aMainProgram
|
|
|
|
? aMainProgram->getString()
|
2021-10-27 18:18:30 +00:00
|
|
|
: aPname
|
|
|
|
? aPname->getString()
|
2021-02-17 16:54:13 +00:00
|
|
|
: DrvName(name).name;
|
2021-05-17 15:49:20 +00:00
|
|
|
auto program = outPath + "/bin/" + mainProgram;
|
|
|
|
return UnresolvedApp { App {
|
2023-01-10 16:27:19 +00:00
|
|
|
.context = { DerivedPath::Built {
|
|
|
|
.drvPath = drvPath,
|
2023-01-11 23:57:18 +00:00
|
|
|
.outputs = OutputsSpec::Names { outputName },
|
2023-01-10 16:27:19 +00:00
|
|
|
} },
|
2021-05-17 15:49:20 +00:00
|
|
|
.program = program,
|
|
|
|
}};
|
2020-06-29 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2022-04-14 12:04:19 +00:00
|
|
|
throw Error("attribute '%s' has unsupported type '%s'", cursor->getAttrPathStr(), type);
|
2021-05-17 15:49:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 14:04:47 +00:00
|
|
|
// FIXME: move to libcmd
|
|
|
|
App UnresolvedApp::resolve(ref<Store> evalStore, ref<Store> store)
|
2021-05-17 15:49:20 +00:00
|
|
|
{
|
|
|
|
auto res = unresolved;
|
|
|
|
|
Make command infra less stateful and more regular
Already, we had classes like `BuiltPathsCommand` and `StorePathsCommand`
which provided alternative `run` virtual functions providing the
implementation with more arguments. This was a very nice and easy way to
make writing command; just fill in the virtual functions and it is
fairly clear what to do.
However, exception to this pattern were `Installable{,s}Command`. These
two classes instead just had a field where the installables would be
stored, and various side-effecting `prepare` and `load` machinery too
fill them in. Command would wish out those fields.
This isn't so clear to use.
What this commit does is make those command classes like the others,
with richer `run` functions.
Not only does this restore the pattern making commands easier to write,
it has a number of other benefits:
- `prepare` and `load` are gone entirely! One command just hands just
hands off to the next.
- `useDefaultInstallables` because `defaultInstallables`. This takes
over `prepare` for the one case that needs it, and provides enough
flexiblity to handle `nix repl`'s idiosyncratic migration.
- We can use `ref` instead of `std::shared_ptr`. The former must be
initialized (so it is like Rust's `Box` rather than `Option<Box>`,
This expresses the invariant that the installable are in fact
initialized much better.
This is possible because since we just have local variables not
fields, we can stop worrying about the not-yet-initialized case.
- Fewer lines of code! (Finally I have a large refactor that makes the
number go down not up...)
- `nix repl` is now implemented in a clearer way.
The last item deserves further mention. `nix repl` is not like the other
installable commands because instead working from once-loaded
installables, it needs to be able to load them again and again.
To properly support this, we make a new superclass
`RawInstallablesCommand`. This class has the argument parsing and
completion logic, but does *not* hand off parsed installables but
instead just the raw string arguments.
This is exactly what `nix repl` needs, and allows us to instead of
having the logic awkwardly split between `prepare`,
`useDefaultInstallables,` and `load`, have everything right next to each
other. I think this will enable future simplifications of that argument
defaulting logic, but I am saving those for a future PR --- best to keep
code motion and more complicated boolean expression rewriting separate
steps.
The "diagnostic ignored `-Woverloaded-virtual`" pragma helps because C++
doesn't like our many `run` methods. In our case, we don't mind the
shadowing it all --- it is *intentional* that the derived class only
provides a `run` method, and doesn't call any of the overridden `run`
methods.
Helps with https://github.com/NixOS/rfcs/pull/134
2023-02-04 17:03:47 +00:00
|
|
|
Installables installableContext;
|
2021-05-17 15:49:20 +00:00
|
|
|
|
|
|
|
for (auto & ctxElt : unresolved.context)
|
|
|
|
installableContext.push_back(
|
Make command infra less stateful and more regular
Already, we had classes like `BuiltPathsCommand` and `StorePathsCommand`
which provided alternative `run` virtual functions providing the
implementation with more arguments. This was a very nice and easy way to
make writing command; just fill in the virtual functions and it is
fairly clear what to do.
However, exception to this pattern were `Installable{,s}Command`. These
two classes instead just had a field where the installables would be
stored, and various side-effecting `prepare` and `load` machinery too
fill them in. Command would wish out those fields.
This isn't so clear to use.
What this commit does is make those command classes like the others,
with richer `run` functions.
Not only does this restore the pattern making commands easier to write,
it has a number of other benefits:
- `prepare` and `load` are gone entirely! One command just hands just
hands off to the next.
- `useDefaultInstallables` because `defaultInstallables`. This takes
over `prepare` for the one case that needs it, and provides enough
flexiblity to handle `nix repl`'s idiosyncratic migration.
- We can use `ref` instead of `std::shared_ptr`. The former must be
initialized (so it is like Rust's `Box` rather than `Option<Box>`,
This expresses the invariant that the installable are in fact
initialized much better.
This is possible because since we just have local variables not
fields, we can stop worrying about the not-yet-initialized case.
- Fewer lines of code! (Finally I have a large refactor that makes the
number go down not up...)
- `nix repl` is now implemented in a clearer way.
The last item deserves further mention. `nix repl` is not like the other
installable commands because instead working from once-loaded
installables, it needs to be able to load them again and again.
To properly support this, we make a new superclass
`RawInstallablesCommand`. This class has the argument parsing and
completion logic, but does *not* hand off parsed installables but
instead just the raw string arguments.
This is exactly what `nix repl` needs, and allows us to instead of
having the logic awkwardly split between `prepare`,
`useDefaultInstallables,` and `load`, have everything right next to each
other. I think this will enable future simplifications of that argument
defaulting logic, but I am saving those for a future PR --- best to keep
code motion and more complicated boolean expression rewriting separate
steps.
The "diagnostic ignored `-Woverloaded-virtual`" pragma helps because C++
doesn't like our many `run` methods. In our case, we don't mind the
shadowing it all --- it is *intentional* that the derived class only
provides a `run` method, and doesn't call any of the overridden `run`
methods.
Helps with https://github.com/NixOS/rfcs/pull/134
2023-02-04 17:03:47 +00:00
|
|
|
make_ref<InstallableDerivedPath>(store, DerivedPath { ctxElt }));
|
2021-05-17 07:48:51 +00:00
|
|
|
|
2022-03-02 12:54:08 +00:00
|
|
|
auto builtContext = Installable::build(evalStore, store, Realise::Outputs, installableContext);
|
2021-05-17 15:49:20 +00:00
|
|
|
res.program = resolveString(*store, unresolved.program, builtContext);
|
2021-05-18 11:54:05 +00:00
|
|
|
if (!store->isInStore(res.program))
|
2021-05-17 15:49:20 +00:00
|
|
|
throw Error("app program '%s' is not in the Nix store", res.program);
|
|
|
|
|
|
|
|
return res;
|
2020-06-29 12:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|