Skip to content

Commit c689377

Browse files
authored
Merge pull request #3 from procrastivity/chore/orb-setup
fix: preserve Nix environment after orb activation
2 parents f20cd1c + 2ae1220 commit c689377

2 files changed

Lines changed: 45 additions & 10 deletions

File tree

‎.agents/resume‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,34 @@
22
set -euo pipefail
33

44
# Setup persists the complete flake environment in ~/.bash_profile. Resume is
5-
# intentionally a fast, non-installing health check; the login shell restores
6-
# the toolchain before Amp or a supervised service starts.
5+
# intentionally a fast, non-installing repair and health check. Amp writes the
6+
# current thread environment after setup; keep the Nix hook after that block so
7+
# its PATH value cannot replace the dev shell's PATH.
8+
profile="$HOME/.bash_profile"
9+
activation="$HOME/.config/docker-extras/orb-dev-shell.sh"
10+
marker='# docker-extras Nix development shell'
11+
if [ ! -f "$activation" ]; then
12+
exit 0
13+
fi
14+
15+
touch "$profile"
16+
temporary="$(mktemp)"
17+
awk -v marker="$marker" '
18+
$0 == marker { skipping = 1; next }
19+
skipping && $0 == "" { skipping = 0; next }
20+
!skipping { print }
21+
' "$profile" >"$temporary"
22+
cat "$temporary" >"$profile"
23+
rm -f "$temporary"
24+
printf '\n%s\nsource %q\n\n' "$marker" "$activation" >>"$profile"
25+
726
nix_bin="$(command -v nix || true)"
827
if [ -z "$nix_bin" ] && [ -x /nix/var/nix/profiles/default/bin/nix ]; then
928
nix_bin=/nix/var/nix/profiles/default/bin/nix
1029
fi
30+
if [ -z "$nix_bin" ] && [ -x "$HOME/.nix-profile/bin/nix" ]; then
31+
nix_bin="$HOME/.nix-profile/bin/nix"
32+
fi
1133
if [ -n "$nix_bin" ]; then
1234
"$nix_bin" --version >/dev/null
1335
fi

‎.agents/setup‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,20 @@ allow_direnv() {
126126
}
127127

128128
persist_nix_environment() {
129-
local nix_bin nix_bin_dir profile="$HOME/.bash_profile" nix_profile
129+
local activation_dir="$HOME/.config/docker-extras"
130+
local activation="$activation_dir/orb-dev-shell.sh"
131+
local marker='# docker-extras Nix development shell'
132+
local nix_bin nix_bin_dir profile="$HOME/.bash_profile" nix_profile temporary
130133
nix_bin="$(command -v nix)"
131134
nix_bin_dir="$(dirname "$nix_bin")"
132135
nix_profile=/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
133136
if [ ! -f "$nix_profile" ]; then
134137
nix_profile="$HOME/.nix-profile/etc/profile.d/nix.sh"
135138
fi
136-
local marker='# docker-extras Nix development shell'
137-
if grep -Fqx "$marker" "$profile" 2>/dev/null; then
138-
return
139-
fi
140-
141-
cat >>"$profile" <<EOF
142139

143-
$marker
140+
mkdir -p "$activation_dir"
141+
cat >"$activation" <<EOF
142+
# Generated by docker-extras .agents/setup.
144143
if [ -f "$nix_profile" ]; then
145144
# shellcheck disable=SC1090
146145
source "$nix_profile"
@@ -150,6 +149,20 @@ if [[ -z "\${IN_NIX_SHELL:-}" && "\${PWD}" == "$repo_root" ]]; then
150149
export PATH="$nix_bin_dir:\${PATH}"
151150
fi
152151
EOF
152+
153+
# Amp applies the thread environment after setup and sources it from the
154+
# end of .bash_profile. Remove any previous copy, then append this managed
155+
# source block so the dev shell wins over that environment.
156+
touch "$profile"
157+
temporary="$(mktemp)"
158+
awk -v marker="$marker" '
159+
$0 == marker { skipping = 1; next }
160+
skipping && $0 == "" { skipping = 0; next }
161+
!skipping { print }
162+
' "$profile" >"$temporary"
163+
cat "$temporary" >"$profile"
164+
rm -f "$temporary"
165+
printf '\n%s\nsource %q\n\n' "$marker" "$activation" >>"$profile"
153166
}
154167

155168
verify_dev_shell() {

0 commit comments

Comments
 (0)