2023-02-03 21:53:40 +02:00
|
|
|
#include "installable-flake.hh"
|
2023-02-05 19:16:17 +02:00
|
|
|
#include "command-installable-value.hh"
|
2020-07-30 19:33:22 +03:00
|
|
|
#include "common-args.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
2020-10-09 23:18:08 +03:00
|
|
|
#include "local-fs-store.hh"
|
2020-07-30 19:33:22 +03:00
|
|
|
#include "fs-accessor.hh"
|
2023-02-06 06:28:18 +02:00
|
|
|
#include "eval-inline.hh"
|
2020-07-30 19:33:22 +03:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2023-02-05 19:16:17 +02:00
|
|
|
struct CmdBundle : InstallableValueCommand
|
2020-07-30 19:33:22 +03:00
|
|
|
{
|
2022-03-30 23:10:42 +03:00
|
|
|
std::string bundler = "github:NixOS/bundlers";
|
2020-07-30 23:18:48 +03:00
|
|
|
std::optional<Path> outLink;
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2020-07-30 23:03:57 +03:00
|
|
|
CmdBundle()
|
2020-07-30 19:33:22 +03:00
|
|
|
{
|
|
|
|
addFlag({
|
2020-07-30 23:03:57 +03:00
|
|
|
.longName = "bundler",
|
2021-01-13 15:18:04 +02:00
|
|
|
.description = fmt("Use a custom bundler instead of the default (`%s`).", bundler),
|
2020-07-30 19:33:22 +03:00
|
|
|
.labels = {"flake-url"},
|
2020-07-30 23:03:57 +03:00
|
|
|
.handler = {&bundler},
|
2020-07-30 19:33:22 +03:00
|
|
|
.completer = {[&](size_t, std::string_view prefix) {
|
|
|
|
completeFlakeRef(getStore(), prefix);
|
|
|
|
}}
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "out-link",
|
|
|
|
.shortName = 'o',
|
2021-01-13 15:18:04 +02:00
|
|
|
.description = "Override the name of the symlink to the build result. It defaults to the base name of the app.",
|
2020-07-30 19:33:22 +03:00
|
|
|
.labels = {"path"},
|
|
|
|
.handler = {&outLink},
|
|
|
|
.completer = completePath
|
|
|
|
});
|
2020-11-21 06:28:49 +02:00
|
|
|
|
2020-07-30 19:33:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
2020-07-30 23:03:57 +03:00
|
|
|
return "bundle an application so that it works outside of the Nix store";
|
2020-07-30 19:33:22 +03:00
|
|
|
}
|
|
|
|
|
2020-12-17 12:45:59 +02:00
|
|
|
std::string doc() override
|
2020-07-30 19:33:22 +03:00
|
|
|
{
|
2020-12-17 12:45:59 +02:00
|
|
|
return
|
|
|
|
#include "bundle.md"
|
|
|
|
;
|
2020-07-30 19:33:22 +03:00
|
|
|
}
|
|
|
|
|
2020-07-31 18:30:12 +03:00
|
|
|
Category category() override { return catSecondary; }
|
|
|
|
|
2022-02-11 19:11:08 +02:00
|
|
|
// FIXME: cut&paste from CmdRun.
|
2020-07-30 19:33:22 +03:00
|
|
|
Strings getDefaultFlakeAttrPaths() override
|
|
|
|
{
|
2022-01-28 16:56:58 +02:00
|
|
|
Strings res{
|
2022-02-11 19:11:08 +02:00
|
|
|
"apps." + settings.thisSystem.get() + ".default",
|
2022-01-28 16:56:58 +02:00
|
|
|
"defaultApp." + settings.thisSystem.get()
|
|
|
|
};
|
2020-07-30 19:33:22 +03:00
|
|
|
for (auto & s : SourceExprCommand::getDefaultFlakeAttrPaths())
|
|
|
|
res.push_back(s);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Strings getDefaultFlakeAttrPathPrefixes() override
|
|
|
|
{
|
2022-02-11 19:11:08 +02:00
|
|
|
Strings res{"apps." + settings.thisSystem.get() + "."};
|
2020-07-30 19:33:22 +03:00
|
|
|
for (auto & s : SourceExprCommand::getDefaultFlakeAttrPathPrefixes())
|
|
|
|
res.push_back(s);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-02-05 19:16:17 +02:00
|
|
|
void run(ref<Store> store, ref<InstallableValue> installable) override
|
2020-07-30 19:33:22 +03:00
|
|
|
{
|
|
|
|
auto evalState = getEvalState();
|
|
|
|
|
2022-02-14 21:39:44 +02:00
|
|
|
auto val = installable->toValue(*evalState).first;
|
2021-10-31 19:19:53 +02:00
|
|
|
|
2023-01-11 09:00:44 +02:00
|
|
|
auto [bundlerFlakeRef, bundlerName, extendedOutputsSpec] = parseFlakeRefWithFragmentAndExtendedOutputsSpec(bundler, absPath("."));
|
2020-07-30 19:33:22 +03:00
|
|
|
const flake::LockFlags lockFlags{ .writeLockFile = false };
|
2022-02-14 21:39:44 +02:00
|
|
|
InstallableFlake bundler{this,
|
2023-08-16 19:29:23 +03:00
|
|
|
evalState, std::move(bundlerFlakeRef), bundlerName, std::move(extendedOutputsSpec),
|
2022-02-11 19:11:08 +02:00
|
|
|
{"bundlers." + settings.thisSystem.get() + ".default",
|
|
|
|
"defaultBundler." + settings.thisSystem.get()
|
|
|
|
},
|
2022-02-14 21:39:44 +02:00
|
|
|
{"bundlers." + settings.thisSystem.get() + "."},
|
|
|
|
lockFlags
|
|
|
|
};
|
2020-07-30 19:33:22 +03:00
|
|
|
|
|
|
|
auto vRes = evalState->allocValue();
|
2022-01-21 18:43:11 +02:00
|
|
|
evalState->callFunction(*bundler.toValue(*evalState).first, *val, *vRes, noPos);
|
2020-07-30 19:33:22 +03:00
|
|
|
|
|
|
|
if (!evalState->isDerivation(*vRes))
|
2020-07-30 23:03:57 +03:00
|
|
|
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2020-09-18 14:10:42 +03:00
|
|
|
auto attr1 = vRes->attrs->get(evalState->sDrvPath);
|
2020-07-30 23:18:48 +03:00
|
|
|
if (!attr1)
|
|
|
|
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
|
2020-07-30 19:33:22 +03: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 03:31:10 +02:00
|
|
|
NixStringContext context2;
|
2023-01-19 14:23:04 +02:00
|
|
|
auto drvPath = evalState->coerceToStorePath(attr1->pos, *attr1->value, context2, "");
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2020-09-18 14:10:42 +03:00
|
|
|
auto attr2 = vRes->attrs->get(evalState->sOutPath);
|
2020-07-30 23:18:48 +03:00
|
|
|
if (!attr2)
|
2020-07-30 23:03:57 +03:00
|
|
|
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2023-01-19 14:23:04 +02:00
|
|
|
auto outPath = evalState->coerceToStorePath(attr2->pos, *attr2->value, context2, "");
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2023-01-11 23:32:30 +02: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-16 00:39:04 +02:00
|
|
|
.drvPath = makeConstantStorePathRef(drvPath),
|
2023-01-11 23:32:30 +02:00
|
|
|
.outputs = OutputsSpec::All { },
|
|
|
|
},
|
|
|
|
});
|
2020-07-30 19:33:22 +03:00
|
|
|
|
|
|
|
auto outPathS = store->printStorePath(outPath);
|
|
|
|
|
2022-01-21 18:43:11 +02:00
|
|
|
if (!outLink) {
|
2022-03-04 20:47:32 +02:00
|
|
|
auto * attr = vRes->attrs->get(evalState->sName);
|
|
|
|
if (!attr)
|
|
|
|
throw Error("attribute 'name' missing");
|
2023-01-19 14:23:04 +02:00
|
|
|
outLink = evalState->forceStringNoCtx(*attr->value, attr->pos, "");
|
2022-01-21 18:43:11 +02:00
|
|
|
}
|
2020-07-30 19:33:22 +03:00
|
|
|
|
2022-01-21 18:43:11 +02:00
|
|
|
// TODO: will crash if not a localFSStore?
|
2020-09-03 12:26:36 +03:00
|
|
|
store.dynamic_pointer_cast<LocalFSStore>()->addPermRoot(outPath, absPath(*outLink));
|
2020-07-30 19:33:22 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-30 23:03:57 +03:00
|
|
|
static auto r2 = registerCommand<CmdBundle>("bundle");
|