From 6e448cf9587d0b5f0b7dd6f5963b0814267e04e3 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sat, 23 Sep 2023 18:36:25 +0300 Subject: [PATCH 1/4] feat: bash language server --- configuration.nix | 1 + lib/languages.nix | 10 ++- lib/types/languages.nix | 2 +- modules/languages/bash/bash.nix | 121 +++++++++++++++++++++++++++++ modules/languages/bash/config.nix | 80 +++++++++++++++++++ modules/languages/bash/default.nix | 6 ++ modules/languages/default.nix | 15 ++-- 7 files changed, 221 insertions(+), 14 deletions(-) create mode 100644 modules/languages/bash/bash.nix create mode 100644 modules/languages/bash/config.nix create mode 100644 modules/languages/bash/default.nix diff --git a/configuration.nix b/configuration.nix index 36816ebb3..ec777d3e4 100644 --- a/configuration.nix +++ b/configuration.nix @@ -72,6 +72,7 @@ inputs: let python.enable = isMaximal; dart.enable = isMaximal; elixir.enable = false; + bash.enable = true; }; vim.visuals = { diff --git a/lib/languages.nix b/lib/languages.nix index 18d678d6f..f07880b95 100644 --- a/lib/languages.nix +++ b/lib/languages.nix @@ -1,7 +1,4 @@ -# From home-manager: https://github.com/nix-community/home-manager/blob/master/modules/lib/booleans.nix {lib}: { - # Converts a boolean to a yes/no string. This is used in lots of - # configuration formats. diagnosticsToLua = { lang, config, @@ -22,4 +19,11 @@ value = diagnostics.${type}.nullConfig package; }) config); + + mkEnable = desc: + lib.mkOption { + description = "Turn on ${desc} for enabled languages by default"; + type = lib.types.bool; + default = false; + }; } diff --git a/lib/types/languages.nix b/lib/types/languages.nix index 577db5053..6b0675957 100644 --- a/lib/types/languages.nix +++ b/lib/types/languages.nix @@ -1,6 +1,6 @@ {lib}: with lib; let - diagnosticSubmodule = {...}: { + diagnosticSubmodule = _: { options = { type = mkOption { description = "Type of diagnostic to enable"; diff --git a/modules/languages/bash/bash.nix b/modules/languages/bash/bash.nix new file mode 100644 index 000000000..873857558 --- /dev/null +++ b/modules/languages/bash/bash.nix @@ -0,0 +1,121 @@ +{ + pkgs, + config, + lib, + ... +}: +with builtins; let + inherit (lib) mkOption mkEnableOption types; + + cfg = config.vim.languages.bash; + + defaultServer = "bash-ls"; + servers = { + bash-ls = { + package = pkgs.nodePackages.bash-language-server; + lspConfig = '' + lspconfig.bashls.setup{ + capabilities = capabilities; + on_attach = default_on_attach; + cmd = ${ + if isList cfg.lsp.package + then nvim.lua.expToLua cfg.lsp.package + else ''{"${cfg.lsp.package}/bin/bash-language-server", "start"}'' + }; + } + ''; + }; + }; + + defaultFormat = "shfmt"; + formats = { + shfmt = { + package = pkgs.shfmt; + nullConfig = '' + table.insert( + ls_sources, + null_ls.builtins.formatting.shfmt.with({ + command = "${pkgs.shfmt}/bin/shfmt", + }) + ) + ''; + }; + }; + + defaultDiagnostics = ["shellcheck"]; + diagnostics = { + shellcheck = { + package = pkgs.shellcheck; + nullConfig = pkg: '' + table.insert( + ls_sources, + null_ls.builtins.diagnostics.shellcheck.with({ + command = "${pkg}/bin/shellcheck", + }) + ) + ''; + }; + }; +in { + options.vim.languages.bash = { + enable = mkEnableOption "Bash language support"; + + treesitter = { + enable = mkOption { + description = "Bash treesitter"; + type = types.bool; + default = config.vim.languages.enableTreesitter; + }; + package = lib.nvim.types.mkGrammarOption pkgs "bash"; + }; + + lsp = { + enable = mkEnableOption "Enable Bash LSP support" // {default = config.vim.languages.enableLSP;}; + + server = mkOption { + description = "Bash LSP server to use"; + type = with types; enum (attrNames servers); + default = defaultServer; + }; + + package = mkOption { + description = "bash-language-server package, or the command to run as a list of strings"; + example = lib.literalExpression ''[lib.getExe pkgs.nodePackages.bash-language-server "start"]''; + type = with types; either package (listOf str); + default = pkgs.nodePackages.bash-language-server; + }; + }; + + format = { + enable = mkOption { + description = "Enable Bash formatting"; + type = types.bool; + default = config.vim.languages.enableFormat; + }; + type = mkOption { + description = "Bash formatter to use"; + type = with types; enum (attrNames formats); + default = defaultFormat; + }; + + package = mkOption { + description = "Bash formatter package"; + type = types.package; + default = formats.${cfg.format.type}.package; + }; + }; + + extraDiagnostics = { + enable = mkOption { + description = "Enable extra Bash diagnostics"; + type = types.bool; + default = config.vim.languages.enableExtraDiagnostics; + }; + types = lib.nvim.types.diagnostics { + langDesc = "Bash"; + inherit diagnostics; + inherit defaultDiagnostics; + }; + }; + }; +} diff --git a/modules/languages/bash/config.nix b/modules/languages/bash/config.nix new file mode 100644 index 000000000..efd0ea95f --- /dev/null +++ b/modules/languages/bash/config.nix @@ -0,0 +1,80 @@ +{ + pkgs, + config, + lib, + ... +}: +with lib; +with builtins; let + cfg = config.vim.languages.bash; + diagnostics = { + shellcheck = { + package = pkgs.shellcheck; + nullConfig = pkg: '' + table.insert( + ls_sources, + null_ls.builtins.diagnostics.shellcheck.with({ + command = "${pkg}/bin/shellcheck", + }) + ) + ''; + }; + }; + + formats = { + shfmt = { + package = pkgs.shfmt; + nullConfig = '' + table.insert( + ls_sources, + null_ls.builtins.formatting.shfmt.with({ + command = "${pkgs.shfmt}/bin/shfmt", + }) + ) + ''; + }; + }; + + servers = { + bash-ls = { + package = pkgs.nodePackages.bash-language-server; + lspConfig = '' + lspconfig.bashls.setup{ + capabilities = capabilities; + on_attach = default_on_attach; + cmd = ${ + if isList cfg.lsp.package + then nvim.lua.expToLua cfg.lsp.package + else ''{"${cfg.lsp.package}/bin/bash-language-server", "start"}'' + }; + } + ''; + }; + }; +in { + config = mkIf cfg.enable (mkMerge [ + (mkIf cfg.treesitter.enable { + vim.treesitter.enable = true; + vim.treesitter.grammars = [cfg.treesitter.package]; + }) + + (mkIf cfg.lsp.enable { + vim.lsp.lspconfig.enable = true; + vim.lsp.lspconfig.sources.bash-lsp = servers.${cfg.lsp.server}.lspConfig; + }) + + (mkIf cfg.format.enable { + vim.lsp.null-ls.enable = true; + vim.lsp.null-ls.sources.bash-format = formats.${cfg.format.type}.nullConfig; + }) + + (mkIf cfg.extraDiagnostics.enable { + vim.lsp.null-ls.enable = true; + vim.lsp.null-ls.sources = lib.nvim.languages.diagnosticsToLua { + lang = "bash"; + config = cfg.extraDiagnostics.types; + inherit diagnostics; + }; + }) + ]); +} diff --git a/modules/languages/bash/default.nix b/modules/languages/bash/default.nix new file mode 100644 index 000000000..7dd72f7f1 --- /dev/null +++ b/modules/languages/bash/default.nix @@ -0,0 +1,6 @@ +_: { + imports = [ + ./bash.nix + ./config.nix + ]; +} diff --git a/modules/languages/default.nix b/modules/languages/default.nix index f7e16c63c..be8736129 100644 --- a/modules/languages/default.nix +++ b/modules/languages/default.nix @@ -1,17 +1,12 @@ -{lib, ...}: -with lib; let - mkEnable = desc: - mkOption { - description = "Turn on ${desc} for enabled languages by default"; - type = types.bool; - default = false; - }; +{lib, ...}: let + inherit (lib.nvim.languages) mkEnable; in { imports = [ ./markdown ./tidal ./dart ./elixir + ./bash ./clang.nix ./go.nix @@ -31,8 +26,8 @@ in { options.vim.languages = { enableLSP = mkEnable "LSP"; enableDAP = mkEnable "Debug Adapter"; - enableTreesitter = mkEnable "treesitter"; - enableFormat = mkEnable "formatting"; + enableTreesitter = mkEnable "Treesitter"; + enableFormat = mkEnable "Formatting"; enableExtraDiagnostics = mkEnable "extra diagnostics"; }; } From 9b63ac826e2d70418337be5218e9ce645ed89200 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 22 Oct 2023 18:08:48 +0300 Subject: [PATCH 2/4] languages/bash: use mkEnableOption for enable options --- modules/languages/bash/bash.nix | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/modules/languages/bash/bash.nix b/modules/languages/bash/bash.nix index 873857558..150ad0d16 100644 --- a/modules/languages/bash/bash.nix +++ b/modules/languages/bash/bash.nix @@ -61,11 +61,7 @@ in { enable = mkEnableOption "Bash language support"; treesitter = { - enable = mkOption { - description = "Bash treesitter"; - type = types.bool; - default = config.vim.languages.enableTreesitter; - }; + enable = mkEnableOption "Bash treesitter" // {default = config.vim.languages.enableTreesitter;}; package = lib.nvim.types.mkGrammarOption pkgs "bash"; }; @@ -106,11 +102,8 @@ in { }; extraDiagnostics = { - enable = mkOption { - description = "Enable extra Bash diagnostics"; - type = types.bool; - default = config.vim.languages.enableExtraDiagnostics; - }; + enable = mkEnableOption "extra Bash diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;}; + types = lib.nvim.types.diagnostics { langDesc = "Bash"; inherit diagnostics; From 0e85c54a9ab33abf86bda676dc70456e3dea4a4f Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 22 Oct 2023 18:10:28 +0300 Subject: [PATCH 3/4] lib/languages: add back HM notice --- lib/languages.nix | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/languages.nix b/lib/languages.nix index f07880b95..18cfc3711 100644 --- a/lib/languages.nix +++ b/lib/languages.nix @@ -1,4 +1,7 @@ +# From home-manager: https://github.com/nix-community/home-manager/blob/master/modules/lib/booleans.nix {lib}: { + # Converts a boolean to a yes/no string. This is used in lots of + # configuration formats. diagnosticsToLua = { lang, config, From 599d487487dbceb931fa8925dfb2534842493eb9 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 23 Oct 2023 03:49:27 +0300 Subject: [PATCH 4/4] languages/bash: do not enable in minimal configuration Co-authored-by: Ching Pei Yang <59727193+horriblename@users.noreply.github.com> --- configuration.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configuration.nix b/configuration.nix index ec777d3e4..2fdf65546 100644 --- a/configuration.nix +++ b/configuration.nix @@ -72,7 +72,7 @@ inputs: let python.enable = isMaximal; dart.enable = isMaximal; elixir.enable = false; - bash.enable = true; + bash.enable = isMaximal; }; vim.visuals = {