#readwise # Nix language basics ![rw-book-cover](https://rdl.ink/render/https%3A%2F%2Fnix.dev%2Ftutorials%2Fnix-language) ## Metadata - Author: [[nix.dev]] - Full Title: Nix language basics - URL: https://nix.dev/tutorials/nix-language ## Summary The Nix language is used to create and manage derivations, which are detailed descriptions for generating new files from existing ones. This tutorial helps you understand the basic structure of Nix code, including functions and expressions, but does not cover all features in depth. For further learning, refer to the Nix manual and explore practical applications like creating shell environments and packaging software. ## Highlights **The Nix language is designed for conveniently creating and composing *derivations* – precise descriptions of how contents of existing files are used to derive new files.** It is a domain-specific, purely functional, lazily evaluated, dynamically typed programming language. ([View Highlight](https://read.readwise.io/read/01j9x179g4m9gm2qsgz9nfpyh5)) ^pm7mej --- If you are familiar with JSON, imagine the Nix language as *JSON with functions*. ([View Highlight](https://read.readwise.io/read/01j9x1mn383t0fq292ehgfy2e5)) --- `builtins` Also known as “primitive operations” or “primops”. **Nix comes with many functions that are built into the language. They are implemented in C++ as part of the Nix language interpreter.** Note The Nix manual lists all [Built-in Functions](https://nix.dev/manual/nix/stable/language/builtins.html), and shows how to use them. These functions are available under the `builtins` constant. ([View Highlight](https://read.readwise.io/read/01jcstqg92a307tb7f02qeve4f)) ^7dpsl8 --- `import` **Most built-in functions are only accessible through `builtins`. A notable exception is `import`, which is also available at the top level.** `import` takes a path to a Nix file, reads it to evaluate the contained Nix expression, and returns the resulting value. If the path points to a directory, the file `default.nix` in that directory is used instead. ([View Highlight](https://read.readwise.io/read/01jcstt1cty8vp152pv8ftfw5s)) --- **There is only one impurity in the Nix language that is relevant here: reading files from the file system as *build inputs*.** Build inputs are files that derivations refer to in order to describe how to derive new files. When run, a derivation will only have access to explicitly declared build inputs. **The only way to specify build inputs in the Nix language is explicitly with:** **• File system paths** **• Dedicated functions.** **Nix and the Nix language refer to files by their content hash.** If file contents are not known in advance, it’s unavoidable to read files during expression evaluation. Note Nix supports other types of impure expressions, such as [lookup paths](https://nix.dev/guides/best-practices#search-path) or the constant [`builtins.currentSystem`](https://nix.dev/manual/nix/stable/language/builtin-constants.html#builtins-currentSystem). We do not cover those here in more detail, as they do not matter for how the Nix language works in principle, and because they are discouraged for the very reason of breaking reproducibility. ([View Highlight](https://read.readwise.io/read/01jctf3fz6ceyj6c4tchftj59h)) --- **Whenever a file system path is used in [string interpolation](https://nix.dev/tutorials/nix-language#string-interpolation), the contents of that file are copied to a special location in the file system, the *Nix store*, as a side effect.** The evaluated string then contains the Nix store path assigned to that file. ([View Highlight](https://read.readwise.io/read/01jcvs47x00xk7e6zgrhenn47g)) --- `pkgs.lib` **The [`nixpkgs`](https://github.com/NixOS/nixpkgs) repository contains an attribute set called [`lib`](https://github.com/NixOS/nixpkgs/blob/master/lib/default.nix), which provides a large number of useful functions. They are implemented in the Nix language, as opposed to [`builtins`](https://nix.dev/tutorials/nix-language#builtins), which are part of the language itself.** Note **The Nixpkgs manual lists all [Nixpkgs library functions](https://nixos.org/manual/nixpkgs/stable/#sec-functions-library).** These functions are usually accessed through `pkgs.lib`, as the Nixpkgs attribute set is given the name `pkgs` by convention. ... For historical reasons, some of the functions in `pkgs.lib` are equivalent to [`builtins`](https://nix.dev/tutorials/nix-language#builtins) of the same name. --- **Derivations are at the core of both Nix and the Nix language:** **• The Nix language is used to describe derivations.** **• Nix runs derivations to produce *build results*.** **• Build results can in turn be used as inputs for other derivations.** **The Nix language primitive to declare a derivation is the built-in impure function `derivation`.** **It is usually wrapped by the Nixpkgs build mechanism `stdenv.mkDerivation`, which hides much of the complexity involved in non-trivial build procedures.** Note You will probably never encounter `derivation` in practice. Whenever you encounter `mkDerivation`, it denotes something that Nix will eventually *build*. Example: [a package using `mkDerivation`](https://nix.dev/tutorials/nix-language#mkderivation-example) **The evaluation result of `derivation` (and `mkDerivation`) is an [attribute set](https://nix.dev/tutorials/nix-language#attrset) with a certain structure and a special property: It can be used in [string interpolation](https://nix.dev/tutorials/nix-language#string-interpolation), and in that case evaluates to the Nix store path of its build result.** ([View Highlight](https://read.readwise.io/read/01jcvsa1e3sp3tkgg092ngaykt)) ---