Skip to content

plugins/alpha: migrate to mkNeovimPlugin #3040

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
285 changes: 157 additions & 128 deletions plugins/by-name/alpha/default.nix
Original file line number Diff line number Diff line change
@@ -1,86 +1,74 @@
{
lib,
config,
options,
pkgs,
...
}:
{ lib, ... }:
let
inherit (lib) types mkOption;

cfg = config.plugins.alpha;

sectionType = types.submodule {
freeformType = with types; attrsOf anything;
options = {
type = mkOption {
type = types.enum [
"button"
"group"
"padding"
"text"
"terminal"
];
description = "Type of section";
};

val = lib.nixvim.mkNullOrOption (
with types;
nullOr (oneOf [

# "button", "text"
(maybeRaw str)
# "padding"
int
(listOf (
either
# "text" (list of strings)
str
# "group"
(attrsOf anything)
))
])
) "Value for section";

opts = mkOption {
type = with types; attrsOf anything;
default = { };
description = "Additional options for the section";
};
};
};
inherit (lib) mkOption types;
inherit (lib.nixvim)
defaultNullOpts
mkAssertions
mkRaw
nestedLiteralLua
toLuaObject
;
in
{
options = {
plugins.alpha = {
enable = lib.mkEnableOption "alpha-nvim";

package = lib.mkPackageOption pkgs "alpha-nvim" {
default = [
"vimPlugins"
"alpha-nvim"
];
};

# TODO: deprecated 2024-08-29 remove after 24.11
iconsEnabled = mkOption {
type = types.bool;
description = "Toggle icon support. Installs nvim-web-devicons.";
visible = false;
};
lib.nixvim.plugins.mkNeovimPlugin {
name = "alpha";
packPathName = "alpha-nvim";
package = "alpha-nvim";

maintainers = [ lib.maintainers.HeitorAugustoLN ];

callSetup = false;
Copy link
Member

Choose a reason for hiding this comment

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

Typically we have an explanatory comment.


settingsOptions = {
layout =
let
sectionType = types.submodule {
freeformType = with types; attrsOf anything;
options = {
type = mkOption {
type = types.enum [
"button"
"group"
"padding"
"text"
"terminal"
];
example = "button";
description = ''
The type of the section.
'';
};

theme = mkOption {
type = with types; nullOr (maybeRaw str);
apply = v: if lib.isString v then lib.nixvim.mkRaw "require'alpha.themes.${v}'.config" else v;
default = null;
example = "dashboard";
description = "You can directly use a pre-defined theme.";
};
val = defaultNullOpts.mkNullableWithRaw' {
type =
with types;
oneOf [
# button || text
(maybeRaw str)
# padding
int
(listOf (
either str # text (list of strings)
(attrsOf anything) # group
))
];
example = "Some text";
description = ''
The value for the section.
'';
};

layout = mkOption {
type = with types; either (maybeRaw str) (listOf sectionType);
default = [ ];
description = "List of sections to layout for the dashboard";
opts = mkOption {
type = with types; attrsOf anything;
default = { };
description = ''
Additional options for the section.
'';
};
};
};
in
defaultNullOpts.mkNullableWithRaw' {
type = with types; either str (listOf sectionType);
example = [
{
type = "padding";
Expand Down Expand Up @@ -135,77 +123,118 @@ in
};
}
];
description = ''
List of sections to layout for the dashboard
'';
};

opts = lib.nixvim.mkNullOrOption (with types; attrsOf anything) ''
opts = defaultNullOpts.mkAttrsOf' {
type = types.anything;
description = ''
Optional global options.
'';
};
};

config =
let
layoutDefined = cfg.layout != [ ];
themeDefined = cfg.theme != null;

opt = options.plugins.alpha;
in
lib.mkIf cfg.enable {

# TODO: added 2024-09-20 remove after 24.11
warnings = lib.nixvim.mkWarnings "plugins.alpha" {
when = opt.iconsEnabled.isDefined;
message = ''
The option definition `plugins.alpha.iconsEnabled' in ${lib.showFiles opt.iconsEnabled.files} has been deprecated; please remove it.
'';
};

plugins.web-devicons =
lib.mkIf
(
opt.iconsEnabled.isDefined
&& cfg.iconsEnabled
&& !(
config.plugins.mini.enable
&& config.plugins.mini.modules ? icons
&& config.plugins.mini.mockDevIcons
)
)
settingsExample = {
layout = [
{
type = "padding";
val = 2;
}
{
type = "text";
val = [
"███╗ ██╗██╗██╗ ██╗██╗ ██╗██╗███╗ ███╗"
"████╗ ██║██║╚██╗██╔╝██║ ██║██║████╗ ████║"
"██╔██╗ ██║██║ ╚███╔╝ ██║ ██║██║██╔████╔██║"
"██║╚██╗██║██║ ██╔██╗ ╚██╗ ██╔╝██║██║╚██╔╝██║"
"██║ ╚████║██║██╔╝ ██╗ ╚████╔╝ ██║██║ ╚═╝ ██║"
"╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝ ╚═══╝ ╚═╝╚═╝ ╚═╝"
];
opts = {
position = "center";
hl = "Type";
};
}
{
type = "padding";
val = 2;
}
{
type = "group";
val = [
{
enable = lib.mkOverride 1490 true;
};
type = "button";
val = " New file";
on_press = nestedLiteralLua "function() vim.cmd[[ene]] end";
opts.shortcut = "n";
}
{
type = "button";
val = " Quit Neovim";
on_press = nestedLiteralLua "function() vim.cmd[[qa]] end";
opts.shortcut = "q";
}
];
}
{
type = "padding";
val = 2;
}
{
type = "text";
val = "Inspiring quote here.";
opts = {
position = "center";
hl = "Keyword";
};
}
];
};

extraOptions = {
theme = defaultNullOpts.mkStr' {
description = ''
The theme to use for the dashboard.
'';
example = "dashboard";
apply =
value: if builtins.isString value then mkRaw ''require("alpha.themes.${value}").config'' else value;
};
};

extraPlugins = [ cfg.package ];
optionsRenamedToSettings = [
Copy link
Member

Choose a reason for hiding this comment

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

This should have a comment saying when it was added.

Also, we need to update the test cases. See CI failures: https://buildbot.nix-community.org/#/builders/2885/builds/3656/steps/1/logs/stdio

"opts"
"layout"
];

assertions = lib.nixvim.mkAssertions "plugins.alpha" [
extraConfig =
cfg:
let
layoutDefined = cfg.settings.layout != null;
themeDefined = cfg.theme != null;
in
{
assertions = mkAssertions "plugins.alpha" [
{
assertion = themeDefined || layoutDefined;
assertion = layoutDefined || themeDefined;
message = ''
You have to either set a `theme` or define some sections in `layout`.
'';
}
{
assertion = !(themeDefined && layoutDefined);
assertion = !(themeDefined && toLuaObject cfg.settings != "{}");
message = ''
You can't define both a `theme` and custom options.
Set `plugins.alpha.theme = null` if you want to configure alpha manually using the `layout` option.
'';
}
];

extraConfigLua =
let
setupOptions =
if themeDefined then
cfg.theme
else
(with cfg; {
inherit layout opts;
});
in
''
require('alpha').setup(${lib.nixvim.toLuaObject setupOptions})
require('alpha.term')
'';
plugins.alpha.luaConfig.content = ''
require('alpha').setup(${toLuaObject (if themeDefined then cfg.theme else cfg.settings)});
Copy link
Member

Choose a reason for hiding this comment

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

This feels like it could be confusing to users. I wonder if we should have it so that we do this in the module system?

settings = mkDerivedConfig opts.theme (
  value:
  if builtins.isString value then mkRaw ''require("alpha.themes.${value}").config'' else value
);

However, that'd require refactoring settings to a maybeRaw type.

As a minimum, the weird behaviour of discarding all configured settings if a theme is defined should be documented somewhere.

require('alpha.term')
'';
};
}