2023-02-03 19:53:40 +00:00
|
|
|
#include "installable-flake.hh"
|
2023-02-05 17:16:17 +00:00
|
|
|
#include "command-installable-value.hh"
|
2020-07-30 16:33:22 +00:00
|
|
|
#include "common-args.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
2020-10-09 20:18:08 +00:00
|
|
|
#include "local-fs-store.hh"
|
2020-07-30 16:33:22 +00:00
|
|
|
#include "fs-accessor.hh"
|
2023-02-06 04:28:18 +00:00
|
|
|
#include "eval-inline.hh"
|
2020-07-30 16:33:22 +00:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2023-02-05 17:16:17 +00:00
|
|
|
struct CmdBundle : InstallableValueCommand
|
2020-07-30 16:33:22 +00:00
|
|
|
{
|
2022-03-30 20:10:42 +00:00
|
|
|
std::string bundler = "github:NixOS/bundlers";
|
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",
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = fmt("Use a custom bundler instead of the default (`%s`).", bundler),
|
2020-07-30 16:33:22 +00:00
|
|
|
.labels = {"flake-url"},
|
2020-07-30 20:03:57 +00:00
|
|
|
.handler = {&bundler},
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
Change-Id: If18cd5be78da4a70635e3fdcac6326dbfeea71a5
(cherry picked from commit 67eb37c1d0de28160cd25376e51d1ec1b1c8305b)
2024-03-19 02:23:20 +00:00
|
|
|
.completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) {
|
|
|
|
completeFlakeRef(completions, getStore(), prefix);
|
2020-07-30 16:33:22 +00:00
|
|
|
}}
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "out-link",
|
|
|
|
.shortName = 'o',
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = "Override the name of the symlink to the build result. It defaults to the base name of the app.",
|
2020-07-30 16:33:22 +00:00
|
|
|
.labels = {"path"},
|
|
|
|
.handler = {&outLink},
|
|
|
|
.completer = completePath
|
|
|
|
});
|
2020-11-21 04:28:49 +00:00
|
|
|
|
2020-07-30 16:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-17 10:45:59 +00:00
|
|
|
std::string doc() override
|
2020-07-30 16:33:22 +00:00
|
|
|
{
|
2020-12-17 10:45:59 +00:00
|
|
|
return
|
|
|
|
#include "bundle.md"
|
|
|
|
;
|
2020-07-30 16:33:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 15:30:12 +00:00
|
|
|
Category category() override { return catSecondary; }
|
|
|
|
|
2022-02-11 17:11:08 +00:00
|
|
|
// FIXME: cut&paste from CmdRun.
|
2020-07-30 16:33:22 +00:00
|
|
|
Strings getDefaultFlakeAttrPaths() override
|
|
|
|
{
|
2022-01-28 14:56:58 +00:00
|
|
|
Strings res{
|
2022-02-11 17:11:08 +00:00
|
|
|
"apps." + settings.thisSystem.get() + ".default",
|
2022-01-28 14:56:58 +00:00
|
|
|
"defaultApp." + settings.thisSystem.get()
|
|
|
|
};
|
2020-07-30 16:33:22 +00:00
|
|
|
for (auto & s : SourceExprCommand::getDefaultFlakeAttrPaths())
|
|
|
|
res.push_back(s);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Strings getDefaultFlakeAttrPathPrefixes() override
|
|
|
|
{
|
2022-02-11 17:11:08 +00:00
|
|
|
Strings res{"apps." + settings.thisSystem.get() + "."};
|
2020-07-30 16:33:22 +00:00
|
|
|
for (auto & s : SourceExprCommand::getDefaultFlakeAttrPathPrefixes())
|
|
|
|
res.push_back(s);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-02-05 17:16:17 +00:00
|
|
|
void run(ref<Store> store, ref<InstallableValue> installable) override
|
2020-07-30 16:33:22 +00:00
|
|
|
{
|
|
|
|
auto evalState = getEvalState();
|
|
|
|
|
2022-02-14 19:39:44 +00:00
|
|
|
auto val = installable->toValue(*evalState).first;
|
2021-10-31 17:19:53 +00:00
|
|
|
|
2023-01-11 07:00:44 +00:00
|
|
|
auto [bundlerFlakeRef, bundlerName, extendedOutputsSpec] = parseFlakeRefWithFragmentAndExtendedOutputsSpec(bundler, absPath("."));
|
2020-07-30 16:33:22 +00:00
|
|
|
const flake::LockFlags lockFlags{ .writeLockFile = false };
|
2022-02-14 19:39:44 +00:00
|
|
|
InstallableFlake bundler{this,
|
2023-08-16 16:29:23 +00:00
|
|
|
evalState, std::move(bundlerFlakeRef), bundlerName, std::move(extendedOutputsSpec),
|
2022-02-11 17:11:08 +00:00
|
|
|
{"bundlers." + settings.thisSystem.get() + ".default",
|
|
|
|
"defaultBundler." + settings.thisSystem.get()
|
|
|
|
},
|
2022-02-14 19:39:44 +00:00
|
|
|
{"bundlers." + settings.thisSystem.get() + "."},
|
|
|
|
lockFlags
|
|
|
|
};
|
2020-07-30 16:33:22 +00:00
|
|
|
|
|
|
|
auto vRes = evalState->allocValue();
|
2022-01-21 16:43:11 +00:00
|
|
|
evalState->callFunction(*bundler.toValue(*evalState).first, *val, *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-09-18 11:10:42 +00:00
|
|
|
auto attr1 = vRes->attrs->get(evalState->sDrvPath);
|
2020-07-30 20:18:48 +00:00
|
|
|
if (!attr1)
|
|
|
|
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
|
2020-07-30 16:33:22 +00:00
|
|
|
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 01:31:10 +00:00
|
|
|
NixStringContext context2;
|
2023-01-19 12:23:04 +00:00
|
|
|
auto drvPath = evalState->coerceToStorePath(attr1->pos, *attr1->value, context2, "");
|
2020-07-30 16:33:22 +00:00
|
|
|
|
2020-09-18 11:10:42 +00:00
|
|
|
auto attr2 = vRes->attrs->get(evalState->sOutPath);
|
2020-07-30 20:18:48 +00:00
|
|
|
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
|
|
|
|
2023-01-19 12:23:04 +00:00
|
|
|
auto outPath = evalState->coerceToStorePath(attr2->pos, *attr2->value, context2, "");
|
2020-07-30 16:33:22 +00:00
|
|
|
|
2023-01-11 21:32:30 +00:00
|
|
|
store->buildPaths({
|
|
|
|
DerivedPath::Built {
|
Make the Derived Path family of types inductive for dynamic derivations
We want to be able to write down `foo.drv^bar.drv^baz`:
`foo.drv^bar.drv` is the dynamic derivation (since it is itself a
derivation output, `bar.drv` from `foo.drv`).
To that end, we create `Single{Derivation,BuiltPath}` types, that are
very similar except instead of having multiple outputs (in a set or
map), they have a single one. This is for everything to the left of the
rightmost `^`.
`NixStringContextElem` has an analogous change, and now can reuse
`SingleDerivedPath` at the top level. In fact, if we ever get rid of
`DrvDeep`, `NixStringContextElem` could be replaced with
`SingleDerivedPath` entirely!
Important note: some JSON formats have changed.
We already can *produce* dynamic derivations, but we can't refer to them
directly. Today, we can merely express building or example at the top
imperatively over time by building `foo.drv^bar.drv`, and then with a
second nix invocation doing `<result-from-first>^baz`, but this is not
declarative. The ethos of Nix of being able to write down the full plan
everything you want to do, and then execute than plan with a single
command, and for that we need the new inductive form of these types.
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-01-15 22:39:04 +00:00
|
|
|
.drvPath = makeConstantStorePathRef(drvPath),
|
2023-01-11 21:32:30 +00:00
|
|
|
.outputs = OutputsSpec::All { },
|
|
|
|
},
|
|
|
|
});
|
2020-07-30 16:33:22 +00:00
|
|
|
|
|
|
|
auto outPathS = store->printStorePath(outPath);
|
|
|
|
|
2022-01-21 16:43:11 +00:00
|
|
|
if (!outLink) {
|
2022-03-04 18:47:32 +00:00
|
|
|
auto * attr = vRes->attrs->get(evalState->sName);
|
|
|
|
if (!attr)
|
|
|
|
throw Error("attribute 'name' missing");
|
2023-01-19 12:23:04 +00:00
|
|
|
outLink = evalState->forceStringNoCtx(*attr->value, attr->pos, "");
|
2022-01-21 16:43:11 +00:00
|
|
|
}
|
2020-07-30 16:33:22 +00:00
|
|
|
|
2022-01-21 16:43:11 +00:00
|
|
|
// TODO: will crash if not a localFSStore?
|
2020-09-03 09:26:36 +00:00
|
|
|
store.dynamic_pointer_cast<LocalFSStore>()->addPermRoot(outPath, absPath(*outLink));
|
2020-07-30 16:33:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-30 20:03:57 +00:00
|
|
|
static auto r2 = registerCommand<CmdBundle>("bundle");
|