Skip to content

Instantly share code, notes, and snippets.

@naranyala
Created August 6, 2025 04:22
Show Gist options
  • Save naranyala/1a952705e47a5798c1b9673cf390e9b0 to your computer and use it in GitHub Desktop.
Save naranyala/1a952705e47a5798c1b9673cf390e9b0 to your computer and use it in GitHub Desktop.
enable your non-nixos linux distro experience nix-shell, then visit: https://search.nixos.org/packages
#!/usr/bin/env bash
set -euo pipefail
# 🧾 Config
DEFAULT_NIX_FILE="$HOME/.default-nix-shell.nix"
SHELL_WRAPPER_FLAG="IN_NIX_SHELL"
BASHRC="$HOME/.bashrc"
ZSHRC="$HOME/.zshrc"
# πŸ“¦ Step 1: Install Nix if missing
if ! command -v nix-shell &>/dev/null; then
echo "πŸ”§ Installing Nix package manager..."
curl -L https://nixos.org/nix/install | sh
. "$HOME/.nix-profile/etc/profile.d/nix.sh"
fi
# πŸ§ͺ Step 2: Verify nix-shell
if ! command -v nix-shell &>/dev/null; then
echo "❌ nix-shell not found after install. Aborting."
exit 1
fi
# πŸ“ Step 3: Create default nix-shell environment
if [[ ! -f "$DEFAULT_NIX_FILE" ]]; then
cat > "$DEFAULT_NIX_FILE" <<'EOF'
with import <nixpkgs> {};
mkShell {
buildInputs = [ bash git coreutils ];
shellHook = ''
echo "βœ… Entered default nix-shell environment"
'';
}
EOF
echo "πŸ“ Created default nix-shell environment at $DEFAULT_NIX_FILE"
fi
# πŸ” Step 4: Inject wrapper into .bashrc / .zshrc
inject_wrapper() {
local rcfile="$1"
if [[ -f "$rcfile" ]] && ! grep -q "$SHELL_WRAPPER_FLAG" "$rcfile"; then
echo "πŸ” Injecting nix-shell wrapper into $rcfile"
cat >> "$rcfile" <<EOF
# πŸš€ Auto-launch nix-shell as default shell
if [[ -z "\$$SHELL_WRAPPER_FLAG" && -f "$DEFAULT_NIX_FILE" ]]; then
export $SHELL_WRAPPER_FLAG=1
exec nix-shell "$DEFAULT_NIX_FILE"
fi
EOF
fi
}
inject_wrapper "$BASHRC"
inject_wrapper "$ZSHRC"
# βœ… Step 5: Confirm
echo "πŸŽ‰ nix-shell is now set to launch by default in your shell sessions."
echo "You can customize the environment in: $DEFAULT_NIX_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment