|
| 1 | +#!/usr/bin/env sh |
| 2 | + |
| 3 | +# Initialize Nix store volume directory |
| 4 | +# This script runs as root during container build |
| 5 | + |
| 6 | +if [ "${KEEP_GOING:-false}" = "true" ]; then |
| 7 | + set +e |
| 8 | +else |
| 9 | + set -e |
| 10 | +fi |
| 11 | +set -x |
| 12 | + |
| 13 | +# Create Nix store mount point with permissions for all users |
| 14 | +mkdir -p /.persist-nix-store |
| 15 | +chmod 777 /.persist-nix-store |
| 16 | + |
| 17 | +# Generate the postCreateCommand script that runs in user context |
| 18 | +INIT_SCRIPT_PATH="/usr/local/share/persist-nix-init.sh" |
| 19 | + |
| 20 | +tee "$INIT_SCRIPT_PATH" >/dev/null <<'EOF' |
| 21 | +#!/usr/bin/env sh |
| 22 | +
|
| 23 | +# Initialize Nix store symlinks |
| 24 | +# This script runs in user context (postCreateCommand) |
| 25 | +
|
| 26 | +set -e |
| 27 | +set -x |
| 28 | +
|
| 29 | +# Check if a path is a mount point |
| 30 | +is_mount_point() { |
| 31 | + if command -v mountpoint >/dev/null 2>&1; then |
| 32 | + mountpoint -q "$1" |
| 33 | + else |
| 34 | + # Fallback for systems without mountpoint command |
| 35 | + mount | grep -q " on $(readlink -f "$1") " |
| 36 | + fi |
| 37 | + return $? |
| 38 | +} |
| 39 | +
|
| 40 | +# Only proceed if Nix is installed |
| 41 | +if ! command -v nix >/dev/null 2>&1; then |
| 42 | + echo "Nix not found, skipping persist-nix initialization" |
| 43 | + exit 0 |
| 44 | +fi |
| 45 | +
|
| 46 | +# Handle existing /nix/store directory |
| 47 | +if [ -d /nix/store ] && [ ! -L /nix/store ]; then |
| 48 | + # Check if it's already a mount point (e.g., from another devcontainer feature) |
| 49 | + if is_mount_point /nix/store 2>/dev/null; then |
| 50 | + echo "Note: /nix/store is already mounted by another feature, skipping backup" |
| 51 | + else |
| 52 | + echo "Backing up existing /nix/store to /nix/store.bak" |
| 53 | + sudo mv /nix/store /nix/store.bak |
| 54 | + fi |
| 55 | +fi |
| 56 | +
|
| 57 | +# Ensure /nix directory exists |
| 58 | +sudo mkdir -p /nix |
| 59 | +
|
| 60 | +# Create symlink from persistent volume to /nix/store |
| 61 | +if [ ! -L /nix/store ]; then |
| 62 | + echo "Creating symlink /nix/store -> /.persist-nix-store" |
| 63 | + sudo ln -s /.persist-nix-store /nix/store |
| 64 | +else |
| 65 | + echo "/nix/store symlink already exists" |
| 66 | +fi |
| 67 | +
|
| 68 | +echo "persist-nix initialization complete" |
| 69 | +EOF |
| 70 | + |
| 71 | +# Make the init script executable |
| 72 | +chmod +x "$INIT_SCRIPT_PATH" |
0 commit comments