summaryrefslogtreecommitdiff
path: root/util/podman/norce_prefetch_build_and_run.sh
blob: 318ec4a62bfc6034941813cd0363d5453bb79b04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env bash
set -euo pipefail

# --- Usage ---
# ./build.sh <url> <sha256>
if [[ $# -ne 2 ]]; then
    echo "Usage: $0 <url> <sha256>"
    exit 1
fi

SRC_URL="$1"
SRC_HASH="$2"

NIX_STORE_VOL="nix-store"
NIX_STATE_VOL="nix-state"

# --- Ensure Podman volumes exist ---
ensure_volume() {
    local vol="$1"
    if ! podman volume inspect "$vol" >/dev/null 2>&1; then
        echo "📦 Creating Podman volume: $vol"
        podman volume create "$vol" >/dev/null
    fi
}

ensure_volume "$NIX_STORE_VOL"
ensure_volume "$NIX_STATE_VOL"

# --- Detect host CA bundle ---
HOST_CA_BUNDLE=""
if [[ -f /etc/ssl/certs/ca-certificates.crt ]]; then
    HOST_CA_BUNDLE="/etc/ssl/certs/ca-certificates.crt"
elif [[ -f /etc/pki/tls/certs/ca-bundle.crt ]]; then
    HOST_CA_BUNDLE="/etc/pki/tls/certs/ca-bundle.crt"
fi

# --- Run inside Nix container ---
podman run --rm -it \
    -v "$NIX_STORE_VOL":/nix/store \
    -v "$NIX_STATE_VOL":/nix/var \
    -v /etc/ssl/certs:/etc/ssl/certs:ro \
    -v /etc/pki:/etc/pki:ro \
    ${HOST_CA_BUNDLE:+-v "$HOST_CA_BUNDLE:$HOST_CA_BUNDLE:ro"} \
    -e SRC_URL="$SRC_URL" \
    -e SRC_HASH="$SRC_HASH" \
    -e SSL_CERT_FILE="$HOST_CA_BUNDLE" \
    -e NIX_SSL_CERT_FILE="$HOST_CA_BUNDLE" \
    docker.io/nixos/nix:latest \
    nix-shell -p bash nix-prefetch-url --run "
        set -euo pipefail

        echo \"⬇ Fetching into nix store...\"

        STORE_PATH=\$(nix-prefetch-url --unpack --type sha256 \"\$SRC_URL\" \"\$SRC_HASH\")

        echo \"📦 Source stored at: \$STORE_PATH\"

        if [[ ! -d \"\$STORE_PATH\" ]]; then
            echo \"❌ Expected unpacked directory in nix store\"
            exit 1
        fi

        cd \"\$STORE_PATH\"

        echo \"📂 Entered: \$(pwd)\"

        if [[ ! -f default.nix ]]; then
            echo \"❌ No default.nix found in source\"
            exit 1
        fi

        echo \"🔨 Running nix-build...\"
        nix-build default.nix --out-link result

        BIN_DIR=./result/bin
        if [[ ! -d \"\$BIN_DIR\" ]]; then
            echo \"ℹ No binaries produced.\"
            exit 0
        fi

        mapfile -t BINARIES < <(ls -1 \"\$BIN_DIR\")
        if (( \${#BINARIES[@]} == 0 )); then
            echo \"ℹ No binaries found in result/bin\"
            exit 0
        fi

        echo \"Available binaries:\"
        select CHOSEN_BIN in \"\${BINARIES[@]}\" \"Quit\"; do
            if [[ \"\$CHOSEN_BIN\" == \"Quit\" ]]; then
                echo \"Exiting.\"
                break
            elif [[ -n \"\$CHOSEN_BIN\" ]]; then
                echo \"▶ Running \$CHOSEN_BIN...\"
                \"\$BIN_DIR/\$CHOSEN_BIN\"
                break
            else
                echo \"Invalid selection, try again.\"
            fi
        done
    "

echo "✅ Done!"
echo "   • Persistent Nix store: $NIX_STORE_VOL"
echo "   • Persistent Nix state: $NIX_STATE_VOL"