Skip to content

Instantly share code, notes, and snippets.

@puemos
Last active October 26, 2025 10:33
Show Gist options
  • Save puemos/cca422fd0a87ea5320d709bad8e8c25d to your computer and use it in GitHub Desktop.
Save puemos/cca422fd0a87ea5320d709bad8e8c25d to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
export POSTGRES_USER=postgres
export POSTGRES_PASSWORD=postgres
export POSTGRES_DB=app_dev
# PATH setup
export PATH=$HOME/.local/bin:$HOME/.local/share/mise/shims:$HOME/.cache/rebar3/bin:$PATH
export HEX_CACERTS_PATH=$CODEX_PROXY_CERT
cat >> ~/.bashrc << 'EOF'
export PATH=$HOME/.local/bin:$HOME/.local/share/mise/shims:$HOME/.cache/rebar3/bin:$PATH
export HEX_CACERTS_PATH=$CODEX_PROXY_CERT
EOF
echo "Starting parallel setup..."
# =============================================================================
# PARALLEL TRACK 1: PostgreSQL (completely independent)
# =============================================================================
(
echo "[PG] Installing PostgreSQL..."
apt-get update && \
apt-get install -y --no-install-recommends postgresql=16+* postgresql-contrib=16+* && \
rm -rf /var/lib/apt/lists/*
echo "[PG] Starting PostgreSQL..."
service postgresql start
timeout 10 bash -c 'until pg_isready -q 2>/dev/null; do sleep 0.5; done'
echo "[PG] Setting up database..."
su postgres -c "psql -v ON_ERROR_STOP=1" << EOFPG
DO \$\$
BEGIN
IF NOT EXISTS (SELECT FROM pg_user WHERE usename = '${POSTGRES_USER}') THEN
CREATE USER "${POSTGRES_USER}" WITH SUPERUSER PASSWORD '${POSTGRES_PASSWORD}';
ELSE
ALTER USER "${POSTGRES_USER}" WITH PASSWORD '${POSTGRES_PASSWORD}';
END IF;
END \$\$;
SELECT 'CREATE DATABASE "${POSTGRES_DB}" OWNER "${POSTGRES_USER}"'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${POSTGRES_DB}')\gexec
EOFPG
echo "[PG] PostgreSQL setup complete"
) &
PG_PID=$!
# =============================================================================
# PARALLEL TRACK 2: Mise plugins (needed before install)
# =============================================================================
(
echo "[MISE] Installing mise plugins..."
mise plugin add erlang https://github.com/michallepicki/asdf-erlang-prebuilt-ubuntu-24.04.git 2>/dev/null || true
mise plugin add elixir https://github.com/mise-plugins/mise-elixir.git 2>/dev/null || true
echo "[MISE] Plugins ready"
) &
MISE_PLUGIN_PID=$!
# =============================================================================
# PARALLEL TRACK 3: Pre-download rebar3 (independent of everything)
# =============================================================================
(
echo "[REBAR3] Downloading rebar3..."
REBAR3_PATH="$HOME/.cache/rebar3/bin"
mkdir -p "$REBAR3_PATH"
curl -sL -o "$REBAR3_PATH/rebar3" https://s3.amazonaws.com/rebar3/rebar3
chmod +x "$REBAR3_PATH/rebar3"
echo "[REBAR3] Download complete"
) &
REBAR3_PID=$!
# Wait for mise plugins before installing tools
wait $MISE_PLUGIN_PID
# =============================================================================
# SEQUENTIAL: Erlang/Elixir installation (can't parallelize)
# =============================================================================
echo "[MISE] Installing Erlang and Elixir..."
mise install [email protected] [email protected]
mise use --global [email protected] [email protected]
# =============================================================================
# PARALLEL: Hex installation while we finish rebar3
# =============================================================================
(
echo "[HEX] Installing hex fork..."
mix archive.install --force github 650health/hex branch latest || exit 1
echo "[HEX] Hex installation complete"
) &
HEX_PID=$!
# Wait for rebar3 download and configure it
wait $REBAR3_PID
echo "[REBAR3] Configuring rebar3..."
mix local.rebar rebar3 "$HOME/.cache/rebar3/bin/rebar3"
# Wait for hex before proceeding to deps
wait $HEX_PID
# =============================================================================
# CRITICAL PATH: Mix deps and compile (OPTIMIZED FOR SPEED)
# =============================================================================
# Detect CPU cores and set optimal compilation settings
CPU_CORES=$(nproc)
PARTITION_COUNT=$((CPU_CORES / 2))
# Ensure minimum of 2, maximum of CPU_CORES
PARTITION_COUNT=$(( PARTITION_COUNT < 2 ? 2 : PARTITION_COUNT ))
PARTITION_COUNT=$(( PARTITION_COUNT > CPU_CORES ? CPU_CORES : PARTITION_COUNT ))
echo "=========================================="
echo "CPU Optimization Settings:"
echo " - CPU Cores: ${CPU_CORES}"
echo " - Parallel Partitions: ${PARTITION_COUNT}"
echo "=========================================="
# Set all optimization environment variables
export MIX_OS_DEPS_COMPILE_PARTITION_COUNT=$PARTITION_COUNT # Parallel deps compilation (Elixir 1.17+)
export ERL_AFLAGS="+P 1048576" # Increase max Erlang processes (default 1M)
export MAKEFLAGS="-j${CPU_CORES}" # Parallel make for native deps
export MIX_ENV=dev
export MIX_BUILD_EMBEDDED=false
echo "Starting mix deps.get..."
time mix deps.get
echo "=========================================="
echo "Starting parallel compilation..."
echo "This is optimized and should be 2-4x faster"
echo "=========================================="
time mix do deps.compile, compile
# Don't block on PostgreSQL - it's independent
wait $PG_PID 2>/dev/null || echo "[PG] PostgreSQL finished earlier"
echo "=========================================="
echo "✅ Setup complete!"
echo "=========================================="
@puemos
Copy link
Author

puemos commented Aug 15, 2025

Pay attention to change POSTGRES_DB="app_dev"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment