Skip to content

Commit f7f5758

Browse files
WeepingDogeltaoky
andauthored
Update distribution.md: Add NixOS package management section with usage examples for nix-shell and nix-env (#94)
* Add NixOS section to distribution.md, covering core concepts, package management, and system rollback features. * Update distribution.md: Replace instances of "通道" with "频道" for consistency in terminology * Update distribution.md: Change "通道" to "频道" for terminology consistency * Refactor distribution.md: Add inline comments to console commands for clarity and improved understanding * Fix formatting in distribution.md: Remove extra space in Nix Pills link for consistency * Update distribution.md: Add NixOS package management section with usage examples for nix-shell and nix-env * Update docs/Appendix/distribution.md 嗯 使用这个改法不错 Co-authored-by: taoky <[email protected]> * Update distribution.md: Enhance NixOS package management section with additional tips and formatting improvements * Update distribution.md: Add spacing for improved readability in NixOS package management tips section * Update distribution.md: Fix formatting in NixOS package management section for better readability * Remove search commands and contents about nix search. * Update distribution.md: Add reference link for NixOS development environment management --------- Co-authored-by: taoky <[email protected]>
1 parent 6d350b2 commit f7f5758

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

docs/Appendix/distribution.md

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,24 +180,44 @@ NixOS 使用哈希值来标识每个包,相同内容的包总是有相同的
180180

181181
NixOS 使用 `nix` 命令进行软件包管理:
182182

183+
#### 使用 `nix-shell` {#nixos-nix-shell}
184+
185+
`nix-shell` 会启动一个新的 shell,在其中 `PATH` 等环境变量会被临时修改,指向由 Nix 构建的工具所在路径。借助这个环境,你可以方便地尝试新程序或依赖。需要注意,这些修改仅在该 `nix-shell` 会话中有效;一旦退出或重启,环境就会恢复原状。
186+
183187
```console
184-
$ nix search nixpkgs firefox # 搜索软件包
188+
$ nix-shell -p firefox # 安装软件包
189+
```
185190

191+
#### 使用 `nix-env` {#nixos-nix-env}
186192

187-
$ nix-env -iA nixpkgs.firefox # 安装软件包(临时,重启后消失)
193+
!!! warning "注意"
194+
195+
使用 `nix-env` 会永久修改本地安装包配置文件。用户必须像使用传统包管理器那样更新和维护该配置文件,这将放弃许多使 Nix 具有独特强大功能的优势。建议改用 `nix-shell` 或 NixOS 配置文件。
196+
197+
```console
198+
$ nix-env -iA nixos.firefox # 安装软件包
199+
```
200+
201+
#### 使用配置文件 {#nixos-config}
202+
203+
将以下 Nix 代码添加到你的 NixOS 配置文件中,该文件通常位于 `/etc/nixos/configuration.nix`
204+
205+
```nix
206+
environment.systemPackages = [
207+
pkgs.firefox
208+
];
209+
```
188210

211+
```console
189212
$ sudo nixos-rebuild switch # 在配置文件中添加软件包(推荐方式),编辑 /etc/nixos/configuration.nix,然后运行
190213
```
191214

192215
#### 配置文件管理 {#nixos-config-management}
193216

194217
```console
195218
$ sudo nixos-rebuild switch # 应用配置更改
196-
197219
$ sudo nixos-rebuild test # 测试配置(不应用)
198-
199220
$ sudo nixos-rebuild boot # 启动到新配置
200-
201221
$ sudo nixos-rebuild switch --rollback # 回滚到上一个配置
202222
```
203223

@@ -207,9 +227,7 @@ NixOS 的回滚功能是其最强大的特性之一:
207227

208228
```console
209229
$ sudo nix-env --list-generations --profile /nix/var/nix/profiles/system # 查看可用的系统配置
210-
211230
$ sudo nixos-rebuild switch --rollback # 回滚到上一个配置
212-
213231
$ sudo nixos-rebuild switch --option system-profiles /nix/var/nix/profiles/system-123-link # 回滚到特定配置
214232
```
215233

@@ -219,9 +237,7 @@ NixOS 使用频道来管理软件包集合:
219237

220238
```console
221239
$ nix-channel --list # 查看当前频道
222-
223240
$ sudo nix-channel --update # 更新频道
224-
225241
$ sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos # 切换到不稳定频道
226242
```
227243

@@ -247,11 +263,10 @@ Nix Flakes 是 Nix 的新特性,提供了更好的可重现性和依赖管理
247263

248264
### 开发环境 {#nixos-development}
249265

250-
NixOS 提供了强大的开发环境管理:
266+
NixOS 提供了强大的开发环境管理[^2]
251267

252268
```console
253269
$ nix-shell -p python3 nodejs # 进入包含特定软件包的 shell
254-
255270
$ nix-shell # 使用 shell.nix 文件定义开发环境
256271
```
257272

@@ -275,3 +290,5 @@ $ nix-shell # 使用 shell.nix 文件定义开发环境
275290
- [wrapper-manager](https://viperml.github.io/wrapper-manager/)
276291

277292
NixOS 的学习曲线相对陡峭,但一旦掌握,它提供了传统发行版无法比拟的系统管理体验。特别适合需要可重现环境、频繁实验或需要强系统一致性的用户。
293+
294+
[^2](https://nixos.wiki/wiki/Development_environment_with_nix-shell)

0 commit comments

Comments
 (0)