Skip to content

Commit 54d1188

Browse files
committed
lib/deprecation: init with mkDeprecatedSubOptionModule
Similar to `lib.mkRemovedOptionModule` but tweaked to work with sub-options (e.g. settings options). Also uses warnings instead of assertions.
1 parent 66c8592 commit 54d1188

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lib/deprecation.nix

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{ lib, ... }:
2+
with lib;
3+
rec {
4+
# Get a (sub)option by walking the path,
5+
# checking for submodules along the way
6+
getOptionRecursive =
7+
opt: prefix: optionPath:
8+
if optionPath == [ ] then
9+
opt
10+
else if isOption opt then
11+
getOptionRecursive (opt.type.getSubOptions prefix) prefix optionPath
12+
else
13+
let
14+
name = head optionPath;
15+
opt' = getAttr name opt;
16+
prefix' = prefix ++ [ name ];
17+
optionPath' = drop 1 optionPath;
18+
in
19+
getOptionRecursive opt' prefix' optionPath';
20+
21+
# Like mkRemovedOptionModule, but has support for nested sub-options
22+
# and uses warnings instead of assertions.
23+
mkDeprecatedSubOptionModule =
24+
optionPath: replacementInstructions:
25+
{ options, ... }:
26+
{
27+
options = setAttrByPath optionPath (mkOption {
28+
# When (e.g.) `mkAttrs` is used on a submodule, this option will be evaluated.
29+
# Therefore we have to apply _something_ (null) when there's no definition.
30+
apply =
31+
v:
32+
let
33+
# Avoid "option used but not defined" errors
34+
res = builtins.tryEval v;
35+
in
36+
if res.success then res.value else null;
37+
visible = false;
38+
});
39+
config.warnings =
40+
let
41+
opt = getOptionRecursive options [ ] optionPath;
42+
in
43+
optional opt.isDefined ''
44+
The option definition `${showOption optionPath}' in ${showFiles opt.files} is deprecated.
45+
${replacementInstructions}
46+
'';
47+
};
48+
49+
}

lib/helpers.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let
99
nixvimTypes = import ./types.nix { inherit lib nixvimOptions; };
1010
nixvimUtils = import ./utils.nix { inherit lib nixvimTypes _nixvimTests; };
1111
nixvimOptions = import ./options.nix { inherit lib nixvimTypes nixvimUtils; };
12+
nixvimDeprecation = import ./deprecation.nix { inherit lib; };
1213
inherit (import ./to-lua.nix { inherit lib; }) toLuaObject;
1314
in
1415
{
@@ -30,3 +31,4 @@ in
3031
// nixvimUtils
3132
// nixvimOptions
3233
// nixvimBuilders
34+
// nixvimDeprecation

0 commit comments

Comments
 (0)