What is a Nix expression in regard to Nix package management?

旧巷老猫 提交于 2019-12-10 15:46:51

问题


Even after reading the Nix manuals it is still confusing what Nix expressions really are. Sometimes they are referred to as derivations, but store derivations also mean something else.


回答1:


In Nix, a Nix expression is just a general term for any type of value that you can write in the Nix language. A Nix expression can be a set, a list, a number, a string, a function, a name, a arithmetic operation, a function call, and much more.

Nix expressions can contain other Nix expressions: for example, the expression 1 + 2 contains two expressions inside it: 1 and 2.

People often like to write complicated Nix expressions that represent how to build a piece of software. Those expressions are really just sets with some special attributes. The Nix software can evaluate such an expression and turn it into a .drv file (a very simple, compact way of describing how to build some software), which can then be built.

You can do lots of things with the Nix language and Nix expressions that don't involve derivations or building software. The nix eval command lets you evaluate a Nix expression. Run nix eval --help to see its help screen, or run these commands to evaluate some simple expressions:

nix eval '(1 + 2)'  # gives 3

nix eval '({ a = 1; b = 2; }.a)'  # gives 1

(For some reason, this command seems to require parentheses to be put around most of the Nix expressions it evaluates, but that just seems like a bug or odd design choice, and surrounding parentheses are not an essential part of every Nix expression.)




回答2:


A Nix expression is a set of instructions describing how to build a software component (package, project, application, etc.) using the Nix purely functional language.

To quote Gabriel Gonzalez: "You can think of a derivation as a language-agnostic recipe for how to build something (such as a Haskell package)."


Nix expressions are also commonly called derivations (as in Nix derivation expressions), but

*------------------------------------------------------*
|                                                      |
|       STORE DERIVATION =/= NIX EXPRESSION            |
|                                                      |
*------------------------------------------------------*
|                                                      |
| NIX EXPRESSION == function                           |
|                                                      |
| ( Describes how to build a component. That is, how ) |
| ( to  compose  its  input parameters, which can be ) |
| ( other components as well.                        ) |
|                                                      |
| STORE DERIVATION == function application             |
|                                                      |
| ( Call a  Nix  expression with concrete arguments. ) |
| ( Corollary: a single Nix  expression  can produce ) |
| ( different derivations depending on the inputs.   ) |
|                                                      |
*------------------------------------------------------*

The purpose of Nix expressions is to produce a store derivation that can be built into a component (executable, library, etc.).

For context:

Image taken from Eelco Dolstra's PhD thesis, section "2.4 Store derivations".

Extra

Normal form of a Nix expression

According to section "5.4 Translating Nix expressions to store derivations" in Eelco Dolstra's PhD thesis:

The normal form [of a Nix expression] should be

  • a call to derivation, or

  • a nested structure of lists and attribute sets that contain calls to derivation.

In any case, these derivation Nix expressions are subsequently translated to store derivations.

What is a software component?

A package, application, development environment, software library, etc.

More formally from "3.1 What is a component?" in Eelco Dolstra's PhD thesis:

A software component is                      

    *-------------------------------------*
1.  | a software artifact that is subject |
    | to automatic composition            |
    *-------------------------------------*

    It can require, and be required by,   
    other components.                     

    *----------------------*              
2.  | a unit of deployment |              
    *----------------------*    

(That entire section is worth reading.)



来源:https://stackoverflow.com/questions/58243554/what-is-a-nix-expression-in-regard-to-nix-package-management

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!