From af9f2f6b10cb0f478a28bdc08978da16c124bf67 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Wed, 22 Nov 2023 06:11:41 -0700 Subject: [PATCH] Have DAPs work in way that allows being shadowed by nix-shell/direnv. --- jrpotter/default.nix | 83 +++++++++++++++++------------ jrpotter/neovim/default.nix | 12 +++++ jrpotter/neovim/lang/bash.nix | 10 ++-- jrpotter/neovim/lang/c.nix | 23 ++++++++ jrpotter/neovim/lang/elixir.nix | 8 +-- jrpotter/neovim/lang/lua.nix | 6 ++- jrpotter/neovim/lang/nix.nix | 6 ++- jrpotter/neovim/lang/python.nix | 23 ++++---- jrpotter/neovim/lang/typescript.nix | 8 +-- jrpotter/neovim/lua/init/c.lua | 32 +++++++++++ jrpotter/neovim/lua/init/python.lua | 24 ++++----- 11 files changed, 158 insertions(+), 77 deletions(-) create mode 100644 jrpotter/neovim/lang/c.nix create mode 100644 jrpotter/neovim/lua/init/c.lua diff --git a/jrpotter/default.nix b/jrpotter/default.nix index 0f0567e..9a6e429 100644 --- a/jrpotter/default.nix +++ b/jrpotter/default.nix @@ -1,44 +1,59 @@ -{ pkgs, ... }: +{ config, pkgs, lib, ... }: { + options.home.extraPythonPackages = lib.mkOption { + type = lib.types.listOf lib.types.string; + example = '' + [ debugpy mccabe ] + ''; + description = lib.mdDoc '' + Extra Python packages that should be linked to the topmost Python + interpreter. + ''; + }; + imports = [ ./git.nix ./neovim ./wezterm ]; - home = { - username = "jrpotter"; - homeDirectory = "/home/jrpotter"; + config = { + home = { + username = "jrpotter"; + homeDirectory = "/home/jrpotter"; - packages = with pkgs; [ - anki-bin - bitwarden - elan - firefox - gnumake - mullvad-vpn - python3 - unzip - wezterm - zotero - ]; + packages = with pkgs; [ + anki-bin + bitwarden + clang + elan + firefox + gnumake + mullvad-vpn + (python3.withPackages + (ps: builtins.map (s: ps.${s}) config.home.extraPythonPackages)) + unzip + wezterm + zotero + ]; + }; + + programs = { + bash.enable = true; + direnv.enable = true; + git.enable = true; + home-manager.enable = true; + neovim.enable = true; + }; + + # This value determines the Home Manager release that + # your configuration is compatible with. This helps avoid + # breakage when a new Home Manager release introduces + # backwards-incompatible changes. + # + # You can update Home Manager without changing this value. + # See the Home Manager release notes for a list of state + # version changes in each release. + home.stateVersion = "23.05"; }; - - programs = { - bash.enable = true; - direnv.enable = true; - git.enable = true; - home-manager.enable = true; - neovim.enable = true; - }; - - # This value determines the Home Manager release that - # your configuration is compatible with. This helps avoid - # breakage when a new Home Manager release introduces - # backwards-incompatible changes. - # - # You can update Home Manager without changing this value. - # See the Home Manager release notes for a list of state - # version changes in each release. - home.stateVersion = "23.05"; } diff --git a/jrpotter/neovim/default.nix b/jrpotter/neovim/default.nix index 9d7b167..50ea5d2 100644 --- a/jrpotter/neovim/default.nix +++ b/jrpotter/neovim/default.nix @@ -32,6 +32,7 @@ let plugin = (pkgs.vimPlugins.nvim-treesitter.withPlugins ( ps: with ps; [ bash + c elixir lua nix @@ -67,8 +68,19 @@ in }; }; + # Notice that within our imports we use `home.packages` instead of + # `extraPackages`. The latter is preferable but comes with `$PATH`-related + # problems. + # + # Specifically, Home Manager appends paths specifed in `extraPackages` to the + # end of `$PATH` meaning any already defined instance of some package will be + # used instead. Prepending is not an option either since that would break + # environments like those produced by `direnv` or `nix-shell`. + # + # https://github.com/nix-community/home-manager/pull/1756 imports = [ ./lang/bash.nix + ./lang/c.nix ./lang/elixir.nix ./lang/lean.nix ./lang/lua.nix diff --git a/jrpotter/neovim/lang/bash.nix b/jrpotter/neovim/lang/bash.nix index 81b9f0d..8bc3ba7 100644 --- a/jrpotter/neovim/lang/bash.nix +++ b/jrpotter/neovim/lang/bash.nix @@ -1,13 +1,13 @@ { pkgs, ... }: { + home.packages = with pkgs; [ + nodePackages.bash-language-server + shellcheck + ]; + programs.neovim = { nvim-lspconfig = '' require('init.lsp').setup(require('lspconfig').bashls) {} ''; - - extraPackages = with pkgs; [ - nodePackages.bash-language-server - shellcheck - ]; }; } diff --git a/jrpotter/neovim/lang/c.nix b/jrpotter/neovim/lang/c.nix new file mode 100644 index 0000000..21a469b --- /dev/null +++ b/jrpotter/neovim/lang/c.nix @@ -0,0 +1,23 @@ +{ pkgs, ... }: +{ + home.packages = with pkgs; [ + clang-tools + vscode-extensions.vadimcn.vscode-lldb + ]; + + programs.neovim = { + nvim-dap = '' + require('init.c').nvim_dap({ + command = '${pkgs.vscode-extensions.vadimcn.vscode-lldb}/share/vscode/extensions/vadimcn.vscode-lldb/adapter/codelldb' + }) + ''; + + nvim-lspconfig = '' + require('init.c').nvim_lspconfig() + ''; + }; + + xdg.configFile."nvim/after/ftplugin/c.lua".text = '' + require('init.dap').buffer_map() + ''; +} diff --git a/jrpotter/neovim/lang/elixir.nix b/jrpotter/neovim/lang/elixir.nix index 0d95eac..ab45918 100644 --- a/jrpotter/neovim/lang/elixir.nix +++ b/jrpotter/neovim/lang/elixir.nix @@ -1,14 +1,14 @@ { pkgs, ... }: { + home.packages = with pkgs; [ + elixir-ls + ]; + programs.neovim = { nvim-lspconfig = '' require('init.lsp').setup(require('lspconfig').elixirls) { cmd = { 'elixir-ls' }, } ''; - - extraPackages = with pkgs; [ - elixir-ls - ]; }; } diff --git a/jrpotter/neovim/lang/lua.nix b/jrpotter/neovim/lang/lua.nix index 3dcbff5..ca60ec4 100644 --- a/jrpotter/neovim/lang/lua.nix +++ b/jrpotter/neovim/lang/lua.nix @@ -1,10 +1,12 @@ { pkgs, ... }: { + home.packages = with pkgs;[ + lua-language-server + ]; + programs.neovim = { nvim-lspconfig = '' require('init.lua').nvim_lspconfig() ''; - - extraPackages = [ pkgs.lua-language-server ]; }; } diff --git a/jrpotter/neovim/lang/nix.nix b/jrpotter/neovim/lang/nix.nix index 75ceeb4..5dcb342 100644 --- a/jrpotter/neovim/lang/nix.nix +++ b/jrpotter/neovim/lang/nix.nix @@ -1,10 +1,12 @@ { pkgs, ... }: { + home.packages = with pkgs; [ + nil + ]; + programs.neovim = { nvim-lspconfig = '' require('init.lsp').setup(require('lspconfig').nil_ls) {} ''; - - extraPackages = [ pkgs.nil ]; }; } diff --git a/jrpotter/neovim/lang/python.nix b/jrpotter/neovim/lang/python.nix index 4b1b7f7..2ac0c6f 100644 --- a/jrpotter/neovim/lang/python.nix +++ b/jrpotter/neovim/lang/python.nix @@ -1,15 +1,14 @@ -{ pkgs, ... }: -let - venv = pkgs.python3.withPackages (ps: with ps; [ - debugpy - mccabe - pycodestyle - pyflakes - python-lsp-server - python-lsp-black - ]); -in +{ ... }: { + home.extraPythonPackages = [ + "debugpy" + "mccabe" + "pycodestyle" + "pyflakes" + "python-lsp-server" + "python-lsp-black" + ]; + programs.neovim = { nvim-dap = '' require('init.python').nvim_dap() @@ -18,8 +17,6 @@ in nvim-lspconfig = '' require('init.python').nvim_lspconfig() ''; - - extraPackages = [ venv ]; }; xdg.configFile."nvim/after/ftplugin/python.lua".text = '' diff --git a/jrpotter/neovim/lang/typescript.nix b/jrpotter/neovim/lang/typescript.nix index 335e703..810bf4a 100644 --- a/jrpotter/neovim/lang/typescript.nix +++ b/jrpotter/neovim/lang/typescript.nix @@ -1,13 +1,13 @@ { pkgs, ... }: { + home.packages = with pkgs; [ + nodePackages.typescript-language-server + ]; + programs.neovim = { nvim-lspconfig = '' require('init.lsp').setup(require('lspconfig').tsserver) {} ''; - - extraPackages = with pkgs; [ - nodePackages.typescript-language-server - ]; }; } diff --git a/jrpotter/neovim/lua/init/c.lua b/jrpotter/neovim/lua/init/c.lua new file mode 100644 index 0000000..a87a040 --- /dev/null +++ b/jrpotter/neovim/lua/init/c.lua @@ -0,0 +1,32 @@ +local M = {} + +function M.nvim_dap(options) + local dap = require('dap') + local key = 'codelldb' + + dap.adapters[key] = { + type = 'server', + port = '${port}', + executable = { + command = options.command, + args = {'--port', '${port}'}, + }, + } + + dap.configurations.c = dap.configurations.c or {} + table.insert(dap.configurations.c, { + name = 'Launch Executable', + type = key, + request = 'launch', + program = function() + return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file') + end, + cwd = '${workspaceFolder}', + }) +end + +function M.nvim_lspconfig() + require('init.lsp').setup(require('lspconfig').clangd) {} +end + +return M diff --git a/jrpotter/neovim/lua/init/python.lua b/jrpotter/neovim/lua/init/python.lua index af2d244..da932a2 100644 --- a/jrpotter/neovim/lua/init/python.lua +++ b/jrpotter/neovim/lua/init/python.lua @@ -2,23 +2,21 @@ local M = {} function M.nvim_dap() local dap = require('dap') + local key = 'debugpy' - dap.adapters.python = function(callback, config) - callback({ - name = 'debugpy', - type = 'executable', - command = 'python3', - args = { '-m', 'debugpy.adapter' }, - options = { - source_filetype = 'python', - }, - }) - end + dap.adapters[key] = { + type = 'executable', + command = 'python3', + args = { '-m', 'debugpy.adapter' }, + options = { + source_filetype = 'python', + }, + } dap.configurations.python = dap.configurations.python or {} table.insert(dap.configurations.python, { - name = 'Launch', - type = 'python', + name = 'Launch File', + type = key, request = 'launch', program = '${file}', cwd = '${workspaceFolder}',