Skip to content

Commit efdf548

Browse files
authored
🤖 ci: use nix devshell PATH for mux bash hooks (#34)
## Summary Enable Mux bash tool invocations to use the project's Nix dev shell toolchain by caching and exporting a devshell PATH in `.mux/tool_env`. ## Background This workspace had an outdated system `kind` binary (`v0.10.0`) on PATH, which defaulted to old Kubernetes node images (`kindest/node:v1.20.2`). We want Mux command execution to consistently use flake-provided tools instead. ## Implementation - Added `mux_use_nix_devshell_path()` to `.mux/tool_env`. - Uses a workspace-scoped cache file (`/tmp/mux-nix-path-<workspace-tag>`) to avoid collisions between concurrent workspaces. - Refreshes the cache when missing/empty, or when `flake.nix` / `flake.lock` is newer than the cache file. - Uses atomic cache writes (`mktemp` + `mv`) and fail-fast assertions when a usable cache cannot be produced. - Calls the function before defining/running existing helper functions so every Mux `bash` tool call picks up the devshell PATH. ## Validation - `make verify-vendor` - `make test` - `make build` - `make lint` - `bash -n .mux/tool_env` - `kind version` (from Mux bash shell, now resolves to Nix `kind v0.31.0`) - `kubectl version --client` (from Mux bash shell, now resolves to Nix `v1.35.0`) ## Risks Low risk. Change is scoped to Mux bash hook environment setup and does not modify application runtime logic. Main behavioral change is PATH precedence in Mux-invoked bash commands; this is intentional and constrained to workspace shell execution. --- _Generated with [`mux`](https://github.com/coder/mux) • Model: `openai:gpt-5.3-codex` • Thinking: `xhigh` • Cost: `$1.22`_ <!-- mux-attribution: model=openai:gpt-5.3-codex thinking=xhigh costs=1.22 -->
1 parent ce23fa8 commit efdf548

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

‎.mux/tool_env‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,80 @@
22
# Sourced by Mux before every bash tool invocation.
33
# Docs: https://mux.coder.com/hooks/tools
44

5+
mux_use_nix_devshell_path() {
6+
local flake_root=""
7+
8+
# Resolve the flake root from stable workspace signals before considering current cwd.
9+
if [[ -n "${MUX_PROJECT_PATH:-}" && -f "${MUX_PROJECT_PATH}/flake.nix" ]]; then
10+
flake_root="${MUX_PROJECT_PATH}"
11+
elif [[ -f "flake.nix" ]]; then
12+
flake_root="$(pwd)"
13+
else
14+
local git_root=""
15+
git_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
16+
if [[ -n "${git_root}" && -f "${git_root}/flake.nix" ]]; then
17+
flake_root="${git_root}"
18+
else
19+
return 0
20+
fi
21+
fi
22+
23+
if ! command -v nix >/dev/null 2>&1; then
24+
return 0
25+
fi
26+
27+
# Keep cache file workspace-specific to avoid collisions between parallel workspaces.
28+
local workspace_cache_tag="${MUX_WORKSPACE_ID:-${MUX_WORKSPACE_NAME:-${flake_root}}}"
29+
workspace_cache_tag="$(printf '%s' "${workspace_cache_tag}" | tr -cs 'A-Za-z0-9._-' '_')"
30+
31+
local cache_file="/tmp/mux-nix-path-${workspace_cache_tag}"
32+
33+
local should_refresh=0
34+
if [[ ! -s "${cache_file}" ]]; then
35+
should_refresh=1
36+
elif [[ "${flake_root}/flake.nix" -nt "${cache_file}" ]]; then
37+
should_refresh=1
38+
elif [[ -f "${flake_root}/flake.lock" && "${flake_root}/flake.lock" -nt "${cache_file}" ]]; then
39+
should_refresh=1
40+
fi
41+
42+
if [[ "${should_refresh}" -eq 1 ]]; then
43+
local tmp_cache_file
44+
tmp_cache_file="$(mktemp "${cache_file}.tmp.XXXXXX")"
45+
46+
if nix develop "${flake_root}" -c bash -c 'echo "$PATH"' >"${tmp_cache_file}" 2>/dev/null; then
47+
if [[ ! -s "${tmp_cache_file}" ]]; then
48+
rm -f "${tmp_cache_file}"
49+
echo "assertion failed: nix develop returned an empty PATH" >&2
50+
return 96
51+
fi
52+
mv "${tmp_cache_file}" "${cache_file}"
53+
else
54+
rm -f "${tmp_cache_file}"
55+
echo "assertion failed: failed to refresh nix devshell PATH cache: ${cache_file}" >&2
56+
return 96
57+
fi
58+
fi
59+
60+
if [[ -s "${cache_file}" ]]; then
61+
export PATH="$(<"${cache_file}")"
62+
return 0
63+
fi
64+
65+
echo "assertion failed: nix devshell PATH cache file missing or empty: ${cache_file}" >&2
66+
return 96
67+
}
68+
69+
if mux_use_nix_devshell_path; then
70+
:
71+
else
72+
status=$?
73+
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
74+
return "${status}"
75+
fi
76+
exit "${status}"
77+
fi
78+
579
run_and_report() {
680
local step_name="${1:-}"
781
if [[ -z "${step_name}" ]]; then

0 commit comments

Comments
 (0)