In a previous post
I explained how to manage Neovim plugins with Nix and Home Manager.
In this post I want to go further and show how to migrate Neovim configuration
from ~/.config/nvim to ~/.config/home-manager entirely. The end result will
be to split our Neovim setup into multiple modules that colocate plugin sourcing
and configuration.
If you haven't read the post linked above, do so now. We will assume the configuration of that post as the basis for the rest of the steps.
- First, create a new directory at
~/.config/home-manager/nvimand move your existing Home Manager Neovim config to~/.config/home-manager/nvim/default.nix:
{ pkgs, lib, ...}:
let
fromGitHub = ref: repo: pkgs.vimUtils.buildVimPluginFrom2Nix {
pname = "${lib.strings.sanitizeDerivationName repo}";
version = ref;
src = builtins.fetchGit {
url = "https://github.com/${repo}.git";
ref = ref;
};
};
in
{
programs.neovim = {
enable = true;
defaultEditor = true;
viAlias = true;
vimAlias = true;
vimdiffAlias = true;
plugins = with pkgs.vimPlugins; [
nvim-lspconfig
nvim-treesitter.withAllGrammars
plenary-nvim
gruvbox-material
mini-nvim
(fromGitHub "HEAD" "elihunter173/dirbuf.nvim")
];
};
}- Next, edit
~/.config/home-manager/home.nixand import the new file. Note that Nix looks fordefault.nixin the directory automatically, so we don't have to specify it.
{ ... }:
{
imports = [
./nvim
];
home = {
username = "test";
homeDirectory = "/home/test";
};
}- Now make a
~/.config/home-manager/functionsdirectory and split out thefromGitHubfunction into a new file at~/.config/home-manager/functions/fromGitHub.nix. This will make the function reusable across multiple files.
{user, repo, ref ? "HEAD", buildScript ? ":"}:
let
pkgs = import <nixpkgs> {};
in
pkgs.vimUtils.buildVimPlugin {
pname = "${pkgs.lib.strings.sanitizeDerivationName repo}";
version = ref;
src = builtins.fetchGit {
url = "https://github.com/${user}/${repo}.git";
inherit ref;
};
inherit buildScript;
}- Modify
~/.config/home-manager/nvim/default.nixto use the new function file.
# [...]
let
fromGitHub = import ../functions/fromGitHub.nix;
in
# [...]- Write a new file at
~/.config/home-manager/nvim/colorscheme.nix. The/* lua */comment will allow LSP and Treesitter to work within the following multiline string, assuming your Neovim configuration is setup correctly.
{ pkgs, ... }:
{
programs.neovim = {
plugins = with pkgs.vimPlugins; [
gruvbox-material
];
extraLuaConfig = /* lua */ ''
vim.o.termguicolors = true
vim.cmd('colorscheme gruvbox-material')
vim.g.gruvbox_material_background = 'hard'
'';
};
}- Import the new file from
default.nixand remove the colorscheme plugin from thepluginslist.
{ config, pkgs, lib, ...}:
let
fromGitHub = import ../functions/fromGitHub.nix;
in
{
imports = [
./colorscheme.nix
];
programs.neovim = {
enable = true;
defaultEditor = true;
viAlias = true;
vimAlias = true;
vimdiffAlias = true;
plugins = with pkgs.vimPlugins; [
nvim-lspconfig
nvim-treesitter.withAllGrammars
plenary-nvim
mini-nvim
(fromGitHub "HEAD" "elihunter173/dirbuf.nvim")
];
};
}- Repeat the above pattern of moving plugins and config to various imports
until all your Neovim configuration is fully within Home Manager. Then
run
$ home-manager switch -b nvim-backupto let Home Manager produce your Neovim configuration programatically. Now you can stop backing up~/.config/nvimand rollback your configuration whenever something goes wrong.
This appears to be the flake-compatible version of fetchFromGit
fetchFromGit.nix:
default.nix:
I'd love to hear if this isn't correct. I'm still at the stage of poking the file until the error goes away.