Home

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

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:

SyntaxDescription
+, -, *, /Numerical operations
+String concatenation
++List concatenation
==Equality
>, >=, <, <=Ordering comparators
&&Logical AND
`
e1 -> e2Logical implication (i.e. `!e1
!Boolean negation
set.attrAccess attribute attr in attribute set set
set ? attributeTest whether attribute set contains an attribute
left // rightMerge 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.


built with nix

via the MIT License