Skip to content

Commit 71d6d06

Browse files
authored
Add NixOS section to distribution.md, covering core concepts, package management, and system rollback features. (#91)
1 parent 19a11ba commit 71d6d06

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

docs/Appendix/distribution.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,143 @@ $ emerge --search audacity # 搜索名字中含 audacity 的包
135135
尽管 Systemd 已经成为了 Linux 发行版主流选择的 init,OpenRC 仍然是 Gentoo 默认的 init(关于 init 的简介,可参考[第四章](../Ch04/index.md))。
136136

137137
[^1]: <https://wiki.centos.org/zh/HowTos/SELinux>
138+
139+
## NixOS {#nixos}
140+
141+
[NixOS](https://nixos.org/) 是一个基于 Nix 包管理器的 Linux 发行版,其最大的特点是**声明式配置****原子性更新**。与传统发行版不同,NixOS 的整个系统配置都通过一个配置文件来管理,这使得系统配置可以版本控制、可重现,并且可以轻松回滚。
142+
143+
### 核心概念 {#nixos-concepts}
144+
145+
#### 声明式配置 {#nixos-declarative}
146+
147+
NixOS 的整个系统配置都写在 `/etc/nixos/configuration.nix` 文件中。这个文件描述了系统应该是什么样子,而不是如何一步步构建系统。例如:
148+
149+
```nix
150+
{ config, pkgs, ... }:
151+
152+
{
153+
# 启用 SSH 服务
154+
services.openssh.enable = true;
155+
156+
# 安装软件包
157+
environment.systemPackages = with pkgs; [
158+
firefox
159+
vim
160+
git
161+
];
162+
163+
# 用户配置
164+
users.users.alice = {
165+
isNormalUser = true;
166+
extraGroups = [ "wheel" ];
167+
};
168+
}
169+
```
170+
171+
#### 不可变包存储 {#nixos-immutable}
172+
173+
NixOS 使用哈希值来标识每个包,相同内容的包总是有相同的哈希值。这意味着:
174+
175+
- 不同版本的软件可以同时存在而不会冲突
176+
- 系统更新是原子性的,要么完全成功,要么完全失败
177+
- 可以轻松回滚到任何之前的配置
178+
179+
### 软件包管理 {#nixos-package-management}
180+
181+
NixOS 使用 `nix` 命令进行软件包管理:
182+
183+
```console
184+
$ nix search nixpkgs firefox # 搜索软件包
185+
186+
187+
$ nix-env -iA nixpkgs.firefox # 安装软件包(临时,重启后消失)
188+
189+
$ sudo nixos-rebuild switch # 在配置文件中添加软件包(推荐方式),编辑 /etc/nixos/configuration.nix,然后运行
190+
```
191+
192+
#### 配置文件管理 {#nixos-config-management}
193+
194+
```console
195+
$ sudo nixos-rebuild switch # 应用配置更改
196+
197+
$ sudo nixos-rebuild test # 测试配置(不应用)
198+
199+
$ sudo nixos-rebuild boot # 启动到新配置
200+
201+
$ sudo nixos-rebuild switch --rollback # 回滚到上一个配置
202+
```
203+
204+
### 系统回滚 {#nixos-rollback}
205+
206+
NixOS 的回滚功能是其最强大的特性之一:
207+
208+
```console
209+
$ sudo nix-env --list-generations --profile /nix/var/nix/profiles/system # 查看可用的系统配置
210+
211+
$ sudo nixos-rebuild switch --rollback # 回滚到上一个配置
212+
213+
$ sudo nixos-rebuild switch --option system-profiles /nix/var/nix/profiles/system-123-link # 回滚到特定配置
214+
```
215+
216+
### 频道 (Channels) {#nixos-channels}
217+
218+
NixOS 使用频道来管理软件包集合:
219+
220+
```console
221+
$ nix-channel --list # 查看当前频道
222+
223+
$ sudo nix-channel --update # 更新频道
224+
225+
$ sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos # 切换到不稳定频道
226+
```
227+
228+
### Nix Flakes {#nixos-flakes}
229+
230+
Nix Flakes 是 Nix 的新特性,提供了更好的可重现性和依赖管理:
231+
232+
```nix
233+
# flake.nix 示例
234+
{
235+
inputs = {
236+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
237+
};
238+
239+
outputs = { self, nixpkgs }: {
240+
nixosConfigurations.mySystem = nixpkgs.lib.nixosSystem {
241+
system = "x86_64-linux";
242+
modules = [ ./configuration.nix ];
243+
};
244+
};
245+
}
246+
```
247+
248+
### 开发环境 {#nixos-development}
249+
250+
NixOS 提供了强大的开发环境管理:
251+
252+
```console
253+
$ nix-shell -p python3 nodejs # 进入包含特定软件包的 shell
254+
255+
$ nix-shell # 使用 shell.nix 文件定义开发环境
256+
```
257+
258+
### 与其他发行版的主要区别 {#nixos-differences}
259+
260+
1. **配置方式**:传统发行版通过修改各种配置文件,NixOS 通过一个主配置文件
261+
2. **包管理**:传统发行版使用包管理器安装软件,NixOS 通过配置文件声明需要的软件
262+
3. **系统更新**:传统发行版更新可能失败并留下不一致状态,NixOS 更新是原子性的
263+
4. **回滚能力**:传统发行版回滚困难,NixOS 可以轻松回滚到任何之前的配置
264+
5. **可重现性**:NixOS 配置可以完全重现相同的系统环境
265+
266+
### 学习资源 {#nixos-resources}
267+
268+
- [NixOS 官方手册](https://nixos.org/manual/nixos/stable/)
269+
- [NixOS Wiki](https://nixos.wiki/)
270+
- [Nix Pills - 深入学习 Nix 概念](https://nixos.org/guides/nix-pills/)
271+
- [Awesome Nix - Nix 生态系统资源](https://github.com/nix-community/awesome-nix)
272+
- [NixOS 中文](https://nixos-cn.org/)
273+
- [NixOS 与 Flakes 一份非官方的新手指南](https://nixos-and-flakes.thiscute.world/zh/)
274+
- [nix_resources](https://linktr.ee/nix_resources)
275+
- [wrapper-manager](https://viperml.github.io/wrapper-manager/)
276+
277+
NixOS 的学习曲线相对陡峭,但一旦掌握,它提供了传统发行版无法比拟的系统管理体验。特别适合需要可重现环境、频繁实验或需要强系统一致性的用户。

0 commit comments

Comments
 (0)