Merge pull request #1321 from ppenguin/improve-terraformls
Some checks are pending
Set up binary cache / cachix (default) (push) Waiting to run
Set up binary cache / cachix (maximal) (push) Waiting to run
Set up binary cache / cachix (nix) (push) Waiting to run
Treewide Checks / Validate flake (push) Waiting to run
Treewide Checks / Check formatting (push) Waiting to run
Treewide Checks / Check source tree for typos (push) Waiting to run
Treewide Checks / Validate documentation builds (push) Waiting to run
Treewide Checks / Validate documentation builds-1 (push) Waiting to run
Treewide Checks / Validate documentation builds-2 (push) Waiting to run
Treewide Checks / Validate documentation builds-3 (push) Waiting to run
Treewide Checks / Validate hyperlinks in documentation sources (push) Waiting to run
Treewide Checks / Validate Editorconfig conformance (push) Waiting to run
Build and deploy documentation / Check latest commit (push) Waiting to run
Build and deploy documentation / publish (push) Blocked by required conditions

terraform/hcl: improve options; fix terraform-cli error; add tofu-ls
This commit is contained in:
raf 2026-03-17 14:36:48 +03:00 committed by GitHub
commit a7cfe3c6dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 103 additions and 26 deletions

View file

@ -20,6 +20,9 @@
];
}
```
- `languages.{terraform,hcl}`: LSP servers now default to `tofu-ls`. While this
is unlikely to cause any noticeable change in behavior or breakage, it's
mentioned just in case.
Some other settings and commands are now deprecated but are still supported.
@ -99,6 +102,14 @@
- `toggleterm` open map now also works when in terminal mode
[ppenguin](https://github.com/ppenguin):
- Improved/harmonized for `terraform` and `hcl`:
- formatting (use `terraform fmt` or `tofu fmt` for `tf` files)
- LSP config
- Added `tofu` and `tofu-ls` as (free) alternative to `terrraform` and
`terraform-ls`
[jtliang24](https://github.com/jtliang24):
- Updated nix language plugin to use pkgs.nixfmt instead of

View file

@ -8,20 +8,27 @@
inherit (lib.options) mkEnableOption mkOption literalExpression;
inherit (lib.meta) getExe;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.types) bool enum listOf;
inherit (lib.types) enum listOf;
inherit (lib.nvim.types) mkGrammarOption deprecatedSingleOrListOf;
inherit (lib.nvim.attrsets) mapListToAttrs;
cfg = config.vim.languages.hcl;
defaultServers = ["terraform-ls"];
defaultServers = ["tofuls-hcl"];
servers = {
terraform-ls = {
terraformls-hcl = {
enable = true;
cmd = [(getExe pkgs.terraform-ls) "serve"];
filetypes = ["terraform" "terraform-vars"];
filetypes = ["hcl"];
root_markers = [".git"];
};
tofuls-hcl = {
enable = true;
cmd = [(getExe pkgs.tofu-ls) "serve"];
filetypes = ["hcl"];
root_markers = [".terraform" ".git"];
};
# TODO: package nomad-lsp and offer as an option here too
};
defaultFormat = ["hclfmt"];
@ -29,6 +36,11 @@
hclfmt = {
command = getExe pkgs.hclfmt;
};
nomad-fmt = {
command = getExe pkgs.nomad;
args = ["fmt" "$FILENAME"];
stdin = false;
};
};
in {
options.vim.languages.hcl = {
@ -66,7 +78,7 @@ in {
defaultText = literalExpression "config.vim.languages.enableFormat";
};
type = mkOption {
type = deprecatedSingleOrListOf "vim.language.hcl.format.type" (enum (attrNames formats));
type = listOf (enum (attrNames formats));
default = defaultFormat;
description = "HCL formatter to use";
};
@ -97,18 +109,21 @@ in {
}
'';
}
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.package];
})
(mkIf cfg.lsp.enable {
vim.lsp.servers =
vim = {
lsp.servers =
mapListToAttrs (n: {
name = n;
value = servers.${n};
})
cfg.lsp.servers;
};
})
(mkIf cfg.format.enable {

View file

@ -9,23 +9,43 @@
inherit (lib.modules) mkIf mkMerge;
inherit (lib.meta) getExe;
inherit (lib.types) enum listOf;
inherit (lib.nvim.types) mkGrammarOption;
inherit (lib.nvim.types) mkGrammarOption deprecatedSingleOrListOf;
inherit (lib.nvim.attrsets) mapListToAttrs;
cfg = config.vim.languages.terraform;
defaultServers = ["terraformls"];
defaultServers = ["tofuls-tf"];
servers = {
terraformls = {
terraformls-tf = {
enable = true;
cmd = [(getExe pkgs.terraform-ls) "serve"];
filetypes = ["terraform" "terraform-vars"];
filetypes = ["terraform" "terraform-vars" "tf"];
root_markers = [".terraform" ".git"];
};
tofuls-tf = {
enable = true;
cmd = [(getExe pkgs.tofu-ls) "serve"];
filetypes = ["terraform" "terraform-vars" "tf"];
root_markers = [".terraform" ".git"];
};
};
defaultFormat = ["tofu-fmt"];
formats = {
tofu-fmt = {
command = "${getExe pkgs.opentofu}";
args = ["fmt" "$FILENAME"];
stdin = false;
};
terraform-fmt = {
command = "${getExe pkgs.terraform}";
args = ["fmt" "$FILENAME"];
stdin = false;
};
};
in {
options.vim.languages.terraform = {
enable = mkEnableOption "Terraform/HCL support";
enable = mkEnableOption "Terraform support";
treesitter = {
enable =
@ -44,14 +64,28 @@ in {
default = config.vim.lsp.enable;
defaultText = literalExpression "config.vim.lsp.enable";
};
servers = mkOption {
type = listOf (enum (attrNames servers));
default = defaultServers;
description = "Terraform LSP server to use";
};
};
format = {
enable =
mkEnableOption "Enable Terraform formatting"
// {
default = config.vim.languages.enableFormat;
defaultText = literalExpression "config.vim.languages.enableFormat";
};
type = mkOption {
type = listOf (enum (attrNames formats));
default = defaultFormat;
description = "Terraform formatter to use";
};
};
};
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
@ -59,12 +93,29 @@ in {
})
(mkIf cfg.lsp.enable {
vim.lsp.servers =
vim = {
lsp.servers =
mapListToAttrs (n: {
name = n;
value = servers.${n};
})
cfg.lsp.servers;
};
})
(mkIf cfg.format.enable {
vim.formatter.conform-nvim = {
enable = true;
setupOpts = {
formatters_by_ft.terraform = cfg.format.type;
formatters =
mapListToAttrs (name: {
inherit name;
value = formats.${name};
})
cfg.format.type;
};
};
})
]);
}