Skip to content

Instantly share code, notes, and snippets.

@agucova
Last active December 12, 2025 20:27
Show Gist options
  • Select an option

  • Save agucova/c676716885807cd56a8d38a4c9368c3a to your computer and use it in GitHub Desktop.

Select an option

Save agucova/c676716885807cd56a8d38a4c9368c3a to your computer and use it in GitHub Desktop.
Minimal repro for functional test failures in Nix under macOS
#!/usr/bin/env bash
echo "=============================================="
echo "Minimal Reproduction: macOS Shebang Bug"
echo "=============================================="
echo ""
echo "Platform: $(uname -s) $(uname -m)"
echo "Nix version: $(nix --version)"
echo ""
# Test WITHOUT shebang
echo "=== Testing WITHOUT shebang (expect ~20% SIGSEGV) ==="
passed=0
failed=0
for i in $(seq 1 10); do
timestamp=$(date +%s%N)
cat > /tmp/no-shebang-test-$i.nix << EOF
{ pkgs ? import <nixpkgs> {} }:
let
# Script WITHOUT shebang - like tests/functional/shell.nix
fooNoShebang = pkgs.runCommand "foo-$timestamp" {} ''
mkdir -p \$out/bin
echo 'echo foo' > \$out/bin/foo
chmod a+rx \$out/bin/foo
'';
in pkgs.runCommand "test-$timestamp" {
nativeBuildInputs = [ pkgs.bash pkgs.coreutils fooNoShebang ];
} ''
set -e
mkdir -p \$out
export PATH="\${fooNoShebang}/bin:\$PATH"
# Run foo in subshell capture (like the actual tests do)
result=\$(foo)
if [ "\$result" != "foo" ]; then
exit 1
fi
echo "ok" > \$out/result
''
EOF
result=$(nix-build /tmp/no-shebang-test-$i.nix --no-out-link 2>&1)
exit_code=$?
if [ $exit_code -eq 0 ]; then
((passed++))
echo -n "."
else
((failed++))
if echo "$result" | grep -q "exit code 139"; then
echo -n "X" # SIGSEGV
else
echo -n "!" # Other error
fi
fi
rm -f /tmp/no-shebang-test-$i.nix
done
echo ""
echo "Results: $passed/10 passed, $failed/10 failed"
echo ""
# Test WITH shebang
echo "=== Testing WITH shebang (expect 100% success) ==="
passed=0
failed=0
for i in $(seq 1 10); do
timestamp=$(date +%s%N)
cat > /tmp/with-shebang-test-$i.nix << EOF
{ pkgs ? import <nixpkgs> {} }:
let
shell = "\${pkgs.bash}/bin/bash";
# Script WITH shebang - the fix
fooWithShebang = pkgs.runCommand "foo-fixed-$timestamp" {} ''
mkdir -p \$out/bin
echo '#!'\${shell} > \$out/bin/foo
echo 'echo foo' >> \$out/bin/foo
chmod a+rx \$out/bin/foo
'';
in pkgs.runCommand "test-fixed-$timestamp" {
nativeBuildInputs = [ pkgs.bash pkgs.coreutils fooWithShebang ];
} ''
set -e
mkdir -p \$out
export PATH="\${fooWithShebang}/bin:\$PATH"
result=\$(foo)
if [ "\$result" != "foo" ]; then
exit 1
fi
echo "ok" > \$out/result
''
EOF
result=$(nix-build /tmp/with-shebang-test-$i.nix --no-out-link 2>&1)
exit_code=$?
if [ $exit_code -eq 0 ]; then
((passed++))
echo -n "."
else
((failed++))
echo -n "X"
fi
rm -f /tmp/with-shebang-test-$i.nix
done
echo ""
echo "Results: $passed/10 passed, $failed/10 failed"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment