diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8a9deba --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +result +log diff --git a/nvim/bytecompile.nix b/nvim/bytecompile.nix new file mode 100644 index 0000000..fff0a33 --- /dev/null +++ b/nvim/bytecompile.nix @@ -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 + ''; + }; +} diff --git a/nvim/default.nix b/nvim/default.nix index ab10678..684b864 100644 --- a/nvim/default.nix +++ b/nvim/default.nix @@ -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 ]; diff --git a/nvim/mknvim.nix b/nvim/mknvim.nix index bec96d3..f3b916c 100644 --- a/nvim/mknvim.nix +++ b/nvim/mknvim.nix @@ -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;