docs(Chinese): sync updates from #222

This commit is contained in:
Ryan Yin 2025-05-05 11:55:19 +08:00
parent d889861c21
commit 03f21022a4
2 changed files with 12 additions and 16 deletions

View File

@ -33,22 +33,20 @@ Flake Registry 是一个 Flake 注册中心,它可以帮助我们在使用 `ni
前面说明了 `NIX_PATH` 与 Flake Registry 的作用。在日常使用中,我们一般都会希望能在执行
`nix repl '<nixpkgs>'`, `nix run nixpkgs#ponysay hello` 等命令时,使用的 nixpkgs 与系统一
致,这就需要我们自定义 `NIX_PATH` 与 Flake Registry。另外 `nix-channel` 虽然也能与 flakes
特性共存,但实际上 flakes 已经能够完全替代它了,所以我们也可以将其关闭。
致。**NixOS 24.05 自带了这一功能**PR [automatic flake registry])。此外我们也可以主动关闭 `nix-channel`,因为 flakes 已经能够完全替代它。
在你的 NixOS 配置中,添加如下 module 即可实现上述需求:
[automatic flake registry]: https://github.com/NixOS/nixpkgs/pull/254405
在你的 NixOS 配置中,推荐使用如下简洁配置:
```nix
{lib, nixpkgs, ...}: {
# make `nix run nixpkgs#nixpkgs` use the same nixpkgs as the one used by this flake.
nix.registry.nixpkgs.flake = nixpkgs;
nix.channel.enable = false; # remove nix-channel related tools & configs, we use flakes instead.
# but NIX_PATH is still used by many useful tools, so we set it to the same value as the one used by this flake.
# Make `nix repl '<nixpkgs>'` use the same nixpkgs as the one used by this flake.
environment.etc."nix/inputs/nixpkgs".source = "${nixpkgs}";
# https://github.com/NixOS/nix/issues/9574
nix.settings.nix-path = lib.mkForce "nixpkgs=/etc/nix/inputs/nixpkgs";
# this is set automatically by nixpkgs.lib.nixosSystem but might be required
# if one is not using that:
# nixpkgs.flake.source = nixpkgs;
}
```

View File

@ -74,15 +74,13 @@ NixOS & Flakes 新手指南,就有必要介绍下 Flakes 实验特性所依赖
的旧的 Nix 命令行工具与相关概念。在查找资料时,如果看到它们直接忽略掉就行
`nix-collect-garbage` 除外,该命令目前暂无替代):
1. `nix-channel`: 与 apt/yum/pacman 等其他 Linux 发行版的包管理工具类似,传统的 Nix 也以
stable/unstable/test 等 channel 的形式来管理软件包的版本,可通过此命令修改 Nix 的
channel 信息。
1. Nix Flakes 在 `flake.nix` 中通过 `inputs` 声明依赖包的数据源,通过 `flake.lock` 锁定
依赖版本,完全取代掉了 `nix-channel` 的功能。
1. `nix-channel`: 与 apt/yum/pacman 等其他 Linux 发行版的包管理工具类似,`nix-channel` 通过 stable/unstable channels如 apt/yum/管理诸如 nixpkgs 等 `inputs` 的版本。传统上,这为 Nix 语言提供了 `<nixpkgs>` 的引用来源。
1. 在 Flakes 中,`nix-channel` 的功能被 Flake 注册表(`nix registry`)取代,用于为交互式命令行(例如 `nix run nixpkgs#hello`)提供「全局的默认 nixpkgs」。当使用 `flake.nix` 时,`inputs` 的版本由 flake 自己管理。
2. Flakes 通过 `flake.nix` 中的 `inputs` 部分管理每个 Flake 中 nixpkgs 及其他 `inputs` 的版本,而非依赖全局状态。
2. `nix-env`: 用于管理用户环境的软件包,是传统 Nix 的核心命令行工具。它从 `nix-channel`
义的数据源中安装软件包,所以安装的软件包版本受 channel 影响。
1. 通过 `nix-env` 安装的包不会被自动记录到 Nix 的声明式配置中,是完全脱离掌控的,无法在
其他主机上复现,因此不推荐使用。
其他主机上复现,且升级由 `nix-env` 安装的包时可能因未保存属性名产生不可预测的结果,因此不推荐使用。
2. New CLI 中对应的命令为 `nix profile`,我个人不太推荐初学者直接尝试它.
3. `nix-shell`: nix-shell 用于创建一个临时的 shell 环境
1. 这玩意儿可能有点复杂了,因此在 New CLI 中它被拆分成了三个子命令 `nix develop`,