Last active
September 9, 2024 14:33
-
-
Save penguincoder/3e5e7aa98c9f436484cdfdb9892adefc to your computer and use it in GitHub Desktop.
A shell script to bootstrap your machine with nix, direnv, and devenv.
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 | |
# | |
# Copyright (c) 2024 Andrew Coleman <[email protected]> | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# | |
# This is a script to bootstrap machine with nix, direnv, and devenv. | |
# | |
# Usage with curl: | |
# curl -s https://gist.githubusercontent.com/penguincoder/3e5e7aa98c9f436484cdfdb9892adefc/raw | bash | |
# | |
# Usage with wget: | |
# wget -qO- https://gist.githubusercontent.com/penguincoder/3e5e7aa98c9f436484cdfdb9892adefc/raw | bash | |
# | |
# Format: shfmt -w -i 4 bootstrap.sh | |
# Lint: shellcheck bootstrap.sh | |
# vim: tabstop=4 expandtab | |
# | |
set -u | |
# set DEBUG to any value to enable verbose script execution | |
[ -n "${DEBUG:-}" ] && set -x | |
NIX_INSTALLER=$(mktemp /tmp/nix-installer-XXXXXX) | |
trap 'rm -f "$NIX_INSTALLER"' EXIT | |
INSTALLER_URL=https://install.determinate.systems/nix | |
if command -v curl >/dev/null 2>&1; then | |
curl --proto '=https' --tlsv1.2 -sSf -o "$NIX_INSTALLER" -L "$INSTALLER_URL" | |
elif command -v wget >/dev/null 2>&1; then | |
wget -q -O "$NIX_INSTALLER" --https-only "$INSTALLER_URL" | |
else | |
echo "๐ curl and wget are missing! please install manually and try again. ๓ฐก " | |
echo | |
echo "Examples:" | |
echo " Debian / Ubuntu / Mint: sudo apt install -y curl" | |
echo " Fedora: sudo dnf install -y curl" | |
echo " Arch: sudo pacman -S curl" | |
echo " Mac: brew install curl" | |
exit 1 | |
fi | |
SHOULD_RESTART=0 | |
if ! sh "$NIX_INSTALLER" self-test; then | |
SHOULD_RESTART=1 | |
NIX_INSTALLER_BINARY_ROOT=https://install.determinate.systems/nix/tag/v0.23.0 | |
export NIX_INSTALLER_BINARY_ROOT | |
sh "$NIX_INSTALLER" install --no-confirm | |
unset NIX_INSTALLER_BINARY_ROOT | |
fi | |
if ! command -v nix >/dev/null 2>&1; then | |
# shellcheck disable=SC1091 | |
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh | |
# shellcheck disable=SC1091 | |
. /nix/var/nix/profiles/default/etc/profile.d/nix.sh | |
fi | |
info() { | |
nix run nixpkgs#gum -- log -t DateTime -sl info "$@" | |
} | |
warn() { | |
nix run nixpkgs#gum -- log -t DateTime -sl warn "$@" | |
} | |
error() { | |
nix run nixpkgs#gum -- log -t DateTime -sl error "$@" | |
} | |
if command -v git >/dev/null 2>&1; then | |
info "git is installed" | |
else | |
warn "git is warning, adding to nix profile" | |
nix profile install nixpkgs#git | |
fi | |
validate_direnv_config() { | |
if [ "$(basename "$SHELL")" = "zsh" ]; then | |
if grep 'direnv hook' "$HOME/.zshrc"; then | |
info "๐ direnv hook appears to be present in your .zshrc" | |
else | |
SHOULD_RESTART=1 | |
warn "๓ฐก direnv hook is not present; modifing your .zshrc" | |
# shellcheck disable=SC2016 | |
echo 'eval "$(direnv hook zsh)"' | tee -a "$HOME/.zshrc" | |
fi | |
elif [ "$(basename "$SHELL")" = "bash" ]; then | |
if grep 'direnv hook' "$HOME/.bashrc"; then | |
info "๐ direnv hook appears to be present in your .bashrc" | |
else | |
SHOULD_RESTART=1 | |
warn "๓ฐก direnv hook is not present; modifing your .bashrc" | |
# shellcheck disable=SC2016 | |
echo 'eval "$(direnv hook bash)"' | tee -a "$HOME/.bashrc" | |
fi | |
else | |
error "๐ This is an unsupported shell. ๓ฐก Install direnv hook manually. ๐ช" | |
error "Visit ๐ https://direnv.net ๐ for more documentation." | |
fi | |
} | |
if command -v direnv >/dev/null 2>&1; then | |
info "๐ direnv is installed" | |
else | |
warn "๓ฐก direnv is missing, adding to nix profile" | |
nix profile install nixpkgs#direnv | |
validate_direnv_config | |
fi | |
if command -v devenv >/dev/null 2>&1; then | |
info "๐ devenv is installed" | |
else | |
warn "๓ฐก devenv is missing, adding to nix profile" | |
nix profile install --accept-flake-config nixpkgs#devenv | |
fi | |
if [ "$SHOULD_RESTART" = "1" ]; then | |
error "๐ Your shell configuration was changed, you must restart your shell ๐" | |
error "๐ฎ ๐จ DO NOT SKIP THIS STEP!" | |
exit 0 | |
fi | |
info "๐ ๐ฅณ Your shell environment has been validated and is ready to use nix for local environments ๐ค " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment