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/nvim
and 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.nix
and import the new file. Note that Nix looks fordefault.nix
in 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/functions
directory and split out thefromGitHub
function 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.nix
to 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.nix
and remove the colorscheme plugin from theplugins
list.
{ 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-backup
to let Home Manager produce your Neovim configuration programatically. Now you can stop backing up~/.config/nvim
and rollback your configuration whenever something goes wrong.
Hoping that u can help ... getting strange error on home-manager switch
22:00:23$ hmeb
error:
… while calling the 'derivationStrict' builtin
builds Ok if I comment out this (fromGitHub "HEAD" "elihunter173/dirbuf.nvim")