|
|
(87 intermediate revisions by 42 users not shown) |
Line 1: |
Line 1: |
| | <languages/> |
| | <translate> |
| | <!--T:1--> |
| | The Nix language is designed for conveniently creating and composing <i>derivations</i> – precise descriptions of how contents of existing files are used to derive new files. |
|
| |
|
| {{Expansion|This article is incomplete.}} | | <!--T:2--> |
| | {{Note|Check the [https://nixos.org/manual/nix/stable/language/ Nix Reference Manual on the Nix Language] for up-to-date documentation and [https://nix.dev/tutorials/nix-language Nix language basics] for a gentle introduction.}} |
| | The .nix files are written in the Nix language. While being a NixOS user doesn't demand complete mastery of Nix, customizing code snippets is necessary. |
|
| |
|
| This [[:Category:Discussion|discussion]] article covers the syntax, semantics, typing, compilation, tooling and libraries of the Nix Expression Language.
| | <!--T:3--> |
| | == See also == |
| | * [[Nix Expression Language: Learning resources|Learning resources]] |
| | * [[Editor Modes for Nix Files]] |
| | * [[Nix Language: Tips & Tricks]] |
| | * [[Nix Language Quirks]] |
|
| |
|
| <blockquote>The Nix expression language is a pure, lazy, functional language. Purity means that operations in the language don't have side-effects (for instance, there is no variable assignment). Laziness means that arguments to functions are evaluated only when they are needed. Functional means that functions are “normal” values that can be passed around and manipulated in interesting ways. The language is not a full-featured, general purpose language. Its main job is to describe packages, compositions of packages, and the variability within packages.</blockquote> | | <!--T:4--> |
| <cite>— From the [https://nixos.org/nix/manual/#ch-expression-language nix manual]</cite>
| | [[Category:Pedias]] |
| | |
| The language was designed especially for the [[Nix Package Manager]].
| |
| | |
| == Language Features ==
| |
| This section describes the main language features of the nix expression language
| |
| === lazy evaluation ===
| |
| | |
| Not all expressions in nixpkgs will be evaluated and instantiated as nix performs evaluation only when needed for a finished output. In the following example <code>abort</code> will never be triggered as the variable it belogs to is unused:
| |
| <syntaxHighlight lang=nix>
| |
| let
| |
| a = abort "will never happen";
| |
| b = "hello";
| |
| c = "world"
| |
| in b + c
| |
| </syntaxHighlight>
| |
| | |
| === functional paradigm ===
| |
| Functional Programming is a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions or declarations instead of statements.
| |
| | |
| see also: [https://en.wikipedia.org/wiki/Functional_programming]
| |
| | |
| === purity ===
| |
| | |
| A pure function is a function where the return value is only determined by its input values, without observable side effects. In Nix, all build operations try to be as pure as possible to achieve reproducible builds. This means that wherever you build the packages as few side effects as possible should have an impact onto the build.
| |
| | |
| === expressions ===
| |
| | |
| When Nix tutorials talk about '''Nix Expressions''' they typically mean the definition of a function with multiple inputs which as a result in a derivation. However a Nix expression can be everything, from a simple string, to function to a set of expressions.
| |
| | |
| === types ===
| |
| | |
| TODO
| |
| | |
| === functions ===
| |
| | |
| TODO
| |
| | |
| === imports ===
| |
| | |
| TODO
| |
| | |
| === notable constructs ===
| |
| Nix looks a lot like JSON with functions but also provides a number of very specialized constructs which can help you build clean and easy to read expressions. In this sub-chapter the most notable constructs will be shown by example:
| |
| | |
| ==== <code>with</code> statement ====
| |
| The <code>with</code> statement introduces the lexical scope of a set into the expression which follows.
| |
| Common usages are:
| |
| ===== '''On top of expressions''' =====
| |
| You will see the with statement a lot at the beginning of expression definition. Most of the time it is used to load the lib functions into the namespace for quick access.
| |
| <syntaxHighlight lang=nix>
| |
| {lib, ... }:
| |
| | |
| with lib;
| |
| {
| |
| options = {
| |
| networking.hosts = mkOption {
| |
| type = with types; attrsOf ( listOf str);
| |
| default = {};
| |
| };
| |
| };
| |
| ...
| |
| }
| |
| </syntaxHighlight>
| |
| instead of:
| |
| <syntaxHighlight lang=nix>
| |
| {lib, ... }:
| |
| {
| |
| options = {
| |
| networking.hosts = lib.mkOption {
| |
| type = lib.types.attrsOf ( lib.types.listOf lib.types.str);
| |
| default = {};
| |
| };
| |
| };
| |
| ...
| |
| }
| |
| </syntaxHighlight>
| |
| | |
| ===== '''In package input definitions''' =====
| |
| <syntaxHighlight lang=nix>
| |
| {pkgs}:
| |
| {
| |
| ...
| |
| buildInputs = with pkgs; [ curl php coreutils procps ffmpeg ];
| |
| }
| |
| </syntaxHighlight>
| |
| Instead of :
| |
| <syntaxHighlight lang=nix>
| |
| {pkgs}:
| |
| {
| |
| ...
| |
| buildInputs = [ pkgs.curl pkgs.php pkgs.coreutils pkgs.procps pkgs.ffmpeg ];
| |
| }
| |
| </syntaxHighlight>
| |
| ===== '''In the package meta tag''' =====
| |
| <syntaxHighlight lang=nix>
| |
| {stdenv, ...}:
| |
| {
| |
| ...
| |
| meta = with stdenv.lib; {
| |
| license = with licenses; [ lgp3 gpl3 ];
| |
| maintainers = with maintainers; [ adisbladis lassulus ];
| |
| };
| |
| }
| |
| </syntaxHighlight>
| |
| Instead of :
| |
| <syntaxHighlight lang=nix>
| |
| {stdenv, ...}:
| |
| {
| |
| ...
| |
| meta = {
| |
| license = [ stdenv.lib.licenses.lgp3 stdenv.lib.licenses.gpl3 ];
| |
| maintainers = [ stdenv.lib.maintainers.adisbladis stdenv.lib.maintainers.lassulus ];
| |
| };
| |
| }
| |
| </syntaxHighlight>
| |
| ===== '''In a default.nix of an external package''' =====
| |
| <syntaxHighlight language=nix>
| |
| with import <nixpkgs> {};
| |
| stdenv.mkDerivation rec {
| |
| name = "mytool-env";
| |
| src = ./.;
| |
| buildInputs = with pkgs;[
| |
| python34
| |
| python34Packages.docopt
| |
| ];
| |
| | |
| shellHook =''
| |
| export HISTFILE=$PWD/histfile
| |
| '' ;
| |
| }
| |
| </syntaxHighlight>
| |
| ==== <code>let ... in</code> statement ====
| |
| TODO
| |
| ==== <code>inherit</code> statement ====
| |
| TODO
| |
| ==== <code>rec</code> statement ====
| |
| TODO
| |
| | |
| == Learning Resources ==
| |
| | |
| The [https://nixos.org/nix/manual/#ch-expression-language manual] provides a '''reference''' of the Nix language. All language constructs you may use in nix are defined here, together with code snippets.
| |
| | |
| [https://medium.com/@MrJamesFisher/nix-by-example-a0063a1a4c55 Nix By Example] is a step-by-step tutorial.
| |
| The [https://nixos.org/nixos/nix-pills/index.html nix pills] also provide a lot of insight into the language and functional package management in general.
| |
| | |
| == Development Tools ==
| |
| | |
| === Syntax Highlighting & Editor Modes ===
| |
| | |
| Nix language has decent syntax highlighting (SH) support among popular code editors, but refactoring/autocomplete is still rare.
| |
| | |
| Reference: [[Editor Modes for Nix Files]]
| |
| | |
| [[Category:Discussion]] | |
| [[Category:Nix Language]] | | [[Category:Nix Language]] |
| [[Category:Incomplete]]
| | </translate> |