summaryrefslogtreecommitdiff
path: root/modules/home-manager/assert-valid-neovim-config/default.nix
blob: 218436a56f82eb8ef0c6359db9817106c3530f58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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.";
    }
  ];
}