Skip to content

Instantly share code, notes, and snippets.

@jason9075
Created August 12, 2025 04:31
Show Gist options
  • Select an option

  • Save jason9075/d2be0686322fed6501cf541b38cc4cbd to your computer and use it in GitHub Desktop.

Select an option

Save jason9075/d2be0686322fed6501cf541b38cc4cbd to your computer and use it in GitHub Desktop.
Test for steam-run in NixOS
#!/usr/bin/env bash
set -euo pipefail
# ===== 可調參數 ===============================================================
: "${HELLO_VER:=2.10-3}"
: "${DEB_URL_I386:=https://ftp.debian.org/debian/pool/main/h/hello/hello_${HELLO_VER}_i386.deb}"
DEMO_DIR="${HOME}/.cache/steamrun-demo-hello32"
BIN_NAME="hello32"
# ==============================================================================
need() { command -v "$1" >/dev/null 2>&1 || { echo "[!] 缺少指令:$1" >&2; exit 127; }; }
need curl; need ar; need tar; need file
mkdir -p "$DEMO_DIR"
cd "$DEMO_DIR"
echo "[i] 下載 i386 hello:$DEB_URL_I386"
curl -fsSL "$DEB_URL_I386" -o hello_i386.deb
echo "[i] 解包 .deb"
rm -rf extract && mkdir -p extract
# .deb 是 ar 檔,裡面會有 control.tar.* 與 data.tar.*
ar x hello_i386.deb --output=extract
DATA_TAR="$(ls extract/data.tar.* | head -n1 || true)"
[[ -n "$DATA_TAR" ]] || { echo "[!] 找不到 data.tar.*" >&2; exit 2; }
case "$DATA_TAR" in
*.xz) tar -C extract -xf "$DATA_TAR" ;;
*.zst) tar -C extract --zstd -xf "$DATA_TAR" ;;
*.gz) tar -C extract -xzf "$DATA_TAR" ;;
*) echo "[!] 不支援的 data.tar 格式:$DATA_TAR" >&2; exit 2 ;;
esac
# 目標可執行檔
cp -f extract/usr/bin/hello "./${BIN_NAME}"
chmod +x "./${BIN_NAME}"
echo "------------------------------------------------------------"
echo "[i] 目標:$DEMO_DIR/${BIN_NAME}"
file "./${BIN_NAME}" || true
if command -v readelf >/dev/null 2>&1; then
readelf -l "./${BIN_NAME}" | sed -n '/Requesting program interpreter/p' || true
fi
if command -v patchelf >/dev/null 2>&1; then
echo "[i] interpreter (patchelf):$(patchelf --print-interpreter "./${BIN_NAME}" 2>/dev/null || true)"
fi
echo "------------------------------------------------------------"
echo "[1] 直接在 NixOS 原生環境執行:"
set +e
"./${BIN_NAME}"
RC_NATIVE=$?
set -e
echo " -> 結束碼:$RC_NATIVE"
echo
echo "[2] 用 steam-run 執行(自帶 32-bit 相容層):"
# 有 steam-run 就直接用;沒有就臨時 nix shell 拉起來
if command -v steam-run >/dev/null 2>&1; then
set +e
steam-run -- "./${BIN_NAME}"
RC_STEAM=$?
set -e
else
echo " (steam-run 不在 PATH,用 nix shell 臨時啟動)"
set +e
nix shell nixpkgs#steam-run -c steam-run -- "./${BIN_NAME}"
RC_STEAM=$?
set -e
fi
echo " -> 結束碼:$RC_STEAM"
echo "------------------------------------------------------------"
if [[ $RC_NATIVE -ne 0 && $RC_STEAM -eq 0 ]]; then
echo "[✓] Demo 成功:原生失敗(多半缺 /lib/ld-linux.so.2),steam-run 成功。"
elif [[ $RC_NATIVE -eq 0 && $RC_STEAM -eq 0 ]]; then
echo "[i] 兩邊都成功:你的系統已提供 32-bit 相容層(可能啟用 nix-ld)。"
echo " 若要重現失敗 → 暫時關閉 nix-ld 或在純淨環境再測。"
else
echo "[!] 仍失敗:請檢查"
echo " - 檔案是否在 \$HOME(steam-run sandbox 才看得到)"
echo " - 網路/封包遭阻擋、或 steam-run 缺依賴"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment