diff options
Diffstat (limited to 'modules/home-manager/assert-valid-neovim-config/default.nix')
-rw-r--r-- | modules/home-manager/assert-valid-neovim-config/default.nix | 43 |
1 files changed, 43 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."; + } + ]; +} |