Nix flake check to check for error messages

I am trying to write a nix flake check to see if neovim has any configuration errors.

is there a way to write a check to see if any output is generated from the following command? “nvim --headless -c q”

thanks have not found much documentation on nix checks

you can look at how some (neo)vim plugins are tested e.g, pkgs/applications/editors/vim/plugins/vim-command-check-hook.sh .

Checks are really just derivations. You could do something like this:

{
  inputs = {
    nixpkgs.url = "nixpkgs";
    nix-filter.url = "github:numtide/nix-filter";
  };

  outputs = let
    system = "x86_64-linux"; # Use flake-utils if this is too restrictive
    pkgs = nixpkgs.legacyPackages.${system}; # I think, been a while since I last used this without import
  in
    {
      self,
      nixpkgs,
      nix-filter,
    }: {
      checks."${system}" = {
        vim = pkgs.stdenv.mkDerivation {
          # Using `nix-filter` to prevent issues with the directory
          # name of `self` not being reproducible, and so the check
          # doesn't want to re-run every time any file is changed.
          src = nix-filter {
            root = self;
            include = [
              ./config/nvimrc # I presume
            ];
          };
        };

        buildInputs = [
          self.packages."${system}".vim # If you have a custom vim package
        ];

        doCheck = true;
        dontConfigure = true;
        dontBuild = true;
        dontInstall = true;

        checkPhase = ''
          # We *must* create some output, usually contains test logs for checks
          mkdir -p "$out"

          # Probably want to do something to ensure your config file is read, too
          nvim --headless -c q > "$out/nvim.log"

          if [ -z "$(cat "$out/nvim.log")" ]; then
              exit 1
          fi
        '';
      };
    };
}
1 Like

working solution, thanks for the help

checks = {
  neovim-check-config = pkgs.runCommand "neovim-check-config" {
    buildInputs = [pkgs.git];
  } ''
    # We *must* create some output, usually contains test logs for checks
    mkdir -p "$out"

    # Probably want to do something to ensure your config file is read, too
    export HOME=$TMPDIR
    ${self.packages."${system}".default}/bin/nvim --headless -c "q" 2> "$out/nvim.log"

    if [ -n "$(cat "$out/nvim.log")" ]; then
      echo "output: "$(cat "$out/nvim.log")""
      exit 1
    fi
'';
 };
1 Like