1
0
mirror of https://github.com/nix-community/home-manager synced 2024-06-29 09:58:32 +02:00
home-manager/modules/programs/vim.nix

185 lines
4.9 KiB
Nix
Raw Normal View History

2017-08-28 16:33:22 +02:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.programs.vim;
2020-03-30 21:54:31 +02:00
defaultPlugins = [ pkgs.vimPlugins.vim-sensible ];
2017-08-28 16:33:22 +02:00
knownSettings = {
background = types.enum [ "dark" "light" ];
2018-10-15 12:47:13 +02:00
backupdir = types.listOf types.str;
copyindent = types.bool;
2018-10-15 12:47:13 +02:00
directory = types.listOf types.str;
expandtab = types.bool;
hidden = types.bool;
history = types.int;
ignorecase = types.bool;
modeline = types.bool;
2018-10-15 12:47:13 +02:00
mouse = types.enum [ "n" "v" "i" "c" "h" "a" "r" ];
mousefocus = types.bool;
mousehide = types.bool;
mousemodel = types.enum [ "extend" "popup" "popup_setpos" ];
number = types.bool;
relativenumber = types.bool;
shiftwidth = types.int;
smartcase = types.bool;
tabstop = types.int;
2018-10-15 12:47:13 +02:00
undodir = types.listOf types.str;
undofile = types.bool;
};
vimSettingsType = types.submodule {
2020-02-02 00:39:17 +01:00
options = let
opt = name: type:
mkOption {
type = types.nullOr type;
default = null;
visible = false;
};
2020-02-02 00:39:17 +01:00
in mapAttrs opt knownSettings;
};
setExpr = name: value:
let
2020-02-02 00:39:17 +01:00
v = if isBool value then
(if value then "" else "no") + name
else
"${name}=${
if isList value then concatStringsSep "," value else toString value
}";
in optionalString (value != null) ("set " + v);
plugins = let
vpkgs = pkgs.vimPlugins;
getPkg = p:
if isDerivation p then
[ p ]
else
optional (isString p && hasAttr p vpkgs) vpkgs.${p};
in concatMap getPkg cfg.plugins;
in {
2017-08-28 16:33:22 +02:00
options = {
programs.vim = {
enable = mkEnableOption "Vim";
2017-08-28 16:33:22 +02:00
plugins = mkOption {
type = with types; listOf (either str package);
2017-08-28 16:33:22 +02:00
default = defaultPlugins;
example = literalExpression "[ pkgs.vimPlugins.YankRing ]";
description = ''
List of vim plugins to install. To get a list of supported plugins run:
{command}`nix-env -f '<nixpkgs>' -qaP -A vimPlugins`.
Note: String values are deprecated, please use actual packages.
2017-08-28 16:33:22 +02:00
'';
};
settings = mkOption {
type = vimSettingsType;
2020-02-02 00:39:17 +01:00
default = { };
example = literalExpression ''
{
expandtab = true;
history = 1000;
background = "dark";
}
'';
description = ''
At attribute set of Vim settings. The attribute names and
corresponding values must be among the following supported
options.
2020-02-02 00:39:17 +01:00
${concatStringsSep "\n" (mapAttrsToList (n: v: ''
{var}`${n}`
: ${v.description}
2020-02-02 00:39:17 +01:00
'') knownSettings)}
See the Vim documentation for detailed descriptions of these
options. Use [](#opt-programs.vim.extraConfig) to manually
set any options not listed above.
'';
};
2017-08-28 16:33:22 +02:00
extraConfig = mkOption {
type = types.lines;
default = "";
example = ''
set nocompatible
set nobackup
'';
description = "Custom .vimrc lines";
2017-08-28 16:33:22 +02:00
};
package = mkOption {
type = types.package;
description = "Resulting customized vim package";
readOnly = true;
};
packageConfigurable = mkOption {
type = types.package;
description = "Vim package to customize";
default = pkgs.vim-full or pkgs.vim_configurable;
defaultText = literalExpression "pkgs.vim-full";
example = literalExpression "pkgs.vim";
};
defaultEditor = mkOption {
type = types.bool;
default = false;
description = ''
Whether to configure {command}`vim` as the default
editor using the {env}`EDITOR` environment variable.
'';
};
2017-08-28 16:33:22 +02:00
};
};
2020-02-02 00:39:17 +01:00
config = (let
customRC = ''
${concatStringsSep "\n" (remove "" (mapAttrsToList setExpr
2020-02-02 00:39:17 +01:00
(builtins.intersectAttrs knownSettings cfg.settings)))}
2017-08-28 16:33:22 +02:00
2020-02-02 00:39:17 +01:00
${cfg.extraConfig}
'';
2017-08-28 16:33:22 +02:00
vim = cfg.packageConfigurable.customize {
2020-02-02 00:39:17 +01:00
name = "vim";
vimrcConfig = {
inherit customRC;
2017-08-28 16:33:22 +02:00
2020-02-02 00:39:17 +01:00
packages.home-manager.start = plugins;
};
2020-02-02 00:39:17 +01:00
};
in mkIf cfg.enable {
assertions = let
packagesNotFound =
filter (p: isString p && (!hasAttr p pkgs.vimPlugins)) cfg.plugins;
in [{
assertion = packagesNotFound == [ ];
message = "Following VIM plugin not found in pkgs.vimPlugins: ${
concatMapStringsSep ", " (p: ''"${p}"'') packagesNotFound
}";
}];
warnings = let stringPlugins = filter isString cfg.plugins;
in optional (stringPlugins != [ ]) ''
Specifying VIM plugins using strings is deprecated, found ${
concatMapStringsSep ", " (p: ''"${p}"'') stringPlugins
} as strings.
'';
home.packages = [ cfg.package ];
home.sessionVariables = mkIf cfg.defaultEditor { EDITOR = "vim"; };
2020-02-02 00:39:17 +01:00
programs.vim = {
package = vim;
plugins = defaultPlugins;
};
});
2017-08-28 16:33:22 +02:00
}