package nix-syntax

All checked builds pass
  1. Overview
  2. Docs
Parser for the Nix expression language

Install

dune-project
 Dependency

Authors

Maintainers

Sources

nix-syntax-0.2.0.tbz
sha256=aaac044e2253202bbdb499f167a1158813d47b5db8d8b0b3bbaba6e2afb4059e
sha512=01ec7378562a3b2687dad4dfd073be4cc3a2d321ccd42f543fa9916410d7f7534a7a4a8dbc64ceba425295b77bf64dd20dc71dd76685c4ded76acc19e560c6ae

Description

nix-syntax parses Nix expressions into a syntax tree that keeps the source locations of every node and the comments, so tools can inspect and rewrite Nix files without losing their layout.

Tags

nix parser

Added to opam-repository:

README

nix-syntax

A parser for the Nix expression language, written in OCaml.

It was called flocaml until version 0.1.2.

nix-syntax reads Nix expressions into a syntax tree that follows the concrete syntax. Nothing is desugared, parentheses are kept, and every node carries its source location. Comments are collected alongside the tree, and the source can also be read as a lossless list of tokens. This makes it suitable for tools that inspect or rewrite Nix files.

Status

nix-syntax is in early development, and its API may still change.

On every .nix file in nixpkgs, nix-syntax produces the same expressions as nix-instantiate --parse. test/oracle/compare.sh DIR runs this comparison on the files under DIR, and test/oracle/cases.sh runs it on expressions that Nix rejects after parsing. test/roundtrip checks on the same files that printing a tree gives it back, and that the tokens give back the source.

Usage

match Nix_syntax.parse_file "default.nix" with
| Ok { expr; comments } -> Format.printf "%a@." Nix_syntax.Printer.expr expr
| Error e -> Format.eprintf "%a@." Nix_syntax.Syntax_error.pp e

Besides the parser, the library has the following modules.

Module

Purpose

Ast

The syntax tree

Check

Finds the errors that Nix reports after parsing, such as an attribute defined twice, a duplicate function argument or an unbound variable

Printer

Prints a tree as Nix source, with the parentheses it needs

Token

Splits the source into tokens that cover every byte, whitespace and comments included

Ast_iterator, Ast_mapper

Visit and rewrite trees, in the style of OCaml's compiler-libs

Locate

Finds the nodes at a point of the source

Line_index

Converts byte offsets to lines and columns in bytes, UTF-16 or code points, for editors

Literal

Computes the values of string literals and attribute names as Nix does

Ast_printer

Dumps a tree for debugging

The nix-syntax command gives access to most of them.

$ nix-syntax parse -e '{ a ? 1 }: a + 1'
(Lambda (Param_pattern (a (Int 1))) (Add (Var a) (Int 1)))
$ nix-syntax print -e '{a=1;}'
{ a = 1; }
$ nix-syntax tokens default.nix
$ nix-syntax check default.nix

Experimental syntax such as the pipe operators |> and <| is rejected unless enabled with --feature pipe-operators, or with ~features in the API.

Building

nix-syntax needs OCaml 4.14 or later and dune 3.0 or later.

With Nix, nix build builds the package and runs the tests, and nix develop gives a shell for working on it.

nix build
nix develop -c dune test

With opam, install the dependencies and build with dune.

opam install . --deps-only --with-test
dune build

License

ISC

Dependencies (6)

  1. cmdliner >= "1.3"
  2. sedlex >= "3.2"
  3. menhirLib >= "20230608"
  4. menhir build & >= "20230608"
  5. ocaml >= "4.14"
  6. dune >= "3.0"

Dev Dependencies (1)

  1. odoc with-doc

Optional Dependencies

None

Used by

None

Conflicts

None

Build status

Results of building nix-syntax with each compiler on check.ci.ocaml.org, run of against opam-repository 6e2873f. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
0.2.0 (this version) n/a good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure