deinonyxus/files/system/usr/bin/mount-nix.sh

59 lines
1.9 KiB
Bash
Raw Normal View History

2026-01-14 18:19:46 +02:00
#!/usr/bin/env bash
set -euo pipefail
# Bind-mount /var/lib/nix-store to /nix.
# If /var/lib/nix-store is empty, seed it from the baked store in /usr/share/nix-store.
mkdir -p /usr/share/nix-store /var/lib/nix-store /nix
copy_seed_store() {
if command -v rsync >/dev/null 2>&1; then
rsync -aH --delete /usr/share/nix-store/ /var/lib/nix-store/
else
cp -a /usr/share/nix-store/. /var/lib/nix-store/
fi
}
2026-01-14 19:45:46 +02:00
sync_missing_store() {
# Ensure any baked store paths exist in /var without clobbering user additions.
if command -v rsync >/dev/null 2>&1; then
rsync -aH --ignore-existing /usr/share/nix-store/store/ /var/lib/nix-store/store/
fi
}
2026-01-14 18:46:55 +02:00
ensure_system_profile() {
local seed_profile="/usr/share/nix-store/var/nix/profiles/system"
local target_profile="/var/lib/nix-store/var/nix/profiles/system"
mkdir -p /var/lib/nix-store/var/nix/profiles
if { [ ! -e "$target_profile" ] || [ -L "$target_profile" ] && [ ! -e "$(readlink -f "$target_profile")" ]; } \
2026-01-14 19:45:46 +02:00
&& { [ -e "$seed_profile" ] || [ -L "$seed_profile" ]; }; then
2026-01-14 18:46:55 +02:00
cp -a "$seed_profile" "$target_profile"
fi
}
2026-01-14 18:19:46 +02:00
if ! mountpoint -q /nix; then
if [ -z "$(ls -A /var/lib/nix-store 2>/dev/null)" ] && compgen -G "/usr/share/nix-store/*" >/dev/null; then
copy_seed_store
fi
2026-01-14 18:46:55 +02:00
ensure_system_profile
2026-01-14 19:45:46 +02:00
sync_missing_store
2026-01-14 18:46:55 +02:00
2026-01-14 18:19:46 +02:00
mount --bind /var/lib/nix-store /nix
2026-01-14 19:45:46 +02:00
# Force an executable SELinux context on the bind mount so systemd can exec nix-daemon.
# Use a permissive fallback if the label option is rejected.
if ! mount -o remount,bind,exec,context=system_u:object_r:bin_t:s0 /nix 2>/dev/null; then
mount -o remount,bind,exec /nix
fi
2026-01-14 18:19:46 +02:00
# Ensure daemon paths exist and labels are sane.
if command -v systemd-tmpfiles >/dev/null 2>&1; then
systemd-tmpfiles --create /usr/lib/tmpfiles.d/nix-daemon.conf
fi
if command -v restorecon >/dev/null 2>&1; then
restorecon -RF /var/lib/nix-store /nix || true
fi
fi