diff --git a/doc/manual/src/release-notes/rl-next.md b/doc/manual/src/release-notes/rl-next.md index d795e7b11..ad8c27dbc 100644 --- a/doc/manual/src/release-notes/rl-next.md +++ b/doc/manual/src/release-notes/rl-next.md @@ -1,3 +1,9 @@ # Release X.Y (202?-??-??) +* `nix bundle` breaking API change now supports bundlers of the form + `bundler..= 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. diff --git a/src/nix/bundle.cc b/src/nix/bundle.cc index adb5b3e73..113ceca33 100644 --- a/src/nix/bundle.cc +++ b/src/nix/bundle.cc @@ -51,7 +51,9 @@ struct CmdBundle : InstallableCommand Strings getDefaultFlakeAttrPaths() override { - Strings res{"defaultApp." + settings.thisSystem.get()}; + Strings res{ + "defaultApp." + settings.thisSystem.get() + }; for (auto & s : SourceExprCommand::getDefaultFlakeAttrPaths()) res.push_back(s); return res; @@ -59,7 +61,10 @@ struct CmdBundle : InstallableCommand Strings getDefaultFlakeAttrPathPrefixes() override { - Strings res{"apps." + settings.thisSystem.get() + "."}; + Strings res{ + "apps." + settings.thisSystem.get() + "." + + }; for (auto & s : SourceExprCommand::getDefaultFlakeAttrPathPrefixes()) res.push_back(s); return res; @@ -69,29 +74,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 == "" ? "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({"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()); + Strings{bundlerName == "" ? "defaultBundler." + settings.thisSystem.get() : settings.thisSystem.get() + "." + bundlerName, bundlerName}, + Strings({"","bundlers."}), lockFlags); 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()); @@ -113,9 +113,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()->addPermRoot(outPath, absPath(*outLink)); } }; diff --git a/src/nix/bundle.md b/src/nix/bundle.md index 5e2298376..a5186a996 100644 --- a/src/nix/bundle.md +++ b/src/nix/bundle.md @@ -18,19 +18,51 @@ 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 *installable* into a single +self-extracting executable. See the [`bundlers` +homepage](https://github.com/NixOS/bundlers) for more details. > **Note** > > 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.` + +If an attribute *name* is given, `nix run` tries the following flake +output attributes: + +* `bundler..` + +# Bundlers + +A bundler is specified by a flake output attribute named +`bundlers..` or `defaultBundler.`. 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. )"" diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 101d38c6c..19bf9ab51 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -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->has(state->symbols.create("program")) || - !v.lambda.fun->formals->has(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 diff --git a/tests/flake-bundler.sh b/tests/flake-bundler.sh new file mode 100644 index 000000000..699920f60 --- /dev/null +++ b/tests/flake-bundler.sh @@ -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 < 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 + diff --git a/tests/local.mk b/tests/local.mk index 2ec9baa12..47717fa67 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -48,6 +48,7 @@ nix_tests = \ flakes.sh \ flake-local-settings.sh \ flake-searching.sh \ + flake-bundler.sh \ build.sh \ repl.sh ca/repl.sh \ ca/build.sh \