mirror of
https://github.com/ryan4yin/nixos-and-flakes-book.git
synced 2025-02-27 15:50:50 +01:00
feat: nix module's default parameters & specialArgs
This commit is contained in:
parent
d41370c05b
commit
7918e3d88e
@ -125,7 +125,7 @@ Note that the copied template cannot be used directly. You need to modify it to
|
||||
#
|
||||
# A Nix Module can be an attribute set, or a function that
|
||||
# returns an attribute set. By default, if a Nix Module is a
|
||||
# function, this function can only have the following parameters:
|
||||
# function, this function have the following default parameters:
|
||||
#
|
||||
# lib: the nixpkgs function library, which provides many
|
||||
# useful functions for operating Nix expressions:
|
||||
@ -143,11 +143,12 @@ Note that the copied template cannot be used directly. You need to modify it to
|
||||
# this parameter is rarely used,
|
||||
# you can ignore it for now.
|
||||
#
|
||||
# Only these parameters can be passed by default.
|
||||
# If you need to pass other parameters,
|
||||
# The default parameters mentioned above are automatically generated by Nixpkgs.
|
||||
# However, if you need to pass other non-default parameters to the submodules,
|
||||
# you'll have to manually configure these parameters using `specialArgs`.
|
||||
# you must use `specialArgs` by uncomment the following line:
|
||||
#
|
||||
# specialArgs = {...} # pass custom arguments into all sub module.
|
||||
# specialArgs = {...}; # pass custom arguments into all sub module.
|
||||
modules = [
|
||||
# Import the configuration.nix here, so that the
|
||||
# old configuration file can still take effect.
|
||||
@ -164,6 +165,14 @@ We defined a NixOS system called `nixos-test` with a configuration file at `./co
|
||||
|
||||
To apply the configuration to a system with hostname `nixos-test`, run `sudo nixos-rebuild switch --flake /etc/nixos#nixos-test`. No changes will be made to the system because we imported the old configuration file in `/etc/nixos/flake.nix`, so the actual state we declared remains unchanged.
|
||||
|
||||
The comments in the above code are already quite detailed, but let's emphasize a few points here:
|
||||
|
||||
1. Default parameters like `lib`, `pkgs`, `config`, and others are automatically generated by Nixpkgs and can be automatically injected into submodules without the need for additional declarations here.
|
||||
|
||||
2. In `specialArgs = {...};`, the content of the attribute set is omitted here. Its contents are automatically injected into submodules through name matching.
|
||||
|
||||
1. A common usage, for instance, is to directly write `specialArgs = inputs;`, enabling all data sources from the `inputs` attribute set to be used in the submodules.
|
||||
|
||||
## Managing System Packages with Flakes
|
||||
|
||||
After the switch, we can manage the system using Flakes. One common requirement is installing packages. We have previously seen how to install packages using `environment.systemPackages` from the official `nixpkgs` repository.
|
||||
|
@ -118,7 +118,7 @@ cat flake.nix
|
||||
# 那么它的参数就是当前的 NixOS Module 的参数.
|
||||
#
|
||||
# 根据 Nix Wiki 对 Nix modules 的描述,默认情况下,
|
||||
# Nix Module 函数的参数可以有这几个:
|
||||
# Nix Module 函数的默认参数有几个:
|
||||
#
|
||||
# lib: nixpkgs 自带的函数库,提供了许多操作 Nix 表达式的实用函数
|
||||
# 详见 https://nixos.org/manual/nixpkgs/stable/#id-1.4
|
||||
@ -131,8 +131,10 @@ cat flake.nix
|
||||
# 常用于从 nixpkgs 中导入一些额外的模块
|
||||
# 这个参数通常都用不到,我只在制作 iso 镜像时用到过
|
||||
#
|
||||
# 如果需要传其他非默认参数,就得使用 specialArgs,你可以取消注释如下这行来启用该参数
|
||||
# specialArgs = inputs # 将 inputs 中的参数传入所有子模块
|
||||
# 上述默认参数都由 Nixpkgs 自动生成。而如果你需要将其他非默认参数传递到子模块,
|
||||
# 就得使用 specialArgs 手动设定这些参数,你可以取消注释如下这行来启用该参数:
|
||||
#
|
||||
# specialArgs = {...}; # 将 inputs 中的参数传入所有子模块
|
||||
modules = [
|
||||
# 这里导入之前我们使用的 configuration.nix,这样旧的配置文件仍然能生效
|
||||
# 注: configuration.nix 本身也是一个 Nix Module,因此可以直接在这里导入
|
||||
@ -148,6 +150,12 @@ cat flake.nix
|
||||
|
||||
现在执行 `sudo nixos-rebuild switch --flake /etc/nixos#nixos-test` 应用配置,系统应该没有任何变化,因为我们仅仅是切换到了 Nix Flakes,配置内容与之前还是一致的。
|
||||
|
||||
上述代码的注释已经非常详细了,这里再着重说明几点:
|
||||
|
||||
1. `lib` `pkgs` `config` 等默认参数都由 Nixpkgs 自动生成,并可被自动注入到子模块,无需在此处额外声明。
|
||||
1. `specialArgs = {...};` 这里省略了 attribute set 的内容,其中的内容会被通过名称匹配的方式自动注入到子模块中。
|
||||
1. 常见用法比如直接写 `specialArgs = inputs;`,这样所有 inputs 中的 flake 数据源就都可以在子模块中使用了。
|
||||
|
||||
## 通过 Flakes 来管理系统软件 {#manage-system-software-with-flakes}
|
||||
|
||||
切换完毕后,我们就可以通过 Flakes 来管理系统了。管系统最常见的需求就是装软件,我们在前面已经见识过如何通过 `environment.systemPackages` 来安装 `pkgs` 中的包,这些包都来自官方的 nixpkgs 仓库。
|
||||
|
Loading…
Reference in New Issue
Block a user