Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/helpers: add deprecation.mkTransitionOptionModule #1657

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions lib/deprecation-helpers.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{ lib, nixvimUtils, ... }:
{
/**
Produce a NixOS Module that warns users the option path is transitioning from one option to another.

The primary use case is for adding a plugin variant,
with the intention of it using the "primary" option path for that plugin.

It is recommended that after transitioning, `lib.mkAliasOptionModule` is used,
to avoid breaking configs that used the `to` path.

If there's no plan to transition, just use `lib.mkRenamedOptionModule`.

# Example
```nix
mkTransitionOptionModule {
from = [ "plugins" "surround" ];
to = [ "plugins" "surround-vim" ];
future = [ "plugins" "surround-nvim" ];
takeover = "24.11";
}
=> <nixos-module>
```

# Type
```
mkTransitionOptionModule :: AttrSet -> NixosModule
```
*/
mkTransitionOptionModule =
{
# The path `to` used to be found at and `future` will eventually be moved to.
from,
# The option the alias points to, previously found at `from`.
to,
# The new option; will takeover `from` in the future
future,
# The date or release after which `future` will be moved to `from`.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #1657 (comment)

Suggested change
# The date or release after which `future` will be moved to `from`.
# The nixos release after which `future` will be moved to `from`.

?

takeover ? lib.trivial.release,
}:
# Return a module
{
lib,
options,
config,
...
}:
let
inherit (lib)
setAttrByPath
getAttrFromPath
attrByPath
showFiles
showOption
optional
;
opt = getAttrFromPath from options;
toOpt = getAttrFromPath to options;
in
Comment on lines +57 to +59
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should produce useful error messages when used incorrectly:

Suggested change
opt = getAttrFromPath from options;
toOpt = getAttrFromPath to options;
in
get = path: attrByPath path (abort "mkTransitionOptionModule: can't find option `${showOption path}`") options;
opt = get from;
toOpt = get to;
in
# Also assert `future` exists
# get will abort before the assert does, only used to force it to evaluate
assert (get future) != null;

{
options = setAttrByPath from (
lib.mkOption {
description = "Alias of `${showOption to}`.";
apply = attrByPath to (abort "Renaming error: option `${showOption to}' does not exist.") config;
type = toOpt.type or (lib.types.submodule { });
visible = false;
}
);

config = lib.mkMerge [
{
warnings = optional opt.isDefined ''
The option `${showOption from}' defined in ${showFiles opt.files} has been renamed to `${showOption to}'.
This has been done to avoid confusion with the new option `${showOption future}'.

At some point after ${takeover}, `${showOption future}' will be moved to `${showOption from}'.
'';
}
(lib.modules.mkAliasAndWrapDefsWithPriority (setAttrByPath to) opt)
];
};
}
1 change: 1 addition & 0 deletions lib/helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ in
maintainers = import ./maintainers.nix;
keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
deprecation = import ./deprecation-helpers.nix { inherit lib nixvimUtils; };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've scoped this for now. Would you rather it be a top-level helper?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think scoped is nicer

neovim-plugin = import ./neovim-plugin.nix {
inherit
lib
Expand Down