A month ago this jekyll-powered blog was hosted on GitHub pages. Now it’s served by NixOS hosted on DigitalOcean. Because many parts of NixOS are rarely documented I’d like to share how I’ve resolved the issues I’ve encountered in the process.

The first thing I did after booting into the fresh installed NixOS was trying to check out the default configuration /etc/nixos/configuration.nix with vim. Turns out vim is not installed by default.

Setting Up Vim

Setting up a plain vim installation on NixOS is rather simple. Just append vim to the list of system packages:

environment.systemPackages = with pkgs; [ vim ]

There are two ways to do this:

  • either you use the nano text editor which is installed by default, or
  • you download the /etc/nixos/configuration.nix file, update it locally and then reupload it.

After this is done just run nixos-rebuild switch and you should be able to run vim.

A Custom vimrc

NixOS still doesn’t feel like home without my custom vimrc settings. So this was my next task. After googling around and looking at some other people’s configuration.nix I used the following approach:

environment.systemPackages = with pkgs; [
    git wget # ...
    (
        with import <nixpkgs> {};

        vim_configurable.customize {
            # Specifies the vim binary name.
            # E.g. set this to "my-vim" and you need to type "my-vim" to open this vim
            # This allows to have multiple vim packages installed (e.g. with a different set of plugins)
            name = "vim";
            vimrcConfig.customRC = ''
                # Here you can specify what usually goes into `~/.vimrc` 
                syntax enable
            '';
        }
    )
];

As you can see adding custom entries to .vimrc is pretty simple. But it’s kind of ugly to have this huge expression inside of the configuration.nix. Luckily we can just move the whole vim configuration into another file.

So let’s create a file vim.nix in /etc/nixos with the content of the above vim-expression:

with import <nixpkgs> {};

vim_configurable.customize {
    # Specifies the vim binary name.
    # E.g. set this to "my-vim" and you need to type "my-vim" to open this vim
    # This allows to have multiple vim packages installed (e.g. with a different set of plugins)
    name = "vim";
    vimrcConfig.customRC = ''
        # Here one can specify what usually goes into `~/.vimrc` 
        syntax enable
    '';
}

Inside the configuration.nix we just need to import the vim.nix like this:

environment.systemPackages = with pkgs; [
    git wget (import ./vim.nix) # ...
];

After running nixos-rebuild switch vim should now use the specified vimrc settings.

Plugins

My vim experience is not complete without plugins. After spending some more time on google and inside the nixpkgs repository, I’ve found a pretty simple solution on how to configure vim plugins: vim_configurable supports plugins out of the box.

We just have to specify the plugins we need inside the vim.nix file:

with import <nixpkgs> {};

vim_configurable.customize {
    name = "vim";
    vimrcConfig.customRC = ''
        # ...
    '';
    # Use the default plugin list shipped with nixpkgs
    vimrcConfig.vam.knownPlugins = pkgs.vimPlugins;
    vimrcConfig.vam.pluginDictionaries = [
        { names = [
            # Here you can place all your vim plugins
            # They are installed managed by `vam` (a vim plugin manager)
            "Syntastic"
            "ctrlp"
        ]; }
    ];
}

Be aware that only the most common vim plugins are available by default. Here is the list of available plugins.

Just a quick nixos-rebuild switch and the plugins should be ready.

Sharing The Configuration Across Different Computers

The vim.nix placed on the server running this blog is equivalent to the .vimrc running on my local OSX machine. Because the nix package manager is also available for OSX it should be possible to install the vim.nix locally. So it’s possible to have the same vim setup on localhost and on a remote machine.

I haven’t tried this out yet, but I’m pretty sure this is possible. Feel free to try it out.

Update: It’s working :) You can just run nix-env -f vim.nix -i vim on OSX to install the vim.nix-package.

Takeaways

Customizing vim on NixOS is pretty simple once you know how to do it.

Thanks for reading :) Follow me on twitter if you’re interested in more of this stuff! In case you spotted a mistake above or something is not working, feel free to contact me.