Files
NixOsConfig/modules/cli/neovim.nix
T
faumaray 079cceb554 update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-10-29 16:02:14 +03:00

124 lines
5.1 KiB
Nix

# Neovim (nixpkgs, 0.12) with the owner's configuration repository: inputs.nvim
# (git.faumaray.ru faumaray/nvim, pinned in flake.lock; `nix flake update nvim`).
# Plugins and colours are the repository's business, not Nix's: its init.lua
# bootstraps zpack.nvim with Neovim's own vim.pack, which clones the plugins on
# the first start (git, network) into ~/.local/share/nvim/site/pack/core/opt at
# the revisions of nvim-pack-lock.json. Stylix's neovim target stays off.
#
# - ~/.config/nvim is a real directory whose files link read-only into the
# pinned repository (recursive), so the configuration cannot drift; plugins
# may still add files there (cd-project.nvim does).
# - Except nvim-pack-lock.json: vim.pack rewrites it whenever it installs,
# repairs or updates a plugin and stops with EROFS on a read-only file (the
# pinned lockfile lags behind the plugin specs). It is a writable copy, reset
# to the pinned one (the old copy kept as .prev) when the pinned one changes;
# the switch then says to run :ZRestore in Neovim.
# - A new pin also empties vim.loader's bytecode cache: store files all have
# mtime 1, so an edit that keeps a file's size would keep running old code.
# - The wrapper appends extraPackages to PATH (--suffix), so inside a dev shell
# (modules/dev/shells) the shell's compilers, clangd and rust-analyzer come
# first. It carries only what the editor needs everywhere.
{ inputs, ... }:
{
flake.modules.homeManager.neovim =
{
config,
lib,
pkgs,
...
}:
let
lockName = "nvim-pack-lock.json";
# The repository without its lockfile, linked file by file.
nvimConfig = lib.cleanSourceWith {
name = "nvim-config";
src = "${inputs.nvim}";
filter = path: _type: baseNameOf path != lockName;
};
# codelldb, the debug adapter of nvim-dap and rustaceanvim, from the VS Code
# extension: that one is in the binary cache, the standalone
# vscode-lldb.adapter attribute is not (it would compile on every update).
codelldb = pkgs.runCommandLocal "codelldb" { meta.mainProgram = "codelldb"; } ''
adapter=${pkgs.vscode-extensions.vadimcn.vscode-lldb}/share/vscode/extensions/vadimcn.vscode-lldb/adapter/codelldb
test -x "$adapter" # fail loudly if the extension layout changes
mkdir -p $out/bin
ln -s "$adapter" $out/bin/codelldb
'';
in
{
programs.neovim = {
enable = true;
defaultEditor = true;
viAlias = true;
vimAlias = true;
# home-manager always has an init.lua to write (the wrapper's provider
# settings); pass it to nvim with --cmd instead, so ~/.config/nvim/init.lua
# stays the repository's.
sideloadInitLua = true;
extraPackages = with pkgs; [
# vim.pack clones with git; blink.cmp downloads its fuzzy matcher with
# curl and asks cc for the libc; tree-sitter-manager compiles parsers
# with the tree-sitter CLI and cc; make for build hooks and :make.
git
curl
gnumake
gcc
tree-sitter
# Pickers (telescope, fzf-lua, snacks) and nvim-dap's executable picker.
ripgrep
fd
fzf
# Language tools of the configuration that no project toolchain brings.
lua-language-server
stylua
codespell
neocmakelsp
cmake-format
codelldb
devcontainer
];
};
xdg.configFile.nvim = {
source = nvimConfig;
recursive = true;
};
home.activation.nvimPackLock = lib.hm.dag.entryAfter [ "linkGeneration" ] ''
nvimPin=${inputs.nvim}
nvimLock=${config.xdg.configHome}/nvim/${lockName}
nvimState=${config.xdg.stateHome}/nvim
nvimLuac=${config.xdg.cacheHome}/nvim/luac
# A new pinned configuration: empty vim.loader's bytecode cache.
if [ "$(cat "$nvimState/nix-config" 2>/dev/null)" != "$nvimPin" ]; then
if [ -d "$nvimLuac" ]; then
run find "$nvimLuac" -mindepth 1 -delete
fi
run mkdir -p "$nvimState"
run sh -c 'printf "%s\n" "$1" > "$2"' sh "$nvimPin" "$nvimState/nix-config"
fi
# A new pinned lockfile, or no writable copy: start again from the pinned one.
if [ -f "$nvimPin/${lockName}" ]; then
nvimLockHash=$(sha256sum "$nvimPin/${lockName}" | cut -d' ' -f1)
if [ -L "$nvimLock" ] || [ ! -f "$nvimLock" ] ||
[ "$(cat "$nvimState/nix-lock" 2>/dev/null)" != "$nvimLockHash" ]; then
if [ -f "$nvimLock" ] && [ ! -L "$nvimLock" ]; then
run cp -f "$nvimLock" "$nvimLock.prev"
echo "neovim: pinned plugin lockfile changed; run :ZRestore (zpack 1.x) or :ZPack restore (2.x) in Neovim"
fi
run rm -f "$nvimLock"
run install -m 0644 "$nvimPin/${lockName}" "$nvimLock"
run mkdir -p "$nvimState"
run sh -c 'printf "%s\n" "$1" > "$2"' sh "$nvimLockHash" "$nvimState/nix-lock"
fi
fi
'';
};
}