Files
nix/README.md
2025-05-09 14:40:19 +08:00

214 lines
7.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# NixOS 和 Home Manager 配置
这是一个基于 Nix Flakes 的 NixOS 和 Home Manager 配置仓库,支持多台机器的配置管理。当前系统版本基于 NixOS 24.11。
## 目录结构
```
├── home # home manager 配置信息
│ ├── core.nix # 核心的通用配置,由其他配置引入
│ ├── desktop.nix # 桌面环境配置
│ └── server.nix # 服务器配置
├── libs # 自定义库函数
├── modules # 通用模块,不同机器可以根据的需要引入
│ ├── home # home manager 通用模块
│ └── nixos # nixos 通用模块
├── overlays # 安装包的修改配置
│ └── default.nix # 覆盖配置入口
├── pkgs # 自定义软件包
├── profiles # 不同机器的配置文件, 放置只有特定主机可以使用的配置
│ ├── apollo # 主服务器配置
│ │ ├── default.nix # 入口文件负责创建nixosSystem并选择使用的nixpkgs版本
│ │ ├── home # apollo的home-manager配置
│ │ └── nixos # apollo的NixOS配置
│ │ └── network.nix # 网络配置
│ ├── gaea # 主用机配置
│ │ ├── default.nix # 入口文件负责创建nixosSystem并选择使用的nixpkgs版本
│ │ ├── home # gaea的home-manager配置
│ │ └── nixos # gaea的NixOS配置
│ └── luna # 虚拟机配置
│ ├── default.nix # 入口文件负责创建nixosSystem并选择使用的nixpkgs版本
│ ├── hardware-configuration.nix # 硬件配置
│ └── network.nix # 网络配置
├── flake.lock # flake 锁定文件
├── flake.nix # nix flake 入口
├── nixos-install.sh # nixos 全新安装脚本
├── nixos-switch.sh # 日常更新系统脚本
├── non-nixos-install.sh # 非NixOS系统安装home-manager配置脚本
└── nixpkgs.nix # 从flake.lock中提取nixpkgs版本信息用于非flake命令
```
## 如何安装?
0. 准备一个 64 位的 nixos [minimal iso image](https://channels.nixos.org/nixos-24.11/latest-nixos-minimal-x86_64-linux.iso) 烧录好,然后进入 live 系统。
1. 分区
使用 fdisk 或 parted 工具进行分区。现在假设两个分区为:`/dev/sda1` `/dev/sda2`
```bash
# 使用parted分区
parted /dev/nvme0n1 mklabel gpt
parted /dev/nvme0n1 mkpart primary fat32 1MiB 1024MiB
parted /dev/nvme0n1 mkpart primary btrfs 1024MiB 95%
```
2. 格式化分区
```bash
mkfs.fat -F 32 /dev/nvme0n1p1 # boot / EFI 分区
mkfs.btrfs /dev/nvme0n1p2 # 系统分区
```
3. 挂载
```bash
mount /dev/nvme0n1p2 /mnt
btrfs subvolume create /mnt/home # home 分区
btrfs subvolume create /mnt/nix # nix 分区
btrfs subvolume create /mnt/swap # swap 分区
umount /mnt
mount -o compress=zstd /dev/nvme0n1p2 /mnt
mkdir -p /mnt/{boot,nix,home,swap}
mount /dev/nvme0n1p1 /mnt/boot
mount -o compress=zstd,noatime,subvol=nix /dev/nvme0n1p2 /mnt/nix
mount -o compress=noatime,subvol=swap /dev/nvme0n1p2 /mnt/swap
mount -o compress=zstd,subvol=home /dev/nvme0n1p2 /mnt/home
btrfs filesystem mkswapfile --size 16g --uuid clear /mnt/swap/swapfile
```
3.1 不变系统
如果希望使用不变原子系统(即系统更新时不会修改当前运行的系统,而是创建新的系统生成),在 profile 中引入 `modules/nixos/sysatomic.nix` 模块。这种情况下的挂载方式更简单:
```bash
mkdir -p /mnt/{boot,nix}
mount /dev/nvme0n1p2 /mnt/nix
mount /dev/nvme0n1p1 /mnt/boot
```
4. 生成一个基本的配置
```bash
nixos-generate-config --root /mnt
```
5. 克隆仓库到本地
```bash
git clone https://github.com/synebula/.nix.git /mnt/nix/.nix
cd /mnt/nix/.nix
```
6. 将 /mnt/etc/nixos 中的 `hardware-configuration.nix` 拷贝到 `/mnt/.nix/profiles/<profile>/hardware-configuration.nix` 其中`<profile>`指需要的 profile。
```bash
cp /mnt/etc/nixos/hardware-configuration.nix /mnt/.nix/profiles/<profile>/hardware-configuration.nix
```
7. 用户名修改: 编辑 `/mnt/.nix/flake.nix` 修改 **username**、**useremail** 和 **sysversion**(系统版本)变量。
8. 使用 `mkpasswd {PASSWORD} -m sha-512` 命令生成的密码哈希串替换掉 `/mnt/.nix/modules/nixos/user-group.nix` 中的 `users.users.<name>.hashedPassword` 值替换掉。
9. 安装
```bash
./nixos-install.sh <profile>
# 或者
nixos-install --option substituters "https://mirrors.ustc.edu.cn/nix-channels/store https://cache.nixos.org" --no-root-passwd --flake .#<profile>
```
10. 重启
```bash
reboot
```
## 日常使用
### NixOS 系统更新
在完成对配置文件的修改后,使用以下命令更新系统:
```bash
# 执行 bash alias
nixos-switch
# 或执行本机脚本
./nixos-switch.sh
```
### 非 NixOS 系统安装 home-manager 配置
在非 NixOS 系统上(如 Ubuntu、Arch Linux 等),可以使用以下命令安装 home-manager 配置:
```bash
./non-nixos-install.sh
```
## 自定义配置
要添加新的机器配置,可以在`profiles`目录下创建新的目录,并根据现有的配置文件进行修改。
要添加新的模块,可以在`modules`目录下创建新的模块文件,并在相应的配置文件中引入。
## profiles 目录和 nixosSystem 创建
在这个配置中,我们使用了一个抽象的方法来创建 nixosSystem同时保留了每个 profile 自行决定使用哪个版本的 nixpkgs 的能力,并简化了参数传递:
1. **libs/mkNixosSystem.nix** - 这个文件提供了一个通用函数,用于创建 nixosSystem。它接受以下参数
- `args`: 从 flake.nix 传递的所有参数,包含 self、inputs、outputs、username 等
- `nixpkgs`: 要使用的 nixpkgs (由 profile 的 default.nix 决定)
- `path`: profile 目录的路径
2. **profiles/[hostname]/default.nix** - 这是配置的入口文件,负责选择使用的 nixpkgs 版本并调用 libs.mkNixosSystem 函数。
3. **profiles/[hostname]/configuration.nix** - 包含实际的配置内容,如系统模块、服务、用户设置等。
## 垃圾清理
home-manager profiles 文件夹
```bash
~/.local/state/nix/profiles/
```
1. 列出历史版本
```bash
# 最新api
nix profile history --profile /nix/var/nix/profiles/system
nix profile history --profile ~/.local/state/nix/profiles/home-manager
# nixos系统api
nixos-rebuild list-generations
# 旧版api
nix-env --list-generations -p /nix/var/nix/profiles/system
```
2. 查看根依赖
```bash
nix-store --query --roots /nix/store/ijr7hck016n92ds7zh9syv51qv4cl8zg-wechat-uos-4.0.0.23
```
3. 删除历史generations
```bash
nix profile wipe-history --profile ~/.local/state/nix/profiles/home-manager # 清除所有非当前
nix profile wipe-history --older-than 1d --profile /nix/var/nix/profiles/system # 保留最近7天
nix profile wipe-history --older-than 7d --profile ~/.local/state/nix/profiles/home-manager # home-manager单独清除
```
4. 删除旧版本
```bash
nix-collect-garbage --delete-older-than 30d
nix-collect-garbage -d # --delete-old 清除所有未使用的
```