Byte compile config, plugins & nvim runtime

This commit is contained in:
2024-12-24 17:33:18 +01:00
parent 21137550b2
commit 84811c65dd
4 changed files with 108 additions and 14 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
result
log

85
nvim/bytecompile.nix Normal file
View File

@@ -0,0 +1,85 @@
{
pkgs,
lib,
...
}: rec {
# Stolen from https://github.com/nix-community/nixvim/pull/1887
writeByteCompiledLua = name: text:
pkgs.runCommandLocal name {inherit text;} ''
echo -n "$text" > "$out"
${lib.getExe' pkgs.luajit "luajit"} -bd -- "$out" "$out"
'';
byteCompileLuaHook = pkgs.makeSetupHook {name = "byte-compile-lua-hook";} (
let
luajit = lib.getExe' pkgs.luajit "luajit";
in
pkgs.writeText "byte-compile-lua-hook.sh" # bash
''
byteCompileLuaPostFixup() {
# Target is a single file
if [[ -f $out ]]; then
if [[ $out = *.lua ]]; then
tmp=$(mktemp)
${luajit} -bd -- "$out" "$tmp"
mv "$tmp" "$out"
fi
return
fi
# Target is a directory
while IFS= read -r -d "" file; do
tmp=$(mktemp -u "$file.XXXX")
# Ignore invalid lua files
if ${luajit} -bd -- "$file" "$tmp"; then
mv "$tmp" "$file"
else
echo "WARNING: Ignoring byte compiling error for '$file' lua file" >&2
fi
done < <(find "$out" -type f,l -name "*.lua" -print0)
}
postFixupHooks+=(byteCompileLuaPostFixup)
''
);
byteCompileLuaDrv = drv:
drv.overrideAttrs (
prev:
{
nativeBuildInputs = prev.nativeBuildInputs or [] ++ [byteCompileLuaHook];
}
// lib.optionalAttrs (prev ? buildCommand) {
buildCommand = ''
${prev.buildCommand}
runHook postFixup
'';
}
);
byteCompile = plugins: let
byteCompile = p:
(byteCompileLuaDrv p).overrideAttrs (
prev: lib.optionalAttrs (prev ? dependencies) {dependencies = map byteCompile prev.dependencies;}
);
in
map (p: p // {plugin = byteCompile p.plugin;}) plugins;
byteCompileVim = package:
pkgs.symlinkJoin {
name = "neovim-byte-compiled-${lib.getVersion package}";
paths = [package];
inherit (package) lua meta;
nativeBuildInputs = [byteCompileLuaHook];
postBuild =
# bash
''
# Replace Nvim's binary symlink with a regular file,
# or Nvim will use original runtime directory
rm $out/bin/nvim
cp ${package}/bin/nvim $out/bin/nvim
runHook postFixup
'';
};
}

View File

@@ -21,11 +21,12 @@ in
}
# TODO: make grammars optional
nvim-treesitter.withAllGrammars
# nvim-treesitter.withAllGrammars
catppuccin-nvim
];
lsp = with pkgs; [
extraPackages = with pkgs; [
# lsp
# see for helpers https://github.com/dundalek/lazy-lsp.nvim/blob/master/lua/lazy-lsp/servers.lua
haskell-language-server
rust-analyzer
@@ -43,9 +44,7 @@ in
helm-ls
zls
lua-language-server
];
extraPackages = with pkgs; [
# Give access to gdbus for color-scheme detection (vim-lumen).
glib
];

View File

@@ -1,14 +1,12 @@
{
pkgs,
lib,
stdenv,
...
}: {
package ? pkgs.neovim,
config,
plugins ? [],
extraPackages ? [],
lsp ? [],
extraLuaPackages ? p: [],
extraPython3Packages ? p: [],
withPython3 ? false,
@@ -34,18 +32,28 @@
))
plugins;
externalPackages = extraPackages ++ lsp;
in
pkgs.wrapNeovimUnstable package {
# maybe use true here?
autoconfigure = false;
autowrapRuntimeDeps = false;
initLua =
# lua
''
vim.opt.rtp:remove(vim.fn.stdpath('config')) -- ~/.config/nvim
vim.opt.rtp:remove(vim.fn.stdpath('config') .. "/after") -- ~/.config/nvim/after
vim.opt.rtp:prepend('${config}')
vim.opt.rtp:prepend('${config}/after')
plugins = normalizePlugins plugins;
${builtins.readFile (config + "/init.lua")}
'';
builder = (import ./bytecompile.nix) {inherit pkgs lib;};
nvim = builder.byteCompileVim package;
in
pkgs.wrapNeovimUnstable nvim {
plugins = builder.byteCompile (normalizePlugins plugins);
wrapRc = false;
wrapperArgs = builtins.concatStringsSep " " [
(lib.optionals (externalPackages != []) ''--prefix PATH : "${lib.makeBinPath externalPackages}"'')
(lib.optionals (extraPackages != []) ''--prefix PATH : "${lib.makeBinPath extraPackages}"'')
''--add-flags "-u ${builder.writeByteCompiledLua "init.lua" initLua}"''
];
inherit withPython3 withNodeJs withPerl withRuby extraPython3Packages extraLuaPackages;