From c94db0535c4ddd32957fd80d0713797ec73cc70b Mon Sep 17 00:00:00 2001 From: Tom Bereknyei Date: Fri, 21 Jan 2022 11:43:11 -0500 Subject: [PATCH] Refactor bundler API Bundlers now expect to be located at bundlers.. and are a function from derivations to derivations. --- src/nix/bundle.cc | 32 ++++++++++---------------------- src/nix/bundle.md | 41 ++++++++++++++++++++++++++++++++++++----- src/nix/flake.cc | 28 +++++++++++++++++++--------- 3 files changed, 65 insertions(+), 36 deletions(-) diff --git a/src/nix/bundle.cc b/src/nix/bundle.cc index 9fc2656bb..445db5d5f 100644 --- a/src/nix/bundle.cc +++ b/src/nix/bundle.cc @@ -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()->addPermRoot(outPath, absPath(*outLink)); } }; diff --git a/src/nix/bundle.md b/src/nix/bundle.md index 5e2298376..317b7000b 100644 --- a/src/nix/bundle.md +++ b/src/nix/bundle.md @@ -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.` + +If an attribute *name* is given, `nix run` tries the following flake +output attributes: + +* `bundler..` + +# Bundlers + +An bundlers 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 a derivation and returns a derivation. )"" diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 97f4d911c..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->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