mirror of
https://github.com/ryan4yin/nixos-and-flakes-book.git
synced 2025-06-21 20:41:25 +02:00
polish: nixpkgs module system
This commit is contained in:
parent
0dc44d3e8d
commit
d69ed1964f
@ -9,10 +9,14 @@
|
|||||||
You might be wondering why the `/etc/nixos/configuration.nix` configuration file adheres
|
You might be wondering why the `/etc/nixos/configuration.nix` configuration file adheres
|
||||||
to the Nixpkgs Module definition and can be referenced directly within the `flake.nix`.
|
to the Nixpkgs Module definition and can be referenced directly within the `flake.nix`.
|
||||||
|
|
||||||
This is because the Nixpkgs repository contains a significant amount of NixOS
|
To understand this, we need to first learn about the origin of the Nixpkgs module system
|
||||||
implementation source code, primarily written in Nix. To manage and maintain such a large
|
and its purpose.
|
||||||
volume of Nix code and to allow users to customize various functions of their NixOS
|
|
||||||
systems, a modular system for Nix code is essential.
|
All the implementation code of NixOS is stored in the
|
||||||
|
[Nixpkgs/nixos](https://github.com/NixOS/nixpkgs/tree/master/nixos) directory, and most of
|
||||||
|
these source codes are written in the Nix language. To write and maintain such a large
|
||||||
|
amount of Nix code, and to allow users to flexibly customize various functions of their
|
||||||
|
NixOS system, a modular system for Nix code is essential.
|
||||||
|
|
||||||
This modular system for Nix code is also implemented within the Nixpkgs repository and is
|
This modular system for Nix code is also implemented within the Nixpkgs repository and is
|
||||||
primarily used for modularizing NixOS system configurations. However, it is also widely
|
primarily used for modularizing NixOS system configurations. However, it is also widely
|
||||||
@ -95,9 +99,9 @@ submodules. The difference between them is:
|
|||||||
passed through `_module.args` in `imports = [ ... ];`, it will result in an
|
passed through `_module.args` in `imports = [ ... ];`, it will result in an
|
||||||
`infinite recursion` error**. In this case, you must use `specialArgs` instead.
|
`infinite recursion` error**. In this case, you must use `specialArgs` instead.
|
||||||
|
|
||||||
I personally prefer `specialArgs` because it is more straightforward and easier to use, and
|
I personally prefer `specialArgs` because it is more straightforward and easier to use,
|
||||||
the naming style of `_xxx` makes it feel like an internal thing that is not suitable for use
|
and the naming style of `_xxx` makes it feel like an internal thing that is not suitable
|
||||||
in user configuration files.
|
for use in user configuration files.
|
||||||
|
|
||||||
Suppose you want to pass a certain dependency to a submodule for use. You can use the
|
Suppose you want to pass a certain dependency to a submodule for use. You can use the
|
||||||
`specialArgs` parameter to pass the `inputs` to all submodules:
|
`specialArgs` parameter to pass the `inputs` to all submodules:
|
||||||
|
@ -8,11 +8,14 @@
|
|||||||
为什么 `/etc/nixos/configuration.nix` 这个配置文件会符合 Nixpkgs Module 定义,从而能直接在
|
为什么 `/etc/nixos/configuration.nix` 这个配置文件会符合 Nixpkgs Module 定义,从而能直接在
|
||||||
`flake.nix` 中引用它呢?可能会有读者觉得这有点出乎意料。
|
`flake.nix` 中引用它呢?可能会有读者觉得这有点出乎意料。
|
||||||
|
|
||||||
这实际是因为 Nixpkgs 中包含了大量 NixOS 的实现源码,这些源码大都使用 Nix 语言编写。为了编
|
要理解这一点,我们得先了解下 Nixpkgs 模块系统的由来以及它的用途。
|
||||||
写维护如此多的 Nix 代码,并且使用户能灵活地自定义其 NixOS 系统的各项功能,就必须要有一套
|
|
||||||
Nix 代码的模块化系统。
|
|
||||||
|
|
||||||
这套 Nix 代码的模块系统的实现也同样在 Nixpkgs 仓库中,它主要被用于 NixOS 系统配置的模块
|
NixOS 的所有实现代码都存放在
|
||||||
|
[Nixpkgs/nixos](https://github.com/NixOS/nixpkgs/tree/master/nixos) 目录中,这些源码大都使
|
||||||
|
用 Nix 语言编写。为了编写维护如此多的 Nix 代码,并且使用户能灵活地自定义其 NixOS 系统的各
|
||||||
|
项功能,就必须要有一套 Nix 代码的模块化系统。
|
||||||
|
|
||||||
|
这套 Nix 代码的模块系统的实现也同样存放在 Nixpkgs 仓库中,它主要被用于 NixOS 系统配置的模块
|
||||||
化,但也有其他的应用,比如 nix-darwin 跟 home-manager 都大量使用了这套模块系统。
|
化,但也有其他的应用,比如 nix-darwin 跟 home-manager 都大量使用了这套模块系统。
|
||||||
|
|
||||||
既然 NixOS 是基于这套模块系统构建的,那它的配置文件(包括 `/etc/nixos/configuration.nix`)
|
既然 NixOS 是基于这套模块系统构建的,那它的配置文件(包括 `/etc/nixos/configuration.nix`)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user