/ bootstrap.sh
bootstrap.sh
1 #!/usr/bin/env bash 2 3 notify() { 4 echo -e "\033[0;34m[i]\033[0m $1" >&2 5 } 6 7 error() { 8 echo -e "\033[0;31m[x]\033[0m $1" >&2 9 exit 1 10 } 11 12 acquire_sudo() { 13 if [[ $UID == 0 ]]; then 14 notify "Running as root" 15 SUDO="" 16 elif $(sudo -v); then 17 notify "Got sudo permissions" 18 SUDO="sudo" 19 else 20 error "Failed to get sudo permissions" 21 fi 22 } 23 24 prompt() { 25 echo -en "\033[0;33m[?]\033[0m $1 \033[0;33m(y/N)\033[0m " 26 read -r INPUT 27 28 [[ "$INPUT" =~ ^[Yy]$ ]] && return 0 || return 1 29 } 30 31 # Variables 32 OS=$(uname -s) 33 DOTDIR="$HOME/_/" 34 DOTDIR_REPO="https://github.com/0x616e6f73/_" 35 36 NIX_SCRIPT="https://install.determinate.systems/nix" 37 BREW_SCRIPT="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh" 38 39 # Requirements 40 command -v git &>/dev/null || error "'git' is not installed" 41 command -v curl &>/dev/null || error "'curl' is not installed" 42 acquire_sudo 43 44 # Install Homebrew if not already installed 45 if ! command -v /opt/homebrew/bin/brew &>/dev/null; then 46 notify "Installing Homebrew" 47 48 curl -fsSL "$BREW_SCRIPT" | NONINTERACTIVE=1 bash 49 eval $(/opt/homebrew/bin/brew shellenv) 50 else 51 notify "Homebrew is already installed" 52 fi 53 54 # Install Rosetta if not already installed 55 if ! command -v pkgutil --pkg-info com.apple.pkg.RosettaUpdateAuto &>/dev/null; then 56 notify "Installing Rosetta" 57 softwareupdate --install-rosetta --agree-to-license 58 else 59 notify "Rosetta is already installed" 60 fi 61 62 # Install Nix if not already installed (using Determinate Systems installer) 63 if ! launchctl print system/org.nixos.nix-daemon &>/dev/null; then 64 notify "Installing Nix (Determinate Systems)" 65 curl -fsSL "$NIX_SCRIPT" | sh -s -- install --determinate 66 source /etc/bashrc 67 else 68 notify "Nix is already installed" 69 source /etc/static/bashrc 70 fi 71 72 if [[ ! -d "$DOTDIR" ]]; then 73 notify "Cloning dotfiles from $DOTDIR_REPO" 74 git clone "$DOTDIR_REPO" "$DOTDIR" 75 else 76 notify "Dotfiles are already cloned" 77 fi 78 79 # Build and configure nix-darwin 80 if ! command -v darwin-rebuild &>/dev/null; then 81 notify "Installing nix-darwin" 82 83 # flake.nix expects "net-2" as the hostname 84 $SUDO scutil --set LocalHostName net-2 85 dscacheutil -flushcache 86 87 # After nix-darwin is installed and configured it's in the PATH 88 command nix build .#darwinConfigurations.$(hostname -s).system \ 89 --extra-experimental-features "nix-command flakes" 90 91 # We need to link run to private/var/run for nix-darwin 92 if ! grep -q "private/var/run" /etc/synthetic.conf; then 93 notify "Appending nix-darwin path to /etc/synthetic.conf" 94 95 printf "run\tprivate/var/run\n" | $SUDO tee -a /etc/synthetic.conf 96 /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -t 97 else 98 notify "nix-darwin path already exists in /etc/synthetic.conf" 99 fi 100 101 # Run nix-darwin's rebuild command 102 $DOTDIR/result/sw/bin/darwin-rebuild switch --flake $DOTDIR 103 else 104 notify "nix-darwin is already installed" 105 fi