neovim.nix 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. { lib
  2. , stdenv
  3. , writeShellScriptBin
  4. , writeTextFile
  5. , neovim-unwrapped
  6. , makeWrapper
  7. , fd
  8. , ripgrep
  9. , node-lsp
  10. , vimPlugins
  11. , fetchFromGitHub
  12. , withPlayground ? false
  13. , withLuadev ? false
  14. }:
  15. let
  16. buildNeovimPlugin = attrs: stdenv.mkDerivation ({
  17. forceShare= [ "man" "info" ];
  18. installPhase = ''
  19. cp -r . $out
  20. if [ -d "$out/doc" ]; then
  21. ${neovim-unwrapped}/bin/nvim -N -u NONE -i NONE -n -E -s -V1 -c "helptags $out/doc" +quit!
  22. fi
  23. '';
  24. } // attrs);
  25. extra-treesitter-textobjects = writeTextFile {
  26. name = "extra-treesitter-textobjects";
  27. destination = "/queries/ecma/textobjects.scm";
  28. text = ''
  29. ;; extends
  30. ; consider array elements as @parameter text objects too
  31. (array
  32. "," @_start .
  33. (_) @parameter.inner
  34. (#make-range! "parameter.outer" @_start @parameter.inner))
  35. (array
  36. . (_) @parameter.inner
  37. . ","? @_end
  38. (#make-range! "parameter.outer" @parameter.inner @_end))
  39. '';
  40. };
  41. tsc = writeShellScriptBin "tsc" ''
  42. if [ -x "./node_modules/.bin/tsc" ]; then
  43. exec ./node_modules/.bin/tsc "$@"
  44. else
  45. exec ${node-lsp}/lib/node_modules/node-lsp/node_modules/.bin/tsc "$@"
  46. fi
  47. '';
  48. extraPath = [
  49. fd
  50. ripgrep
  51. node-lsp
  52. tsc
  53. ];
  54. nixpkgsPlugins =
  55. let
  56. # we take nvim-treesitter from nixpkgs as the plugin and grammar versions
  57. # must match exactly
  58. nvim-treesitter = (vimPlugins.nvim-treesitter.withPlugins (p: with p; [
  59. tree-sitter-bash
  60. tree-sitter-css
  61. tree-sitter-elvish
  62. tree-sitter-go
  63. tree-sitter-javascript
  64. tree-sitter-json
  65. tree-sitter-lua
  66. tree-sitter-nix
  67. tree-sitter-ruby
  68. tree-sitter-tsx
  69. tree-sitter-typescript
  70. ] ++ lib.optionals withPlayground [
  71. tree-sitter-query
  72. ]));
  73. in
  74. [ nvim-treesitter ] ++ nvim-treesitter.dependencies
  75. ++ (lib.optionals withPlayground [
  76. vimPlugins.playground
  77. ])
  78. ++ (lib.optionals withLuadev [
  79. vimPlugins.nvim-luadev
  80. ]);
  81. pinnedPlugins = import ./plugins {
  82. inherit buildNeovimPlugin fetchFromGitHub;
  83. };
  84. plugins = (lib.attrValues pinnedPlugins)
  85. ++ nixpkgsPlugins
  86. ++ [
  87. extra-treesitter-textobjects
  88. ];
  89. in
  90. stdenv.mkDerivation {
  91. pname = "nvim";
  92. version = neovim-unwrapped.version;
  93. initVim = ''
  94. let g:loaded_python3_provider = 0
  95. let g:loaded_ruby_provider = 0
  96. let g:loaded_node_provider = 0
  97. let g:loaded_perl_provider = 0
  98. set runtimepath^=${placeholder "out"}/lib
  99. set packpath^=${placeholder "out"}/lib
  100. source ${./settings.lua}
  101. source ${./plugins.lua}
  102. source ${./mappings.lua}
  103. source ${./autocmd.lua}
  104. source ${./lsp.lua}
  105. source ${./make.lua}
  106. source ${./theme.lua}
  107. '' + lib.optionalString withPlayground ''
  108. source ${./playground.lua}
  109. '' + lib.optionalString withLuadev ''
  110. source ${./luadev.lua}
  111. '';
  112. passAsFile = [ "initVim" ];
  113. nativeBuildInputs = [ makeWrapper ];
  114. unpackPhase = ":";
  115. buildPhase = ''
  116. # build pack / runtime dir
  117. mkdir -p lib/pack/nixpkgs/start
  118. ${lib.concatMapStringsSep "\n" (p: ''
  119. ln -s "${p}" "lib/pack/nixpkgs/start/${lib.getName p}"
  120. '') plugins}
  121. # create config file
  122. mkdir etc
  123. cp "$initVimPath" etc/init.vim
  124. # symlink in man pages
  125. mkdir -p share
  126. ln -s ${neovim-unwrapped}/share/man share/man
  127. # make bin
  128. mkdir bin
  129. makeWrapper ${neovim-unwrapped}/bin/nvim bin/nvim \
  130. --prefix PATH : ${lib.makeBinPath extraPath} \
  131. --add-flags -u \
  132. --add-flags $out/etc/init.vim
  133. '';
  134. installPhase = ''
  135. mkdir $out
  136. cp -r * $out
  137. '';
  138. }