Files
nix/README.md

123 lines
3.4 KiB
Markdown
Raw 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.

### 目录结构
```
├── home # home manager 配置信息
│ ├── core.nix # 核心的通用配置,由其他配置引入
│ ├── desktop.nix # 桌面环境配置
│ ├── server.nix # 服务器配置
├── modules # 通用模块,不同机器可以根据的需要引入
│ ├── home # home manager 通用模块
│ │ └── -
│ └── nixos # nixos 通用模块
├── overlays # 安装包的修改配置
│ └── -
├── pkgs
│ └── -
├── profiles # 不同机器的配置文件
│ ├── apollo # 主服务器配置
│ ├── gaea # 主用机配置
│ └── luna # 虚拟机配置
├── secrets
│ └── _public_keys_
├── flake.lock
├── flake.nix # nix flake 入口
├── nixos-install.sh # nixos 全新安装脚本
└── flake.lock
```
### 如何安装?
0. 准备一个 64 位的 nixos [minimal iso image](https://channels.nixos.org/nixos-22.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** 变量。
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
```
### 日常更新系统脚本
```bash
./nixos-switch.sh
```