Skip to content

Instantly share code, notes, and snippets.

@penguincoder
Last active April 9, 2024 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save penguincoder/3e5e7aa98c9f436484cdfdb9892adefc to your computer and use it in GitHub Desktop.
Save penguincoder/3e5e7aa98c9f436484cdfdb9892adefc to your computer and use it in GitHub Desktop.
A shell script to bootstrap your machine with nix, direnv, and devenv.
#!/usr/bin/env bash
#
# Copyright (c) 2024 Andrew Coleman <penguincoder@gmail.com>
#
# 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
sh "$NIX_INSTALLER" install --no-confirm
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