Nix包管理器

From NixOS Wiki
Revision as of 11:49, 23 February 2025 by Mayer (talk | contribs) (Created page with "Nix命令被记录在[https://nixos.org/manual/nix/stable/command-ref/command-ref Nix参考手册]: 主要命令, 工具命令和实验性命令. 在2.0版本(于2018年2月发布)之前, 命令有所不同.")

Nix是一个包管理器和构建系统,解析用 Nix Expression Language(惰性求值的纯函数式语言)指定的可再现的构建指令.Nix表达式是纯函数[1], 接受依赖作为参数,并为包产生一个指定了可再现的构建环境的 derivation .Nix把构建的结果存储在由完整依赖树的哈希值指定的唯一地址中, 创建了一个不可变的包存储 (即nix存储), 这使得原子升级, 回滚, 以及不同版本包的同时安装成为可能,从根本上消除了依赖地狱.

用法

安装

NixOS: 当你在安装NixOS时, Nix就已经安装好了

如果你想在另一个Linux发行版或一台Mac电脑上使用Nix, 你可以进行独立安装: Nix手册的安装部分讲述了从二进制文件或源代码的独立Nix安装过程

Nix命令

Nix命令被记录在Nix参考手册: 主要命令, 工具命令和实验性命令. 在2.0版本(于2018年2月发布)之前, 命令有所不同.

Configuration

On NixOS, Nix is configured through the nix option.

Standalone Nix is configured through nix.conf (usually found in /etc/nix/), which defines a number of settings relating to evaluation, builds, garbage collection, sandboxing, and user permissions. Details on the available options are found in the Nix reference manual.

Even further configuration is possible with Home Manager to manage declarative environments for a single user. For system-wide configuration on Linux, you can use System Manager. For system-wide configuration on macOS, nix-darwin is the preferred solution.

Internals

Nix store

Packages built by Nix are placed in the read-only Nix store, normally found in /nix/store. Each package is given a unique address specified by a cryptographic hash followed by the package name and version, for example /nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1. These prefixes hash all the inputs to the build process, including the source files, the full dependency tree, compiler flags, etc. This allows Nix to simultaneously install different versions of the same package, and even different builds of the same version, for example variants built with different compilers. When adding, removing or updating a package, nothing is removed from the store; instead, symlinks to these packages are added, removed or changed in profiles.

Profiles

In order to construct a coherent user or system environment, Nix symlinks entries of the Nix store into profiles. These are the front-end by which Nix allows rollbacks: since the store is immutable and previous versions of profiles are kept, reverting to an earlier state is simply a matter of change the symlink to a previous profile. To be more precise, Nix symlinks binaries into entries of the Nix store representing the user environments. These user environments are then symlinked into labeled profiles stored in /nix/var/nix/profiles, which are in turn symlinked to the user's ~/.nix-profile.

Sandboxing

When sandbox builds are enabled, Nix will setup an isolated environment for each build process. It is used to remove further hidden dependencies set by the build environment to improve reproducibility. This includes access to the network during the build outside of fetch* functions and files outside the Nix store. Depending on the operating system access to other resources are blocked as well (ex. inter process communication is isolated on Linux); see nix.conf section in the Nix manual for details.

Sandboxing is enabled by default on Linux, and disabled by default on macOS. In pull requests for Nixpkgs people are asked to test builds with sandboxing enabled (see Tested using sandboxing in the pull request template) because in official Hydra builds sandboxing is also used.

To configure Nix for sandboxing, set sandbox = true in /etc/nix/nix.conf; to configure NixOS for sandboxing set nix.useSandbox = true; in configuration.nix. The nix.useSandbox option is true by default since NixOS 17.09.

Alternative Interpreters

There is an ongoing effort to reimplement Nix, from the ground up, in Rust.

There is also a community-led fork of Nix 2.18 named Lix, focused on correctness, usability, and growth. While it has also ported some components of Nix to Rust, it is not a ground-up rewrite like Tvix.

Earlier attempts can be found on riir-nix

Notes

  1. 计算过程中,值不可改变.当函数的输入不变时,函数总是输出相同的结果.