From 9cd3721adfefef2e900e4f3009d4c37f4f12aa9c Mon Sep 17 00:00:00 2001 From: traxys Date: Thu, 22 Feb 2024 08:22:21 +0100 Subject: [PATCH] standalone: Allow to extend a standalone derivation with a new module (#1142) This adds the `nixvimExtend` attribute to the generated standalone derivation, this attribute takes a module as an argument and returns a new standalone derivation with the initial module & the extended module merged together. --- docs/man/default.nix | 1 + docs/mdbook/SUMMARY.md | 1 + docs/user-guide/extending-config.md | 26 ++++++++++++++ flake-modules/tests.nix | 4 +++ tests/extend.nix | 28 +++++++++++++++ wrappers/standalone.nix | 55 ++++++++++++++++------------- 6 files changed, 91 insertions(+), 24 deletions(-) create mode 100644 docs/user-guide/extending-config.md create mode 100644 tests/extend.nix diff --git a/docs/man/default.nix b/docs/man/default.nix index 7affebcf..42ee9cea 100644 --- a/docs/man/default.nix +++ b/docs/man/default.nix @@ -34,6 +34,7 @@ cat \ ${./nixvim-header-start.5} \ ${mkMDSection ../user-guide/helpers.md} \ + ${mkMDSection ../user-guide/extending-config.md} \ ${mkMDSection ../user-guide/faq.md} \ ${./nixvim-header-end.5} \ >$out/nixvim-header.5 diff --git a/docs/mdbook/SUMMARY.md b/docs/mdbook/SUMMARY.md index a1300eab..c8c9bac7 100644 --- a/docs/mdbook/SUMMARY.md +++ b/docs/mdbook/SUMMARY.md @@ -4,6 +4,7 @@ - [Installation](./user-guide/install.md) - [Helpers](./user-guide/helpers.md) +- [Extending a standalone configuration](./user-guide/extending-config.md) - [FAQ](./user-guide/faq.md) # Options diff --git a/docs/user-guide/extending-config.md b/docs/user-guide/extending-config.md new file mode 100644 index 00000000..01686f77 --- /dev/null +++ b/docs/user-guide/extending-config.md @@ -0,0 +1,26 @@ +# Extending a standalone configuration + +Given a `nvim` derivation obtained from `makeNixvim` or `makeNivxmiWithModule` it is possible to create a new derivation with additional options. + +This is done through the `nvim.nixvimExtend` function. This function takes a NixOS module that is going to be merged with the currently set options. + +This attribute is recursive, meaning that it can be applied an arbitrary number of times. + +## Example + +```nix +{makeNixvimWithModule}: let + first = makeNixvimWithModule { + module = { + extraConfigLua = "-- first stage"; + }; + }; + + second = first.nixvimExtend {extraConfigLua = "-- second stage";}; + + third = second.nixvimExtend {extraConfigLua = "-- third stage";}; +in + third +``` + +This will generate a `init.lua` that will contain the three comments from each stages. diff --git a/flake-modules/tests.nix b/flake-modules/tests.nix index c3ad6995..ab255f08 100644 --- a/flake-modules/tests.nix +++ b/flake-modules/tests.nix @@ -25,6 +25,10 @@ inherit makeNixvimWithModule; }; + extend = import ../tests/extend.nix { + inherit pkgs makeNixvimWithModule; + }; + enable-except-in-tests = import ../tests/enable-except-in-tests.nix { inherit pkgs makeNixvimWithModule; inherit (self.lib.${system}.check) mkTestDerivationFromNixvimModule; diff --git a/tests/extend.nix b/tests/extend.nix new file mode 100644 index 00000000..65931556 --- /dev/null +++ b/tests/extend.nix @@ -0,0 +1,28 @@ +{ + makeNixvimWithModule, + pkgs, +}: let + firstStage = makeNixvimWithModule { + module = { + extraConfigLua = "-- first stage"; + }; + }; + + secondStage = firstStage.nixvimExtend {extraConfigLua = "-- second stage";}; + + generated = secondStage.nixvimExtend {extraConfigLua = "-- third stage";}; +in + pkgs.runCommand "extend-test" { + printConfig = "${generated}/bin/nixvim-print-init"; + } '' + config=$($printConfig) + for stage in "first" "second" "third"; do + if ! "$printConfig" | grep -q -- "-- $stage stage"; then + echo "Missing $stage stage in config" + echo "$config" + exit 1 + fi + done + + touch $out + '' diff --git a/wrappers/standalone.nix b/wrappers/standalone.nix index f169259b..09eb487f 100644 --- a/wrappers/standalone.nix +++ b/wrappers/standalone.nix @@ -16,19 +16,20 @@ default_pkgs: { config = {}; }; - eval = lib.evalModules { - modules = - [ - module - {wrapRc = true;} - ] - ++ shared.topLevelModules; - specialArgs = - { - inherit helpers; - } - // extraSpecialArgs; - }; + mkEval = mod: + lib.evalModules { + modules = + [ + mod + {wrapRc = true;} + ] + ++ shared.topLevelModules; + specialArgs = + { + inherit helpers; + } + // extraSpecialArgs; + }; handleAssertions = config: let failedAssertions = map (x: x.message) (lib.filter (x: !x.assertion) config.assertions); @@ -37,15 +38,21 @@ default_pkgs: { then throw "\nFailed assertions:\n${builtins.concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}" else lib.showWarnings config.warnings config; - config = handleAssertions eval.config; + mkNvim = mod: let + config = handleAssertions (mkEval mod).config; + in + (pkgs.symlinkJoin { + name = "nixvim"; + paths = + [ + config.finalPackage + config.printInitPackage + ] + ++ pkgs.lib.optional config.enableMan self.packages.${pkgs.system}.man-docs; + meta.mainProgram = "nvim"; + }) + // { + nixvimExtend = extension: mkNvim {imports = [mod extension];}; + }; in - pkgs.symlinkJoin { - name = "nixvim"; - paths = - [ - config.finalPackage - config.printInitPackage - ] - ++ pkgs.lib.optional config.enableMan self.packages.${pkgs.system}.man-docs; - meta.mainProgram = "nvim"; - } + mkNvim module