Neovim: Difference between revisions

From NixOS Wiki
imported>SaphireLattice
mNo edit summary
imported>Jooooscha
(I reorganized the description and added some information and distinctions between using Neovim with/without Home Manager)
Line 1: Line 1:
__TOC__
__TOC__


== Installation and configuration ==
== Installation and configuration ==
Neovim shares most of it's configuration with [[Vim]]. See the [[Vim|Vim page]] for more details on the use of both.


== Configure Neovim as the default Editor ==
Add this to your <code>configuration.nix</code>


<code>environment.variables.EDITOR = "nvim";</code>
Neovim shares most of its configuration with Vim. See the [[Vim|Vim page]] for more details on the use of both.
 
=== With Home Manager ===
 
[https://nixos.wiki/wiki/Home_Manager Home Manager] has a module for Neovim, which can be enabled via
 
  programs.neovim = {
    enable = true;
    extraConfig = ''
      set number relativenumber
    ''
  };
 
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].
 
==== Configuration ====
 
The Home Manager module does not expose many configuration options.
Therefore, the easiest way to get started is to use the [https://nix-community.github.io/home-manager/options.html#opt-programs.neovim.extraConfig extraConfig] option.
You can copy your old config or directly load your default Neovim config via:
 
  programs.neovim.extraConfig = lib.fileContents ../path/to/your/init.vim;
 
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:


== Set Neovim as <code>vi</code> and <code>vim</code> ==
  environment.variables.EDITOR = "nvim";
Add this to your <code>configuration.nix</code>


  nixpkgs.overlays = [
The Home Manager module does also expose options to automatically add <code>vi</code> and <code>vim</code> aliases.
    (self: super: {
To use them, add the following to your Home Manager configuration:
      neovim = super.neovim.override {
 
        viAlias = true;
  programs.neovim = {
        vimAlias = true;
    viAlias = true;
       };
    vimAlias = true;
     })
  };
 
==== Installing Plugins ====
 
Plugins can be installed using the <code>programs.neovim.plugins</code> option.
You can add only the plugin, or the plugin with its corresponding config:
 
  programs.neovim.plugins = [
    vimPlugins.nvim-tree-lua
    {
      plugin = vim-startify;
       config = "let g:startify_change_to_vcs_root = 0";
     }
   ];
   ];


Alternatively you can use the neovim module (merged in september 2020).
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].
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].
 
=== Without Home Manager ===
 
If you do not use Home Manager, you can use the following code in your NixOS configuration:
 
  programs.neovim = {
    enable = true;
    defaultEditor = true;
  };
 
==== Configuration ====
 
The NixOS module does not have an <code>extraConfig</code> option as the Home Manager module does.
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].
 
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:
 
  environment.variables.EDITOR = "nvim";
 
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:
 
  programs.neovim = {
    viAlias = true;
    vimAlias = true;
  };
 
=== Manually ===


  programs.neovim.enable = true;
You can also manually add Neovim to your packages. This should only be used if the two version above do not work for you.
  programs.neovim.viAlias = true;


Using the module will also make nix manage the neovim configuration, disabling <code>init.vim</code>
  environment.systemPackages = [ pkgs.neovim ];


== Note on errors using default `packages` for plugins requiring Lua modules ==
=== Build Neovim using Nix ===


[https://github.com/nanotee/nvim-lua-guide#a-note-about-packages Due to how the `runtimepath` for Lua modules is processed], your configuration may require `packadd! plugin-name` to require a module. A home-manager example:
You can also compile Neovim using nix. For this, the Neovim GitHub page has more information on this:
[https://github.com/neovim/neovim/wiki/Building-Neovim#nixos--nix Neovim Guide].
 
The Neovim repository also contains a flake.
You can run the master version via the following command:
 
nix run github:neovim/neovim?dir=contrib
 
Finally, there is a [https://github.com/nix-community/neovim-nightly-overlay Neovim Nightly Overlay].
 
== 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:


   programs.neovim = {
   programs.neovim = {
Line 43: Line 117:
     ];
     ];
   }
   }
== To build neovim master ==
Use the official documentation:
https://github.com/neovim/neovim/wiki/Building-Neovim#nixos--nix
The neovim repository now contains a flake so you can run the master version via `nix run github:neovim/neovim?dir=contrib`
There is also an overlay available:
https://github.com/nix-community/neovim-nightly-overlay


== See Also ==
== See Also ==

Revision as of 20:19, 25 December 2022


Installation and configuration

Neovim shares most of its configuration with Vim. See the Vim page for more details on the use of both.

With Home Manager

Home Manager has a module for Neovim, which can be enabled via

 programs.neovim = {
   enable = true;
   extraConfig = 
     set number relativenumber
   
 };

More information about the module can be found here: Home Manager Manual.

Configuration

The Home Manager module does not expose many configuration options. Therefore, the easiest way to get started is to use the extraConfig option. You can copy your old config or directly load your default Neovim config via:

 programs.neovim.extraConfig = lib.fileContents ../path/to/your/init.vim;

To use Neovim as your default editor, you can set the EDITOR environmental variable to "nvim" by adding the following to your NixOS configuration:

 environment.variables.EDITOR = "nvim";

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

 programs.neovim = {
   viAlias = true;
   vimAlias = true;
 };

Installing Plugins

Plugins can be installed using the programs.neovim.plugins option. You can add only the plugin, or the plugin with its corresponding config:

 programs.neovim.plugins = [
   vimPlugins.nvim-tree-lua
   {
     plugin = vim-startify;
     config = "let g:startify_change_to_vcs_root = 0";
   }
 ];

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

An index of official packages can be found in on search.nixos.org. In addition to the official packages, there are several user maintained repositories, such as vim-extra-plugins or NixNeovimPlugins.

Without Home Manager

If you do not use Home Manager, you can use the following code in your NixOS configuration:

 programs.neovim = {
   enable = true;
   defaultEditor = true;
 };

Configuration

The NixOS module does not have an extraConfig option as the Home Manager module does. Instead, you can use the programs.neovim.configure option as described here.

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

 environment.variables.EDITOR = "nvim";

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

 programs.neovim = {
   viAlias = true;
   vimAlias = true;
 };

Manually

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 ];

Build Neovim using Nix

You can also compile Neovim using nix. For this, the Neovim GitHub page has more information on this: Neovim Guide.

The Neovim repository also contains a flake. You can run the master version via the following command:

nix run github:neovim/neovim?dir=contrib

Finally, there is a Neovim Nightly Overlay.

Note on Lua plugins

Due to how the `runtimepath` for Lua modules is processed, your configuration may require packadd! plugin-name to require a module. A home-manager example:

 programs.neovim = {
   plugins = [
     {
       plugin = nvim-colorizer-lua
       config = 
         packadd! nvim-colorizer.lua
         lua require 'colorizer'.setup()
       ;
     }
   ];
 }

See Also