ITADN
dzervas/dotfiles
dzervas/dotfiles · 文件 下载 ZIP
文件最后提交记录最后更新时间
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

NixOS 配置

使用 nix run github:dzervas/dotfiles 测试我的 neovim 配置!:tada:

拜托,我花了好几个小时才弄明白怎么做

Live CD

hardware/iso 中还有一个 Live CD 配置。

下载

在 NixOS(使用 flakes)中:

# Once per system
nix run github:dzervas/dotfiles#iso-auth

nix run github:dzervas/dotfiles#iso-get

在 NixOS 中(面向普通用户):

# Once per system
nix shell nixpkgs#oras nixpkgs#gh -c "gh auth token | oras login ghcr.io --password-stdin -u github"

nix shell nixpkgs#oras -c oras pull ghcr.io/dzervas/dotfiles/nixos-iso:latest

在其他系统中,安装 oras 和 github cli:

# Once per system
gh auth login --scopes "repo,read:org,gist,read:packages"
gh auth token | oras login ghcr.io --password-stdin -u github

oras pull ghcr.io/dzervas/dotfiles/nixos-iso:latest

写入 USB

# Find the USB device
lsblk
# Replace /dev/sdX with the USB device
sudo dd bs=4M status=progress conv=fsync oflag=direct if=$(ls nixos-*-linux.iso) of=/dev/sdX

手动构建

nix build .#iso

全新安装

  • 使用 gparted(sudo -E gparted),创建一个 GPT 分区表,包含一个 1G 的 FAT32 分区,标签为 "BOOT",其余空间作为另一个标签为 "system" 的分区。
  • 使用 cryptsetup luksFormat /dev/sdX2 --label cryptroot 对第二个分区进行 LUKS 格式化。
  • 使用 cryptsetup open /dev/sdX2 cryptroot 打开 LUKS 分区。
  • 使用 mkfs.btrfs -L system /dev/mapper/cryptroot 格式化 LUKS 分区。
  • 创建子卷:
mount /dev/mapper/cryptroot /mnt
btrfs subvolume create /mnt/root
btrfs subvolume create /mnt/home
btrfs subvolume create /mnt/nix
btrfs filesystem mkswapfile --size 16G /swapfile
  • 挂载子卷:
mount -o subvol=root /dev/mapper/cryptroot /mnt
mkdir -p /mnt/home /mnt/nix /mnt/boot
mount -o subvol=home /dev/mapper/cryptroot /mnt/home
mount -o subvol=nix /dev/mapper/cryptroot /mnt/nix
mount /dev/disk/by-label/BOOT /mnt/boot
  • 使用 nixos-install --flake /iso/dotfiles#<hostname> 安装 nixos
  • 它很可能会失败,因此:
nixos-enter
sbctl create-keys
exit
  • 再次运行 nixos-install
  • 首次启动后,在 tty 终端上更改用户密码

故障排除

要修复 store:

nix-store --verify --check-contents --repair

如果文件因损坏而为空且无法修复:

nix-store --query --referrers-closure $(find /store -maxdepth 1 -type f -name '*.drv' -size 0) | xargs nix-store --delete --ignore-liveness
nix-store --gc
nix-store --verify --check-contents --repair

A .nix 文件为空,我不得不 sudo nix-store --query --roots <path> 以找到 home 下的软链接,将其删除并重新运行初始的 home-manager rebuild。

如何更新到特定的 nixpkgs 提交

nix flake update --override-input nixpkgs github:NixOS/nixpkgs/7252b96d60dc2ccf3971e436811cfce42b258669

特殊说明

  • GParted 需要 sudo -E gparted 才能运行
  • 尽管通常不需要,但要更改 M720 Triathlon 的按钮:
sudo nix shell nixpkgs#solaar --command solaar-cli config 1 persistent-remappable-keys "MultiPlatform Gesture Button" "F14"
  • 为了脚本兼容性:
echo -e "#!/bin/sh\nexec /usr/bin/env bash \$@" | sudo tee /bin/bash
echo -e "#!/bin/sh\nexec /usr/bin/env bash \$@" | sudo tee /usr/bin/bash
echo -e "#!/bin/sh\nexec /usr/bin/env python \$@" | sudo tee /usr/bin/python
echo -e "#!/bin/sh\nexec /usr/bin/env python3 \$@" | sudo tee /usr/bin/python3
sudo chmod +x /bin/bash /usr/bin/bash /usr/bin/python /usr/bin/python3
  • 如果已配置使用 opnix 的机器,/etc/opnix.env 需要填充以下内容:
OP_SERVICE_ACCOUNT_TOKEN="{your token here}"
  • 要修复 command-not-found 数据库不存在的问题:
sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos
sudo nix-channel --update

如果某个包构建失败(不安全/其他原因)

要查找谁在使用它:

nixos-rebuild build --show-trace 2>/dev/stdout | grep 'while evaluating derivation'

安全启动

# Crete the keys
sudo sbctl create-keys
# Check that everything is signed (apart from the kernel under nixos dir)
sudo sbctl verify
# Enter setup mode from the BIOS
sudo sbctl enroll-keys --microsoft
# Check that secure boot is enabled
sudo sbctl status

Pre-commit hook 代码检查

.git/hooks/pre-commit:

#!/bin/sh

# Navigate to the root of the Git repository
cd "$(git rev-parse --show-toplevel)" || exit 1

# Run statix check
statix check

# Check if statix check was successful
if [ $? -ne 0 ]; then
  echo "Statix check failed. Please fix the issues before committing."
  exit 1
fi

在多台机器之间暴露二进制缓存

主机端:

# Once:
install -d -m 0700 /etc/nix/cache
nix-store --generate-binary-cache-key desktop /etc/nix/cache/private-key.pem /etc/nix/cache/public-key.pem
chmod 600 /etc/nix/cache/private-key.pem

# To serve:
nix store sign --all --recursive -k /etc/nix/cache/private-key.pem
NIX_SECRET_KEY_FILE=/etc/nix/cache/private-key.pem nix run nixpkgs#nix-serve-ng -- -- --host 0.0.0.0 --port 8181

客户端:

rebuild --option substituters 'http://<host>:8181 https://cache.nixos.org' --option trusted-public-keys '<public-key.pem>'

在获取缓存失败的情况下:nix-collect-garbage