Skip to content

Commit 381df68

Browse files
committed
Revert "add nix chroot fallback"
This reverts commit d7cea4b.
1 parent ae5c87d commit 381df68

1 file changed

Lines changed: 14 additions & 87 deletions

File tree

scripts/run-in-nix.sh

Lines changed: 14 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,32 @@
11
#!/usr/bin/env bash
22
#
3-
# Run a command inside the Nix development environment.
4-
# Fallback order:
5-
# 1) host nix
6-
# 2) docker (nixos/nix)
7-
# 3) rootless nix-user-chroot bootstrap (requires user namespaces + cargo)
3+
# Run a command inside the Nix development environment with a Docker fallback.
84

95
set -euo pipefail
106

117
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
128
export PYTHONPATH="${PYTHONPATH:+$PYTHONPATH:}${ROOT_DIR}"
139

14-
log() { echo "[$(basename "$0")] $*" >&2; }
15-
die() { log "ERROR: $*"; exit 1; }
16-
1710
# Handle standard SHELL interface: -c "command"
18-
if [[ "${1:-}" == "-c" ]]; then
11+
if [ "$1" = "-c" ]; then
1912
shift
2013
set -- bash -c "$@"
2114
fi
2215

23-
# If we're already inside a nix shell/dev env, just run.
24-
if [[ -n "${IN_NIX_SHELL:-}" ]]; then
16+
if [ -n "${IN_NIX_SHELL:-}" ]; then
2517
exec "$@"
2618
fi
2719

28-
NIX_FLAKE="${NIX_FLAKE:-$ROOT_DIR#default}"
29-
NIX_EXPERIMENTAL="${NIX_EXPERIMENTAL:-nix-command flakes}"
30-
31-
run_with_host_nix() {
32-
exec nix develop --experimental-features "$NIX_EXPERIMENTAL" \
33-
"$NIX_FLAKE" --command "$ROOT_DIR/scripts/run-in-nix.sh" "$@"
34-
}
35-
36-
run_with_docker() {
37-
exec docker run --rm -v "$ROOT_DIR:/app" -w /app nixos/nix \
38-
nix develop --experimental-features "$NIX_EXPERIMENTAL" \
39-
".#default" --command /app/scripts/run-in-nix.sh "$@"
40-
}
41-
42-
run_with_user_chroot() {
43-
# 0) Require user namespaces
44-
if ! unshare --user --pid bash -lc 'echo YES' >/dev/null 2>&1; then
45-
die "user namespaces not available (unshare --user --pid failed); cannot use nix-user-chroot fallback"
46-
fi
47-
48-
# 1) Require cargo to build nix-user-chroot
49-
command -v cargo >/dev/null 2>&1 || die "cargo not found; cannot build nix-user-chroot fallback"
50-
51-
# 2) Workspace-local installs and state (cache these paths in CI if you can)
52-
CACHE_DIR="${NIX_CHROOT_CACHE_DIR:-$ROOT_DIR/.cache}"
53-
CARGO_ROOT="${NIX_CHROOT_CARGO_ROOT:-$CACHE_DIR/cargo}"
54-
CHROOT_ROOT="${NIX_CHROOT_ROOT:-$CACHE_DIR/nix-chroot}"
55-
CHROOT_HOME="${NIX_CHROOT_HOME:-$CACHE_DIR/nix-home}"
56-
57-
mkdir -p "$CARGO_ROOT" "$CHROOT_ROOT" "$CHROOT_HOME"
58-
59-
# Nix installer needs xz to unpack the tarball on most distros
60-
command -v xz >/dev/null 2>&1 || die "xz not found (install xz-utils/xz); nix installer will fail without it"
61-
62-
# 3) Ensure nix-user-chroot exists (install locally via cargo)
63-
export PATH="$CARGO_ROOT/bin:$PATH"
64-
if ! command -v nix-user-chroot >/dev/null 2>&1; then
65-
local nuc_ver="${NIX_USER_CHROOT_VERSION:-1.2.2}"
66-
log "building nix-user-chroot (cargo install) into $CARGO_ROOT ..."
67-
cargo install --root "$CARGO_ROOT" "nix-user-chroot@${nuc_ver}"
68-
fi
69-
70-
# 4) Install Nix inside the chroot if missing
71-
local nix_bin_host_path="$CHROOT_ROOT/nix/var/nix/profiles/default/bin/nix"
72-
if [[ ! -x "$nix_bin_host_path" ]]; then
73-
log "bootstrapping nix inside user chroot at $CHROOT_ROOT ..."
74-
nix-user-chroot "$CHROOT_ROOT" bash -lc '
75-
set -euo pipefail
76-
export HOME="'"$CHROOT_HOME"'"
77-
mkdir -p "$HOME"
78-
curl -L https://nixos.org/nix/install | sh -s -- --no-daemon --yes --no-modify-profile --no-channel-add
79-
'
80-
fi
81-
82-
# 5) Run nix develop inside the chroot
83-
# nix-user-chroot uses /nix/etc/nix (not /etc/nix); set NIX_CONF_DIR explicitly.
84-
exec nix-user-chroot "$CHROOT_ROOT" bash -lc '
85-
set -euo pipefail
86-
export HOME="'"$CHROOT_HOME"'"
87-
export NIX_CONF_DIR=/nix/etc/nix
88-
# Ensure nix is on PATH for this non-interactive shell
89-
. /nix/etc/profile.d/nix.sh
90-
exec nix develop --experimental-features "'"$NIX_EXPERIMENTAL"'" "'"$NIX_FLAKE"'" --command "$1" "${@:2}"
91-
' run-in-chroot "$ROOT_DIR/scripts/run-in-nix.sh" "$@"
92-
}
93-
9420
if command -v nix >/dev/null 2>&1; then
95-
run_with_host_nix "$@"
96-
fi
97-
98-
log "Nix is not available."
99-
if command -v docker >/dev/null 2>&1; then
100-
log "Attempting Docker fallback (nixos/nix)."
101-
run_with_docker "$@"
21+
exec nix develop --experimental-features 'nix-command flakes' "$ROOT_DIR#default" --command "$ROOT_DIR/scripts/run-in-nix.sh" "$@" 2>/dev/null
22+
else
23+
echo "Nix is not available. Attempting to use Docker with Nix image."
24+
if command -v docker >/dev/null 2>&1; then
25+
echo "Docker is available, running script inside a Nix container."
26+
exec docker run --rm -v "$ROOT_DIR:/app" -w /app nixos/nix \
27+
nix develop --experimental-features 'nix-command flakes' .#default --command /app/scripts/run-in-nix.sh "$@"
28+
else
29+
echo "Docker is not available. Please install either Nix or Docker to proceed."
30+
exit 1
31+
fi
10232
fi
103-
104-
log "Docker is not available; attempting nix-user-chroot bootstrap fallback."
105-
run_with_user_chroot "$@"

0 commit comments

Comments
 (0)