File tree Expand file tree Collapse file tree 3 files changed +39
-4
lines changed
Expand file tree Collapse file tree 3 files changed +39
-4
lines changed Original file line number Diff line number Diff line change @@ -82,15 +82,15 @@ and `special-fonts-2.nix`. Both files are modules themselves and look similar to
8282
8383``` nix
8484{ config, pkgs, ...}: {
85- # Configuration stuff ...
85+ # Configuration stuff ...
8686}
8787```
8888
8989Both import statements above are equivalent in the parameters they receive:
9090
9191- Statement ` (1) ` imports the function in ` special-fonts-1.nix ` and calls it by passing
9292 ` {config = config; pkgs = pkgs} ` . Basically using the return value of the call (another
93- partial configuration [ attritbute set ] ) inside the ` imports ` list.
93+ partial configuration _ attribute set _ ) inside the ` imports ` list.
9494
9595- Statement ` (2) ` defines a path to a module, whose function Nix will load _ automatically_
9696 when assembling the configuration ` config ` . It will pass all matching arguments from the
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ hello: command not found
2323› hello
2424Hello, world!
2525
26- # ponysay is also available
26+ # cowsay is also available
2727› cowsay " Hello, world!"
2828 _______
2929< hello >
Original file line number Diff line number Diff line change @@ -46,7 +46,42 @@ set 也能被正确合并,具体行为各位看官可以自行探索。
4646> 中找到一句关于 ` imports `
4747> 的描述:` A list of modules. These are merged together to form the final configuration. ` ,可以意会一下...(Nix 的文档真的一言难尽...这么核心的参数文档就这么一句...)
4848
49- 我们可以借助 ` imports ` 参数,将 ` home.nix ` 与 ` configuration.nix ` 拆分成多个 ` .nix ` 文件。
49+ 借助 ` imports ` ,我们可以把 ` home.nix ` 和 ` configuration.nix ` 拆成多个分散在不同 ` .nix `
50+ 文件里的 Nix 模块。先来看一个示例模块 ` packages.nix ` :
51+
52+ ``` nix
53+ {
54+ config,
55+ pkgs,
56+ ...
57+ }: {
58+ imports = [
59+ (import ./special-fonts-1.nix {inherit config pkgs;}) # (1)
60+ ./special-fonts-2.nix # (2)
61+ ];
62+
63+ fontconfig.enable = true;
64+ }
65+ ```
66+
67+ 这个模块在 ` imports ` 里加载了另外两个模块:` special-fonts-1.nix ` 和
68+ ` special-fonts-2.nix ` 。这两个文件本身也是模块,写法大致如下:
69+
70+ ``` nix
71+ { config, pkgs, ... }: {
72+ # Configuration stuff ...
73+ }
74+ ```
75+
76+ 上面两条 import 语句在传入参数这件事上是等价的:
77+
78+ - 语句 ` (1) ` 把 ` special-fonts-1.nix ` 里的函数导入并立即调用,显式传入
79+ ` { config = config; pkgs = pkgs;} ` 。返回值(一个 _ attribute set_ )被放进 ` imports `
80+ 列表。
81+
82+ - 语句 ` (2) ` 只给出模块路径。Nix 在拼装最终 ` config ` 时会** 自动** 加载该文件,并把
83+ ` packages.nix ` 函数头里已有的同名参数自动传进去,效果等同于
84+ ` import ./special-fonts-2.nix { config = config; pkgs = pkgs; } ` 。
5085
5186推荐一个非常好的模块化配置的例子,可以参考一下:
5287
You can’t perform that action at this time.
0 commit comments