aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/vim.nix
blob: 3325bf22516977bdc5a85aae686a5d1703284e42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.programs.vim;
  defaultPlugins = [ pkgs.vimPlugins.vim-sensible ];

  knownSettings = {
    background = types.enum [ "dark" "light" ];
    backupdir = types.listOf types.str;
    copyindent = types.bool;
    directory = types.listOf types.str;
    expandtab = types.bool;
    hidden = types.bool;
    history = types.int;
    ignorecase = types.bool;
    modeline = types.bool;
    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;
    undodir = types.listOf types.str;
    undofile = types.bool;
  };

  vimSettingsType = types.submodule {
    options = let
      opt = name: type:
        mkOption {
          type = types.nullOr type;
          default = null;
          visible = false;
        };
    in mapAttrs opt knownSettings;
  };

  setExpr = name: value:
    let
      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 {
  options = {
    programs.vim = {
      enable = mkEnableOption "Vim";

      plugins = mkOption {
        type = with types; listOf (either str package);
        default = defaultPlugins;
        example = literalExample "[ pkgs.vimPlugins.YankRing ]";
        description = ''
          List of vim plugins to install. To get a list of supported plugins run:
          <command>nix-env -f '&lt;nixpkgs&gt;' -qaP -A vimPlugins</command>.

          </para><para>

          Note: String values are deprecated, please use actual packages.
        '';
      };

      settings = mkOption {
        type = vimSettingsType;
        default = { };
        example = literalExample ''
          {
            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.

          <informaltable frame="none"><tgroup cols="1"><tbody>
          ${concatStringsSep "\n" (mapAttrsToList (n: v: ''
            <row>
              <entry><varname>${n}</varname></entry>
              <entry>${v.description}</entry>
            </row>
          '') knownSettings)}
          </tbody></tgroup></informaltable>

          See the Vim documentation for detailed descriptions of these
          options. Note, use <varname>extraConfig</varname> to
          manually set any options not listed above.
        '';
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        example = ''
          set nocompatible
          set nobackup
        '';
        description = "Custom .vimrc lines";
      };

      package = mkOption {
        type = types.package;
        description = "Resulting customized vim package";
        readOnly = true;
      };
    };
  };

  config = (let
    customRC = ''
      ${concatStringsSep "\n" (filter (v: v != "") (mapAttrsToList setExpr
        (builtins.intersectAttrs knownSettings cfg.settings)))}

      ${cfg.extraConfig}
    '';

    vim = pkgs.vim_configurable.customize {
      name = "vim";
      vimrcConfig = {
        inherit customRC;

        packages.home-manager.start = plugins;
      };
    };
  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 ];

    programs.vim = {
      package = vim;
      plugins = defaultPlugins;
    };
  });
}