diff --git a/modules/lsp/servers/custom/hls.nix b/modules/lsp/servers/custom/hls.nix new file mode 100644 index 00000000..61937d56 --- /dev/null +++ b/modules/lsp/servers/custom/hls.nix @@ -0,0 +1,38 @@ +{ + lib, + pkgs, + config, + options, + ... +}: +{ + options = { + installGhc = lib.mkOption { + type = lib.types.bool; + default = false; + example = true; + description = "Whether to install `ghc`."; + }; + + ghcPackage = lib.mkPackageOption pkgs "ghc" { }; + }; + + config = lib.mkIf config.enable { + warnings = lib.nixvim.mkWarnings "lsp.servers.hls" { + when = options.installGhc.highestPrio == 1500; + message = '' + `hls` relies on `ghc` (the Glasgow Haskell Compiler). + - Set `${options.installGhc} = true` to install it automatically with Nixvim. + You can customize which package to install by changing `${options.ghcPackage}`. + - Set `${options.installGhc} = false` to not have it install through Nixvim. + By doing so, you will dismiss this warning. + ''; + }; + + packages = lib.mkIf config.installGhc { + ${if config.packageFallback then "suffix" else "prefix"} = [ + config.ghcPackage + ]; + }; + }; +} diff --git a/plugins/lsp/language-servers/hls.nix b/plugins/lsp/language-servers/hls.nix index 8a7af9d8..5903ec5f 100644 --- a/plugins/lsp/language-servers/hls.nix +++ b/plugins/lsp/language-servers/hls.nix @@ -1,40 +1,44 @@ { - config, lib, pkgs, + config, + options, ... }: let cfg = config.plugins.lsp.servers.hls; - inherit (lib) types; + opts = options.plugins.lsp.servers.hls; + enabled = config.plugins.lsp.enable && cfg.enable; - ghcPackage = lib.optional (cfg.installGhc == true) cfg.ghcPackage; + # The new `installGhc` option doesn't support null values, so check how the old value is defined + installGhcValue = + lib.modules.mergeDefinitions opts.installGhc.loc opts.installGhc.type + opts.installGhc.definitionsWithLocations; + useInstallGhcValue = installGhcValue.optionalValue.value or null != null; in { options.plugins.lsp.servers.hls = { installGhc = lib.mkOption { - type = with types; nullOr bool; + type = lib.types.nullOr lib.types.bool; default = null; example = true; description = "Whether to install `ghc`."; + apply = v: if enabled then config.lsp.servers.hls.installGhc else v; }; - ghcPackage = lib.mkPackageOption pkgs "ghc" { }; + ghcPackage = lib.mkPackageOption pkgs "ghc" { } // { + apply = v: if enabled then config.lsp.servers.hls.ghcPackage else v; + }; }; - config = lib.mkIf cfg.enable { - warnings = lib.nixvim.mkWarnings "plugins.lsp.servers.hls" { - when = cfg.installGhc == null; - message = '' - `hls` relies on `ghc` (the Glasgow Haskell Compiler). - - Set `plugins.lsp.servers.hls.installGhc = true` to install it automatically with Nixvim. - You can customize which package to install by changing `plugins.lsp.servers.hls.ghcPackage`. - - Set `plugins.lsp.servers.hls.installGhc = false` to not have it install through Nixvim. - By doing so, you will dismiss this warning. - ''; + config = lib.mkIf enabled { + lsp.servers.hls = { + installGhc = lib.mkIf useInstallGhcValue ( + lib.modules.mkAliasAndWrapDefsWithPriority lib.id opts.installGhc + ); + ghcPackage = lib.mkIf (opts.ghcPackage.highestPrio < 1500) ( + lib.modules.mkAliasAndWrapDefsWithPriority lib.id opts.ghcPackage + ); }; - - extraPackages = lib.optionals (!cfg.packageFallback) ghcPackage; - extraPackagesAfter = lib.optionals cfg.packageFallback ghcPackage; }; } diff --git a/tests/test-sources/modules/lsp.nix b/tests/test-sources/modules/lsp.nix index ea6c36bb..b819668b 100644 --- a/tests/test-sources/modules/lsp.nix +++ b/tests/test-sources/modules/lsp.nix @@ -187,6 +187,7 @@ hls = { enable = true; packageFallback = true; + installGhc = true; }; }; }; @@ -219,6 +220,7 @@ assertPrefix "nil" nil_ls.package ++ assertSuffix "rust-analyzer" rust_analyzer.package ++ assertSuffix "haskell-language-server" hls.package + ++ assertSuffix "ghc" hls.ghcPackage ); };