Skip to content

Commit 8daacc6

Browse files
committed
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`).
1 parent b52b1fb commit 8daacc6

File tree

7 files changed

+108
-44
lines changed

7 files changed

+108
-44
lines changed

flake/wrappers.nix

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,26 @@
44
...
55
}:
66
let
7-
inherit (lib.modules) importApply;
87
# Added 2025-05-25; warning shown since 2025-08-01 (25.11)
98
# NOTE: top-level binding of a fully resolved value, to avoid printing multiple times
109
homeManagerModulesWarning = lib.warn "nixvim: flake output `homeManagerModules` has been renamed to `homeModules`." null;
10+
11+
# A base configuration used to evaluate the wrapper modules.
12+
#
13+
# While we don't define a `pkgs` or `hostPlatform` here, which would normally
14+
# lead to eval errors, disabling option-declaration checking gives us enough
15+
# laziness to evaluate the options we need.
16+
#
17+
# The `_module.check` module has a key, so we can disable it later in the
18+
# platform wrapper modules.
19+
configuration = self.lib.evalNixvim {
20+
modules = [
21+
{
22+
key = "<internal:nixvim-nocheck-base-eval>";
23+
config._module.check = false;
24+
}
25+
];
26+
};
1127
in
1228
{
1329
perSystem =
@@ -27,17 +43,17 @@ in
2743

2844
flake = {
2945
nixosModules = {
30-
nixvim = importApply ../wrappers/nixos.nix self;
46+
nixvim = configuration.config.build.nixosModule;
3147
default = self.nixosModules.nixvim;
3248
};
3349
# Alias for backward compatibility
3450
homeManagerModules = lib.mapAttrs (_: lib.seq homeManagerModulesWarning) self.homeModules;
3551
homeModules = {
36-
nixvim = importApply ../wrappers/hm.nix self;
52+
nixvim = configuration.config.build.homeModule;
3753
default = self.homeModules.nixvim;
3854
};
3955
nixDarwinModules = {
40-
nixvim = importApply ../wrappers/darwin.nix self;
56+
nixvim = configuration.config.build.nixDarwinModule;
4157
default = self.nixDarwinModules.nixvim;
4258
};
4359
};

modules/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
./output.nix
2525
./performance.nix
2626
./plugins.nix
27+
./wrappers.nix
2728
];
2829
}

modules/wrappers.nix

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
lib,
3+
extendModules,
4+
config,
5+
...
6+
}:
7+
{
8+
options.build = {
9+
nixosModule = lib.mkOption {
10+
type = lib.types.deferredModule;
11+
description = "A NixOS module that installs this Nixvim configuration.";
12+
readOnly = true;
13+
};
14+
homeModule = lib.mkOption {
15+
type = lib.types.deferredModule;
16+
description = "A Home Manager module that installs this Nixvim configuration.";
17+
readOnly = true;
18+
};
19+
nixDarwinModule = lib.mkOption {
20+
type = lib.types.deferredModule;
21+
description = "A nix-darwin module that installs this Nixvim configuration.";
22+
readOnly = true;
23+
};
24+
};
25+
26+
config.build = {
27+
nixosModule = lib.modules.importApply ../wrappers/nixos.nix {
28+
self = config.flake;
29+
inherit extendModules;
30+
};
31+
homeModule = lib.modules.importApply ../wrappers/hm.nix {
32+
self = config.flake;
33+
inherit extendModules;
34+
};
35+
nixDarwinModule = lib.modules.importApply ../wrappers/darwin.nix {
36+
self = config.flake;
37+
inherit extendModules;
38+
};
39+
};
40+
}

wrappers/_shared.nix

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
# The nixvim flake
33
self,
4-
# Extra args for the `evalNixvim` call that produces the type for `programs.nixvim`
5-
evalArgs ? { },
4+
# Function used to evaluate the `programs.nixvim` configuration
5+
extendModules,
66
# Option path where extraFiles should go
77
filesOpt ? null,
88
# Filepath prefix to apply to extraFiles
@@ -45,14 +45,11 @@ let
4545
};
4646
};
4747

48-
nixvimConfiguration = config.lib.nixvim.modules.evalNixvim (
49-
evalArgs
50-
// {
51-
modules = evalArgs.modules or [ ] ++ [
52-
nixpkgsModule
53-
];
54-
}
55-
);
48+
nixvimConfiguration = extendModules {
49+
modules = [ nixpkgsModule ];
50+
disabledModules = [ "<internal:nixvim-nocheck-base-eval>" ];
51+
};
52+
5653
extraFiles = lib.filter (file: file.enable) (lib.attrValues cfg.extraFiles);
5754
in
5855
{

wrappers/darwin.nix

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
self:
1+
{
2+
self,
3+
extendModules,
4+
}:
25
{
36
config,
47
lib,
@@ -12,19 +15,22 @@ let
1215
importApply
1316
;
1417
cfg = config.programs.nixvim;
15-
evalArgs = {
16-
extraSpecialArgs = {
17-
darwinConfig = config;
18-
};
19-
modules = [
20-
./modules/darwin.nix
21-
];
22-
};
2318
in
2419
{
2520
_file = ./darwin.nix;
2621

27-
imports = [ (importApply ./_shared.nix { inherit self evalArgs; }) ];
22+
imports = [
23+
(importApply ./_shared.nix {
24+
inherit self;
25+
inherit
26+
(extendModules {
27+
specialArgs.darwinConfig = config;
28+
modules = [ ./modules/darwin.nix ];
29+
})
30+
extendModules
31+
;
32+
})
33+
];
2834

2935
config = mkIf cfg.enable {
3036
environment.systemPackages = [

wrappers/hm.nix

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
self:
1+
{
2+
self,
3+
extendModules,
4+
}:
25
{
36
config,
47
lib,
@@ -9,21 +12,20 @@ let
912
mkIf
1013
;
1114
cfg = config.programs.nixvim;
12-
evalArgs = {
13-
extraSpecialArgs = {
14-
hmConfig = config;
15-
};
16-
modules = [
17-
./modules/hm.nix
18-
];
19-
};
2015
in
2116
{
2217
_file = ./hm.nix;
2318

2419
imports = [
2520
(import ./_shared.nix {
26-
inherit self evalArgs;
21+
inherit self;
22+
inherit
23+
(extendModules {
24+
specialArgs.hmConfig = config;
25+
modules = [ ./modules/hm.nix ];
26+
})
27+
extendModules
28+
;
2729
filesOpt = [
2830
"xdg"
2931
"configFile"

wrappers/nixos.nix

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
self:
1+
{
2+
self,
3+
extendModules,
4+
}:
25
{
36
config,
47
lib,
@@ -9,21 +12,20 @@ let
912
mkIf
1013
;
1114
cfg = config.programs.nixvim;
12-
evalArgs = {
13-
extraSpecialArgs = {
14-
nixosConfig = config;
15-
};
16-
modules = [
17-
./modules/nixos.nix
18-
];
19-
};
2015
in
2116
{
2217
_file = ./nixos.nix;
2318

2419
imports = [
2520
(import ./_shared.nix {
26-
inherit self evalArgs;
21+
inherit self;
22+
inherit
23+
(extendModules {
24+
specialArgs.nixosConfig = config;
25+
modules = [ ./modules/nixos.nix ];
26+
})
27+
extendModules
28+
;
2729
filesOpt = [
2830
"environment"
2931
"etc"

0 commit comments

Comments
 (0)