fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
4.9 KiB184 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.vim;
8  defaultPlugins = [ pkgs.vimPlugins.vim-sensible ];
9
10  knownSettings = {
11    background = types.enum [ "dark" "light" ];
12    backupdir = types.listOf types.str;
13    copyindent = types.bool;
14    directory = types.listOf types.str;
15    expandtab = types.bool;
16    hidden = types.bool;
17    history = types.int;
18    ignorecase = types.bool;
19    modeline = types.bool;
20    mouse = types.enum [ "n" "v" "i" "c" "h" "a" "r" ];
21    mousefocus = types.bool;
22    mousehide = types.bool;
23    mousemodel = types.enum [ "extend" "popup" "popup_setpos" ];
24    number = types.bool;
25    relativenumber = types.bool;
26    shiftwidth = types.int;
27    smartcase = types.bool;
28    tabstop = types.int;
29    undodir = types.listOf types.str;
30    undofile = types.bool;
31  };
32
33  vimSettingsType = types.submodule {
34    options = let
35      opt = name: type:
36        mkOption {
37          type = types.nullOr type;
38          default = null;
39          visible = false;
40        };
41    in mapAttrs opt knownSettings;
42  };
43
44  setExpr = name: value:
45    let
46      v = if isBool value then
47        (if value then "" else "no") + name
48      else
49        "${name}=${
50          if isList value then concatStringsSep "," value else toString value
51        }";
52    in optionalString (value != null) ("set " + v);
53
54  plugins = let
55    vpkgs = pkgs.vimPlugins;
56    getPkg = p:
57      if isDerivation p then
58        [ p ]
59      else
60        optional (isString p && hasAttr p vpkgs) vpkgs.${p};
61  in concatMap getPkg cfg.plugins;
62
63in {
64  options = {
65    programs.vim = {
66      enable = mkEnableOption "Vim";
67
68      plugins = mkOption {
69        type = with types; listOf (either str package);
70        default = defaultPlugins;
71        example = literalExpression "[ pkgs.vimPlugins.YankRing ]";
72        description = ''
73          List of vim plugins to install. To get a list of supported plugins run:
74          {command}`nix-env -f '<nixpkgs>' -qaP -A vimPlugins`.
75
76          Note: String values are deprecated, please use actual packages.
77        '';
78      };
79
80      settings = mkOption {
81        type = vimSettingsType;
82        default = { };
83        example = literalExpression ''
84          {
85            expandtab = true;
86            history = 1000;
87            background = "dark";
88          }
89        '';
90        description = ''
91          At attribute set of Vim settings. The attribute names and
92          corresponding values must be among the following supported
93          options.
94
95          ${concatStringsSep "\n" (mapAttrsToList (n: v: ''
96            {var}`${n}`
97            : ${v.description}
98          '') knownSettings)}
99
100          See the Vim documentation for detailed descriptions of these
101          options. Use [](#opt-programs.vim.extraConfig) to manually
102          set any options not listed above.
103        '';
104      };
105
106      extraConfig = mkOption {
107        type = types.lines;
108        default = "";
109        example = ''
110          set nocompatible
111          set nobackup
112        '';
113        description = "Custom .vimrc lines";
114      };
115
116      package = mkOption {
117        type = types.package;
118        description = "Resulting customized vim package";
119        readOnly = true;
120      };
121
122      packageConfigurable = mkOption {
123        type = types.package;
124        description = "Vim package to customize";
125        default = pkgs.vim-full or pkgs.vim_configurable;
126        defaultText = literalExpression "pkgs.vim-full";
127        example = literalExpression "pkgs.vim";
128      };
129
130      defaultEditor = mkOption {
131        type = types.bool;
132        default = false;
133        description = ''
134          Whether to configure {command}`vim` as the default
135          editor using the {env}`EDITOR` environment variable.
136        '';
137      };
138    };
139  };
140
141  config = (let
142    customRC = ''
143      ${concatStringsSep "\n" (remove "" (mapAttrsToList setExpr
144        (builtins.intersectAttrs knownSettings cfg.settings)))}
145
146      ${cfg.extraConfig}
147    '';
148
149    vim = cfg.packageConfigurable.customize {
150      name = "vim";
151      vimrcConfig = {
152        inherit customRC;
153
154        packages.home-manager.start = plugins;
155      };
156    };
157  in mkIf cfg.enable {
158    assertions = let
159      packagesNotFound =
160        filter (p: isString p && (!hasAttr p pkgs.vimPlugins)) cfg.plugins;
161    in [{
162      assertion = packagesNotFound == [ ];
163      message = "Following VIM plugin not found in pkgs.vimPlugins: ${
164          concatMapStringsSep ", " (p: ''"${p}"'') packagesNotFound
165        }";
166    }];
167
168    warnings = let stringPlugins = filter isString cfg.plugins;
169    in optional (stringPlugins != [ ]) ''
170      Specifying VIM plugins using strings is deprecated, found ${
171        concatMapStringsSep ", " (p: ''"${p}"'') stringPlugins
172      } as strings.
173    '';
174
175    home.packages = [ cfg.package ];
176
177    home.sessionVariables = mkIf cfg.defaultEditor { EDITOR = "vim"; };
178
179    programs.vim = {
180      package = vim;
181      plugins = defaultPlugins;
182    };
183  });
184}