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.
This commit is contained in:
traxys 2024-02-22 08:22:21 +01:00 committed by GitHub
parent 66c019d638
commit 9cd3721adf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 91 additions and 24 deletions

View file

@ -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

View file

@ -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

View file

@ -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.

View file

@ -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;

28
tests/extend.nix Normal file
View file

@ -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
''

View file

@ -16,10 +16,11 @@ default_pkgs: {
config = {};
};
eval = lib.evalModules {
mkEval = mod:
lib.evalModules {
modules =
[
module
mod
{wrapRc = true;}
]
++ shared.topLevelModules;
@ -37,9 +38,10 @@ 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 {
(pkgs.symlinkJoin {
name = "nixvim";
paths =
[
@ -48,4 +50,9 @@ in
]
++ pkgs.lib.optional config.enableMan self.packages.${pkgs.system}.man-docs;
meta.mainProgram = "nvim";
}
})
// {
nixvimExtend = extension: mkNvim {imports = [mod extension];};
};
in
mkNvim module