diff --git a/docs/nixos-with-flakes/get-started-with-nixos.md b/docs/nixos-with-flakes/get-started-with-nixos.md index 2941275..c24b27b 100644 --- a/docs/nixos-with-flakes/get-started-with-nixos.md +++ b/docs/nixos-with-flakes/get-started-with-nixos.md @@ -14,7 +14,10 @@ The `/etc/nixos/configuration.nix` file is the default and classic method for co To illustrate how to use `/etc/nixos/configuration.nix`, let's consider an example where we enable SSH and add a user named `ryan` to the system. We can achieve this by adding the following content to `/etc/nixos/configuration.nix`: -```nix +```nix{14-38} +# Edit this configuration file to define what should be installed on +# your system. Help is available in the configuration.nix(5) man page +# and in the NixOS manual (accessible by running ‘nixos-help’). { config, pkgs, ... }: { diff --git a/docs/nixos-with-flakes/modularize-the-configuration.md b/docs/nixos-with-flakes/modularize-the-configuration.md index e32cf74..fc38e30 100644 --- a/docs/nixos-with-flakes/modularize-the-configuration.md +++ b/docs/nixos-with-flakes/modularize-the-configuration.md @@ -118,7 +118,7 @@ Using these functions can be very helpful for modularizing the configuration. Yo For example, in my configuration at [ryan4yin/nix-config/blob/main/modules/nixos/core-server.nix#L30](https://github.com/ryan4yin/nix-config/blob/main/modules/nixos/core-server.nix#L30), I define default values like this: -```nix +```nix{6} { lib, pkgs, ... }: { @@ -132,7 +132,7 @@ For example, in my configuration at [ryan4yin/nix-config/blob/main/modules/nixos Then, for my desktop machine, I override the value in [ryan4yin/nix-config/blob/main/modules/nixos/core-desktop.nix#L15](https://github.com/ryan4yin/nix-config/blob/main/modules/nixos/core-desktop.nix#L15) like this: -```nix +```nix{10} { lib, pkgs, ... }: { @@ -150,9 +150,9 @@ Then, for my desktop machine, I override the value in [ryan4yin/nix-config/blob/ ## `lib.mkOrder`, `lib.mkBefore`, and `lib.mkAfter` -In addition to `lib.mkDefault` and `lib.mkForce`, there are also `lib.mkBefore` and `lib.mkAfter`, which are used to set the merge order of **list +In addition to `lib.mkDefault` and `lib.mkForce`, there are also `lib.mkBefore` and `lib.mkAfter`, which are used to set the merge order of \*\*list --type options**. These functions further contribute to the modularization of the configuration. +-type options\*\*. These functions further contribute to the modularization of the configuration. As mentioned earlier, when you define multiple values with the same **override priority**, Nix will throw an error. However, by using `lib.mkOrder`, `lib.mkBefore`, or `lib.mkAfter`, you can define multiple values with the same override priority, and they will be merged in the order you specify. @@ -179,7 +179,7 @@ Therefore, `lib.mkBefore` is a shorthand for `lib.mkOrder 500`, and `lib.mkAfter To test the usage of `lib.mkBefore` and `lib.mkAfter`, let's create a simple Flake project: -```shell +```shell{16-29} # Create flake.nix with the following content › cat < curl -> default package ## References - [Nix modules: Improving Nix's discoverability and usability](https://cfp.nixcon.org/nixcon2020/talk/K89WJY/) -- [Module System - Nixpkgs](https://github.com/NixOS/nixpkgs/blob/nixos-unstable/doc/module-system/module-system.chapter.md) \ No newline at end of file +- [Module System - Nixpkgs](https://github.com/NixOS/nixpkgs/blob/nixos-unstable/doc/module-system/module-system.chapter.md) diff --git a/docs/nixos-with-flakes/nixos-with-flakes-enabled.md b/docs/nixos-with-flakes/nixos-with-flakes-enabled.md index 42795e0..67d2a94 100644 --- a/docs/nixos-with-flakes/nixos-with-flakes-enabled.md +++ b/docs/nixos-with-flakes/nixos-with-flakes-enabled.md @@ -6,7 +6,7 @@ Flakes provide improved reproducibility and a more organized package structure, However, as Flakes is still an experimental feature, it is not enabled by default. To enable Flakes, you need to modify the `/etc/nixos/configuration.nix` file as follows: -```nix +```nix{15} # Edit this configuration file to define what should be installed on # your system. Help is available in the configuration.nix(5) man page # and in the NixOS manual (accessible by running 'nixos-help'). @@ -142,7 +142,7 @@ Now let's learn how to install packages from other sources using Flakes. This pr First, we need to add Helix as an input in `flake.nix`: -```nix +```nix{10,19} { description = "NixOS configuration of Ryan Yin"; @@ -174,7 +174,7 @@ First, we need to add Helix as an input in `flake.nix`: Next, update `configuration.nix` to install `helix` from the `helix` input: -```nix +```nix{3,14-15} # Nix will automatically inject `helix` from specialArgs # into the third parameter of this function through name matching { config, pkgs, helix, ... }: @@ -208,12 +208,10 @@ With the classic configuration method in NixOS, additional cache sources can be To customize the cache source, we must add the related configuration in `flake.nix` using the `nixConfig` parameter. Here's an example: -```nix +```nix{4-19} { description = "NixOS configuration of Ryan Yin"; - # ... - nixConfig = { experimental-features = [ "nix-command" "flakes" ]; substituters = [ diff --git a/docs/zh/nixos-with-flakes/get-started-with-nixos.md b/docs/zh/nixos-with-flakes/get-started-with-nixos.md index dd07e3a..68a11df 100644 --- a/docs/zh/nixos-with-flakes/get-started-with-nixos.md +++ b/docs/zh/nixos-with-flakes/get-started-with-nixos.md @@ -19,7 +19,7 @@ NixOS 的系统配置路径为 `/etc/nixos/configuration.nix`,它包含系统 比如要启用 ssh 并添加一个用户 ryan,只需要在 `/etc/nixos/configuration.nix` 中添加如下配置: -```nix +```nix{14-38} # Edit this configuration file to define what should be installed on # your system. Help is available in the configuration.nix(5) man page # and in the NixOS manual (accessible by running ‘nixos-help’). diff --git a/docs/zh/nixos-with-flakes/modularize-the-configuration.md b/docs/zh/nixos-with-flakes/modularize-the-configuration.md index 776dc00..8ae98b0 100644 --- a/docs/zh/nixos-with-flakes/modularize-the-configuration.md +++ b/docs/zh/nixos-with-flakes/modularize-the-configuration.md @@ -123,7 +123,7 @@ $ tree 举个例子,我在这里定义了一个默认值: -```nix +```nix{6} { lib, pkgs, ... }: { @@ -137,7 +137,7 @@ $ tree 然后在桌面机器的配置中,我强制覆盖了默认值: -```nix +```nix{10} { lib, pkgs, ... }: { @@ -184,7 +184,7 @@ $ tree 为了更直观地理解这两个函数,现在来创建一个 flake 测试下: -```shell +```shell{16-29} # 使用如下内容创建一个 flake.nix 文件 › cat < 注意:这里介绍的手段只能加速部分包的下载,许多 inputs 数据源仍然会从 Github 拉取,另外如果找不到缓存,会执行本地构建,这通常仍然需要从国外下载源码与构建依赖,因此仍然会很慢。为了完全解决速度问题,仍然建议使用旁路由等局域网全局代理方案。 + Nix 为了加快包构建速度,提供了 提前缓存构建结果提供给用户,但是在国内访问这个 cache 地址非常地慢,如果没有全局代理的话,基本上是无法使用的。 另外 Flakes 的数据源基本都是某个 Github 仓库,在国内从 Github 下载 Flakes 数据源也同样非常非常慢。 @@ -203,19 +206,10 @@ Nix 为了加快包构建速度,提供了 提前缓 为了自定义 cache 镜像源,我们必须在 `flake.nix` 中添加相关配置,这就是 `nixConfig` 参数,示例如下: -```nix +```nix{4-19} { description = "NixOS configuration of Ryan Yin"; - # 为了确保够纯,Flake 不依赖系统自身的 /etc/nix/nix.conf,而是在 flake.nix 中通过 nixConfig 设置 - # 但是为了确保安全性,flake 默认仅允许直接设置少数 nixConfig 参数,其他参数都需要在执行 nix 命令时指定 `--accept-flake-config`,否则会被忽略 - # - # 注意:即使添加了国内 cache 镜像,如果有些包国内镜像下载不到,它仍然会走国外。 - # 我的解法是使用 openwrt 旁路由 + openclash 加速下载。 - # 临时修改系统默认网关为我的旁路由 IP: - # sudo ip route add default via 192.168.5.201 - # 还原路由规则: - # sudo ip route del default via 192.168.5.201 nixConfig = { experimental-features = [ "nix-command" "flakes" ]; substituters = [ @@ -245,5 +239,3 @@ Nix 为了加快包构建速度,提供了 提前缓 ``` 改完后使用 `sudo nixos-rebuild switch` 应用配置即可生效,后续所有的包都会优先从国内镜像源查找缓存。 - -> 注:上述手段只能加速部分包的下载,许多 inputs 数据源仍然会从 Github 拉取,另外如果找不到缓存,会执行本地构建,这通常仍然需要从国外下载源码与构建依赖,因此仍然会很慢。为了完全解决速度问题,仍然建议使用旁路由等局域网全局代理方案。