Refactor bundler API

Bundlers now expect to be located at bundlers.<system>.<name> and are a
function from derivations to derivations.
This commit is contained in:
Tom Bereknyei 2022-01-21 11:43:11 -05:00
parent 3be810f5db
commit c94db0535c
3 changed files with 65 additions and 36 deletions

View file

@ -69,39 +69,24 @@ struct CmdBundle : InstallableCommand
{
auto evalState = getEvalState();
auto app = installable->toApp(*evalState).resolve(getEvalStore(), store);
auto [progFlakeRef, progName] = parseFlakeRefWithFragment(installable->what(), absPath("."));
const flake::LockFlags lockFlagsProg{ .writeLockFile = false };
auto programInstallable = InstallableFlake(this,
evalState, std::move(progFlakeRef),
Strings{progName == "" ? "defaultPackage" : progName},
Strings({"packages."+settings.thisSystem.get()+".","legacyPackages."+settings.thisSystem.get()+"."}), lockFlagsProg);
Strings{progName == "" ? "defaultApp" : progName},
Strings(this->getDefaultFlakeAttrPathPrefixes()),
lockFlagsProg);
auto val = programInstallable.toValue(*evalState).first;
auto [bundlerFlakeRef, bundlerName] = parseFlakeRefWithFragment(bundler, absPath("."));
const flake::LockFlags lockFlags{ .writeLockFile = false };
auto bundler = InstallableFlake(this,
evalState, std::move(bundlerFlakeRef),
Strings{bundlerName == "" ? "defaultBundler" : bundlerName},
Strings{bundlerName == "" ? "defaultBundler." + settings.thisSystem.get() : settings.thisSystem.get() + "." + bundlerName},
Strings({"bundlers."}), lockFlags);
auto attrs = evalState->buildBindings(2);
Value & prog = *evalState->allocAttr(*arg, evalState->symbols.create("program"));
evalState->mkAttrs(prog,val->attrs->size());
for (auto &j : *(val->attrs)){
prog.attrs->push_back(j);
}
prog.attrs->sort();
attrs.alloc("system").mkString(settings.thisSystem.get());
auto vRes = evalState->allocValue();
evalState->callFunction(
*bundler.toValue(*evalState).first,
evalState->allocValue()->mkAttrs(attrs),
*vRes, noPos);
evalState->callFunction(*bundler.toValue(*evalState).first, *val, *vRes, noPos);
if (!evalState->isDerivation(*vRes))
throw Error("the bundler '%s' does not produce a derivation", bundler.what());
@ -123,9 +108,12 @@ struct CmdBundle : InstallableCommand
auto outPathS = store->printStorePath(outPath);
if (!outLink)
outLink = baseNameOf(app.program);
if (!outLink) {
auto &attr = vRes->attrs->need(evalState->sName);
outLink = evalState->forceStringNoCtx(*attr.value,*attr.pos);
}
// TODO: will crash if not a localFSStore?
store.dynamic_pointer_cast<LocalFSStore>()->addPermRoot(outPath, absPath(*outLink));
}
};

View file

@ -18,12 +18,20 @@ R""(
nix (Nix) 2.4pre20201215_e3ddffb
```
* Bundle a Hello using a specific bundler:
```console
# nix bundle --bundler github:NixOS/bundlers#toDockerImage nixpkgs#hello
# docker load < hello-2.10.tar.gz
# docker run hello-2.10:latest hello
Hello, world!
```
# Description
`nix bundle` packs the closure of the [Nix app](./nix3-run.md)
*installable* into a single self-extracting executable. See the
[`nix-bundle` homepage](https://github.com/matthewbauer/nix-bundle)
for more details.
`nix bundle`, by default, packs the closure of the [Nix app](./nix3-run.md)
*installable* into a single self-extracting executable. See the [`nix-bundle`
homepage](https://github.com/matthewbauer/nix-bundle) for more details.
> **Note**
>
@ -31,6 +39,29 @@ for more details.
# Bundler definitions
TODO
If no flake output attribute is given, `nix bundle` tries the following
flake output attributes:
* `defaultBundler.<system>`
If an attribute *name* is given, `nix run` tries the following flake
output attributes:
* `bundler.<system>.<name>`
# Bundlers
An bundlers is specified by a flake output attribute named
`bundlers.<system>.<name>` or `defaultBundler.<system>`. It looks like this:
```nix
bundlers.x86_64-linux.identity = drv: drv;
bundlers.x86_64-linux.blender_2_79 = drv: self.packages.x86_64-linux.blender_2_79;
defaultBundler.x86_64-linux = drv: drv;
```
A bundler must be a function that accepts a derivation and returns a derivation.
)""

View file

@ -475,10 +475,7 @@ struct CmdFlakeCheck : FlakeCommand
state->forceValue(v, pos);
if (!v.isLambda())
throw Error("bundler must be a function");
if (!v.lambda.fun->formals ||
!v.lambda.fun->formals->argNames.count(state->symbols.create("program")) ||
!v.lambda.fun->formals->argNames.count(state->symbols.create("system")))
throw Error("bundler must take formal arguments 'program' and 'system'");
// TODO: check types of inputs/outputs?
} catch (Error & e) {
e.addTrace(pos, hintfmt("while checking the template '%s'", attrPath));
reportError(e);
@ -609,14 +606,27 @@ struct CmdFlakeCheck : FlakeCommand
*attr.value, *attr.pos);
}
else if (name == "defaultBundler")
checkBundler(name, vOutput, pos);
else if (name == "defaultBundler") {
state->forceAttrs(vOutput, pos);
for (auto & attr : *vOutput.attrs) {
checkSystemName(attr.name, *attr.pos);
checkBundler(
fmt("%s.%s", name, attr.name),
*attr.value, *attr.pos);
}
}
else if (name == "bundlers") {
state->forceAttrs(vOutput, pos);
for (auto & attr : *vOutput.attrs)
checkBundler(fmt("%s.%s", name, attr.name),
*attr.value, *attr.pos);
for (auto & attr : *vOutput.attrs) {
checkSystemName(attr.name, *attr.pos);
state->forceAttrs(*attr.value, *attr.pos);
for (auto & attr2 : *attr.value->attrs) {
checkBundler(
fmt("%s.%s.%s", name, attr.name, attr2.name),
*attr2.value, *attr2.pos);
}
}
}
else