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