nixvim: support standalone nixvim

This represents a major rearchitecture for nixvim, so I'm leaving this up to track the progress for now, and to serve as a reference for any breaking changes during transition.

The main change is, of course, being able to use nixvim standalone. To do this, you should use the new build function, which takes in two arguments: the system architecture (e.g. x86_64-linux) and the configuration. For the new configuration, do not use the programs.nixvim. prefix.

For module development, the main change is that you should no longer prefix your modules with programs.nixvim..
This commit is contained in:
Pedro Alves 2022-09-18 11:19:23 +01:00 committed by GitHub
parent bd6f978d51
commit 4ddd3969e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
52 changed files with 1410 additions and 904 deletions

17
modules/colorscheme.nix Normal file
View file

@ -0,0 +1,17 @@
{ config, lib, ... }:
with lib;
{
options = {
colorscheme = mkOption {
type = types.nullOr types.str;
description = "The name of the colorscheme to use";
default = null;
};
};
config = {
extraConfigVim = optionalString (config.colorscheme != "" && config.colorscheme != null) ''
colorscheme ${config.colorscheme}
'';
};
}

131
modules/keymaps.nix Normal file
View file

@ -0,0 +1,131 @@
{ config, lib, helpers, ... }:
with lib;
let
mapOption = types.oneOf [
types.str
(types.submodule {
options = {
silent = mkOption {
type = types.bool;
description = "Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
default = false;
};
nowait = mkOption {
type = types.bool;
description = "Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
default = false;
};
script = mkOption {
type = types.bool;
description = "Equivalent to adding <script> to a map.";
default = false;
};
expr = mkOption {
type = types.bool;
description = "Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
default = false;
};
unique = mkOption {
type = types.bool;
description = "Whether to fail if the map is already defined. Equivalent to adding <unique> to a map.";
default = false;
};
noremap = mkOption {
type = types.bool;
description = "Whether to use the 'noremap' variant of the command, ignoring any custom mappings on the defined action. It is highly advised to keep this on, which is the default.";
default = true;
};
action = mkOption {
type = types.str;
description = "The action to execute.";
};
description = mkOption {
type = types.nullOr types.str;
description = "A textual description of this keybind, to be shown in which-key, if you have it.";
default = null;
};
};
})
];
mapOptions = mode: mkOption {
description = "Mappings for ${mode} mode";
type = types.attrsOf mapOption;
default = { };
};
in
{
options = {
maps = mkOption {
type = types.submodule {
options = {
normal = mapOptions "normal";
insert = mapOptions "insert";
select = mapOptions "select";
visual = mapOptions "visual and select";
terminal = mapOptions "terminal";
normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')";
visualOnly = mapOptions "visual only";
operator = mapOptions "operator-pending";
insertCommand = mapOptions "insert and command-line";
lang = mapOptions "insert, command-line and lang-arg";
command = mapOptions "command-line";
};
};
default = { };
description = ''
Custom keybindings for any mode.
For plain maps (e.g. just 'map' or 'remap') use maps.normalVisualOp.
'';
example = ''
maps = {
normalVisualOp.";" = ":"; # Same as noremap ; :
normal."<leader>m" = {
silent = true;
action = "<cmd>make<CR>";
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR>
};
'';
};
};
config =
let
mappings =
(helpers.genMaps "" config.maps.normalVisualOp) ++
(helpers.genMaps "n" config.maps.normal) ++
(helpers.genMaps "i" config.maps.insert) ++
(helpers.genMaps "v" config.maps.visual) ++
(helpers.genMaps "x" config.maps.visualOnly) ++
(helpers.genMaps "s" config.maps.select) ++
(helpers.genMaps "t" config.maps.terminal) ++
(helpers.genMaps "o" config.maps.operator) ++
(helpers.genMaps "l" config.maps.lang) ++
(helpers.genMaps "!" config.maps.insertCommand) ++
(helpers.genMaps "c" config.maps.command);
in
{
# TODO: Use vim.keymap.set if on nvim >= 0.7
extraConfigLua = optionalString (mappings != [ ]) ''
-- Set up keybinds {{{
do
local __nixvim_binds = ${helpers.toLuaObject mappings}
for i, map in ipairs(__nixvim_binds) do
vim.api.nvim_set_keymap(map.mode, map.key, map.action, map.config)
end
end
-- }}}
'';
};
}

41
modules/options.nix Normal file
View file

@ -0,0 +1,41 @@
{ config, lib, helpers, ... }:
with lib;
{
options = {
options = mkOption {
type = types.attrsOf types.anything;
default = { };
description = "The configuration options, e.g. line numbers";
};
globals = mkOption {
type = types.attrsOf types.anything;
default = { };
description = "Global variables";
};
};
config = {
extraConfigLuaPre = optionalString (config.globals != { }) ''
-- Set up globals {{{
do
local nixvim_globals = ${helpers.toLuaObject config.globals}
for k,v in pairs(nixvim_globals) do
vim.g[k] = v
end
end
-- }}}
'' + optionalString (config.options != { }) ''
-- Set up options {{{
do
local nixvim_options = ${helpers.toLuaObject config.options}
for k,v in pairs(nixvim_options) do
vim.o[k] = v
end
end
-- }}}
'';
};
}

109
modules/output.nix Normal file
View file

@ -0,0 +1,109 @@
{ 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 = {
package = mkOption {
type = types.package;
default = pkgs.neovim-unwrapped;
description = "Neovim to use for nixvim";
};
extraPlugins = mkOption {
type = with types; listOf (either package pluginWithConfigType);
default = [ ];
description = "List of vim plugins to install";
};
extraPackages = mkOption {
type = types.listOf types.package;
default = [ ];
description = "Extra packages to be made available to neovim";
};
extraConfigLua = mkOption {
type = types.lines;
default = "";
description = "Extra contents for init.lua";
};
extraConfigLuaPre = mkOption {
type = types.lines;
default = "";
description = "Extra contents for init.lua before everything else";
};
extraConfigLuaPost = mkOption {
type = types.lines;
default = "";
description = "Extra contents for init.lua after everything else";
};
extraConfigVim = mkOption {
type = types.lines;
default = "";
description = "Extra contents for init.vim";
};
output = mkOption {
type = types.package;
description = "Final package built by nixvim";
readOnly = true;
visible = false;
};
};
config =
let
customRC = config.extraConfigVim + (optionalString (config.extraConfigLua != "" || config.extraConfigLuaPre != "" || config.extraConfigLuaPost != "") ''
lua <<EOF
${config.extraConfigLuaPre}
${config.extraConfigLua}
${config.extraConfigLuaPost}
EOF
'');
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 customRC;
plugins = normalizedPlugins;
};
extraWrapperArgs = optionalString (config.extraPackages != [ ])
''--prefix PATH : "${makeBinPath config.extraPackages}"'';
wrappedNeovim = pkgs.wrapNeovimUnstable config.package (neovimConfig // {
wrapperArgs = lib.escapeShellArgs neovimConfig.wrapperArgs + " " + extraWrapperArgs;
});
in
{
output = wrappedNeovim;
};
}

6
modules/plugins.nix Normal file
View file

@ -0,0 +1,6 @@
{ ... }:
{
imports = [
../plugins/default.nix
];
}