summaryrefslogtreecommitdiff
path: root/modules/programs/vim.nix
blob: 345532e51293998af093ecfbd8fd564a1e8de194 (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.programs.vim;

  text = import ../lib/write-text.nix {
    inherit lib;
    mkTextDerivation = name: text: pkgs.writeText "vim-options-${name}" text;
  };

  vimOptions = concatMapStringsSep "\n" (attr: attr.text) (attrValues cfg.vimOptions);
in

{
  options = {
    programs.vim.enable = mkOption {
      type = types.bool;
      default = false;
      description = "Whether to configure vim.";
    };

    programs.vim.enableSensible = mkOption {
      type = types.bool;
      default = false;
      example = true;
      description = "Enable sensible configuration options for vim.";
    };

    programs.vim.extraKnownPlugins = mkOption {
      type = types.attrsOf types.package;
      default = {};
      example = literalExpression
        ''
        {
          vim-jsx = pkgs.vimUtils.buildVimPluginFrom2Nix {
            name = "vim-javascript-2016-07-29";
            src = pkgs.fetchgit {
              url = "git://github.com/mxw/vim-jsx";
              rev = "261114c925ea81eeb4db1651cc1edced66d6b5d6";
              sha256 = "17pffzwnvsimnnr4ql1qifdh4a0sqqsmcwfiqqzgglvsnzw5vpls";
            };
            dependencies = [];

          };
        }
        '';
      description = "Custom plugin declarations to add to VAM's knownPlugins.";
    };

    programs.vim.plugins = mkOption {
      type = types.listOf types.attrs;
      default = [];
      example = [ { names = [ "surround" "vim-nix" ]; } ];
      description = "VAM plugin dictionaries to use for vim_configurable.";
    };

    programs.vim.package = mkOption {
      internal = true;
      type = types.package;
    };

    programs.vim.vimOptions = mkOption {
      internal = true;
      type = types.attrsOf (types.submodule text);
      default = {};
    };

    programs.vim.vimConfig = mkOption {
      type = types.lines;
      default = "";
      description = "Extra vimrcConfig to use for vim_configurable.";
    };
  };

  config = mkIf cfg.enable {

    environment.systemPackages =
      [ # Include vim_configurable package.
        cfg.package
      ];

    environment.variables.EDITOR = "${cfg.package}/bin/vim";

    environment.etc."vimrc".text = ''
      ${vimOptions}
      ${cfg.vimConfig}

      if filereadable('/etc/vimrc.local')
        source /etc/vimrc.local
      endif
    '';

    programs.vim.package = pkgs.vim_configurable.customize {
      name = "vim";
      vimrcConfig.customRC = config.environment.etc."vimrc".text;
      vimrcConfig.vam = {
        knownPlugins = pkgs.vimPlugins // cfg.extraKnownPlugins;
        pluginDictionaries = cfg.plugins;
      };
    };

    programs.vim.plugins = mkIf cfg.enableSensible [
      { names = [ "fugitive" "surround" "vim-nix" ]; }
    ];

    programs.vim.vimOptions.sensible.text = mkIf cfg.enableSensible ''
      set nocompatible
      filetype plugin indent on
      syntax on

      set et sw=2 ts=2
      set bs=indent,start

      set hlsearch
      set incsearch
      nnoremap // :nohlsearch<CR>

      set list
      set listchars=tab:»·,trail:·,extends:⟩,precedes:⟨
      set fillchars+=vert:\ ,stl:\ ,stlnc:\ 

      set number

      set lazyredraw
      set nowrap
      set showcmd
      set showmatch
    '';

  };
}