Skip to content

Instantly share code, notes, and snippets.

@fern9001
Last active February 9, 2024 18:13
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fern9001/0319d3e0c7c3fcab3008ecda0b3ddcf7 to your computer and use it in GitHub Desktop.
Save fern9001/0319d3e0c7c3fcab3008ecda0b3ddcf7 to your computer and use it in GitHub Desktop.
Fern's NixOS Vim Guide

Fern's NixOS Vim Guide

A newbie friendly guide to configuring Vim in NixOS

File Structure

Create the following file struture in /etc/nixos

/etc/nixos
    |-- apps
        |-- vim
            |-- default.nix 
            |-- vimPlugins.nix
            |-- .vimrc

configruation.nix

import the ./apps/vim directory in your configuration.nix file.

{ config, pkgs, ... }:
{
  imports = 
    [
      ./hardware-configuration.nix
      ./apps/vim
      #...
    ];
#...

default.nix

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"; };
}

vimPlugins.nix

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"
        #...
      ];
    }
  ];
#...
}

Conclusion

Now fill out your .vimrc file with your preferred settings and pull the trigger! nixos-rebuild switch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment