#!/usr/bin/env bash # Launch Global (Gleam + Plushie). Scrubs non-latin-1 env and sets graphics libs. set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "$ROOT" # --- Erlang on PATH (Gleam dies with "Program not found: erl" without it) --- if ! command -v erl >/dev/null 2>&1; then if command -v nix >/dev/null 2>&1; then ERL_OUT="$( nix --extra-experimental-features 'nix-command flakes' build --no-link \ --print-out-paths --builders '' \ nixpkgs#beamPackages.erlang 2>/dev/null | head -1 )" || true if [[ -n "${ERL_OUT:-}" && -x "$ERL_OUT/bin/erl" ]]; then export PATH="$ERL_OUT/bin:$PATH" fi fi # Common Nix store fallbacks already present on this machine if ! command -v erl >/dev/null 2>&1; then for d in /nix/store/*-erlang-*/bin; do if [[ -x "$d/erl" ]]; then export PATH="$d:$PATH" break fi done fi fi if ! command -v erl >/dev/null 2>&1; then echo "error: erl not found. Gleam needs Erlang to run (target=erlang)." >&2 echo " nix shell nixpkgs#beamPackages.erlang nixpkgs#gleam -c gleam run" >&2 echo " # or: nix develop (from this flake)" >&2 exit 1 fi # --- renderer --- if [[ -z "${PLUSHIE_BINARY_PATH:-}" ]]; then if [[ -x "$ROOT/bin/plushie-renderer" ]]; then export PLUSHIE_BINARY_PATH="$ROOT/bin/plushie-renderer" elif [[ -x "$ROOT/result/lib/global/bin/plushie-renderer" ]]; then export PLUSHIE_BINARY_PATH="$ROOT/result/lib/global/bin/plushie-renderer" fi fi # --- packaged shortcut --- if [[ "${1:-}" == "--packaged" ]]; then shift if [[ ! -x "$ROOT/result/bin/global" ]]; then echo "No ./result/bin/global — run: nix build .#global --builders ''" >&2 exit 1 fi exec "$ROOT/result/bin/global" "$@" fi if [[ -z "${PLUSHIE_BINARY_PATH:-}" || ! -x "$PLUSHIE_BINARY_PATH" ]]; then echo "Fetching plushie-renderer..." "$ROOT/scripts/fetch-renderer" export PLUSHIE_BINARY_PATH="$ROOT/bin/plushie-renderer" fi # --- graphics libs (NixOS) --- if command -v nix >/dev/null 2>&1; then GFX="$( nix --extra-experimental-features 'nix-command flakes' eval --raw --impure --expr ' let pkgs = import {}; lib = pkgs.lib; in lib.makeLibraryPath (with pkgs; [ wayland libxkbcommon libGL vulkan-loader libx11 libxcursor libxi libxrandr libffi ]) ' 2>/dev/null || true )" if [[ -n "${GFX:-}" ]]; then export LD_LIBRARY_PATH="${GFX}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" fi fi # --- drop non-latin-1 env (nu prompts crash Plushie env FFI) --- while IFS= read -r k; do unset -v "$k" 2>/dev/null || true done < <(python3 -c ' import os for k, v in list(os.environ.items()): try: k.encode("latin-1"); v.encode("latin-1") except UnicodeEncodeError: if k.replace("_","").isalnum(): print(k) ') exec gleam run "$@"