Merge pull request #5456 from tomberek/bundler_drv

bundle: pass drv attrset instead of path
This commit is contained in:
Eelco Dolstra 2022-02-03 23:40:20 +01:00 committed by GitHub
commit 1aa5994e6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 122 additions and 35 deletions

View file

@ -1,3 +1,9 @@
# Release X.Y (202?-??-??) # Release X.Y (202?-??-??)
* `nix bundle` breaking API change now supports bundlers of the form
`bundler.<system>.<name>= derivation: another-derivation;`. This supports
additional functionality to inspect evaluation information during bundling. A
new [repository](https://github.com/NixOS/bundlers) has various bundlers
implemented.
* `nix store ping` now reports the version of the remote Nix daemon. * `nix store ping` now reports the version of the remote Nix daemon.

View file

@ -51,7 +51,9 @@ struct CmdBundle : InstallableCommand
Strings getDefaultFlakeAttrPaths() override Strings getDefaultFlakeAttrPaths() override
{ {
Strings res{"defaultApp." + settings.thisSystem.get()}; Strings res{
"defaultApp." + settings.thisSystem.get()
};
for (auto & s : SourceExprCommand::getDefaultFlakeAttrPaths()) for (auto & s : SourceExprCommand::getDefaultFlakeAttrPaths())
res.push_back(s); res.push_back(s);
return res; return res;
@ -59,7 +61,10 @@ struct CmdBundle : InstallableCommand
Strings getDefaultFlakeAttrPathPrefixes() override Strings getDefaultFlakeAttrPathPrefixes() override
{ {
Strings res{"apps." + settings.thisSystem.get() + "."}; Strings res{
"apps." + settings.thisSystem.get() + "."
};
for (auto & s : SourceExprCommand::getDefaultFlakeAttrPathPrefixes()) for (auto & s : SourceExprCommand::getDefaultFlakeAttrPathPrefixes())
res.push_back(s); res.push_back(s);
return res; return res;
@ -69,29 +74,24 @@ struct CmdBundle : InstallableCommand
{ {
auto evalState = getEvalState(); 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 == "" ? "defaultApp" : progName},
Strings(this->getDefaultFlakeAttrPathPrefixes()),
lockFlagsProg);
auto val = programInstallable.toValue(*evalState).first;
auto [bundlerFlakeRef, bundlerName] = parseFlakeRefWithFragment(bundler, absPath(".")); auto [bundlerFlakeRef, bundlerName] = parseFlakeRefWithFragment(bundler, absPath("."));
const flake::LockFlags lockFlags{ .writeLockFile = false }; const flake::LockFlags lockFlags{ .writeLockFile = false };
auto bundler = InstallableFlake(this, auto bundler = InstallableFlake(this,
evalState, std::move(bundlerFlakeRef), evalState, std::move(bundlerFlakeRef),
Strings{bundlerName == "" ? "defaultBundler" : bundlerName}, Strings{bundlerName == "" ? "defaultBundler." + settings.thisSystem.get() : settings.thisSystem.get() + "." + bundlerName, bundlerName},
Strings({"bundlers."}), lockFlags); Strings({"","bundlers."}), lockFlags);
auto attrs = evalState->buildBindings(2);
PathSet context;
for (auto & i : app.context)
context.insert("=" + store->printStorePath(i.path));
attrs.alloc("program").mkString(app.program, context);
attrs.alloc("system").mkString(settings.thisSystem.get());
auto vRes = evalState->allocValue(); auto vRes = evalState->allocValue();
evalState->callFunction( evalState->callFunction(*bundler.toValue(*evalState).first, *val, *vRes, noPos);
*bundler.toValue(*evalState).first,
evalState->allocValue()->mkAttrs(attrs),
*vRes, noPos);
if (!evalState->isDerivation(*vRes)) if (!evalState->isDerivation(*vRes))
throw Error("the bundler '%s' does not produce a derivation", bundler.what()); throw Error("the bundler '%s' does not produce a derivation", bundler.what());
@ -113,9 +113,12 @@ struct CmdBundle : InstallableCommand
auto outPathS = store->printStorePath(outPath); auto outPathS = store->printStorePath(outPath);
if (!outLink) if (!outLink) {
outLink = baseNameOf(app.program); 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)); store.dynamic_pointer_cast<LocalFSStore>()->addPermRoot(outPath, absPath(*outLink));
} }
}; };

View file

@ -18,19 +18,51 @@ R""(
nix (Nix) 2.4pre20201215_e3ddffb 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 # Description
`nix bundle` packs the closure of the [Nix app](./nix3-run.md) `nix bundle`, by default, packs the closure of the *installable* into a single
*installable* into a single self-extracting executable. See the self-extracting executable. See the [`bundlers`
[`nix-bundle` homepage](https://github.com/matthewbauer/nix-bundle) homepage](https://github.com/NixOS/bundlers) for more details.
for more details.
> **Note** > **Note**
> >
> This command only works on Linux. > This command only works on Linux.
# Bundler definitions # Flake output attributes
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
A bundler 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 an arbitrary value (typically a
derivation or app definition) and returns a derivation.
)"" )""

View file

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

35
tests/flake-bundler.sh Normal file
View file

@ -0,0 +1,35 @@
source common.sh
clearStore
rm -rf $TEST_HOME/.cache $TEST_HOME/.config $TEST_HOME/.local
cp ./simple.nix ./simple.builder.sh ./config.nix $TEST_HOME
cd $TEST_HOME
cat <<EOF > flake.nix
{
outputs = {self}: {
bundlers.$system.simple = drv:
if drv?type && drv.type == "derivation"
then drv
else self.defaultPackage.$system;
defaultBundler.$system = self.bundlers.$system.simple;
defaultPackage.$system = import ./simple.nix;
defaultApp.$system = {
type = "app";
program = "\${import ./simple.nix}/hello";
};
};
}
EOF
nix build .#
nix bundle --bundler .# .#
nix bundle --bundler .#defaultBundler.$system .#defaultPackage.$system
nix bundle --bundler .#bundlers.$system.simple .#defaultPackage.$system
nix bundle --bundler .#defaultBundler.$system .#defaultApp.$system
nix bundle --bundler .#bundlers.$system.simple .#defaultApp.$system
clearStore

View file

@ -48,6 +48,7 @@ nix_tests = \
flakes.sh \ flakes.sh \
flake-local-settings.sh \ flake-local-settings.sh \
flake-searching.sh \ flake-searching.sh \
flake-bundler.sh \
build.sh \ build.sh \
repl.sh ca/repl.sh \ repl.sh ca/repl.sh \
ca/build.sh \ ca/build.sh \