From 0289522665ac58e476d6470cd942e46a606062d4 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 17 Nov 2023 11:00:35 -0700 Subject: [PATCH] Make neovim config more composable per-language. --- jrpotter/neovim/default.nix | 110 +++++++++++++++++----------- jrpotter/neovim/lang/lua.nix | 10 +++ jrpotter/neovim/lang/nix.nix | 10 +++ jrpotter/neovim/lang/python.nix | 26 +++++++ jrpotter/neovim/lua.nix | 10 --- jrpotter/neovim/lua/init/python.lua | 49 +++++++++++++ jrpotter/neovim/nix.nix | 10 --- jrpotter/neovim/python.nix | 61 --------------- 8 files changed, 162 insertions(+), 124 deletions(-) create mode 100644 jrpotter/neovim/lang/lua.nix create mode 100644 jrpotter/neovim/lang/nix.nix create mode 100644 jrpotter/neovim/lang/python.nix delete mode 100644 jrpotter/neovim/lua.nix create mode 100644 jrpotter/neovim/lua/init/python.lua delete mode 100644 jrpotter/neovim/nix.nix delete mode 100644 jrpotter/neovim/python.nix diff --git a/jrpotter/neovim/default.nix b/jrpotter/neovim/default.nix index 06ef83c..a630e9d 100644 --- a/jrpotter/neovim/default.nix +++ b/jrpotter/neovim/default.nix @@ -1,33 +1,13 @@ -args @ { pkgs, ... }: +{ config, pkgs, lib, ... }: let - conf = { - lua = import ./lua.nix args; - nix = import ./nix.nix args; - python = import ./python.nix args; - }; - nvim-dap = { plugin = pkgs.vimPlugins.nvim-dap; - config = '' - lua << EOF - ${builtins.concatStringsSep "\n" (builtins.map (m: "do\n${m}\nend") [ - conf.python.nvim-dap - ])} - EOF - ''; + config = config.programs.neovim.nvim-dap; }; nvim-lspconfig = { plugin = pkgs.vimPlugins.nvim-lspconfig; - config = '' - lua << EOF - ${builtins.concatStringsSep "\n" (builtins.map (m: "do\n${m}\nend") [ - conf.lua.nvim-lspconfig - conf.nix.nvim-lspconfig - conf.python.nvim-lspconfig - ])} - EOF - ''; + config = config.programs.neovim.nvim-lspconfig; }; nvim-treesitter = { @@ -39,7 +19,6 @@ let ] )); config = '' - lua << EOF require('nvim-treesitter.configs').setup { auto_install = false, highlight = { enable = true }, @@ -53,29 +32,74 @@ let }, }, } - EOF ''; }; in { - programs.neovim = { - extraLuaConfig = '' - vim.o.colorcolumn = '80,100' - vim.o.expandtab = true -- Spaces instead of tabs. - vim.o.shiftwidth = 2 -- # of spaces to use for each (auto)indent. - vim.o.tabstop = 2 -- # of spaces a in the file counts for. - ''; - extraPackages = ( - conf.lua.extraPackages ++ - conf.nix.extraPackages ++ - conf.python.extraPackages - ); - plugins = [ - nvim-dap - nvim-lspconfig - nvim-treesitter + options.programs.neovim = { + nvim-dap = lib.mkOption { + type = lib.types.lines; + example = '' + require('...').nvim_dap() + ''; + description = lib.mdDoc '' + Language-specific configurations for the `nvim-dap` plugin. + ''; + }; + + nvim-lspconfig = lib.mkOption { + type = lib.types.lines; + example = '' + require('...').nvim_lspconfig() + ''; + description = lib.mdDoc '' + Language-specific configurations for the `nvim-lspconfig` plugin. + ''; + }; + }; + + imports = [ + ./lang/lua.nix + ./lang/nix.nix + ./lang/python.nix + ]; + + config = { + programs.neovim = { + plugins = map (p: { + inherit (p) plugin; + config = "lua << EOF\n${p.config}\nEOF"; + }) [ + nvim-dap + nvim-lspconfig + nvim-treesitter + ]; + viAlias = true; + vimAlias = true; + }; + + xdg.configFile."nvim/init.lua".text = lib.mkMerge [ + # Extra Lua configuration to be prepended to `init.lua`. Extend the Lua + # loader to search for our /nix/store/.../?.lua files. + (let + lua = pkgs.stdenv.mkDerivation { + name = "lua"; + src = ./lua; + installPhase = '' + mkdir -p $out/ + cp -r ./* $out/ + ''; + }; + in lib.mkBefore '' + package.path = '${lua}/?.lua;' .. package.path + '') + # Extra Lua configuration to be appended to `init.lua`. + (lib.mkAfter '' + vim.o.colorcolumn = '80,100' + vim.o.expandtab = true -- Spaces instead of tabs. + vim.o.shiftwidth = 2 -- # of spaces to use for each (auto)indent. + vim.o.tabstop = 2 -- # of spaces a in the file counts for. + '') ]; - viAlias = true; - vimAlias = true; }; } diff --git a/jrpotter/neovim/lang/lua.nix b/jrpotter/neovim/lang/lua.nix new file mode 100644 index 0000000..1b541da --- /dev/null +++ b/jrpotter/neovim/lang/lua.nix @@ -0,0 +1,10 @@ +{ pkgs, ... }: +{ + programs.neovim = { + nvim-lspconfig = '' + require('lspconfig').lua_ls.setup { } + ''; + + extraPackages = [ pkgs.lua-language-server ]; + }; +} diff --git a/jrpotter/neovim/lang/nix.nix b/jrpotter/neovim/lang/nix.nix new file mode 100644 index 0000000..48be98b --- /dev/null +++ b/jrpotter/neovim/lang/nix.nix @@ -0,0 +1,10 @@ +{ pkgs, ... }: +{ + programs.neovim = { + nvim-lspconfig = '' + require('lspconfig').nil_ls.setup {} + ''; + + extraPackages = [ pkgs.nil ]; + }; +} diff --git a/jrpotter/neovim/lang/python.nix b/jrpotter/neovim/lang/python.nix new file mode 100644 index 0000000..23279fe --- /dev/null +++ b/jrpotter/neovim/lang/python.nix @@ -0,0 +1,26 @@ +{ pkgs, ... }: +let + venv = pkgs.python3.withPackages (ps: with ps; [ + debugpy + mccabe + pycodestyle + pyflakes + python-lsp-server + python-lsp-black + ]); +in +{ + programs.neovim = { + nvim-dap = '' + require('init.python').nvim_dap({ + command = '${venv}/bin/python3.10', + }) + ''; + + nvim-lspconfig = '' + require('init.python').nvim_lspconfig() + ''; + + extraPackages = [ venv ]; + }; +} diff --git a/jrpotter/neovim/lua.nix b/jrpotter/neovim/lua.nix deleted file mode 100644 index 505d18b..0000000 --- a/jrpotter/neovim/lua.nix +++ /dev/null @@ -1,10 +0,0 @@ -{ pkgs, ... }: -{ - nvim-lspconfig = '' - require('lspconfig').lua_ls.setup { } - ''; - - extraPackages = with pkgs; [ - lua-language-server - ]; -} diff --git a/jrpotter/neovim/lua/init/python.lua b/jrpotter/neovim/lua/init/python.lua new file mode 100644 index 0000000..0b327b8 --- /dev/null +++ b/jrpotter/neovim/lua/init/python.lua @@ -0,0 +1,49 @@ +local module = {} + +function module.nvim_dap(options) + local dap = require('dap') + + dap.adapters.python = function(callback, config) + callback({ + name = 'debugpy', + type = 'executable', + command = options.command, + args = { '-m', 'debugpy.adapter' }, + options = { + source_filetype = 'python', + }, + }) + end + + dap.configurations.python = dap.configurations.python or {} + table.insert(dap.configurations.python, { + name = 'Launch', + type = 'python', + request = 'launch', + program = '${file}', + cwd = '${workspaceFolder}', + }) +end + +function module.nvim_lspconfig() + require('lspconfig').pylsp.setup { + settings = { + pylsp = { + -- `flake8` currently fails in some cases. Prefer the default set of + -- utilities instead. + -- https://github.com/python-lsp/python-lsp-server/pull/434 + configurationSources = 'pycodestyle', + plugins = { + autopep8 = { enabled = false }, + black = { enabled = true }, + mccabe = { enabled = true }, + pycodestyle = { enabled = true }, + pyflakes = { enabled = true }, + yapf = { enabled = false }, + }, + }, + }, + } +end + +return module diff --git a/jrpotter/neovim/nix.nix b/jrpotter/neovim/nix.nix deleted file mode 100644 index 8d0e798..0000000 --- a/jrpotter/neovim/nix.nix +++ /dev/null @@ -1,10 +0,0 @@ -{ pkgs, ... }: -{ - nvim-lspconfig = '' - require('lspconfig').nil_ls.setup { } - ''; - - extraPackages = with pkgs; [ - nil - ]; -} diff --git a/jrpotter/neovim/python.nix b/jrpotter/neovim/python.nix deleted file mode 100644 index 5496a4f..0000000 --- a/jrpotter/neovim/python.nix +++ /dev/null @@ -1,61 +0,0 @@ -{ pkgs, ... }: -let - venv = pkgs.python3.withPackages (ps: with ps; [ - debugpy - mccabe - pycodestyle - pyflakes - python-lsp-server - python-lsp-black - ]); -in -{ - nvim-dap = '' - local dap = require('dap') - - dap.adapters.python = function(callback, config) - callback({ - name = 'debugpy', - type = 'executable', - command = '${venv}/bin/python3.10', - args = { '-m', 'debugpy.adapter' }, - options = { - source_filetype = 'python', - }, - }) - end - - dap.configurations.python = dap.configurations.python or {} - table.insert(dap.configurations.python, { - name = 'Launch', - type = 'python', - request = 'launch', - program = "''${file}", - cwd = "''${workspaceFolder}", - }) - ''; - - nvim-lspconfig = '' - require('lspconfig').pylsp.setup { - settings = { - pylsp = { - -- `flake8` currently fails in some cases: - -- https://github.com/python-lsp/python-lsp-server/pull/434 - configurationSources = 'pycodestyle', - plugins = { - autopep8 = { enabled = false }, - black = { enabled = true }, - mccabe = { enabled = true }, - pycodestyle = { enabled = true }, - pyflakes = { enabled = true }, - yapf = { enabled = false }, - }, - }, - }, - } - ''; - - extraPackages = [ - venv - ]; -}