Created
August 6, 2025 04:22
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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