Enable creation of other config files than init.lua (#246)

This commit is contained in:
Luc Chabassier 2023-04-20 22:41:37 +02:00 committed by GitHub
parent bc468178ae
commit a6eec507cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 283 additions and 115 deletions

View file

@ -27,28 +27,6 @@ with lib; let
}; };
in { in {
options = { options = {
viAlias = mkOption {
type = types.bool;
default = false;
description = ''
Symlink <command>vi</command> to <command>nvim</command> binary.
'';
};
vimAlias = mkOption {
type = types.bool;
default = false;
description = ''
Symlink <command>vim</command> to <command>nvim</command> binary.
'';
};
package = mkOption {
type = types.package;
default = pkgs.neovim-unwrapped;
description = "Neovim to use for nixvim";
};
extraPlugins = mkOption { extraPlugins = mkOption {
type = with types; listOf (either package pluginWithConfigType); type = with types; listOf (either package pluginWithConfigType);
default = []; default = [];
@ -64,42 +42,41 @@ in {
extraConfigLua = mkOption { extraConfigLua = mkOption {
type = types.lines; type = types.lines;
default = ""; default = "";
description = "Extra contents for init.lua"; description = "Extra contents for the file";
}; };
extraConfigLuaPre = mkOption { extraConfigLuaPre = mkOption {
type = types.lines; type = types.lines;
default = ""; default = "";
description = "Extra contents for init.lua before everything else"; description = "Extra contents for the file before everything else";
}; };
extraConfigLuaPost = mkOption { extraConfigLuaPost = mkOption {
type = types.lines; type = types.lines;
default = ""; default = "";
description = "Extra contents for init.lua after everything else"; description = "Extra contents for the file after everything else";
}; };
extraConfigVim = mkOption { extraConfigVim = mkOption {
type = types.lines; type = types.lines;
default = ""; default = "";
description = "Extra contents for init.vim"; description = "Extra contents for the file, in vimscript";
}; };
wrapRc = mkOption { type = mkOption {
type = types.bool; type = types.enum ["vim" "lua"];
description = "Should the config be included in the wrapper script"; default = "lua";
default = false; description = "Whether the generated file is a vim or a lua file";
}; };
finalPackage = mkOption { path = mkOption {
type = types.package;
description = "Wrapped neovim";
readOnly = true;
};
initContent = mkOption {
type = types.str; type = types.str;
description = "The content of the init.lua file"; description = "Path of the file relative to the config directory";
};
content = mkOption {
type = types.str;
description = "The content of the config file";
readOnly = true; readOnly = true;
visible = false; visible = false;
}; };
@ -112,71 +89,29 @@ in {
}; };
config = let config = let
defaultPlugin = { contentLua = ''
plugin = null; ${config.extraConfigLuaPre}
config = ""; vim.cmd([[
optional = false; ${config.extraConfigVim}
}; ]])
${config.extraConfigLua}
${config.extraConfigLuaPost}
'';
normalizedPlugins = map (x: contentVim = ''
defaultPlugin lua << EOF
// (
if x ? plugin
then x
else {plugin = x;}
))
config.extraPlugins;
neovimConfig = pkgs.neovimUtils.makeNeovimConfig ({
inherit (config) viAlias vimAlias extraLuaPackages;
# inherit customRC;
plugins = normalizedPlugins;
}
# Necessary to make sure the runtime path is set properly in NixOS 22.05,
# or more generally before the commit:
# cda1f8ae468 - neovim: pass packpath via the wrapper
// optionalAttrs (functionArgs pkgs.neovimUtils.makeNeovimConfig ? configure) {
configure.packages = {
nixvim = {
start = map (x: x.plugin) normalizedPlugins;
opt = [];
};
};
});
customRC =
''
vim.cmd([[
${neovimConfig.neovimRcContent}
]])
''
+ (optionalString (config.extraConfigLuaPre != "") ''
${config.extraConfigLuaPre} ${config.extraConfigLuaPre}
'') EOF
+ (optionalString (config.extraConfigVim != "") '' ${config.extraConfigVim}
vim.cmd([[ lua << EOF
${config.extraConfigVim}
]])
'')
+ (optionalString (config.extraConfigLua != "" || config.extraConfigLuaPost != "") ''
${config.extraConfigLua} ${config.extraConfigLua}
${config.extraConfigLuaPost} ${config.extraConfigLuaPost}
''); EOF
'';
extraWrapperArgs = builtins.concatStringsSep " " (
(optional (config.extraPackages != [])
''--prefix PATH : "${makeBinPath config.extraPackages}"'')
++ (optional (config.wrapRc)
''--add-flags -u --add-flags "${pkgs.writeText "init.lua" customRC}"'')
);
wrappedNeovim = pkgs.wrapNeovimUnstable config.package (neovimConfig
// {
wrapperArgs = lib.escapeShellArgs neovimConfig.wrapperArgs + " " + extraWrapperArgs;
wrapRc = false;
});
in { in {
finalPackage = wrappedNeovim; content =
initContent = customRC; if config.type == "lua"
then contentLua
else contentVim;
}; };
} }

View file

@ -1,10 +1,18 @@
{ modules: {
lib, lib,
pkgs, pkgs,
config,
... ...
}: let }: let
inherit (lib) mkEnableOption mkOption mkOptionType mkForce mkMerge mkIf types; inherit (lib) mkEnableOption mkOption mkOptionType mkForce mkMerge mkIf types;
in { in {
topLevelModules =
[
./modules/output.nix
(import ./modules/files.nix (modules pkgs))
]
++ (modules pkgs);
helpers = mkOption { helpers = mkOption {
type = mkOptionType { type = mkOptionType {
name = "helpers"; name = "helpers";
@ -14,4 +22,16 @@ in {
description = "Use this option to access the helpers"; description = "Use this option to access the helpers";
default = import ../plugins/helpers.nix {inherit (pkgs) lib;}; default = import ../plugins/helpers.nix {inherit (pkgs) lib;};
}; };
configFiles = let
cfg = config.programs.nixvim;
in
lib.mapAttrs'
(
_: file:
lib.nameValuePair
"nvim/${file.path}"
{text = file.content;}
)
cfg.files;
} }

View file

@ -5,19 +5,19 @@ modules: {
... ...
} @ args: let } @ args: let
inherit (lib) mkEnableOption mkOption mkOptionType mkForce mkMerge mkIf types; inherit (lib) mkEnableOption mkOption mkOptionType mkForce mkMerge mkIf types;
shared = import ./_shared.nix args; shared = import ./_shared.nix modules args;
cfg = config.programs.nixvim; cfg = config.programs.nixvim;
in { in {
options = { options = {
programs.nixvim = mkOption { programs.nixvim = mkOption {
default = {}; default = {};
type = types.submodule ((modules pkgs) type = types.submodule ([
++ [
{ {
options.enable = mkEnableOption "nixvim"; options.enable = mkEnableOption "nixvim";
config.wrapRc = mkForce true; config.wrapRc = mkForce true;
} }
]); ]
++ shared.topLevelModules);
}; };
nixvim.helpers = shared.helpers; nixvim.helpers = shared.helpers;
}; };

View file

@ -5,18 +5,23 @@ modules: {
... ...
} @ args: let } @ args: let
inherit (lib) mkEnableOption mkOption mkOptionType mkMerge mkIf types; inherit (lib) mkEnableOption mkOption mkOptionType mkMerge mkIf types;
shared = import ./_shared.nix args; shared = import ./_shared.nix modules args;
cfg = config.programs.nixvim; cfg = config.programs.nixvim;
files =
shared.configFiles
// {
"nvim/init.lua".text = cfg.initContent;
};
in { in {
options = { options = {
programs.nixvim = mkOption { programs.nixvim = mkOption {
default = {}; default = {};
type = types.submodule ((modules pkgs) type = types.submodule ([
++ [
{ {
options.enable = mkEnableOption "nixvim"; options.enable = mkEnableOption "nixvim";
} }
]); ]
++ shared.topLevelModules);
}; };
nixvim.helpers = shared.helpers; nixvim.helpers = shared.helpers;
}; };
@ -26,7 +31,7 @@ in {
(mkMerge [ (mkMerge [
{home.packages = [cfg.finalPackage];} {home.packages = [cfg.finalPackage];}
(mkIf (!cfg.wrapRc) { (mkIf (!cfg.wrapRc) {
xdg.configFile."nvim/init.lua".text = cfg.initContent; xdg.configFile = files;
}) })
{ {
warnings = cfg.warnings; warnings = cfg.warnings;

View file

@ -0,0 +1,64 @@
modules: {
pkgs,
config,
lib,
...
}: let
inherit (lib) types;
fileModuleType = types.submodule ({
name,
config,
...
}: {
imports = modules;
options.plugin = lib.mkOption {
type = types.package;
description = "A derivation with the content of the file in it";
readOnly = true;
internal = true;
};
config = {
path = name;
type = lib.mkDefault (
if lib.hasSuffix ".vim" name
then "vim"
else "lua"
);
plugin = pkgs.writeTextDir config.path config.content;
};
});
in {
options = {
files = lib.mkOption {
type = types.attrsOf fileModuleType;
description = "Files to include in the vim config";
default = {};
};
filesPlugin = lib.mkOption {
type = types.package;
description = "A derivation with all the files inside";
internal = true;
readOnly = true;
};
};
config = let
files = config.files;
concatFilesOption = attr:
lib.flatten (lib.mapAttrsToList (_: file: builtins.getAttr attr file) files);
in {
# Each file can declare plugins/packages/warnings/assertions
extraPlugins = concatFilesOption "extraPlugins";
extraPackages = concatFilesOption "extraPackages";
warnings = concatFilesOption "warnings";
assertions = concatFilesOption "assertions";
# A directory with all the files in it
filesPlugin = pkgs.buildEnv {
name = "nixvim-config";
paths = lib.mapAttrsToList (_: file: file.plugin) files;
ignoreCollisions = true; # Collisions can't happen by construction
};
};
}

134
wrappers/modules/output.nix Normal file
View file

@ -0,0 +1,134 @@
{
pkgs,
config,
lib,
...
}:
with lib; let
pluginWithConfigType = types.submodule {
options = {
config = mkOption {
type = types.lines;
description = "vimscript for this plugin to be placed in init.vim";
default = "";
};
optional =
mkEnableOption "optional"
// {
description = "Don't load by default (load with :packadd)";
};
plugin = mkOption {
type = types.package;
description = "vim plugin";
};
};
};
in {
options = {
viAlias = mkOption {
type = types.bool;
default = false;
description = ''
Symlink <command>vi</command> to <command>nvim</command> binary.
'';
};
vimAlias = mkOption {
type = types.bool;
default = false;
description = ''
Symlink <command>vim</command> to <command>nvim</command> binary.
'';
};
package = mkOption {
type = types.package;
default = pkgs.neovim-unwrapped;
description = "Neovim to use for nixvim";
};
wrapRc = mkOption {
type = types.bool;
description = "Should the config be included in the wrapper script";
default = false;
};
finalPackage = mkOption {
type = types.package;
description = "Wrapped neovim";
readOnly = true;
};
initContent = mkOption {
type = types.str;
description = "The content of the init.lua file";
readOnly = true;
visible = false;
};
};
config = let
defaultPlugin = {
plugin = null;
config = "";
optional = false;
};
normalizedPlugins = map (x:
defaultPlugin
// (
if x ? plugin
then x
else {plugin = x;}
))
config.extraPlugins;
neovimConfig = pkgs.neovimUtils.makeNeovimConfig ({
inherit (config) viAlias vimAlias;
# inherit customRC;
plugins = normalizedPlugins;
}
# Necessary to make sure the runtime path is set properly in NixOS 22.05,
# or more generally before the commit:
# cda1f8ae468 - neovim: pass packpath via the wrapper
// optionalAttrs (functionArgs pkgs.neovimUtils.makeNeovimConfig ? configure) {
configure.packages = {
nixvim = {
start = map (x: x.plugin) normalizedPlugins;
opt = [];
};
};
});
customRC =
''
vim.cmd([[
${neovimConfig.neovimRcContent}
]])
''
+ config.content;
extraWrapperArgs = builtins.concatStringsSep " " (
(optional (config.extraPackages != [])
''--prefix PATH : "${makeBinPath config.extraPackages}"'')
++ (optional (config.wrapRc)
''--add-flags -u --add-flags "${pkgs.writeText "init.lua" customRC}"'')
);
wrappedNeovim = pkgs.wrapNeovimUnstable config.package (neovimConfig
// {
wrapperArgs = lib.escapeShellArgs neovimConfig.wrapperArgs + " " + extraWrapperArgs;
wrapRc = false;
});
in {
type = lib.mkForce "lua";
finalPackage = wrappedNeovim;
initContent = customRC;
extraPlugins =
if config.wrapRc
then [config.filesPlugin]
else [];
};
}

View file

@ -5,18 +5,23 @@ modules: {
... ...
} @ args: let } @ args: let
inherit (lib) mkEnableOption mkOption mkOptionType mkMerge mkIf types; inherit (lib) mkEnableOption mkOption mkOptionType mkMerge mkIf types;
shared = import ./_shared.nix args; shared = import ./_shared.nix modules args;
cfg = config.programs.nixvim; cfg = config.programs.nixvim;
files =
shared.configFiles
// {
"nvim/sysinit.lua".text = cfg.initContent;
};
in { in {
options = { options = {
programs.nixvim = mkOption { programs.nixvim = mkOption {
default = {}; default = {};
type = types.submodule ((modules pkgs) type = types.submodule ([
++ [
{ {
options.enable = mkEnableOption "nixvim"; options.enable = mkEnableOption "nixvim";
} }
]); ]
++ shared.topLevelModules);
}; };
nixvim.helpers = shared.helpers; nixvim.helpers = shared.helpers;
}; };
@ -26,7 +31,7 @@ in {
(mkMerge [ (mkMerge [
{environment.systemPackages = [cfg.finalPackage];} {environment.systemPackages = [cfg.finalPackage];}
(mkIf (!cfg.wrapRc) { (mkIf (!cfg.wrapRc) {
environment.etc."nvim/sysinit.lua".text = cfg.initContent; environment.etc = files;
environment.variables."VIM" = "/etc/nvim"; environment.variables."VIM" = "/etc/nvim";
}) })
{ {

View file

@ -6,8 +6,13 @@ default_pkgs: modules: {
wrap = {wrapRc = true;}; wrap = {wrapRc = true;};
shared = import ./_shared.nix modules {
inherit pkgs lib;
config = {};
};
eval = lib.evalModules { eval = lib.evalModules {
modules = (modules pkgs) ++ [module wrap]; modules = [module wrap] ++ shared.topLevelModules;
}; };
handleAssertions = config: let handleAssertions = config: let