From 799c811ac53ef9820dd007b6ddf33390964c6bef Mon Sep 17 00:00:00 2001 From: Flameopathic <64027365+Flameopathic@users.noreply.github.com> Date: Wed, 24 Sep 2025 02:25:45 -0500 Subject: [PATCH] {neovim,nixvim,nvf}: use mkTarget (#1535) Link: https://github.com/nix-community/stylix/pull/1535 Tested-by: Adam M. Szalkowski Approved-by: Adam M. Szalkowski Co-authored-by: Shahar "Dawn" Or Reviewed-by: NAHO <90870942+trueNAHO@users.noreply.github.com> Tested-by: NAHO <90870942+trueNAHO@users.noreply.github.com> --- modules/neovim/darwin.nix | 5 +- modules/neovim/hm.nix | 6 +- modules/neovim/meta.nix | 4 +- modules/neovim/neovide-common.nix | 4 +- modules/neovim/neovim.nix | 102 +++++++------ modules/neovim/nixos.nix | 5 +- modules/neovim/nixvim.nix | 233 +++++++++++++++++------------- modules/neovim/nvf.nix | 86 +++++------ 8 files changed, 251 insertions(+), 194 deletions(-) diff --git a/modules/neovim/darwin.nix b/modules/neovim/darwin.nix index 40b8a15b..75e4db17 100644 --- a/modules/neovim/darwin.nix +++ b/modules/neovim/darwin.nix @@ -1,6 +1,7 @@ +{ mkTarget, lib, ... }: { - imports = [ - ./nvf.nix + imports = map (module: lib.modules.importApply module mkTarget) [ ./nixvim.nix + ./nvf.nix ]; } diff --git a/modules/neovim/hm.nix b/modules/neovim/hm.nix index 2b1e5380..cc1ddfe6 100644 --- a/modules/neovim/hm.nix +++ b/modules/neovim/hm.nix @@ -1,10 +1,10 @@ { mkTarget, lib, ... }: { - imports = [ + imports = map (module: lib.modules.importApply module mkTarget) [ ./neovim.nix - (lib.modules.importApply ./neovide.nix mkTarget) + ./neovide.nix ./nixvim.nix ./nvf.nix - (lib.modules.importApply ./vim.nix mkTarget) + ./vim.nix ]; } diff --git a/modules/neovim/meta.nix b/modules/neovim/meta.nix index 45474357..906bc368 100644 --- a/modules/neovim/meta.nix +++ b/modules/neovim/meta.nix @@ -38,7 +38,7 @@ as normal. However, when using Nixvim's ["standalone" configuration mode][NixVim Standalone], you will need to pass Stylix's generated config to Nixvim yourself. - The generated config can be accessed as `config.lib.stylix.nixvim.config`. You + The generated config can be accessed as `config.stylix.targets.nixvim.exportedModule`. You can use this as a module in your standalone Nixvim Configuration or an extension of it. @@ -54,7 +54,7 @@ let inherit (pkgs.stdenv.hostPlatform) system; nixvim-package = inputs.nixvim-config.packages.''${system}.default; - extended-nixvim = nixvim-package.extend config.lib.stylix.nixvim.config; + extended-nixvim = nixvim-package.extend config.stylix.targets.nixvim.exportedModule; in { environment.systemPackages = [ extended-nixvim ]; diff --git a/modules/neovim/neovide-common.nix b/modules/neovim/neovide-common.nix index 083be484..3753b922 100644 --- a/modules/neovim/neovide-common.nix +++ b/modules/neovim/neovide-common.nix @@ -1,8 +1,8 @@ # imported from `modules/nixvim/nixvim.nix` -stylix: { +opacity: { extraConfigLua = '' if vim.g.neovide then - vim.g.neovide_normal_opacity = ${toString stylix.opacity.terminal} + vim.g.neovide_normal_opacity = ${toString opacity.terminal} end ''; } diff --git a/modules/neovim/neovim.nix b/modules/neovim/neovim.nix index 5eae8c31..e1b7d39c 100644 --- a/modules/neovim/neovim.nix +++ b/modules/neovim/neovim.nix @@ -1,12 +1,13 @@ +mkTarget: { - config, lib, pkgs, ... }: -{ - options.stylix.targets.neovim = { - enable = config.lib.stylix.mkEnableTarget "Neovim" true; +mkTarget { + name = "neovim"; + humanName = "Neovim"; + extraOptions = { plugin = lib.mkOption { type = lib.types.enum [ "base16-nvim" @@ -20,14 +21,47 @@ signColumn = lib.mkEnableOption "background transparency for the Neovim sign column"; numberLine = lib.mkEnableOption "background transparency for the NeoVim number/relativenumber column"; }; + + pluginColorConfig = lib.mkOption { + type = lib.types.lines; + default = ""; + internal = true; + }; }; - config = - lib.mkIf (config.stylix.enable && config.stylix.targets.neovim.enable) + configElements = [ + ( + { colors, cfg }: { - programs.neovim = + stylix.targets.neovim.pluginColorConfig = + with colors.withHashtag; + if cfg.plugin == "base16-nvim" then + '' + require('base16-colorscheme').setup({ + base00 = '${base00}', base01 = '${base01}', base02 = '${base02}', base03 = '${base03}', + base04 = '${base04}', base05 = '${base05}', base06 = '${base06}', base07 = '${base07}', + base08 = '${base08}', base09 = '${base09}', base0A = '${base0A}', base0B = '${base0B}', + base0C = '${base0C}', base0D = '${base0D}', base0E = '${base0E}', base0F = '${base0F}' + }) + '' + else + '' + require('mini.base16').setup({ + palette = { + base00 = '${base00}', base01 = '${base01}', base02 = '${base02}', base03 = '${base03}', + base04 = '${base04}', base05 = '${base05}', base06 = '${base06}', base07 = '${base07}', + base08 = '${base08}', base09 = '${base09}', base0A = '${base0A}', base0B = '${base0B}', + base0C = '${base0C}', base0D = '${base0D}', base0E = '${base0E}', base0F = '${base0F}' + } + }) + ''; + } + ) + ( + { cfg }: + { + programs.neovim.plugins = let - cfg = config.stylix.targets.neovim; transparencyCfg = toString ( lib.optional cfg.transparentBackground.main '' vim.cmd.highlight({ "Normal", "guibg=NONE", "ctermbg=NONE" }) @@ -43,39 +77,21 @@ '' ); in - { - plugins = [ - (lib.mkIf (cfg.plugin == "base16-nvim") { - plugin = pkgs.vimPlugins.base16-nvim; - type = "lua"; - config = with config.lib.stylix.colors.withHashtag; '' - require('base16-colorscheme').setup({ - base00 = '${base00}', base01 = '${base01}', base02 = '${base02}', base03 = '${base03}', - base04 = '${base04}', base05 = '${base05}', base06 = '${base06}', base07 = '${base07}', - base08 = '${base08}', base09 = '${base09}', base0A = '${base0A}', base0B = '${base0B}', - base0C = '${base0C}', base0D = '${base0D}', base0E = '${base0E}', base0F = '${base0F}' - }) - - ${transparencyCfg} - ''; - }) - (lib.mkIf (cfg.plugin == "mini.base16") { - plugin = pkgs.vimPlugins.mini-nvim; - type = "lua"; - config = with config.lib.stylix.colors.withHashtag; '' - require('mini.base16').setup({ - palette = { - base00 = '${base00}', base01 = '${base01}', base02 = '${base02}', base03 = '${base03}', - base04 = '${base04}', base05 = '${base05}', base06 = '${base06}', base07 = '${base07}', - base08 = '${base08}', base09 = '${base09}', base0A = '${base0A}', base0B = '${base0B}', - base0C = '${base0C}', base0D = '${base0D}', base0E = '${base0E}', base0F = '${base0F}' - } - }) - - ${transparencyCfg} - ''; - }) - ]; - }; - }; + [ + { + plugin = + if cfg.plugin == "mini.base16" then + pkgs.vimPlugins.mini-nvim + else + pkgs.vimPlugins.base16-nvim; + type = "lua"; + config = lib.concatLines [ + cfg.pluginColorConfig + transparencyCfg + ]; + } + ]; + } + ) + ]; } diff --git a/modules/neovim/nixos.nix b/modules/neovim/nixos.nix index 40b8a15b..75e4db17 100644 --- a/modules/neovim/nixos.nix +++ b/modules/neovim/nixos.nix @@ -1,6 +1,7 @@ +{ mkTarget, lib, ... }: { - imports = [ - ./nvf.nix + imports = map (module: lib.modules.importApply module mkTarget) [ ./nixvim.nix + ./nvf.nix ]; } diff --git a/modules/neovim/nixvim.nix b/modules/neovim/nixvim.nix index 6093078d..8b45fa01 100644 --- a/modules/neovim/nixvim.nix +++ b/modules/neovim/nixvim.nix @@ -1,89 +1,19 @@ +mkTarget: { - config, lib, options, + config, ... }: -let - cfg = config.stylix.targets.nixvim; - # Maps `stylix.targets.plugin` values to the appropriate nixvim configuration - pluginConfigs = { - "base16-nvim" = { - inherit highlightOverride; - - colorschemes.base16 = { - enable = true; - - colorscheme = { - inherit (config.lib.stylix.colors.withHashtag) - base00 - base01 - base02 - base03 - base04 - base05 - base06 - base07 - base08 - base09 - base0A - base0B - base0C - base0D - base0E - base0F - ; - }; - }; - }; - "mini.base16" = { - inherit highlightOverride; - - plugins.mini = { - enable = true; - - modules.base16.palette = { - inherit (config.lib.stylix.colors.withHashtag) - base00 - base01 - base02 - base03 - base04 - base05 - base06 - base07 - base08 - base09 - base0A - base0B - base0C - base0D - base0E - base0F - ; - }; - }; - }; - }; - # Transparent is used a few times below - transparent = { - bg = "none"; - ctermbg = "none"; - }; - highlightOverride = { - Normal = lib.mkIf cfg.transparentBackground.main transparent; - NonText = lib.mkIf cfg.transparentBackground.main transparent; - SignColumn = lib.mkIf cfg.transparentBackground.signColumn transparent; - LineNr = lib.mkIf cfg.transparentBackground.numberLine transparent; - LineNrAbove = lib.mkIf cfg.transparentBackground.numberLine transparent; - LineNrBelow = lib.mkIf cfg.transparentBackground.numberLine transparent; - }; -in -{ - options.stylix.targets.nixvim = { - enable = config.lib.stylix.mkEnableTarget "nixvim" true; +mkTarget { + name = "nixvim"; + humanName = "NixVim"; + extraOptions = { plugin = lib.mkOption { - type = lib.types.enum (builtins.attrNames pluginConfigs); + type = lib.types.enum [ + "base16-nvim" + "mini.base16" + ]; default = "mini.base16"; description = "Plugin used for the colorscheme"; }; @@ -92,8 +22,127 @@ in signColumn = lib.mkEnableOption "background transparency for the NeoVim sign column"; numberLine = lib.mkEnableOption "background transparency for the NeoVim number/relativenumber column"; }; + + module = lib.mkOption { + type = lib.types.deferredModule; + internal = true; + }; + pluginConfigs = lib.mkOption { + type = lib.types.anything; + default = { }; + internal = true; + }; + exportedModule = lib.mkOption { + type = lib.types.deferredModule; + description = '' + Theming configuration which can be merged with your own. See + [Standalone Mode](#standalone-mode) documentation. + ''; + readOnly = true; + }; }; + configElements = [ + ( + { colors }: + { + stylix.targets.nixvim.pluginConfigs = { + "base16-nvim".colorschemes.base16.colorscheme = { + inherit (colors.withHashtag) + base00 + base01 + base02 + base03 + base04 + base05 + base06 + base07 + base08 + base09 + base0A + base0B + base0C + base0D + base0E + base0F + ; + }; + "mini.base16".plugins.mini.modules.base16.palette = { + inherit (colors.withHashtag) + base00 + base01 + base02 + base03 + base04 + base05 + base06 + base07 + base08 + base09 + base0A + base0B + base0C + base0D + base0E + base0F + ; + }; + }; + } + ) + ( + { fonts }: + { + stylix.targets.nixvim.module.opts.guifont = + "${fonts.monospace.name}:h${toString fonts.sizes.terminal}"; + } + ) + ( + { opacity }: + { + stylix.targets.nixvim.module = lib.modules.importApply ./neovide-common.nix opacity; + } + ) + ( + { cfg }: + { + stylix.targets.nixvim = { + pluginConfigs = + let + # Transparent is used a few times below + transparent = { + bg = "none"; + ctermbg = "none"; + }; + highlightOverride = { + Normal = lib.mkIf cfg.transparentBackground.main transparent; + NonText = lib.mkIf cfg.transparentBackground.main transparent; + SignColumn = lib.mkIf cfg.transparentBackground.signColumn transparent; + LineNr = lib.mkIf cfg.transparentBackground.numberLine transparent; + LineNrAbove = lib.mkIf cfg.transparentBackground.numberLine transparent; + LineNrBelow = lib.mkIf cfg.transparentBackground.numberLine transparent; + }; + in + { + "base16-nvim" = { + colorschemes.enable = true; + inherit highlightOverride; + }; + "mini.base16" = { + plugins.mini.enable = true; + inherit highlightOverride; + }; + }; + module = cfg.pluginConfigs.${cfg.plugin}; + exportedModule = cfg.module; + }; + programs = lib.optionalAttrs (options.programs ? nixvim) { + nixvim = cfg.module; + }; + } + ) + ]; + imports = [ (lib.mkRenamedOptionModuleWith { from = [ @@ -131,26 +180,12 @@ in "signColumn" ]; }) - ]; - config = lib.mkMerge [ - { - lib.stylix.nixvim.config = { - imports = [ - (lib.modules.importApply ./neovide-common.nix config.stylix) - ]; - config = lib.mkMerge [ - pluginConfigs.${cfg.plugin} - { - opts.guifont = "${config.stylix.fonts.monospace.name}:h${toString config.stylix.fonts.sizes.terminal}"; - } - ]; - }; - } - (lib.mkIf (config.stylix.enable && cfg.enable && options.programs ? nixvim) ( - lib.optionalAttrs (options.programs ? nixvim) { - programs.nixvim = config.lib.stylix.nixvim.config; + ( + { config, ... }: + { + lib.stylix.nixvim.config = lib.warn "stylix: `config.lib.stylix.nixvim.config` has been renamed to `config.stylix.targets.nixvim.exportedModule` and will be removed after 26.11" config.stylix.targets.nixvim.exportedModule; } - )) + ) ]; } diff --git a/modules/neovim/nvf.nix b/modules/neovim/nvf.nix index fc134182..b3e74c71 100644 --- a/modules/neovim/nvf.nix +++ b/modules/neovim/nvf.nix @@ -1,15 +1,13 @@ +mkTarget: { - config, lib, options, ... }: -let - cfg = config.stylix.targets.nvf; -in -{ - options.stylix.targets.nvf = { - enable = config.lib.stylix.mkEnableTarget "nvf" true; +mkTarget { + name = "nvf"; + humanName = "nvf"; + extraOptions = { plugin = lib.mkOption { type = lib.types.enum [ "base16" @@ -21,40 +19,46 @@ in transparentBackground = lib.mkEnableOption "background transparency for the main Neovim window"; }; - config = - lib.mkIf (config.stylix.enable && cfg.enable && options.programs ? nvf) - ( - lib.optionalAttrs (options.programs ? nvf) { - programs.nvf.settings.vim = { - theme = { - enable = true; - name = cfg.plugin; - base16-colors = { - inherit (config.lib.stylix.colors.withHashtag) - base00 - base01 - base02 - base03 - base04 - base05 - base06 - base07 - base08 - base09 - base0A - base0B - base0C - base0D - base0E - base0F - ; - }; - transparent = cfg.transparentBackground; - }; - statusline = lib.mkIf (cfg.plugin == "base16") { - lualine.theme = "base16"; + configElements = lib.optionals (options.programs ? nvf) [ + ( + { colors, cfg }: + { + programs.nvf.settings.vim = { + theme = { + enable = true; + name = cfg.plugin; + base16-colors = { + inherit (colors.withHashtag) + base00 + base01 + base02 + base03 + base04 + base05 + base06 + base07 + base08 + base09 + base0A + base0B + base0C + base0D + base0E + base0F + ; }; }; - } - ); + statusline = lib.mkIf (cfg.plugin == "base16") { + lualine.theme = "base16"; + }; + }; + } + ) + ( + { cfg }: + { + programs.nvf.settings.vim.theme.transparent = cfg.transparentBackground; + } + ) + ]; }