From 5a5f60269bd5e530de1d7355385cffb4e5f882e4 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Wed, 3 Dec 2025 08:28:35 +0000 Subject: [PATCH] wrappers: expose platform wrapper modules via `build.*Module` options Expose the platform wrapper modules as the Nixvim configuration options `build.nixosModule`, `build.homeModule`, and `build.nixDarwinModule`. This makes it possible to reuse a single Nixvim configuration across NixOS, Home Manager, and nix-darwin without re-importing modules into `programs.nixvim` manually. Evaluating these wrapper modules requires a "bare" Nixvim configuration; one that does not define `pkgs` or `nixpkgs.hostPlatform`. Such a configuration would normally fail to evaluate, but disabling `_module.check` provides a sufficiently lazy evaluation to access the wrapper options. To prevent the `_module.check = false` module from leaking into user configs, it has a unique module key and gets disabled inside the wrapper modules (`wrappers/_shared.nix`). --- flake/wrappers.nix | 24 ++++++++++++++++++++---- modules/default.nix | 1 + modules/wrappers.nix | 40 ++++++++++++++++++++++++++++++++++++++++ wrappers/_shared.nix | 19 +++++++++---------- wrappers/darwin.nix | 26 ++++++++++++++++---------- wrappers/hm.nix | 22 ++++++++++++---------- wrappers/nixos.nix | 22 ++++++++++++---------- 7 files changed, 110 insertions(+), 44 deletions(-) create mode 100644 modules/wrappers.nix diff --git a/flake/wrappers.nix b/flake/wrappers.nix index 2d2d437bcd..7cf9093bab 100644 --- a/flake/wrappers.nix +++ b/flake/wrappers.nix @@ -4,10 +4,26 @@ ... }: let - inherit (lib.modules) importApply; # Added 2025-05-25; warning shown since 2025-08-01 (25.11) # NOTE: top-level binding of a fully resolved value, to avoid printing multiple times homeManagerModulesWarning = lib.warn "nixvim: flake output `homeManagerModules` has been renamed to `homeModules`." null; + + # A base configuration used to evaluate the wrapper modules. + # + # While we don't define a `pkgs` or `hostPlatform` here, which would normally + # lead to eval errors, disabling option-declaration checking gives us enough + # laziness to evaluate the options we need. + # + # The `_module.check` module has a key, so we can disable it later in the + # platform wrapper modules. + configuration = self.lib.evalNixvim { + modules = [ + { + key = ""; + config._module.check = false; + } + ]; + }; in { perSystem = @@ -27,17 +43,17 @@ in flake = { nixosModules = { - nixvim = importApply ../wrappers/nixos.nix self; + nixvim = configuration.config.build.nixosModule; default = self.nixosModules.nixvim; }; # Alias for backward compatibility homeManagerModules = lib.mapAttrs (_: lib.seq homeManagerModulesWarning) self.homeModules; homeModules = { - nixvim = importApply ../wrappers/hm.nix self; + nixvim = configuration.config.build.homeModule; default = self.homeModules.nixvim; }; nixDarwinModules = { - nixvim = importApply ../wrappers/darwin.nix self; + nixvim = configuration.config.build.nixDarwinModule; default = self.nixDarwinModules.nixvim; }; }; diff --git a/modules/default.nix b/modules/default.nix index 55a97a1cd9..9bbdff82bb 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -24,5 +24,6 @@ ./output.nix ./performance.nix ./plugins.nix + ./wrappers.nix ]; } diff --git a/modules/wrappers.nix b/modules/wrappers.nix new file mode 100644 index 0000000000..3ae15c69ec --- /dev/null +++ b/modules/wrappers.nix @@ -0,0 +1,40 @@ +{ + lib, + extendModules, + config, + ... +}: +{ + options.build = { + nixosModule = lib.mkOption { + type = lib.types.deferredModule; + description = "A NixOS module that installs this Nixvim configuration."; + readOnly = true; + }; + homeModule = lib.mkOption { + type = lib.types.deferredModule; + description = "A Home Manager module that installs this Nixvim configuration."; + readOnly = true; + }; + nixDarwinModule = lib.mkOption { + type = lib.types.deferredModule; + description = "A nix-darwin module that installs this Nixvim configuration."; + readOnly = true; + }; + }; + + config.build = { + nixosModule = lib.modules.importApply ../wrappers/nixos.nix { + self = config.flake; + inherit extendModules; + }; + homeModule = lib.modules.importApply ../wrappers/hm.nix { + self = config.flake; + inherit extendModules; + }; + nixDarwinModule = lib.modules.importApply ../wrappers/darwin.nix { + self = config.flake; + inherit extendModules; + }; + }; +} diff --git a/wrappers/_shared.nix b/wrappers/_shared.nix index 78d41498c1..efae482ec9 100644 --- a/wrappers/_shared.nix +++ b/wrappers/_shared.nix @@ -1,8 +1,8 @@ { # The nixvim flake self, - # Extra args for the `evalNixvim` call that produces the type for `programs.nixvim` - evalArgs ? { }, + # Function used to evaluate the `programs.nixvim` configuration + extendModules, # Option path where extraFiles should go filesOpt ? null, # Filepath prefix to apply to extraFiles @@ -45,14 +45,13 @@ let }; }; - nixvimConfiguration = config.lib.nixvim.modules.evalNixvim ( - evalArgs - // { - modules = evalArgs.modules or [ ] ++ [ - nixpkgsModule - ]; - } - ); + nixvimConfiguration = extendModules { + modules = [ + nixpkgsModule + { disabledModules = [ "" ]; } + ]; + }; + extraFiles = lib.filter (file: file.enable) (lib.attrValues cfg.extraFiles); in { diff --git a/wrappers/darwin.nix b/wrappers/darwin.nix index 2c1082fa3b..691b1a3cc5 100644 --- a/wrappers/darwin.nix +++ b/wrappers/darwin.nix @@ -1,4 +1,7 @@ -self: +{ + self, + extendModules, +}: { config, lib, @@ -12,19 +15,22 @@ let importApply ; cfg = config.programs.nixvim; - evalArgs = { - extraSpecialArgs = { - darwinConfig = config; - }; - modules = [ - ./modules/darwin.nix - ]; - }; in { _file = ./darwin.nix; - imports = [ (importApply ./_shared.nix { inherit self evalArgs; }) ]; + imports = [ + (importApply ./_shared.nix { + inherit self; + inherit + (extendModules { + specialArgs.darwinConfig = config; + modules = [ ./modules/darwin.nix ]; + }) + extendModules + ; + }) + ]; config = mkIf cfg.enable { environment.systemPackages = [ diff --git a/wrappers/hm.nix b/wrappers/hm.nix index 4786773846..9e4a4ab725 100644 --- a/wrappers/hm.nix +++ b/wrappers/hm.nix @@ -1,4 +1,7 @@ -self: +{ + self, + extendModules, +}: { config, lib, @@ -9,21 +12,20 @@ let mkIf ; cfg = config.programs.nixvim; - evalArgs = { - extraSpecialArgs = { - hmConfig = config; - }; - modules = [ - ./modules/hm.nix - ]; - }; in { _file = ./hm.nix; imports = [ (import ./_shared.nix { - inherit self evalArgs; + inherit self; + inherit + (extendModules { + specialArgs.hmConfig = config; + modules = [ ./modules/hm.nix ]; + }) + extendModules + ; filesOpt = [ "xdg" "configFile" diff --git a/wrappers/nixos.nix b/wrappers/nixos.nix index e82b7fdcf4..1fc63c6f56 100644 --- a/wrappers/nixos.nix +++ b/wrappers/nixos.nix @@ -1,4 +1,7 @@ -self: +{ + self, + extendModules, +}: { config, lib, @@ -9,21 +12,20 @@ let mkIf ; cfg = config.programs.nixvim; - evalArgs = { - extraSpecialArgs = { - nixosConfig = config; - }; - modules = [ - ./modules/nixos.nix - ]; - }; in { _file = ./nixos.nix; imports = [ (import ./_shared.nix { - inherit self evalArgs; + inherit self; + inherit + (extendModules { + specialArgs.nixosConfig = config; + modules = [ ./modules/nixos.nix ]; + }) + extendModules + ; filesOpt = [ "environment" "etc"