Code Monkey home page Code Monkey logo

nix-linguist's Introduction

Warning
As of July 2023, GitHub Linguist has began adopting tree-sitter based grammars.
The Nix Community now maintains the relevant language grammar
https://github.com/github-linguist/linguist/commit/e855ef2b6f90c34074061a2e17acbe853e61b483

Note
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
As such, for other parsing usages, this syntax file should not be used as it is lax (not strict).

Nix

Note
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 the throw.

    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.

This section describes the language constructs in Nix. It is a small language and most of these should be self-explanatory.

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; }

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.


built with nix

via the MIT License

nix-linguist's People

Contributors

sambacha avatar supersandro2000 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

supersandro2000

nix-linguist's Issues

Feat: provide `nix shell` highlighting

currently nix-shell does not have proper highlighting

bash

#!/usr/bin/env nix-shell
#!nix-shell -i bash -p nix-prefetch-git

nix-prefetch-git https://github.com/nixos/nixpkgs.git \
                 --rev refs/heads/nixos-unstable-small > ./nix/nixpkgs.json

nix

#!/usr/bin/env nix-shell
#!nix-shell -i bash -p nix-prefetch-git

nix-prefetch-git https://github.com/nixos/nixpkgs.git \
                 --rev refs/heads/nixos-unstable-small > ./nix/nixpkgs.json

console

#!/usr/bin/env nix-shell
#!nix-shell -i bash -p nix-prefetch-git

nix-prefetch-git https://github.com/nixos/nixpkgs.git \
                 --rev refs/heads/nixos-unstable-small > ./nix/nixpkgs.json

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.