Jump to content

Neovim: Difference between revisions

m
imported>Onny
(Add example system configuration)
(14 intermediate revisions by 8 users not shown)
Line 5: Line 5:
=== With Home Manager ===
=== With Home Manager ===


[https://nixos.wiki/wiki/Home_Manager Home Manager] has a module for Neovim, which can be enabled via
[[Home Manager]] has a module for Neovim, which can be enabled via


  programs.neovim = {
<syntaxhighlight lang="nix">
    enable = true;
programs.neovim = {
    extraConfig = <nowiki>''</nowiki>
  enable = true;
      set number relativenumber
  extraConfig = ''
    <nowiki>''</nowiki>;
    set number relativenumber
  };
  '';
};
</syntaxhighlight>


More information about the module can be found here: [https://nix-community.github.io/home-manager/options.html#opt-programs.neovim.enable Home Manager Manual].
More information about the module can be found here: [https://nix-community.github.io/home-manager/options.xhtml#opt-programs.neovim.enable Home Manager Manual].


=== System-wide ===
=== System-wide ===
Line 20: Line 22:
If you do not use Home Manager, you can use the following code in your NixOS configuration:
If you do not use Home Manager, you can use the following code in your NixOS configuration:


  programs.neovim = {
<syntaxhighlight lang="nix">
    enable = true;
programs.neovim = {
    defaultEditor = true;
  enable = true;
  };
  defaultEditor = true;
};
</syntaxhighlight>


You can also manually add Neovim to your packages. This should only be used if the two version above do not work for you.
You can also manually add Neovim to your packages. This should only be used if the two version above do not work for you.


  environment.systemPackages = [ pkgs.neovim ];
<syntaxhighlight lang="nix">
environment.systemPackages = [ pkgs.neovim ];
</syntaxhighlight>


== Configuration ==
== Configuration ==
Line 38: Line 44:
You can copy your old config or directly load your default Neovim config via:
You can copy your old config or directly load your default Neovim config via:


  programs.neovim.extraConfig = lib.fileContents ../path/to/your/init.vim;
<syntaxhighlight lang="nix">
programs.neovim.extraConfig = lib.fileContents ../path/to/your/init.vim;
</syntaxhighlight>


To use Neovim as your default editor, you can set the <code>EDITOR</code> [https://search.nixos.org/options?show=environment.variables&type=packages&query=environment.variables environmental variable] to "nvim" by adding the following to your NixOS configuration:
To use Neovim as your default editor, you can set the <code>EDITOR</code> [https://search.nixos.org/options?show=environment.variables&type=packages&query=environment.variables environmental variable] to "nvim" by adding the following to your NixOS configuration:


  environment.variables.EDITOR = "nvim";
<syntaxhighlight lang="nix">
environment.variables.EDITOR = "nvim";
</syntaxhighlight>


The Home Manager module does also expose options to automatically add <code>vi</code> and <code>vim</code> aliases.
The Home Manager module does also expose options to automatically add <code>vi</code> and <code>vim</code> aliases.
To use them, add the following to your Home Manager configuration:
To use them, add the following to your Home Manager configuration:


  programs.neovim = {
<syntaxhighlight lang="nix">
    viAlias = true;
programs.neovim = {
    vimAlias = true;
  viAlias = true;
  };
  vimAlias = true;
};
</syntaxhighlight>


==== Installing Plugins ====
==== Installing Plugins ====
Line 57: Line 69:
You can add only the plugin, or the plugin with its corresponding config:
You can add only the plugin, or the plugin with its corresponding config:


  programs.neovim.plugins = [
<syntaxhighlight lang="nix">
    pkgs.vimPlugins.nvim-tree-lua
programs.neovim.plugins = [
    {
  pkgs.vimPlugins.nvim-tree-lua
      plugin = pkgs.vimPlugins.vim-startify;
  {
      config = "let g:startify_change_to_vcs_root = 0";
    plugin = pkgs.vimPlugins.vim-startify;
    }
    config = "let g:startify_change_to_vcs_root = 0";
  ];
  }
];
</syntaxhighlight>


If you only add the plugin, you can add the configuration as described above.
If you only add the plugin, you can add the configuration as described above.


An index of official packages can be found in on [https://search.nixos.org/packages?from=0&size=50&sort=relevance&type=packages&query=vimPlugins search.nixos.org].
An index of official packages can be found in on [https://search.nixos.org/packages?from=0&size=50&sort=relevance&type=packages&query=vimPlugins search.nixos.org].
In addition to the official packages, there are several user maintained repositories, such as [https://github.com/m15a/nixpkgs-vim-extra-plugins vim-extra-plugins] or [https://github.com/NixNeovim/NixNeovimPlugins NixNeovimPlugins].
In addition to the official packages, there are several user maintained repositories, such as [https://github.com/m15a/flake-awesome-neovim-plugins awesome-neovim-plugins] or [https://github.com/NixNeovim/NixNeovimPlugins NixNeovimPlugins]. Plugins which are
not available in any of these repositories may be integrated using the <code>vimUtils.buildVimPlugin</code> function from Nixpkgs:
 
<syntaxhighlight lang="nix">
pkgs.vimUtils.buildVimPlugin {
  pname = "whatever";
  version = "whatever";
  src = builtins.fetchGit {
    url = "https://github.com/example/whatever.git";
    ref = "whatever";
  };
  buildScript = ":";
}
</syntaxhighlight>


=== System-wide ===
=== System-wide ===
Line 75: Line 102:
Instead, you can use the <code>programs.neovim.configure</code> option as described [https://search.nixos.org/options?show=programs.neovim.configure&type=packages&query=neovim here].
Instead, you can use the <code>programs.neovim.configure</code> option as described [https://search.nixos.org/options?show=programs.neovim.configure&type=packages&query=neovim here].


The following example configures RC commands and enables the plugin <code>vim-nix</code> to support syntax highlighting for Nix files
The following example configures RC commands and enables the plugin <code>ctrlp</code> to support fuzzy file search (see [https://github.com/ctrlpvim/ctrlp.vim homepage] on how to use it)


  programs.neovim = {
<syntaxhighlight lang="nix">
    enable = true;
programs.neovim = {
    configure = {
  enable = true;
      customRC = ''
  configure = {
        set number
    customRC = ''
        set cc=80
      set number
        set list
      set cc=80
        set listchars=tab:→\ ,space:·,nbsp:␣,trail:•,eol:¶,precedes:«,extends:»
      set list
        if &diff
      set listchars=tab:→\ ,space:·,nbsp:␣,trail:•,eol:¶,precedes:«,extends:»
          colorscheme blue
      if &diff
        endif
        colorscheme blue
      '';
      endif
      packages.myVimPackage = with pkgs.vimPlugins; {
    '';
        start = [ vim-nix ];
    packages.myVimPackage = with pkgs.vimPlugins; {
      };
      start = [ ctrlp ];
     };
     };
   };
   };
};
</syntaxhighlight>


Similarly to the Home Manager module, to set Neovim as your default editor you have to set the <code>EDITOR</code> environment variable like this:
To set Neovim as your default editor:


  environment.variables.EDITOR = "nvim";
<syntaxhighlight lang="nix">
programs.neovim = {
  defaultEditor = true;
};
</syntaxhighlight>


Further, the NixOS module does also expose options to automatically add <code>vi</code> and <code>vim</code> aliases.
Further, the NixOS module does also expose options to automatically add <code>vi</code> and <code>vim</code> aliases.
To use them, add the following to your NixOS configuration:
To use them, add the following to your NixOS configuration:


  programs.neovim = {
<syntaxhighlight lang="nix">
    viAlias = true;
programs.neovim = {
    vimAlias = true;
  viAlias = true;
  };
  vimAlias = true;
};
</syntaxhighlight>
 
== Tips and tricks ==


== Build Neovim using Nix ==
=== Build Neovim using Nix ===


You can also compile Neovim using nix. For this, the Neovim GitHub page has more information on this:
You can also compile Neovim using nix. For this, the Neovim GitHub page has more information on this:
Line 119: Line 156:
Finally, there is a [https://github.com/nix-community/neovim-nightly-overlay Neovim Nightly Overlay].
Finally, there is a [https://github.com/nix-community/neovim-nightly-overlay Neovim Nightly Overlay].


== Note on Lua plugins  ==
=== Note on Lua plugins  ===


Due to how the `runtimepath` for Lua modules is [https://github.com/nanotee/nvim-lua-guide#a-note-about-packages processed], your configuration may require <code>packadd! plugin-name</code> to require a module. A home-manager example:
Due to how the `runtimepath` for Lua modules is [https://github.com/nanotee/nvim-lua-guide#a-note-about-packages processed], your configuration may require <code>packadd! plugin-name</code> to require a module. A home-manager example:


  programs.neovim = {
<syntaxhighlight lang="nix">
    plugins = [
programs.neovim = {
      {
  plugins = [
        plugin = nvim-colorizer-lua
    {
        config = <nowiki>''</nowiki>
      plugin = pkgs.vimPlugins.nvim-colorizer-lua;
          packadd! nvim-colorizer.lua
      config = ''
          lua require 'colorizer'.setup()
        packadd! nvim-colorizer.lua
        <nowiki>''</nowiki>;
        lua << END
       }
require 'colorizer'.setup {
    ];
  '*'; -- Highlight all files, but customize some others.
  }
  '!vim'; -- Exclude vim from highlighting.
}
END
       '';
    }
  ];
}
</syntaxhighlight>


== See Also ==
== See Also ==
1

edit