fix: typos, broken links, and other updates

This commit is contained in:
Ryan Yin 2023-06-23 23:18:01 +08:00
parent 3fe5fb1fa3
commit a2d35c5d18
36 changed files with 100 additions and 58 deletions

View file

@ -4,6 +4,22 @@ An unofficial NixOS & Flakes :book: for beginners: https://nixos-and-flakes.this
中文版: https://nixos-and-flakes.thiscute.world/zh/
## Introduction to Flakes
The flakes experimental feature is a major development for Nix, it introduces a policy for managing dependencies between Nix expressions, it improves reproducibility, composability and usability in the Nix ecosystem. Although it's still an experimental feature, flakes have been widely used by the Nix community.[^1]
Flakes is one of the most significant changes the nix project has ever seen.[^2]
## Warning about Flakes
The benefits of Flakes are obvious, and the entire NixOS community likes it very much. Currently, more than half of the users are using Flakes[^3], so we're pretty sure that Flakes will never be deprecated.
:warning: But **Flakes is still an experimental feature**, there are still many problems with it, and it is likely to introduce some breaking changes in the process of stablizing it, and its uncertain how greatly the breaking changes will be.
So overall, I still recommend everyone to use Flakes, this book is also written around NixOS and Flakes, but please be prepared for the problems that may be caused by the upcomming breaking changes.
## Contribution
> _A real community, however, exists only when its members interact in a meaningful way that deepens their understanding of each other and leads to learning._
@ -17,3 +33,9 @@ Thank you to [all the people](https://github.com/ryan4yin/nixos-and-flakes-book/
## License
[MIT](https://opensource.org/licenses/MIT)
[^1]: [Flakes - NixOS Wiki](https://nixos.wiki/index.php?title=Flakes)
[^2]: [Flakes are such an obviously good thing](https://grahamc.com/blog/flakes-are-an-obviously-good-thing/)
[^3]: [Draft: 1 year roadmap - NixOS Foundation](https://nixos-foundation.notion.site/1-year-roadmap-0dc5c2ec265a477ea65c549cd5e568a9)

View file

@ -62,8 +62,8 @@ export default defineConfig({
link: "/nixos-with-flakes/introduction-to-flakes.md",
},
{
text: "NixOS with Flakes enabled",
link: "/nixos-with-flakes/get-started-with-flakes.md",
text: "NixOS with Flakes Enabled",
link: "/nixos-with-flakes/nixos-with-flakes-enabled.md",
},
{
text: "Started using Home Manager",
@ -79,7 +79,7 @@ export default defineConfig({
},
{
text: "Upgrade or Downgrade Packages",
link: "/nixos-with-flakes/upgrade-or-downgrade-packages.md",
link: "/nixos-with-flakes/downgrade-or-upgrade-packages.md",
},
{
text: "Other useful Tips",
@ -92,7 +92,7 @@ export default defineConfig({
items: [
{ text: "Introduction", link: "/nixpkgs/intro.md" },
{ text: "callPackage", link: "/nixpkgs/callpackage.md" },
{ text: "Overridding", link: "/nixpkgs/override.md" },
{ text: "Overridding", link: "/nixpkgs/overriding.md" },
{ text: "Overlays", link: "/nixpkgs/overlays.md" },
],
},
@ -136,7 +136,7 @@ export default defineConfig({
{
text: "Advanced Topics",
items: [
{ text: "Introduction", link: "/zh/advanced-topics/index.md" },
{ text: "Introduction", link: "/advanced-topics/index.md" },
],
},
],
@ -199,7 +199,7 @@ export default defineConfig({
},
{
text: "使用 Flakes 管理 NixOS",
link: "/zh/nixos-with-flakes/get-started-with-flakes.md",
link: "/zh/nixos-with-flakes/nixos-with-flakes-enabled.md",
},
{
text: "开始使用 Home Manager",
@ -215,7 +215,7 @@ export default defineConfig({
},
{
text: "升级或降级软件包",
link: "/zh/nixos-with-flakes/upgrade-or-downgrade-packages.md",
link: "/zh/nixos-with-flakes/downgrade-or-upgrade-packages.md",
},
{
text: "其他杂七杂八的内容",
@ -228,7 +228,7 @@ export default defineConfig({
items: [
{ text: "介绍", link: "/zh/nixpkgs/intro.md" },
{ text: "callPackage", link: "/zh/nixpkgs/callpackage.md" },
{ text: "Overridding", link: "/zh/nixpkgs/override.md" },
{ text: "Overridding", link: "/zh/nixpkgs/overriding.md" },
{ text: "Overlays", link: "/zh/nixpkgs/overlays.md" },
],
},

View file

@ -1,3 +1,5 @@
## Advanced Topics
After becoming familiar with the NixOS, you can further explore Nix's three manuals and other docs to discover more ways to use it:

View file

@ -122,9 +122,6 @@ nix-repl> outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.ses
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.sessionVariables.BROWSER
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.sessionVariables.DELTA_PAGER
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.sessionVariables.EDITOR
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.sessionVariables.GLFW_IM_MODULE
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.sessionVariables.MANPAGER
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.sessionVariables.QT_IM_MODULE
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.sessionVariables.TERM
# ......omit other outputs
@ -141,11 +138,6 @@ outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.file..config/
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.file..config/fcitx5/profile-bak
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.file..config/i3/config
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.file..config/i3/i3blocks.conf
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.file..config/i3/keybindings
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.file..config/i3/layouts
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.file..config/i3/scripts
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.file..config/i3/wallpaper.png
outputs.nixosConfigurations.ai.config.home-manager.users.ryan.home.file..config/rofi
#......
```

View file

@ -1,3 +1,5 @@
## Best Practices
Nix is powerful and flexible, it provides a lot of ways to do things, making it difficult to find the most suitable way to do your job.
Here are some best practices that I've learned from the community, hope it can help you.

View file

@ -1,5 +1,7 @@
## Advantages & Disadvantages of NixOS
## Advantages of Nix
## Advantages
- **Declarative configuration, Environment as Code, can be managed with Git**
- Nix Flakes lock dependences's version through a lock file named `flake.lock`, to ensure that the system is reproducible. This idea actually borrows from some package managers such as npm, cargo, etc.
@ -12,7 +14,7 @@
- **The community is very active, and there are quite a few third-party projects**. The official package repository, nixpkgs, has many contributors, and many people share their Nix configuration on Github/Gitlab. After browsing through it, the entire ecosystem gives me a sense of excitement in discovering a new continent.
## Disadvantages of Nix
## Disadvantages
- **High learning curve:**: If you want the system to be completely reproducible and avoid pitfalls caused by improper use, you need to learn about the entire design of Nix and manage the system in a declarative manner. You cannot blindly use `nix-env -i` (which is similar to `apt-get install`).
- **Chaotic documentation**: Flakes is still an experimental feature, and there are currently few documents introducing it, Most of the Nix community's documentation only introduces the old cli such as `nix-env`/`nix-channel`. If you want to start learning Nix directly from Flakes, you need to refer to a large number of old documents and extract what you need from them. In addition, some of Nix's current core functions are not well-documented (such as `imports` and Nix Module System), to figure out what it does, it is best to look at the source code...

View file

@ -1,6 +1,3 @@
---
footer: false
---
## Introduction to Nix & NixOS

View file

@ -1,16 +1,13 @@
## Installation
Nix can be installed in multiple ways:
1. Install on macOS/Linux/WSL as a package manager.
2. Install NixOS, it's a Linux distribution that uses Nix to manage the entire system environment.
I chose to directly install NixOS using its official ISO image, to manage the entire system through Nix as much as possible.
This book aims to introduce the usage of NixOS & Flakes, so we just skip the content related only to Nix here.
The installation process is simple, and I won't go into details here.
The installation process of NixOS is simple, and I won't go into details here.
Some materials that may be useful:
1. [Official installation method of Nix](https://nixos.org/download.html): written in bash script, `nix-command` & `flakes` are still experimental features as of 2023-04-23, and need to be manually enabled.
1. You need to refer to the instructions in [Enable flakes - NixOS Wiki](https://nixos.wiki/wiki/Flakes) to enable `nix-command` & `flakes`.
2. The official installer does not provide any uninstallation method. To uninstall Nix on Linux/macOS, you need to manually delete all related files, users, and groups.
2. [The Determinate Nix Installer](https://github.com/DeterminateSystems/nix-installer): a third-party installer written in Rust, which enables `nix-command` & `flakes` by default and provides an uninstallation command.
Please just go to the official download site to see more details: <https://nixos.org/download.html>

View file

@ -1,5 +1,5 @@
## Downgrade or upgrade some packages
## Downgrade or upgrade packages
After using Flakes, most people are currently using the `nixos-unstable` branch of nixpkgs. Sometimes you will encounter some bugs, such as the [chrome/vscode crash problem](https://github.com/swaywm/sway/issues/7562)

View file

@ -1,3 +1,5 @@
## Get Started with NixOS
After learning the basics of the Nix language, we can start using it to configure our NixOS. The default configuration for NixOS is located at `/etc/nixos/configuration.nix`, which contains all the declarative configuration for the system, such as time zone, language, keyboard layout, network, users, file system, boot options, etc.

View file

@ -1,6 +1,8 @@
## Introduction to Flakes
The flakes experimental feature is a major development for Nix, it introduces a policy for managing dependencies between Nix expressions, it improves reproducibility, composability and usability in the Nix ecosystem. Although it's still an experimental feature, flakes have been widely used by the Nix community.[^1]
It's Flakes is one of the most significant changes the nix project has ever seen.[^2]
Flakes is one of the most significant changes the nix project has ever seen.[^2]
## Warning about Flakes

View file

@ -1,5 +1,7 @@
## NixOS with Flakes Enabled
## Enabling Flakes Support
### Enabling Flakes Support
Compared to the default configuration approach of NixOS, Flakes provide better reproducibility and a clearer package structure that is easier to maintain. Therefore, it is recommended to manage NixOS with Flakes.
@ -37,7 +39,7 @@ However, as Flakes is still an experimental feature currently, it's not enabled
Now run `sudo nixos-rebuild switch` to apply the changes, and then you can write the configuration for NixOS with Flakes.
## Switching System Configuration to `flake.nix`
### Switching System Configuration to `flake.nix`
After enabling `flakes`, `sudo nixos-rebuild switch` will try to read`/etc/nixos/flake.nix` first every time you run it, if not found, it will fallback to `/etc/nixos/configuration.nix`.
@ -138,7 +140,7 @@ Now run `sudo nixos-rebuild switch` to apply the configuration, and no changes w
## Manage system software through Flakes
### Manage system software through Flakes
After the switch, we can now manage the system through Flakes. The most common requirement for managing a system is to install softwares. We have seen how to install packages through `environment.systemPackages` before, and these packages are all from the official nixpkgs repository.
@ -202,9 +204,9 @@ Then udpate `configuration.nix` to install `helix` from the input `helix`:
Now deploy the changes by `sudo nixos-rebuild switch`, and then we can start the helix editor by `helix` command.
## Add Custom Cache Mirror
### Add Custom Cache Mirror
> You can skip this section if you don't need to customize the cache mirror.
> You can safely skip this section if you don't need to customize the cache mirror.
To speed up package building, Nix provides <https://cache.nixos.org> to cache build results to avoid build every packages locally.

View file

@ -1,5 +1,7 @@
## Other useful Tips
## Manage the configuration with Git
### Manage the configuration with Git
NixOS configuration is just a set of text files, it is very suitable to be managed with Git, and thus we can easily rollback to a previous version when we encounter some problems.
@ -40,7 +42,7 @@ sudo nixos-rebuild switch --flake .#nixos-test
More operations on Git are not described here. Generally speaking, rollback can be done directly through Git. Only when the system crashes completely, you will need to restart into bootloader and boot the system from the previous historical version.
## View and delete history data {#view-and-delete-history}
### View and delete history data {#view-and-delete-history}
As we mentioned before, each deployment of NixOS will generate a new version, and all versions will be added to the system boot options. In addition to restarting the computer, we can also query all available historical versions through the following command:

View file

@ -1,3 +1,4 @@
## pkgs.callPackage
In the previous content, We have used `import xxx.nix` to import Nix files many times, this syntax simply returns the execution result of the file, without any further processing of the it.

View file

@ -1,3 +1,5 @@
## Nixpkgs's Advanced Usage
`callPackage`, `Overriding`, and `Overlays` are the techniques occasionally used when using Nix to customize the build method of Nix packages.

View file

@ -1,3 +1,5 @@
## Overlays
The `override` we introduced previously will generate a new Derivation, which does not affect the original Derivation in `pkgs`, and is only suitable for use as a local parameter,
if you need to override a Derivation that is also depended on by other Nix packages, then other Nix packages will still use the original Derivation.

View file

@ -1,3 +1,5 @@
## Overriding
Simply put, all Nix packages in nixpkgs can be customized with `<pkg>.override {}` to define some build parameters, which returns a new Derivation that uses custom parameters. For example:

View file

@ -1,3 +1,3 @@
## Usage of Flakes
## Other Usage of Flakes
Up to now, we have written a lot of configuration with Flakes to manage NixOS. Here is a brief introduction to the more detailed content of the Flakes, as well as the new command lines commonly used with flakes.

View file

@ -1,5 +1,5 @@
## V. Basics of The Nix language
## Basics of The Nix language
The Nix language is used to declare the configuration to be built by Nix, if you want to play with NixOS and Flakes and enjoy the benefits they bring, you must learn the basics of this language first.

View file

@ -1,5 +1,5 @@
## 通过 `nix repl` 查看源码、调试配置 {#view-source-code-via-nix-repl}
## 通过 `nix repl` 查看源码、调试配置 {#debug-via-nix-repl}
前面我们已经使用 `nix repl '<nixpkgs>'` 看过很多次源码了,这是一个非常强大的工具,可以帮助我们理解 Nix 的工作原理。

View file

@ -1,5 +1,5 @@
## 使用 Makefile 简化命令
## 使用 Makefile 简化 NixOS 相关命令
> 注意: Makefile 的 target 名称不能与当前目录下的文件或者目录重名,否则 target 将不会被执行!

View file

@ -1,5 +1,7 @@
## NixOS 的优缺点
## NixOS 的优点 {#nix-advantages}
## 优点 {#advantages}
- 声明式配置OS as Code可以直接用 Git 管理配置
- NixOS 使用声明式配置来管理整个系统环境,只要你的配置文件不丢,系统就可以随时还原到任一历史状态(前面提过了,只有在 Nix 配置文件中声明了的状态才可被 NixOS 还原)。
@ -14,7 +16,7 @@
- 因为 Nix 中每个软件包都拥有唯一的 hash其安装路径中也会包含这个 hash 值,因此可以多版本共存。
- 社区很活跃,第三方项目也挺丰富,官方包仓库 nixpkgs 贡献者众多,也有很多人分享自己的 Nix 配置,一遍浏览下来,整个生态给我一种发现新大陆的兴奋感。
## Nix 的缺点 {#nix-disadvantages}
## 缺点 {#disadvantages}
- 学习成本高: 如果你希望系统完全可复现,并且避免各种不当使用导致的坑,那就需要学习了解 Nix 的整个设计,并以声明式的方式管理系统,不能无脑 `nix-env -i`(这类似 `apt-get install`)。
- 文档混乱: 首先 Nix Flakes 目前仍然是实验性特性,介绍它本身的文档目前比较匮乏, Nix 社区绝大多数文档都只介绍了旧的 `nix-env`/`nix-channel`,想直接从 Nix Flakes 开始学习的话,需要参考大量旧文档,从中提取出自己需要的内容。另外一些 Nix 当前的核心功能,官方文档都语焉不详(比如 `imports` 跟 Nixpkgs Module System想搞明白基本只能看源码了...
@ -23,7 +25,7 @@
- 报错信息比较隐晦: 一般的报错提示还是比较清楚的,但是遇到好几次依赖版本有问题或者传参错误提示不出原因,`--show-trace` 直接输出一堆的内部堆栈,都花了很长时间才定位到,通过升级依赖版本或者修正参数后问题解决。
- 猜测导致这个问题的原因有两个,一是 Nix 是动态语言,各种参数都是运行时才确定类型。二是我用到的 flake 包的错误处理逻辑写得不太好,错误提示不清晰,一些隐晦的错误甚至通过错误堆栈也定位不到原因。
## 简单总结下 {#nix-simple-summary}
## 简单总结下 {#summary}
总的来说,我觉得 NixOS 适合那些有一定 Linux 使用经验与编程经验,并且希望对自己的系统拥有更强掌控力的开发者。

View file

@ -1,6 +1,3 @@
---
footer: false
---
## Nix 与 NixOS 简介

View file

@ -1,14 +1,13 @@
## 安装 NixOS
Nix 有多种安装方式:
1. 以包管理器的形式安装到 MacOS/Linux/WSL 三种系统上
2. 也可以直接安装 NixOS这是 Nix 官方推出的一个 Linux 发行版,使用 Nix 包管理器来管理整个系统环境。
我选择了直接使用 NixOS 的 ISO 镜像安装 NixOS 系统,从而最大程度上通过 Nix 管理整个系统环境
本书主要介绍 NixOS 与 Flakes 的使用,因此不展开讨论
安装步骤很简单,这里不多介绍,仅列一下我觉得比较有用的参考资料:
NixOS 的安装步骤很简单,这里不多介绍,仅列一下我觉得比较有用的参考资料:
1. 国内镜像源:<https://mirrors.bfsu.edu.cn/help/nix/>
1. [Nix 的官方安装方式](https://nixos.org/download.html): 使用 bash 脚本编写, 目前2023-04-23为止 `nix-command` & `flakes` 仍然是实验性特性,需要手动开启。
1. 你需要参照 [Enable flakes - NixOS Wiki](https://nixos.wiki/wiki/Flakes) 的说明启用 `nix-command` & `flakes`
2. 官方不提供任何卸载手段,要在 Linux/MacOS 上卸载 Nix你需要手动删除所有相关的文件、用户以及用户组
2. [The Determinate Nix Installer](https://github.com/DeterminateSystems/nix-installer): 第三方使用 Rust 编写的 installer, 默认启用 `nix-command` & `flakes`,并且提供了卸载命令。
1. [NixOS 官网](https://nixos.org/download.html)
2. 国内镜像源:<https://mirrors.bfsu.edu.cn/help/nix/>

View file

@ -1,5 +1,5 @@
## 升级与降级软件包 {#rollback-package-version}
## 降级与升级软件包 {#rollback-package-version}
在使用 Nix Flakes 后,目前大家用得比较多的都是 `nixos-unstable` 分支的 nixpkgs有时候就会遇到一些 bug比如我最近2023/5/6就遇到了 [chrome/vscode 闪退的问题](https://github.com/swaywm/sway/issues/7562)。

View file

@ -1,3 +1,4 @@
## 开始使用 NixOS
了解了 Nix 语言的基本用法之后,我们就可以开始使用 Nix 语言来配置 NixOS 系统了。
NixOS 的系统配置路径为 `/etc/nixos/configuration.nix`,它包含系统的所有声明式配置,如时区、语言、键盘布局、网络、用户、文件系统、启动项、桌面环境等等。

View file

@ -1,3 +1,4 @@
## Flakes 简介
Flakes 实验特性是 Nix 项目的一项重大进展,它引入了一种管理 Nix 表达式之间的依赖关系的策略,提高了 Nix 生态系统中的可复现性、可组合性和可用性。
虽然 Flakes 仍然是一个试验性的功能,但已经被 Nix 社区广泛采用。[^1]

View file

@ -1,3 +1,5 @@
## 使用 Flakes 来管理你的 NixOS
## 启用 NixOS 的 Flakes 支持 {#enable-nix-flakes}

View file

@ -1,3 +1,5 @@
## NixOS 的其他实用技巧
## 使用 Git 管理 NixOS 配置 {#git-manage-nixos-config}

View file

@ -1,3 +1,5 @@
## 安装使用 Home Manager
前面简单提过NixOS 自身的配置文件只能管理系统级别的配置,而用户级别的配置则需要使用 home-manager 来管理。

View file

@ -1,3 +1,5 @@
## pkgs.callPackage
前面我们介绍并大量使用了 `import xxx.nix` 来导入 Nix 文件,这种语法只是单纯地返回该文件的执行结果,不会对该结果进行进一步处理。
比如说 `xxx.nix` 的内容是形如 `{...}: {...}`,那么 `import xxx.nix` 的结果就是该文件中定义的这个函数。

View file

@ -1,3 +1,4 @@
## Overlays
前面介绍的 override 函数都会生成新的 Derivation不影响 pkgs 中原有的 Derivation只适合作为局部参数使用。
但如果你需要覆写的 Derivation 还被其他 Nix 包所依赖,那其他 Nix 包使用的仍然会是原有的 Derivation.

View file

@ -1,3 +1,5 @@
## Overriding
简单的说,所有 nixpkgs 中的 Nix 包都可以通过 `<pkg>.override {}` 来自定义某些构建参数,它返回一个使用了自定义参数的新 Derivation. 举个例子:

View file

@ -1,5 +1,5 @@
## Nix Flakes 的使用 {#nix-flakes-usage}
## Nix Flakes 的其他玩法 {#nix-flakes-usage}
到这里我们已经写了不少 Nix Flakes 配置来管理 NixOS 系统了,这里再简单介绍下 Nix Flakes 更细节的内容,以及常用的 nix flake 命令。

View file

@ -1,5 +1,5 @@
## Flake 命令行的使用 {#flake-commands-usage}
## 新一代 Nix 命令行工具的使用 {#flake-commands-usage}
在启用了 `nix-command` & `flakes` 功能后,我们就可以使用 Nix 提供的新一代 Nix 命令行工具 [New Nix Commands][New Nix Commands] 了,下面列举下其中常用命令的用法:

View file

@ -1,3 +1,5 @@
## Nix 语言入门
Nix 语言是 Nix 包管理器的基础,要想玩得转 NixOS 与 Nix Flakes享受到它们带来的诸多好处就必须学会这门语言。