From 157a74df47daff58c5b6c34a65fac557dafd8e01 Mon Sep 17 00:00:00 2001 From: matt1432 Date: Sun, 22 Dec 2024 03:12:45 -0500 Subject: [PATCH] feat(nvim): load devShells dynamically --- .../neovim/langs/c-lang/default.nix | 16 +++- .../neovim/langs/csharp/default.nix | 74 +++++++++++-------- homeManagerModules/neovim/langs/default.nix | 4 + .../neovim/langs/json/default.nix | 20 +++-- .../neovim/langs/lua/default.nix | 13 +++- .../neovim/langs/markdown/default.nix | 23 ++++-- .../neovim/langs/rust/default.nix | 29 ++++---- .../neovim/langs/web/default.nix | 33 ++++++++- lib/flake/default.nix | 9 +++ modules/base/packages/default.nix | 7 -- 10 files changed, 159 insertions(+), 69 deletions(-) diff --git a/homeManagerModules/neovim/langs/c-lang/default.nix b/homeManagerModules/neovim/langs/c-lang/default.nix index a0557f48..97fb5630 100644 --- a/homeManagerModules/neovim/langs/c-lang/default.nix +++ b/homeManagerModules/neovim/langs/c-lang/default.nix @@ -8,6 +8,7 @@ inherit (pkgs.writers) writeYAML; cfg = config.programs.neovim; + flakeEnv = config.programs.bash.sessionVariables.FLAKE; in { config = mkIf cfg.enable { xdg.configFile."clangd/config.yaml".source = writeYAML "config.yaml" { @@ -21,8 +22,17 @@ in { '' vim.api.nvim_create_autocmd('FileType', { pattern = { 'cpp', 'c' }, - -- FIXME: load direnv here https://github.com/actionshrimp/direnv.nvim?tab=readme-ov-file#using-nvim-lspconfig - command = 'setlocal ts=4 sw=4 sts=0 expandtab', + + callback = function() + vim.cmd[[setlocal ts=4 sw=4 sts=0 expandtab]]; + + if (devShells['c-lang'] == nil) then + devShells['c-lang'] = 1; + + require('nix-develop').nix_develop({'${flakeEnv}#c-lang'}); + vim.cmd[[LspStart]]; + end + end, }); ''; @@ -40,10 +50,12 @@ in { lsp.cmake.setup({ capabilities = default_capabilities, + autostart = false, }); lsp.clangd.setup({ capabilities = default_capabilities, + autostart = false, handlers = require('lsp-status').extensions.clangd.setup(), diff --git a/homeManagerModules/neovim/langs/csharp/default.nix b/homeManagerModules/neovim/langs/csharp/default.nix index 3ad470ce..aeb34360 100644 --- a/homeManagerModules/neovim/langs/csharp/default.nix +++ b/homeManagerModules/neovim/langs/csharp/default.nix @@ -10,19 +10,11 @@ self: { inherit (lib) mkIf; cfg = config.programs.neovim; + flakeEnv = config.programs.bash.sessionVariables.FLAKE; in { config = mkIf cfg.enable { programs = { neovim = { - extraLuaConfig = - # lua - '' - vim.api.nvim_create_autocmd('FileType', { - pattern = { 'cs' }, - command = 'setlocal ts=4 sw=4 sts=0 expandtab', - }); - ''; - plugins = [ { plugin = buildPlugin "roslyn-nvim" vimplugin-roslyn-nvim-src; @@ -38,33 +30,53 @@ in { end, }); - require('roslyn').setup({ - config = { - capabilities = require('cmp_nvim_lsp').default_capabilities(), + local startRoslyn = function() + require('roslyn').setup({ + config = { + capabilities = require('cmp_nvim_lsp').default_capabilities(), - on_attach = function() - vim.lsp.inlay_hint.enable(); - end, + on_attach = function() + vim.lsp.inlay_hint.enable(); + end, - settings = { - ["csharp|inlay_hints"] = { - csharp_enable_inlay_hints_for_implicit_object_creation = true, - csharp_enable_inlay_hints_for_implicit_variable_types = true, - csharp_enable_inlay_hints_for_lambda_parameter_types = true, - csharp_enable_inlay_hints_for_types = true, - dotnet_enable_inlay_hints_for_indexer_parameters = true, - dotnet_enable_inlay_hints_for_literal_parameters = true, - dotnet_enable_inlay_hints_for_object_creation_parameters = true, - dotnet_enable_inlay_hints_for_other_parameters = true, - dotnet_enable_inlay_hints_for_parameters = true, - dotnet_suppress_inlay_hints_for_parameters_that_differ_only_by_suffix = true, - dotnet_suppress_inlay_hints_for_parameters_that_match_argument_name = true, - dotnet_suppress_inlay_hints_for_parameters_that_match_method_intent = true, + settings = { + ["csharp|inlay_hints"] = { + csharp_enable_inlay_hints_for_implicit_object_creation = true, + csharp_enable_inlay_hints_for_implicit_variable_types = true, + csharp_enable_inlay_hints_for_lambda_parameter_types = true, + csharp_enable_inlay_hints_for_types = true, + dotnet_enable_inlay_hints_for_indexer_parameters = true, + dotnet_enable_inlay_hints_for_literal_parameters = true, + dotnet_enable_inlay_hints_for_object_creation_parameters = true, + dotnet_enable_inlay_hints_for_other_parameters = true, + dotnet_enable_inlay_hints_for_parameters = true, + dotnet_suppress_inlay_hints_for_parameters_that_differ_only_by_suffix = true, + dotnet_suppress_inlay_hints_for_parameters_that_match_argument_name = true, + dotnet_suppress_inlay_hints_for_parameters_that_match_method_intent = true, + }, }, }, - }, - exe = 'Microsoft.CodeAnalysis.LanguageServer', + exe = 'Microsoft.CodeAnalysis.LanguageServer', + }); + end; + + vim.api.nvim_create_autocmd('FileType', { + pattern = { 'cs' }, + + callback = function() + vim.cmd[[setlocal ts=4 sw=4 sts=0 expandtab]]; + + if (devShells['csharp'] == nil) then + devShells['csharp'] = 1; + + require('nix-develop').nix_develop({'${flakeEnv}#csharp'}); + startRoslyn(); + + os.execute('sleep 1') + vim.cmd[[e]]; -- reload to attach on current file + end + end, }); ''; } diff --git a/homeManagerModules/neovim/langs/default.nix b/homeManagerModules/neovim/langs/default.nix index 539c428b..4df45eb8 100644 --- a/homeManagerModules/neovim/langs/default.nix +++ b/homeManagerModules/neovim/langs/default.nix @@ -30,6 +30,9 @@ in { mkBefore # lua '' + -- Init object to keep track of loaded devShells + local devShells = {}; + -- Add formatting cmd vim.api.nvim_create_user_command( 'Format', @@ -60,6 +63,7 @@ in { # lsp plugins nvim-lspconfig lsp-status-nvim + nix-develop-nvim # completion plugins cmp-buffer cmp-nvim-lsp diff --git a/homeManagerModules/neovim/langs/json/default.nix b/homeManagerModules/neovim/langs/json/default.nix index 4e0b7cb6..3d4c3409 100644 --- a/homeManagerModules/neovim/langs/json/default.nix +++ b/homeManagerModules/neovim/langs/json/default.nix @@ -6,6 +6,7 @@ inherit (lib) mkIf; cfg = config.programs.neovim; + flakeEnv = config.programs.bash.sessionVariables.FLAKE; in { config = mkIf cfg.enable { programs = { @@ -14,13 +15,18 @@ in { # lua '' vim.api.nvim_create_autocmd('FileType', { - pattern = 'yaml', - command = 'setlocal ts=4 sw=4 sts=0 expandtab', - }); + pattern = { 'json', 'yaml' }, - vim.api.nvim_create_autocmd('FileType', { - pattern = 'json', - command = 'setlocal ts=4 sw=4 sts=0 expandtab', + callback = function() + vim.cmd[[setlocal ts=4 sw=4 sts=0 expandtab]]; + + if (devShells['json'] == nil) then + devShells['json'] = 1; + + require('nix-develop').nix_develop({'${flakeEnv}#json'}); + vim.cmd[[LspStart]]; + end + end, }); local lsp = require('lspconfig'); @@ -28,10 +34,12 @@ in { lsp.jsonls.setup({ capabilities = default_capabilities, + autostart = false, }); lsp.yamlls.setup({ capabilities = default_capabilities, + autostart = false, settings = { yaml = { diff --git a/homeManagerModules/neovim/langs/lua/default.nix b/homeManagerModules/neovim/langs/lua/default.nix index 454b76ab..c8ea2368 100644 --- a/homeManagerModules/neovim/langs/lua/default.nix +++ b/homeManagerModules/neovim/langs/lua/default.nix @@ -24,7 +24,17 @@ in { vim.api.nvim_create_autocmd('FileType', { pattern = 'lua', - command = 'setlocal ts=4 sw=4 sts=0 expandtab', + + callback = function() + vim.cmd[[setlocal ts=4 sw=4 sts=0 expandtab]]; + + if (devShells['lua'] == nil) then + devShells['lua'] = 1; + + require('nix-develop').nix_develop({'${flakeEnv}#lua'}); + vim.cmd[[LspStart]]; + end + end, }); -- IMPORTANT: make sure to setup neodev BEFORE lspconfig @@ -39,6 +49,7 @@ in { require('lspconfig').lua_ls.setup({ capabilities = default_capabilities, + autostart = false, }); ''; } diff --git a/homeManagerModules/neovim/langs/markdown/default.nix b/homeManagerModules/neovim/langs/markdown/default.nix index dd7b0f61..3bde213c 100644 --- a/homeManagerModules/neovim/langs/markdown/default.nix +++ b/homeManagerModules/neovim/langs/markdown/default.nix @@ -12,6 +12,7 @@ self: { inherit (lib) concatStringsSep mkIf; cfg = config.programs.neovim; + flakeEnv = config.programs.bash.sessionVariables.FLAKE; isServer = osConfig.roles.server.sshd.enable or false; githubCSS = pkgs.fetchurl { @@ -25,10 +26,26 @@ in { extraLuaConfig = # lua '' + vim.api.nvim_create_autocmd('FileType', { + pattern = { 'markdown', 'tex' }, + + callback = function() + vim.cmd[[setlocal ts=4 sw=4 sts=0 expandtab]]; + + if (devShells['markdown'] == nil) then + devShells['markdown'] = 1; + + require('nix-develop').nix_develop({'${flakeEnv}#markdown'}); + vim.cmd[[LspStart]]; + end + end, + }); + local lsp = require('lspconfig'); lsp.texlab.setup({ capabilities = require('cmp_nvim_lsp').default_capabilities(), + autostart = false, settings = { texlab = { @@ -80,12 +97,6 @@ in { in # lua '' - -- - vim.api.nvim_create_autocmd('FileType', { - pattern = 'tex', - command = 'setlocal ts=4 sw=4 sts=0 expandtab', - }); - vim.g.knap_settings = { -- HTML htmloutputext = 'html', diff --git a/homeManagerModules/neovim/langs/rust/default.nix b/homeManagerModules/neovim/langs/rust/default.nix index 06c520e1..b58b6648 100644 --- a/homeManagerModules/neovim/langs/rust/default.nix +++ b/homeManagerModules/neovim/langs/rust/default.nix @@ -1,36 +1,37 @@ { config, lib, - pkgs, ... }: let - inherit (lib) attrValues mkIf; + inherit (lib) mkIf; cfg = config.programs.neovim; + flakeEnv = config.programs.bash.sessionVariables.FLAKE; in { config = mkIf cfg.enable { programs = { neovim = { - extraPackages = attrValues { - inherit - (pkgs) - cargo - rustc - rust-analyzer - rustfmt - ; - }; - extraLuaConfig = # lua '' vim.api.nvim_create_autocmd('FileType', { - pattern = { 'rust' }, - command = 'setlocal ts=4 sw=4 sts=0 expandtab', + pattern = { 'rust' }, + + callback = function() + vim.cmd[[setlocal ts=4 sw=4 sts=0 expandtab]]; + + if (devShells['rust'] == nil) then + devShells['rust'] = 1; + + require('nix-develop').nix_develop({'${flakeEnv}#rust'}); + vim.cmd[[LspStart]]; + end + end, }); require('lspconfig').rust_analyzer.setup({ capabilities = require('cmp_nvim_lsp').default_capabilities(), + autostart = false, }); ''; }; diff --git a/homeManagerModules/neovim/langs/web/default.nix b/homeManagerModules/neovim/langs/web/default.nix index 08d4c77e..f9840d95 100644 --- a/homeManagerModules/neovim/langs/web/default.nix +++ b/homeManagerModules/neovim/langs/web/default.nix @@ -10,6 +10,7 @@ self: { inherit (lib) mkIf; cfg = config.programs.neovim; + flakeEnv = config.programs.bash.sessionVariables.FLAKE; in { config = mkIf cfg.enable { programs = { @@ -21,12 +22,32 @@ in { '' vim.api.nvim_create_autocmd('FileType', { pattern = { 'javascript', 'javascriptreact', 'javascript.jsx', 'typescript', 'typescriptreact', 'typescript.tsx', 'css', 'scss' }, - command = 'setlocal ts=4 sw=4 sts=0 expandtab', + + callback = function() + vim.cmd[[setlocal ts=4 sw=4 sts=0 expandtab]]; + + if (devShells['web'] == nil) then + devShells['web'] = 1; + + require('nix-develop').nix_develop({'${flakeEnv}#web'}); + vim.cmd[[LspStart]]; + end + end, }); vim.api.nvim_create_autocmd('FileType', { pattern = 'html', - command = 'setlocal ts=4 sw=4 expandtab', + + callback = function() + vim.cmd[[setlocal ts=4 sw=4 expandtab]]; + + if (devShells['web'] == nil) then + devShells['web'] = 1; + + require('nix-develop').nix_develop({'${flakeEnv}#web'}); + vim.cmd[[LspStart]]; + end + end, }); vim.api.nvim_create_autocmd('FileType', { @@ -40,6 +61,7 @@ in { tsserver.setup({ capabilities = default_capabilities, + autostart = false, handlers = { -- format error code with better error message @@ -52,6 +74,7 @@ in { lsp.eslint.setup({ capabilities = default_capabilities, + autostart = false, -- auto-save on_attach = function(client, bufnr) @@ -102,6 +125,7 @@ in { lsp.cssls.setup({ capabilities = default_capabilities, + autostart = false, settings = { css = { @@ -118,6 +142,7 @@ in { lsp.somesass_ls.setup({ capabilities = default_capabilities, + autostart = false, }); lsp.somesass_ls.manager.config.settings = { somesass = { @@ -134,6 +159,8 @@ in { lsp.html.setup({ capabilities = html_caps, + autostart = false, + settings = { configurationSection = { "html", "css", "javascript" }, embeddedLanguages = { @@ -162,6 +189,7 @@ in { # lua '' local packageInfo = require('package-info'); + packageInfo.setup({ hide_up_to_date = true, package_manager = 'npm', @@ -169,6 +197,7 @@ in { vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { pattern = { 'package.json' }, + callback = function() packageInfo.show({ force = true }); end, diff --git a/lib/flake/default.nix b/lib/flake/default.nix index 9ec48036..6e074ada 100644 --- a/lib/flake/default.nix +++ b/lib/flake/default.nix @@ -19,6 +19,15 @@ inputs: rec { { inherit cudaSupport; allowUnfree = true; + + # FIXME: Roslyn-ls uses dotnet6 https://github.com/NixOS/nixpkgs/blob/d3c42f187194c26d9f0309a8ecc469d6c878ce33/pkgs/by-name/ro/roslyn-ls/package.nix#L21 + permittedInsecurePackages = + [ + "dotnet-core-combined" + "dotnet-sdk-6.0.428" + "dotnet-sdk-wrapped-6.0.428" + ] + ++ (cfg.config.permittedInsecurePackages or []); } // (cfg.config or {}); }; diff --git a/modules/base/packages/default.nix b/modules/base/packages/default.nix index de96a3c3..86bf478d 100644 --- a/modules/base/packages/default.nix +++ b/modules/base/packages/default.nix @@ -22,13 +22,6 @@ in { ; }); - # FIXME: Roslyn-ls uses dotnet6 https://github.com/NixOS/nixpkgs/blob/d3c42f187194c26d9f0309a8ecc469d6c878ce33/pkgs/by-name/ro/roslyn-ls/package.nix#L21 - nixpkgs.config.permittedInsecurePackages = [ - "dotnet-core-combined" - "dotnet-sdk-6.0.428" - "dotnet-sdk-wrapped-6.0.428" - ]; - environment.systemPackages = (optional (cfg.user != "nixos") (self.inputs.nurl.packages.${pkgs.system}.default.overrideAttrs { postInstall = ''