Awesome
nix-linguist
Warning <br /> As of July 2023, GitHub Linguist has began adopting tree-sitter based grammars. <br /> The Nix Community now maintains the relevant language grammar <br /> https://github.com/github-linguist/linguist/commit/e855ef2b6f90c34074061a2e17acbe853e61b483
Note <br /> The Legacy GitHub Linguist Grammar for Nix
Overview
This repo provide's GitHub's Linguist with a grammar file to enhance syntax highlighting when viewed on github.com
.
Warning <br /> As such, for other parsing usages, this syntax file should not be used as it is lax (not strict).
Nix
Note <br /> source, nixery 1pager
-
purely functional. It has no concept of sequential steps being executed, any dependency between operations is established by depending on data from previous operations.
Everything in Nix is an expression, meaning that every directive returns some kind of data.
Evaluating a Nix expression yields a single data structure, it does not execute a sequence of operations.
Every Nix file evaluates to a single expression.
-
lazy. It will only evaluate expressions when their result is actually requested.
For example, the builtin function
throw
causes evaluation to stop. Entering the following expression works fine however, because we never actually ask for the part of the structure that causes thethrow
.let attrs = { a = 15; b = builtins.throw "Oh no!"; }; in "The value of 'a' is ${toString attrs.a}"
-
purpose-built. Nix only exists to be the language for Nix, the package manager. While people have occasionally used it for other use-cases, it is explicitly not a general-purpose language.
Language constructs
This section describes the language constructs in Nix. It is a small language and most of these should be self-explanatory.
Primitives / literals
Nix has a handful of data types which can be represented literally in source code, similar to many other languages.
# numbers
42
1.72394
# strings & paths
"hello"
./some-file.json
# strings support interpolation
"Hello ${name}"
# multi-line strings (common prefix whitespace is dropped)
''
first line
second line
''
# lists (note: no commas!)
[ 1 2 3 ]
# attribute sets (field access with dot syntax)
{ a = 15; b = "something else"; }
# recursive attribute sets (fields can reference each other)
rec { a = 15; b = a * 2; }
Operators
Nix has several operators, most of which are unsurprising:
Syntax | Description |
---|---|
+ , - , * , / | Numerical operations |
+ | String concatenation |
++ | List concatenation |
== | Equality |
> , >= , < , <= | Ordering comparators |
&& | Logical AND |
` | |
e1 -> e2 | Logical implication (i.e. `!e1 |
! | Boolean negation |
set.attr | Access attribute attr in attribute set set |
set ? attribute | Test whether attribute set contains an attribute |
left // right | Merge left & right attribute sets, with the right set taking precedence |
Make sure to understand the //
-operator, as it is used quite a lot and is probably the least familiar one.
Canonical Nix Grammar
canonical grammar for nix
can be sourced via NixOS/nix/blob/master/src/libexpr/lexer.l
Nix BNF Grammar
Another grammar spec format is available in BNF format, here is the grammar file.