feat: polish

This commit is contained in:
Ryan Yin 2023-07-08 18:31:45 +08:00
parent b8653668c3
commit 3b2aae967e
8 changed files with 57 additions and 34 deletions

View file

@ -4,7 +4,7 @@ When working with Flakes, you may encounter situations where you need to downgra
Here's an example of how you can add multiple nixpkgs inputs, each using a different git commit or branch:
```nix
```nix{8-13,19-20,27-44}
{
description = "NixOS configuration of Ryan Yin";
@ -20,17 +20,28 @@ Here's an example of how you can add multiple nixpkgs inputs, each using a diffe
nixpkgs-fd40cef8d.url = "github:nixos/nixpkgs/fd40cef8d797670e203a27a91e4b8e6decf0b90c";
};
outputs = inputs@{ self, nixpkgs, nixpkgs-stable, nixpkgs-fd40cef8d, ... }: {
outputs = inputs@{
self,
nixpkgs,
nixpkgs-stable,
nixpkgs-fd40cef8d,
...
}: {
nixosConfigurations = {
nixos-test = nixpkgs.lib.nixosSystem rec {
system = "x86_64-linux";
# The `specialArgs` parameter passes the non-default nixpkgs instances to other nix modules
# The `specialArgs` parameter passes the
# non-default nixpkgs instances to other nix modules
specialArgs = {
# To use packages from nixpkgs-stable, we configure some parameters for it first
# To use packages from nixpkgs-stable,
# we configure some parameters for it first
pkgs-stable = import nixpkgs-stable {
system = system; # Refer to the `system` parameter from the outer scope recursively
# To use Chrome, we need to allow the installation of non-free software
# Refer to the `system` parameter from
# the outer scope recursively
system = system;
# To use Chrome, we need to allow the
# installation of non-free softwares.
config.allowUnfree = true;
};
pkgs-fd40cef8d = import nixpkgs-fd40cef8d {
@ -54,11 +65,12 @@ In the above example, we have defined multiple nixpkgs inputs: `nixpkgs`, `nixpk
Next, you can refer to the packages from `pkgs-stable` or `pkgs-fd40cef8d` within your submodule. Here's an example of a Home Manager submodule:
```nix
```nix{4-7,13,25}
{
pkgs,
config,
# Nix will search for and inject this parameter from `specialArgs` in `flake.nix`
# Nix will search for and inject this parameter
# from `specialArgs` in `flake.nix`
pkgs-stable,
# pkgs-fd40cef8d,
...
@ -69,20 +81,20 @@ Next, you can refer to the packages from `pkgs-stable` or `pkgs-fd40cef8d` withi
home.packages = with pkgs-stable; [
firefox-wayland
# Chrome Wayland support was broken on the nixos-unstable branch, so we fallback to the stable branch for now
# Chrome Wayland support was broken on the nixos-unstable branch,
# so we fallback to the stable branch for now.
# Reference: https://github.com/swaywm/sway/issues/7562
google-chrome
];
programs.vscode = {
enable
= true;
package = pkgs-stable.vscode; # Refer to vscode from `pkgs-stable` instead of `pkgs`
enable = true;
# Refer to vscode from `pkgs-stable` instead of `pkgs`
package = pkgs-stable.vscode;
};
}
```
By adjusting the configuration as shown above, you can deploy it using `sudo nixos-rebuild switch`. This will downgrade your Firefox/Chrome/VSCode versions to the ones corresponding to `nixpkgs-stable` or `nixpkgs-fd40cef8d`.
> According to [1000 instances of nixpkgs](https://discourse.nixos.org/t/1000-instances-of-nixpkgs/17347), it's not a good practice to use `import` in submodules to customize `nixpkgs`. Each `import` creates a new instance of nixpkgs, which increases build time and memory usage as the configuration grows. To avoid this problem, we create all nixpkgs instances in `flake.nix`.
> According to [1000 instances of nixpkgs](https://discourse.nixos.org/t/1000-instances-of-nixpkgs/17347), it's not a good practice to use `import` in submodules or subflakes to customize `nixpkgs`. Each `import` creates a new instance of nixpkgs, which increases build time and memory usage as the configuration grows. To avoid this problem, we create all nixpkgs instances in `flake.nix`.

View file

@ -20,7 +20,7 @@ The functions of these four files are:
By modifying these files, you can declaratively change the system and home directory status.
As the configuration increases, it becomes difficult to maintain the configuration by relying solely on `configuration.nix` and `home.nix`. Therefore, a better solution is to use the Nix module system to split the configuration into multiple modules and write them in a classified manner.
However, as the configuration grows, relying solely on `configuration.nix` and `home.nix` can lead to bloated and difficult-to-maintain files. A better solution is to use the Nix module system to split the configuration into multiple Nix modules and write them in a classified manner.
The Nix module system provides a parameter, `imports`, which accepts a list of `.nix` files and merges all the configuration defined in these files into the current Nix module. Note that `imports` will not simply overwrite duplicate configuration but handle it more reasonably. For example, if `program.packages = [...]` is defined in multiple modules, then `imports` will merge all `program.packages` defined in all Nix modules into one list. Attribute sets can also be merged correctly. The specific behavior can be explored by yourself.

View file

@ -24,7 +24,8 @@ Another approach is to delete `/etc/nixos` directly and specify the configuratio
sudo mv /etc/nixos /etc/nixos.bak
cd ~/nixos-config
# `--flake .#nixos-test` deploys the flake.nix located in the current directory, and the nixosConfiguration's name is `nixos-test`
# `--flake .#nixos-test` deploys the flake.nix located in
# the current directory, and the nixosConfiguration's name is `nixos-test`
sudo nixos-rebuild switch --flake .#nixos-test
```
@ -34,7 +35,8 @@ Choose the method that suits you best. Afterward, system rollback becomes simple
cd ~/nixos-config
# Switch to the previous commit
git checkout HEAD^1
# Deploy the flake.nix located in the current directory, with the nixosConfiguration's name `nixos-test`
# Deploy the flake.nix located in the current directory,
# with the nixosConfiguration's name `nixos-test`
sudo nixos-rebuild switch --flake .#nixos-test
```
@ -52,7 +54,7 @@ To clean up historical versions and free up storage space, use the following com
```shell
# Delete all historical versions older than 7 days
sudo nix profile wipe-history --profile /nix/var/nix/profiles/system --older-than 7d
sudo nix profile wipe-history --older-than 7d --profile /nix/var/nix/profiles/system
# Run garbage collection after wiping history
sudo nix store gc --debug

View file

@ -2,6 +2,7 @@
NixOS 不遵循 FHS 标准,因此你从网上下载的二进制程序在 NixOS 上大概率是跑不了的。
为了在 NixOS 上跑这些非 NixOS 的二进制程序,需要做一些骚操作。有位老兄在这里总结了 10 种实现此目的的方法:[Different methods to run a non-nixos executable on Nixos](https://unix.stackexchange.com/questions/522822/different-methods-to-run-a-non-nixos-executable-on-nixos),推荐一读。
此外如果你懒得自己折腾,只想实现需求,也可以直接看看这个傻瓜式工具 [nix-alien](https://github.com/thiagokokada/nix-alien).
我个人用的比较多的方法是,直接创建一个 FHS 环境来运行二进制程序,这种方法非常方便易用。

View file

@ -6,7 +6,7 @@
为了实现上述需求,首先修改 `/etc/nixos/flake.nix`,示例内容如下(主要是利用 `specialArgs` 参数):
```nix
```nix{8-13,19-20,27-44}
{
description = "NixOS configuration of Ryan Yin"
@ -14,7 +14,8 @@
# 默认使用 nixos-unstable 分支
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
# 最新 stable 分支的 nixpkgs用于回退个别软件包的版本当前最新版本为 23.05
# 最新 stable 分支的 nixpkgs用于回退个别软件包的版本
# 当前最新版本为 23.05
nixpkgs-stable.url = "github:nixos/nixpkgs/nixos-23.05";
# 另外也可以使用 git commit hash 来锁定版本,这是最彻底的锁定方式
@ -38,8 +39,10 @@
# 这里我们直接在 flake.nix 中创建实例, 再传递到其他子 modules 中使用
# 这样能有效重用 nixpkgs 实例,避免 nixpkgs 实例泛滥。
pkgs-stable = import nixpkgs-stable {
system = system; # 这里递归引用了外部的 system 属性
# 为了拉取 chrome 等软件包,需要允许安装非自由软件
# 这里递归引用了外部的 system 属性
system = system;
# 为了拉取 chrome 等软件包,
# 这里我们需要允许安装非自由软件
config.allowUnfree = true;
};
@ -61,10 +64,11 @@
然后在你对应的 module 中使用该数据源中的包,一个 Home Manager 的子模块示例:
```nix
```nix{4-7,13,25}
{
pkgs,
config,
# nix 会从 flake.nix 的 specialArgs 查找并注入此参数
pkgs-stable,
# pkgs-fd40cef8d, # 也可以使用固定 hash 的 nixpkgs 数据源
@ -72,22 +76,24 @@
}:
{
# 这里从 pkg-stable 中引用包
# # 这里从 pkg-stable 中引用包(而不是默认的 pkgs
home.packages = with pkgs-stable; [
firefox-wayland
# chrome wayland support was broken on nixos-unstable branch, so fallback to stable branch for now
# https://github.com/swaywm/sway/issues/7562
# nixos-unstable 分支中的 Chrome Wayland 支持目前存在问题,
# 因此这里我们将 google-chrome 回滚到 stable 分支,临时解决下 bug.
# 相关 Issue: https://github.com/swaywm/sway/issues/7562
google-chrome
];
programs.vscode = {
enable = true;
package = pkgs-stable.vscode; # 这里也一样,从 pkgs-stable 中引用包
# 这里也一样,从 pkgs-stable 中引用包
package = pkgs-stable.vscode;
};
}
```
配置完成后,通过 `sudo nixos-rebuild switch` 部署即可将 firefox/chrome/vscode 三个软件包回退到 stable 分支的版本。
> 根据 @fbewivpjsbsby 补充的文章 [1000 instances of nixpkgs](https://discourse.nixos.org/t/1000-instances-of-nixpkgs/17347),在子模块中用 `import` 来定制 `nixpkgs` 不是一个好的习惯,因为每次 `import` 都会重新求值并产生一个新的 nixpkgs 实例,在配置越来越多时会导致构建时间变长、内存占用变大。所以这里改为了在 `flake.nix` 中创建所有 nixpkgs 实例。
> 根据 @fbewivpjsbsby 补充的文章 [1000 instances of nixpkgs](https://discourse.nixos.org/t/1000-instances-of-nixpkgs/17347),在子模块或者子 flakes 中用 `import` 来定制 `nixpkgs` 不是一个好的习惯,因为每次 `import` 都会重新求值并产生一个新的 nixpkgs 实例,在配置越来越多时会导致构建时间变长、内存占用变大。所以这里改为了在 `flake.nix` 中创建所有 nixpkgs 实例。

View file

@ -21,7 +21,8 @@ $ tree
- 此配置文件的所有配置项,参见 [Appendix A. Configuration Options - Home Manager](https://nix-community.github.io/home-manager/options.html)
通过修改上面几个配置文件就可以实现对系统与 Home 目录状态的修改。
但是随着配置的增多,单纯依靠 `configuration.nix``home.nix` 会导致配置文件臃肿,难以维护,因此更好的解决方案是通过 Nix 的模块机制,将配置文件拆分成多个模块,分门别类地编写维护。
但是随着配置的增多,单纯依靠 `configuration.nix``home.nix` 会导致配置文件臃肿,难以维护。
更好的解决方案是通过 Nix 的模块机制,将配置文件拆分成多个模块,分门别类地编写维护。
在前面的 Nix 语法一节有介绍过:「`import` 的参数如果为文件夹路径,那么会返回该文件夹下的 `default.nix` 文件的执行结果」,实际 Nix 还提供了一个 `imports` 参数,它可以接受一个 `.nix` 文件列表,并将该列表中的所有配置**合并**Merge到当前的 attribute set 中。注意这里的用词是「合并」,它表明 `imports` 如果遇到重复的配置项,不会简单地按执行顺序互相覆盖,而是更合理地处理。比如说我在多个 modules 中都定义了 `program.packages = [...]`,那么 `imports` 会将所有 modules 中的 `program.packages` 这个 list 合并。不仅 list 能被正确合并attribute set 也能被正确合并,具体行为各位看官可以自行探索。

View file

@ -23,7 +23,8 @@ sudo ln -s ~/nixos-config/ /etc/nixos
sudo mv /etc/nixos /etc/nixos.bak # 备份原来的配置
cd ~/nixos-config
# 通过 --flake .#nixos-test 参数,指定使用当前文件夹的 flake.nix使用的 nixosConfiguraitons 名称为 nixos-test
# 通过 --flake .#nixos-test 参数指定使用当前文件夹的 flake.nix
# 使用的 nixosConfiguraitons 名称为 nixos-test
sudo nixos-rebuild switch --flake .#nixos-test
```
@ -51,7 +52,7 @@ nix profile history --profile /nix/var/nix/profiles/system
```shell
# 清理 7 天之前的所有历史版本
sudo nix profile wipe-history --profile /nix/var/nix/profiles/system --older-than 7d
sudo nix profile wipe-history --older-than 7d --profile /nix/var/nix/profiles/system
# 清理历史版本并不会删除数据,还需要手动 gc 下
sudo nix store gc --debug
```