diff options
author | Linnnus <[email protected]> | 2024-10-12 19:08:21 +0200 |
---|---|---|
committer | Linnnus <[email protected]> | 2024-10-12 19:21:53 +0200 |
commit | 090c4121e805b9eff89b4fc9fc337b320a512c07 (patch) | |
tree | 56a1460a73e1af56b62acea276a143a3e483b895 | |
parent | 3c70da63a972c20b07f18679858c1a51b80d6e68 (diff) |
home/nvim: Assert Lua configuration is valid
-rw-r--r-- | modules/home-manager/assert-valid-neovim-config/default.nix | 43 | ||||
-rw-r--r-- | modules/home-manager/default.nix | 1 |
2 files changed, 44 insertions, 0 deletions
diff --git a/modules/home-manager/assert-valid-neovim-config/default.nix b/modules/home-manager/assert-valid-neovim-config/default.nix new file mode 100644 index 0000000..218436a --- /dev/null +++ b/modules/home-manager/assert-valid-neovim-config/default.nix @@ -0,0 +1,43 @@ +# This HM module asserts the nvim configuration has valid syntax during +# building. This shortens the iteration cycle a bit when working on the Lua +# config as it means I don't have to launch `nvim` only to discover I missed a +# comma. +# +# This approach uses import-from-derivation which _does_ slow down evaluation. +# +# This approach should not affect the final closure size as there is no +# reference to `checkDerivation` in `build.toplevel`. It does however generate +# a bit of garbage in the Nix store every time the Neovim configuration +# changes. +{ + pkgs, + config, + ... +}: let + checkDerivation = + pkgs.runCommand "successful-syntax-check.nix" { + passAsFile = ["luaConfig"]; + luaConfig = config.programs.neovim.generatedConfigs.lua; + } '' + # Print listing to give context to error messages when viewing logs (as + # suggested by the assertion's message). + nl -b a $luaConfigPath + + # `luac` compiles Lua to bytecode, but we can ask it to only parse the input file with `-p`. + # Result is exported via import-from-derivation. + if ${pkgs.lua}/bin/luac -p -- $luaConfigPath; then + echo true >$out + else + echo false >$out + fi + ''; + + isValidSyntax = import "${checkDerivation}"; +in { + assertions = [ + { + assertion = isValidSyntax; + message = "Syntax error in Neovim configuration. Run `nix log ${checkDerivation.drvPath}` for more information."; + } + ]; +} diff --git a/modules/home-manager/default.nix b/modules/home-manager/default.nix index fc8366d..f2b2cf3 100644 --- a/modules/home-manager/default.nix +++ b/modules/home-manager/default.nix @@ -4,4 +4,5 @@ { iterm2 = import ./iterm2; git-credential-lastpass = import ./git-credential-lastpass; + assert-valid-neovim-config = import ./assert-valid-neovim-config; } |