隔壁老王
面向懒猫平台(LZCBox)x86_64 系统的 Nix 持久化方案
从能用到长期可运维的工程化脚本。
脚本地址:
https://github.com/lazycat-contrib/nix_persistence_manager.sh

第一篇《Nix 安装与配置完全指南》已经证明了路线可行:
/nix但当你开始长期使用,会出现更"运维化"的需求:
.profile 的挂载触发不够。所以本篇目标是把整套流程工程化为一个脚本:
懒猫采用只读系统镜像 + Overlay 的架构:
/etc 不是可靠持久入口所以要把可持久化配置放在允许长期保存的区域(例如 /root),并优先采用用户域机制(systemd --user)。
/nix 用 bind mountNix 生态里大量工具默认使用绝对路径 /nix/store。bind mount 的优点:
/nix 仍是"真实路径入口".profile + nix-ready).profile:照顾交互登录体验,登录后自动准备环境。nix-ready:作为硬兜底,任何命令执行前都能先检查并补挂载。/root:mv nix_persistence_manager.sh /root/
chmod +x /root/nix_persistence_manager.sh
一键安装,使用官方推荐版本:
/root/nix_persistence_manager.sh --official
带代理:
/root/nix_persistence_manager.sh --official --proxy http://172.18.0.1:10809
脚本会自动:
https://nixos.org/nix/install 获取官方安装脚本。.profile 冗余行、fish 配置)。脚本文件:nix_persistence_manager.sh(约 830 行,仅支持 x86_64-linux)
| 能力 | 说明 |
|---|---|
| 全新安装 | --official 自动获取官方推荐版本 |
| 迁移配置 | --migrate-only 迁移到隐藏持久目录 |
| bind mount | 自动维护 /nix 挂载 |
| nix.conf | 生成并维护 Nix 配置 |
| 辅助工具 | nix-ready / nix-gc / nix-doctor-lite |
| systemd 服务 | nix-bind.service 自动挂载 + linger |
| 代理支持 | --proxy 统一代理出口 |
| 离线安装 | --tarball 或 --offline-dir |
| 预演模式 | --dry-run 仅打印不执行 |
| 清理回退 | --cleanup / --cleanup --purge-data |
/root/nix_persistence_manager.sh --dry-run --migrate-only
提前确认目录、挂载、systemd 操作是否符合预期。
# 推荐:自动获取官方最新版本
/root/nix_persistence_manager.sh --official
# 带代理
/root/nix_persistence_manager.sh --official --proxy http://172.18.0.1:10809
# 已有 nix 时强制重装
/root/nix_persistence_manager.sh --official --reinstall
/root/nix_persistence_manager.sh --tarball /root/nix-2.34.7-x86_64-linux.tar.xz
/root/nix_persistence_manager.sh --offline-dir /root/offline-packages
脚本会自动在目录内查找 nix-*-x86_64-linux.tar.xz。
/root/nix_persistence_manager.sh \
--download-url https://releases.nixos.org/nix/nix-2.34.7/nix-2.34.7-x86_64-linux.tar.xz \
--sha256 <SHA256值>
/root/nix_persistence_manager.sh --migrate-only
它会:
/root/nix_persistent、旧 /nix 挂载等)。/root/.local/state/nix)。/root/nix_persistence_manager.sh \
--migrate-only \
--app-service-name my-nix-app \
--app-exec '/root/.nix-profile/bin/your-binary --flag'
脚本会:
nix-bind.service(先确保 /nix 可用)。nix-bind.service)。loginctl enable-linger root。.profile 结构说明安装完成后,.profile 结构如下:
# ~/.profile
if [ -n "$BASH_VERSION" ]; then
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# >>> nix-persistence profile.d loader >>>
if [ -d "/root/.profile.d" ]; then
for _p in "/root/.profile.d"/*.sh; do
[ -r "$_p" ] && . "$_p"
done
unset _p
fi
# <<< nix-persistence profile.d loader <<<
nix-bind-mount.sh 内容:
# 挂载 /nix
if ! mountpoint -q "/nix"; then
if [ -L "/nix" ]; then rm -f "/nix"; fi
mkdir -p "/nix"
mount --bind "/root/.local/state/nix" "/nix" 2>/dev/null || true
fi
# source nix profile(设置 PATH 等)
if [ -e "/root/.nix-profile/etc/profile.d/nix.sh" ]; then
. "/root/.nix-profile/etc/profile.d/nix.sh"
fi
# 添加辅助工具到 PATH
case ":$PATH:" in
*":/root/.local/bin:"*) ;;
*) export PATH="/root/.local/bin:$PATH" ;;
esac
关键点:
nix-bind-mount.sh 里完成。nix.fish)会被自动移除(懒猫只用 bash)。# 验证挂载
findmnt /nix
# 验证 Nix
/root/.local/bin/nix-ready nix --version
# 查看服务状态
systemctl --user status nix-bind.service
# 垃圾回收
/root/.local/bin/nix-gc
# 仅清理脚本管理产物(保留 Nix 数据)
/root/nix_persistence_manager.sh --cleanup
# 彻底清理(包含持久化数据)
/root/nix_persistence_manager.sh --cleanup --purge-data
脚本会自动安装 xz-utils。如果网络不通,可提前手动安装:
apt install -y xz-utils
.../deleted 报文表示之前 /nix 的挂载源目录 inode 已被删除。建议:
umount /nix || umount -l /nix
# 找到仍有 store 数据的旧目录
for d in /var/roothome/nix_persistent /var/roothome/.local/state/nix /root/nix_persistent; do
if [ -d "$d/store" ] && [ -n "$(ls -A "$d/store" 2>/dev/null)" ]; then
rsync -aHAX "$d"/ /root/.local/state/nix/
break
fi
done
mount --bind /root/.local/state/nix /nix
这是官方安装器的警告,单用户模式下可以忽略。脚本使用 --no-daemon 参数以单用户模式安装。
/root/.local/bin/nix-ready nix --version
在懒猫平台上做 Nix,核心不是"装上就行",而是"长期稳定可运维"。
这套方案的关键价值:
/nix 语义稳定 — bind mount。--official 无需手动选择版本。
评论
0暂无评论