nix-lang
Parser and printers for the Nix language
nix-lang
nix-lang is a parser, exact printer, and formatter for the Nix language built around a Trees that Grow style AST. Inspired by ghc-exactprint, it supports lossless round-tripping for parsed source while preserving comments, formatting, and multiline structure after modifying the syntax tree. It also provides a fresh syntax tree for generated Nix code together with an RFC 166-oriented formatter for canonical output.
nix-lang-qq provides quasiquoter for building fresh Nix expressions in Haskell file and supports embedding Haskell expressions.
formatExpr [nixQQ| { value = 1; } |]
-- => {
-- value = 1;
-- }
formatExpr [nixQQ| %%(foldl (\acc _ -> mkApp (mkVar "f") acc) (mkVar "x") [1..10 :: Int])]
-- => f (f (f (f (f (f (f (f (f (f x)))))))))
Known unsupported syntax
Although it now parses the entire nixpkgs, some valid syntax is not supported.
- Legacy let
let { x = 233; body = x; }
- Floating number starting with a decimal point
.233
- Chained has-attribute expressions
a ? b ? c ? d
- Operator without white spaces
1+-1
- containers ^>=0.7
- megaparsec ^>=9.7.0
- mtl ^>=2.3.1
- parser-combinators ^>=1.3.1
- prettyprinter ^>=1.7.1
- syb >=0.7.2 && <0.7.5
- template-haskell ^>=2.22.0
- text ^>=2.1.3
- 0.1.0.0