|
2 | 2 | # Sourced by Mux before every bash tool invocation. |
3 | 3 | # Docs: https://mux.coder.com/hooks/tools |
4 | 4 |
|
| 5 | +mux_use_nix_devshell_path() { |
| 6 | + # Keep cache file workspace-specific to avoid collisions between parallel workspaces. |
| 7 | + local workspace_cache_tag="${MUX_WORKSPACE_ID:-${MUX_WORKSPACE_NAME:-${PWD}}}" |
| 8 | + workspace_cache_tag="$(printf '%s' "${workspace_cache_tag}" | tr -cs 'A-Za-z0-9._-' '_')" |
| 9 | + |
| 10 | + local cache_file="/tmp/mux-nix-path-${workspace_cache_tag}" |
| 11 | + |
| 12 | + # Only attempt activation for flake-based projects with nix installed. |
| 13 | + if [[ ! -f "flake.nix" ]]; then |
| 14 | + return 0 |
| 15 | + fi |
| 16 | + if ! command -v nix >/dev/null 2>&1; then |
| 17 | + return 0 |
| 18 | + fi |
| 19 | + |
| 20 | + local should_refresh=0 |
| 21 | + if [[ ! -s "${cache_file}" ]]; then |
| 22 | + should_refresh=1 |
| 23 | + elif [[ "flake.nix" -nt "${cache_file}" ]]; then |
| 24 | + should_refresh=1 |
| 25 | + elif [[ -f "flake.lock" && "flake.lock" -nt "${cache_file}" ]]; then |
| 26 | + should_refresh=1 |
| 27 | + fi |
| 28 | + |
| 29 | + if [[ "${should_refresh}" -eq 1 ]]; then |
| 30 | + local tmp_cache_file |
| 31 | + tmp_cache_file="$(mktemp "${cache_file}.tmp.XXXXXX")" |
| 32 | + |
| 33 | + if nix develop -c bash -c 'echo "$PATH"' >"${tmp_cache_file}" 2>/dev/null; then |
| 34 | + if [[ ! -s "${tmp_cache_file}" ]]; then |
| 35 | + rm -f "${tmp_cache_file}" |
| 36 | + echo "assertion failed: nix develop returned an empty PATH" >&2 |
| 37 | + return 96 |
| 38 | + fi |
| 39 | + mv "${tmp_cache_file}" "${cache_file}" |
| 40 | + else |
| 41 | + rm -f "${tmp_cache_file}" |
| 42 | + if [[ ! -s "${cache_file}" ]]; then |
| 43 | + echo "assertion failed: failed to populate nix devshell PATH cache: ${cache_file}" >&2 |
| 44 | + return 96 |
| 45 | + fi |
| 46 | + fi |
| 47 | + fi |
| 48 | + |
| 49 | + if [[ -s "${cache_file}" ]]; then |
| 50 | + export PATH="$(<"${cache_file}")" |
| 51 | + return 0 |
| 52 | + fi |
| 53 | + |
| 54 | + echo "assertion failed: nix devshell PATH cache file missing or empty: ${cache_file}" >&2 |
| 55 | + return 96 |
| 56 | +} |
| 57 | + |
| 58 | +mux_use_nix_devshell_path |
| 59 | + |
5 | 60 | run_and_report() { |
6 | 61 | local step_name="${1:-}" |
7 | 62 | if [[ -z "${step_name}" ]]; then |
|
0 commit comments