2020-06-29 15:14:23 +03:00
|
|
|
#include "installables.hh"
|
2023-02-03 18:21:47 +02:00
|
|
|
#include "installable-derived-path.hh"
|
2023-02-05 19:16:17 +02:00
|
|
|
#include "installable-value.hh"
|
2020-06-29 15:14:23 +03:00
|
|
|
#include "store-api.hh"
|
|
|
|
#include "eval-inline.hh"
|
2020-06-29 20:08:50 +03:00
|
|
|
#include "eval-cache.hh"
|
|
|
|
#include "names.hh"
|
2021-05-17 10:48:51 +03:00
|
|
|
#include "command.hh"
|
2022-03-18 00:29:15 +02:00
|
|
|
#include "derivations.hh"
|
2023-05-12 01:01:41 +03:00
|
|
|
#include "downstream-placeholder.hh"
|
2020-06-29 15:14:23 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2021-05-17 10:48:51 +03:00
|
|
|
/**
|
|
|
|
* Return the rewrites that are needed to resolve a string whose context is
|
2022-04-19 21:48:13 +03:00
|
|
|
* included in `dependencies`.
|
2021-05-17 10:48:51 +03:00
|
|
|
*/
|
2022-11-21 11:49:01 +02:00
|
|
|
StringPairs resolveRewrites(
|
|
|
|
Store & store,
|
|
|
|
const std::vector<BuiltPathWithResult> & dependencies)
|
2021-05-17 10:48:51 +03:00
|
|
|
{
|
|
|
|
StringPairs res;
|
|
|
|
for (auto & dep : dependencies)
|
2022-11-21 11:49:01 +02:00
|
|
|
if (auto drvDep = std::get_if<BuiltPathBuilt>(&dep.path))
|
2023-07-13 06:33:43 +03:00
|
|
|
if (experimentalFeatureSettings.isEnabled(Xp::CaDerivations))
|
|
|
|
for (auto & [ outputName, outputPath ] : drvDep->outputs)
|
|
|
|
res.emplace(
|
|
|
|
DownstreamPlaceholder::unknownCaOutput(drvDep->drvPath, outputName).render(),
|
|
|
|
store.printStorePath(outputPath)
|
|
|
|
);
|
2021-05-17 10:48:51 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-19 21:48:13 +03:00
|
|
|
* Resolve the given string assuming the given context.
|
2021-05-17 10:48:51 +03:00
|
|
|
*/
|
2022-11-21 11:49:01 +02:00
|
|
|
std::string resolveString(
|
|
|
|
Store & store,
|
|
|
|
const std::string & toResolve,
|
|
|
|
const std::vector<BuiltPathWithResult> & dependencies)
|
2021-05-17 10:48:51 +03:00
|
|
|
{
|
|
|
|
auto rewrites = resolveRewrites(store, dependencies);
|
|
|
|
return rewriteStrings(toResolve, rewrites);
|
|
|
|
}
|
|
|
|
|
2023-02-05 19:16:17 +02:00
|
|
|
UnresolvedApp InstallableValue::toApp(EvalState & state)
|
2020-06-29 15:14:23 +03:00
|
|
|
{
|
2022-04-14 15:04:19 +03:00
|
|
|
auto cursor = getCursor(state);
|
2022-04-19 13:09:12 +03:00
|
|
|
auto attrPath = cursor->getAttrPath();
|
2020-06-29 15:14:23 +03:00
|
|
|
|
2020-06-29 20:08:50 +03:00
|
|
|
auto type = cursor->getAttr("type")->getString();
|
2020-06-29 15:14:23 +03:00
|
|
|
|
2022-10-26 11:05:27 +03:00
|
|
|
std::string expected = !attrPath.empty() &&
|
|
|
|
(state.symbols[attrPath[0]] == "apps" || state.symbols[attrPath[0]] == "defaultApp")
|
|
|
|
? "app" : "derivation";
|
2022-04-19 13:09:12 +03:00
|
|
|
if (type != expected)
|
|
|
|
throw Error("attribute '%s' should have type '%s'", cursor->getAttrPathStr(), expected);
|
|
|
|
|
2021-05-17 18:49:20 +03:00
|
|
|
if (type == "app") {
|
|
|
|
auto [program, context] = cursor->getAttr("program")->getStringWithContext();
|
2021-02-17 18:54:13 +02:00
|
|
|
|
2023-01-10 18:27:19 +02:00
|
|
|
std::vector<DerivedPath> context2;
|
2023-01-03 18:44:59 +02: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-12 01:57:18 +02:00
|
|
|
.outputs = OutputsSpec::All {},
|
2023-01-03 18:44:59 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
[&](const NixStringContextElem::Built & b) -> DerivedPath {
|
|
|
|
return DerivedPath::Built {
|
|
|
|
.drvPath = b.drvPath,
|
2023-01-12 01:57:18 +02:00
|
|
|
.outputs = OutputsSpec::Names { b.output },
|
2023-01-03 18:44:59 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
[&](const NixStringContextElem::Opaque & o) -> DerivedPath {
|
|
|
|
return DerivedPath::Opaque {
|
|
|
|
.path = o.path,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}, c.raw()));
|
2023-01-10 18:27:19 +02:00
|
|
|
}
|
2020-06-29 15:14:23 +03:00
|
|
|
|
2021-05-17 18:49:20 +03:00
|
|
|
return UnresolvedApp{App {
|
|
|
|
.context = std::move(context2),
|
|
|
|
.program = program,
|
|
|
|
}};
|
2020-06-29 20:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (type == "derivation") {
|
|
|
|
auto drvPath = cursor->forceDerivation();
|
2022-04-26 15:01:21 +03: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 21:18:30 +03:00
|
|
|
auto aPname = cursor->maybeGetAttr("pname");
|
2022-04-20 17:39:47 +03:00
|
|
|
auto aMeta = cursor->maybeGetAttr(state.sMeta);
|
2021-02-17 18:54:13 +02:00
|
|
|
auto aMainProgram = aMeta ? aMeta->maybeGetAttr("mainProgram") : nullptr;
|
|
|
|
auto mainProgram =
|
|
|
|
aMainProgram
|
|
|
|
? aMainProgram->getString()
|
2021-10-27 21:18:30 +03:00
|
|
|
: aPname
|
|
|
|
? aPname->getString()
|
2021-02-17 18:54:13 +02:00
|
|
|
: DrvName(name).name;
|
2021-05-17 18:49:20 +03:00
|
|
|
auto program = outPath + "/bin/" + mainProgram;
|
|
|
|
return UnresolvedApp { App {
|
2023-01-10 18:27:19 +02:00
|
|
|
.context = { DerivedPath::Built {
|
|
|
|
.drvPath = drvPath,
|
2023-01-12 01:57:18 +02:00
|
|
|
.outputs = OutputsSpec::Names { outputName },
|
2023-01-10 18:27:19 +02:00
|
|
|
} },
|
2021-05-17 18:49:20 +03:00
|
|
|
.program = program,
|
|
|
|
}};
|
2020-06-29 20:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2022-04-14 15:04:19 +03:00
|
|
|
throw Error("attribute '%s' has unsupported type '%s'", cursor->getAttrPathStr(), type);
|
2021-05-17 18:49:20 +03:00
|
|
|
}
|
|
|
|
|
2021-07-16 17:04:47 +03:00
|
|
|
// FIXME: move to libcmd
|
|
|
|
App UnresolvedApp::resolve(ref<Store> evalStore, ref<Store> store)
|
2021-05-17 18:49:20 +03: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 19:03:47 +02:00
|
|
|
Installables installableContext;
|
2021-05-17 18:49:20 +03: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 19:03:47 +02:00
|
|
|
make_ref<InstallableDerivedPath>(store, DerivedPath { ctxElt }));
|
2021-05-17 10:48:51 +03:00
|
|
|
|
2022-03-02 14:54:08 +02:00
|
|
|
auto builtContext = Installable::build(evalStore, store, Realise::Outputs, installableContext);
|
2021-05-17 18:49:20 +03:00
|
|
|
res.program = resolveString(*store, unresolved.program, builtContext);
|
2021-05-18 14:54:05 +03:00
|
|
|
if (!store->isInStore(res.program))
|
2021-05-17 18:49:20 +03:00
|
|
|
throw Error("app program '%s' is not in the Nix store", res.program);
|
|
|
|
|
|
|
|
return res;
|
2020-06-29 15:14:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|