A newbie friendly guide to configuring Vim in NixOS
Create the following file struture in /etc/nixos
/etc/nixos
|-- apps
|-- vim
|-- default.nix
|-- vimPlugins.nix
|-- .vimrc
import the ./apps/vim
directory in your configuration.nix
file.
{ config, pkgs, ... }:
{
imports =
[
./hardware-configuration.nix
./apps/vim
#...
];
#...
This is where we will configure vim using the vim_configurable
package and the vam
vim addon manager. pkgs.vimPlugins
provides a collection of nix ready/prepackaged common vim plugins.
{ pkgs, ... }:
# make pkgs avaliable in lexical scope of the following expression
with pkgs;
# set the entire package as a local variable to include in environment.systemPackages
let myVim = vim_configurable.customize {
# whatever name you want to use vim by
# vim recommened
name = "vim";
vimrcConfig = {
# import .vimrc
customRC = builtins.readFile ./.vimrc;
# make plugins avaliable to vam
vam.knownPlugins = pkgs.vimPlugins;
# declare plugins to use
vam.pluginDictonaries = [
{
names = [
"vim-nix"
#...
];
}
];
};
};
# include our customized vim package in systemPackages
in {
environment.systemPackages = with pkgs; [ myVim ];
# set vim as default editor
environment.variables = { EDITOR = "vim"; };
}
For plugins not avaliable in pkgs.vimPlugins
we can use buildVimPluginFrom2Nix
to build them. Below is an example with 2 plugins.
with import <nixpkgs> {};
let inherit (vimUtils) buildVimPluginFrom2Nix; in {
"horizon" = buildVimPluginFrom2Nix {
name = "horizon";
src = fetchgit {
# github repo
url = "https://github.com/ntk148v/vim-horizon";
# rev/commit hash
rev = "ca8ca90d14190aeadc352cf9f89c3508c304ec02";
# sha256 hash
sha256 = "ca8ca90d14190aeadc352cf9f89c3508c304ec02";
};
};
"photon" = buildVimPluginFrom2Nix {
name = "photon";
src = fetchgit {
url = "https://github.com/ntk148v/vim-horizon";
rev = "ca8ca90d14190aeadc352cf9f89c3508c304ec02";
sha256 = "ca8ca90d14190aeadc352cf9f89c3508c304ec02";
};
};
}
To get the rev and sha256 hashes install nix-prefetch-github
. run the command with the github username and repository name as shown in the example below.
$ nix-prefetch-github axvr photon.vim
{
"owner": "axvr",
"repo": "photon.vim",
"rev": "046b79c2c210c126575f34a1d96ee66d293e594b",
"sha256": "1hz3m0yz06cgwyrs9v9hxm8cx7lsr4pgn0livpxz62d69cp09c0z",
"fetchSubmodules": true
}
Now import our newly packaged plugins into default.nix
to update vam.knownPlugins
and add the new plugin names to vam.pluginDictionaries
{
#...
vam.knownPlugins = pkgs.vimPlugins // import ./vimPlugins.nix;
vam.pluginDictionaries = [
{
names = [
"hoziron"
"photon"
"vim.nix"
#...
];
}
];
#...
}
Now fill out your .vimrc file with your preferred settings and pull the trigger! nixos-rebuild switch