Skip to content

Commit c4ad4d0

Browse files
committed
modules/nixpkgs: add overlays option
Based on the `nixpkgs.overlays` option available in NixOS, allows users to further customize the `pkgs` instance used when evaluating nixvim. The standard module tests are now provided a few extra module args to enable a test where we instantiate a custom nixpkgs instance.
1 parent b9d17d5 commit c4ad4d0

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

modules/top-level/nixpkgs.nix

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,76 @@ in
5555
> Use this option with care.
5656
'';
5757
};
58+
59+
overlays = lib.mkOption {
60+
type =
61+
let
62+
overlayType = lib.mkOptionType {
63+
name = "nixpkgs-overlay";
64+
description = "nixpkgs overlay";
65+
check = lib.isFunction;
66+
merge = lib.mergeOneOption;
67+
};
68+
in
69+
lib.types.listOf overlayType;
70+
default = [ ];
71+
# First example from https://nixos.org/manual/nixpkgs/unstable/#what-if-your-favourite-vim-plugin-isnt-already-packaged
72+
# Second example from https://github.com/nix-community/nixvim/pull/2430#discussion_r1805700738
73+
# Third example from https://github.com/atimofeev/nixos-config/blob/0b1c1c47c4359d6a2aa9a5eeecb32fa89ad08c88/overlays/neovim-unwrapped.nix
74+
example = lib.literalExpression ''
75+
[
76+
77+
# Add a vim plugin that isn't packaged in nixpkgs
78+
(final: prev: {
79+
easygrep = final.vimUtils.buildVimPlugin {
80+
name = "vim-easygrep";
81+
src = final.fetchFromGitHub {
82+
owner = "dkprice";
83+
repo = "vim-easygrep";
84+
rev = "d0c36a77cc63c22648e792796b1815b44164653a";
85+
hash = "sha256-bL33/S+caNmEYGcMLNCanFZyEYUOUmSsedCVBn4tV3g=";
86+
};
87+
};
88+
})
89+
90+
# Override neovim-unwrapped with one from a flake input
91+
# Using `stdenv.hostPlatform` to access `system`
92+
(final: prev: {
93+
neovim-unwrapped =
94+
inputs.neovim-nightly-overlay.packages.''${final.stdenv.hostPlatform.system}.default;
95+
})
96+
97+
# Override neovim-unwrapped to tweak its desktop entry
98+
(final: prev: {
99+
neovim-unwrapped = prev.neovim-unwrapped.overrideAttrs (old: {
100+
postInstall = old.postInstall or "" + '''
101+
substituteInPlace $out/share/applications/nvim.desktop \
102+
--replace "TryExec=nvim" "" \
103+
--replace "Terminal=true" "Terminal=false" \
104+
--replace "Exec=nvim %F" "Exec=kitty -e nvim %F"
105+
''';
106+
});
107+
})
108+
109+
]
110+
'';
111+
description = ''
112+
List of overlays to apply to Nixpkgs.
113+
This option allows modifying the Nixpkgs package set accessed through the `pkgs` module argument.
114+
115+
For details, see the [Overlays chapter in the Nixpkgs manual](https://nixos.org/manual/nixpkgs/stable/#chap-overlays).
116+
117+
<!-- TODO: Remove -->
118+
Overlays specified using the {option}`nixpkgs.overlays` option will be
119+
applied after the overlays that were already included in `nixpkgs.pkgs`.
120+
121+
<!--
122+
TODO:
123+
If the {option}`nixpkgs.pkgs` option is set, overlays specified using `nixpkgs.overlays`
124+
will be applied after the overlays that were already included in `nixpkgs.pkgs`.
125+
-->
126+
'';
127+
};
58128
};
59129

60130
config =
@@ -64,7 +134,7 @@ in
64134

65135
finalPkgs =
66136
if opt.pkgs.isDefined then
67-
cfg.pkgs
137+
cfg.pkgs.appendOverlays cfg.overlays
68138
else
69139
# TODO: Remove once pkgs can be constructed internally
70140
throw ''

tests/main.nix

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
linkFarm,
88
pkgs,
99
pkgsUnfree,
10+
self,
11+
system,
1012
}:
1113
let
1214
fetchTests = callTest ./fetch-tests.nix { };
@@ -20,6 +22,11 @@ let
2022
module = {
2123
_file = file;
2224
imports = [ module ];
25+
_module.args = {
26+
# Give tests access to the flake
27+
inherit self system;
28+
inherit (self) inputs;
29+
};
2330
};
2431
pkgs = pkgsUnfree;
2532
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
# TODO: expect not setting `nixpkgs.pkgs` to throw
3+
4+
overlays =
5+
{ pkgs, ... }:
6+
{
7+
test.runNvim = false;
8+
9+
nixpkgs.overlays = [
10+
(final: prev: {
11+
foobar = "foobar";
12+
})
13+
];
14+
15+
assertions = [
16+
{
17+
assertion = pkgs.foobar or null == "foobar";
18+
message = ''
19+
Expected `pkgs.foobar` to be "foobar"
20+
'';
21+
}
22+
];
23+
};
24+
25+
# Test that overlays from both `nixpkgs.pkgs` _and_ `nixpkgs.overlays` are applied
26+
stacked_overlays =
27+
{
28+
inputs,
29+
system,
30+
pkgs,
31+
...
32+
}:
33+
{
34+
test.runNvim = false;
35+
36+
nixpkgs.pkgs = import inputs.nixpkgs {
37+
inherit system;
38+
overlays = [
39+
(final: prev: {
40+
foobar = "foobar";
41+
conflict = "a";
42+
})
43+
];
44+
};
45+
46+
nixpkgs.overlays = [
47+
(final: prev: {
48+
hello = "world";
49+
conflict = "b";
50+
})
51+
];
52+
53+
assertions = [
54+
{
55+
assertion = pkgs.foobar or null == "foobar";
56+
message = ''
57+
Expected `pkgs.foobar` to be "foobar"
58+
'';
59+
}
60+
{
61+
assertion = pkgs.hello or null == "world";
62+
message = ''
63+
Expected `pkgs.hello` to be "world"
64+
'';
65+
}
66+
{
67+
assertion = pkgs.conflict or null == "b";
68+
message = ''
69+
Expected `pkgs.conflict` to be "b"
70+
'';
71+
}
72+
];
73+
};
74+
}

0 commit comments

Comments
 (0)