robin/weave
simple neovim wrapper for nix
git clone https://git.robinwobin.dev/robin/weave
b2c82bd
main
1{ 2lib , 3lndir , 4stdenvNoCC , 5callPackage , 6makeBinaryWrapper , 7neovim-unwrapped , 8} : 9 10let 11inherit ( lib ) 12concatMapStrings 13getExe 14getVersion 15makeBinPath 16optionalString 17; 18 19inherit ( builtins ) typeOf baseNameOf ; 20in 21 22lib . extendMkDerivation { 23constructDrv = stdenvNoCC . mkDerivation ; 24 25excludeDrvArgNames = [ 26"extraLuaPackages" 27]; 28 29extendDrvArgs = 30_: 31{ 32# neovim 33pname ? "neovim" , 34versionSuffix ? "wrapped" , 35 36# you can choose the base, i choose neovim-unwrapped 37basePackage ? neovim-unwrapped , 38 39# extra lua packages 40extraLuaPackages ? _: [ ], 41 42# path, see there explanation below 43extraPackages ? [ ], 44 45# providers, these are the providers that will be enabled 46providers ? { }, 47 48# extra init.lua, this is useful for setting up the environment 49extraPreloadLua ? "" , 50 51# plugins 52plugins ? { }, 53pluginOverrides ? null , 54startPlugins ? [ ], 55optPlugins ? [ ], 56 57# our config 58userConfig , 59 60# PERF: clean runtime path so it only contains explicit runtime directories 61# - removes directories from homedir as well as nix data directories 62# - this means some programs that ship extra nvim data files need to be 63# explicitly added to the rtp with a package override 64cleanRuntimePath ? false , 65additionalRuntimePaths ? [ ], 66 67# other customisation 68aliases ? [ ], 69keepDesktopFiles ? false , 70} @args: 71let 72inherit ( basePackage ) lua ; 73luaEnv = lua . withPackages extraLuaPackages ; 74inherit ( lua . pkgs ) luaLib ; 75 76structuredPlugins = import ./plugins { 77inherit lib startPlugins optPlugins ; 78plugins = callPackage ./plugins/package.nix { 79pins = plugins ; 80inherit pluginOverrides ; 81}; 82}; 83 84# i couldn't chose a nice api between attrs and lists, so i just did both lol 85attrsifiedProviders = 86if lib . isAttrs providers then providers else ( lib . genAttrs providers ( _: true )); 87 88# merge providers attrs, with priority to the user providerd options 89pluginProviders = { 90node = false ; 91perl = false ; 92python = false ; 93python3 = false ; 94ruby = false ; 95} 96// attrsifiedProviders ; 97 98mkResultingPath = 99subdir: p: 100"pack/ ${ pname } / ${ subdir } / ${ if typeOf p == "path" then baseNameOf p else ( p . pname or p . name )} " ; 101 102config = stdenvNoCC . mkDerivation { 103name = "neovim-config" ; 104__structuredAttrs = true ; 105 106plugins = structuredPlugins . start ++ structuredPlugins . opt ; 107resultingPaths = 108map ( mkResultingPath "start" ) structuredPlugins . start 109++ map ( mkResultingPath "opt" ) structuredPlugins . opt ; 110 111# didn't know you could do this. thanks getchiee 112buildCommand = 113# bash 114'' 115mkdir -pv $out/parser 116mkdir -pv $out/pack/ ${ pname } /{start,opt} 117 118echo "generating preload.lua" 119cat > $out/preload.lua <<EOF 120vim.g.nix = true 121package.path = " ${ luaLib . genLuaPathAbsStr luaEnv } ;$LUA_PATH" .. package.path 122package.cpath = " ${ luaLib . genLuaCPathAbsStr luaEnv } ;$LUA_CPATH" .. package.cpath 123vim.env.PATH = vim.env.PATH .. ": ${ makeBinPath extraPackages } " 124'' 125+ ( 126if cleanRuntimePath then 127( 128let 129# if the runtime path is cleaned, it will be faster to leave the nvim 130# base paths at the start since most startup dependencies will be 131# found there. 132# $out will be put *before* the additionalRuntimePaths to replicate the behavior of `:prepend('$out')`. 133runtimePaths = [ 134" ${ basePackage } /lib/nvim" 135" ${ basePackage } /share/nvim/runtime" 136"$out" 137" ${ userConfig } " 138] 139++ additionalRuntimePaths ; 140rtp = builtins . concatStringsSep "," runtimePaths ; 141in 142'' 143vim.o.runtimepath = " ${ rtp } " 144vim.o.packpath = " ${ rtp } " 145'' 146) 147else 148'' 149vim.opt.runtimepath:prepend('$out') 150vim.opt.packpath:prepend('$out') 151'' 152) 153+ '' 154vim.loader.enable() 155 156${ lib . concatMapAttrsStringSep "\n" ( provider : enabled : '' 157vim.g.loaded_ ${ provider } _provider = ${ if enabled then "1" else "0" } 158'' ) pluginProviders } 159 160${ extraPreloadLua } 161EOF 162 163shopt -s extglob dotglob 164for (( i = 0; i < "''${#plugins[@]}"; i++ )); do 165source="''${plugins[$i]}" 166path="''${resultingPaths[$i]}" 167dest="$out/$path" 168 169if [[ -d "$dest" ]]; then 170echo "warning: destination '$dest' already exists, skipping" 171continue 172fi 173 174mkdir -p "$dest" 175 176tolink=("$source/"!(parser)) 177if (( "''${#tolink[@]}" )); then 178ln -ns "''${tolink[@]}" -t "$dest" 179fi 180 181if [[ -d "$source/parser" && -n "$(ls -A "$source/parser")" ]]; then 182mkdir -p "$out/parser" 183ln -nsf "$source/parser/"* -t "$out/parser" 184fi 185done 186shopt -u extglob dotglob 187 188${ getExe basePackage } \ 189-n -u NONE -i NONE \ 190--headless \ 191-c "set packpath=$out" \ 192-c "packloadall" \ 193-c "helptags ALL" \ 194+"quit!" 195 196find "$out/pack/ ${ pname } " -type d -empty -print -delete 197 198mkdir "$out/nix-support" 199for i in $(find -L "$out" -name 'propagated-build-inputs'); do 200cat "$i" >> "$out/nix-support/propagated-build-inputs" 201done 202'' ; 203}; 204in 205{ 206inherit pname ; 207version = " ${ args . version or ( getVersion basePackage )}${ 208if versionSuffix != "" then "- ${ versionSuffix } " else "" 209} " ; 210 211# don't allow for simple arugments here these **should** remain this way 212# so the wrapper works correctly 213__structuredAttrs = true ; 214strictDeps = true ; 215 216# this won't really remove anything since we still are running fixup on basePackage and the config 217# but it does speed up the build speed 218dontUnpack = true ; 219dontFixup = true ; 220dontConfigure = true ; 221dontRewriteSymlinks = true ; 222 223nativeBuildInputs = args . nativeBuildInputs or [ ] ++ [ 224makeBinaryWrapper 225lndir 226]; 227 228wrapperArgs = [ 229"--set-default" 230"NVIM_CONFIG" 231" ${ userConfig } " 232 233"--add-flag" 234"--cmd" 235"--add-flag" 236"source ${ config } /preload.lua" 237 238"--set-default" 239"NVIM_APPNAME" 240pname 241] 242++ args . wrapperArgs or [ ]; 243 244installPhase = '' 245runHook preInstall 246 247mkdir -p $ out 248lndir -silent ${ basePackage } $ out 249 250${ optionalString ( ! keepDesktopFiles ) "rm -rf $out/share/applications" } 251 252wrapProgram $ out /bin/nvim "'' $ { wrapperArgs [@]}" 253 254${ concatMapStrings ( alias : '' 255ln -s $out/bin/nvim $out/bin/ ${ alias } 256'' ) aliases } 257 258runHook postInstall 259'' ; 260 261passthru = { 262inherit config ; 263} 264// args . passthru or { }; 265 266meta = 267basePackage . meta 268// { 269priority = ( basePackage . meta . priority or lib . meta . defaultPriority ) - 1 ; 270} 271// args . meta or { }; 272}; 273}