nix

How can I test the syntax of a single nix file?


How can I test the syntax of a single nix file? To check if it compiles correctly.

Often I'm working on a Nixos config and it would be useful to have a faster way to check for syntax errors as opposed to nixos-rebuild build as it takes at least a few seconds.


Solution

  • You can use nix-instantiate:

    nix-instantiate --parse 'path/to/file.nix'
    

    The return value will be zero if the file can be parsed correctly, or nonzero if there are parse errors.

    This will also print out the parsed abstract syntax tree on the terminal. The --quiet option won't suppress that, so you'll need to do:

    nix-instantiate --parse 'path/to/file.nix' >/dev/null