#!/usr/bin/env bash # # Applies flake.nix to this Mac: installs Homebrew and Nix, switches to its configuration, and verifies the result. # # curl -fsSL https://dotfiles.creasty.com/apply | bash # on a new Mac, cloning this repository to ~/dotfiles # ./apply [darwin-rebuild options] # from that clone, to apply changes set -euo pipefail : "${DOTFILES_NOVERIFY:=0}" DOTFILES_PATH="$HOME/dotfiles" NIX=/nix/var/nix/profiles/default/bin/nix print_header() { printf "\e[34m" cat << 'EOF' -------------------------------------------------------------------------------- .' -' -' '-- -._ \ _. _ ''' '''''- \ _' -- _ ___ '-_ \ / / _ ' ''' ._ ' \ / ' _ . '- -_ | | / ╷ ╷ ╭──┐ ╷ | | / / . ╭───┤ ╭───╮ ─┼───┼──┐ │ ╭───╮ ╭───╮ | | | / / '- │ │ │ │ │ │ │ │ ├───╯ ╰───╮ | | | | / • ╰───╯ ╰───╯ ╰─ ╵ ╵ ╵ ╰───╯ ╰───╯ | |\ | | | \ Stellar productivity by @creasty | |\ \ \ ' _ / / | ____ _.- / / __-'' ''''-_. ___ '_____ _..-'''''''''''-....----''''''-. | ----''' '' '''--'''' '' '. '- -------------------------------------------------------------------------------- EOF printf "\e[0m" } # flake.nix configures only Apple silicon; a shell under Rosetta would have Homebrew install for Intel check_os() { if [ "$(uname -s)" != Darwin ] || [ "$(uname -m)" != arm64 ]; then echo 'Sorry, this script is intended only for macOS on Apple silicon, in a shell not under Rosetta' exit 1 fi } # nix-darwin installs what Homebrew provides, but not Homebrew itself. # Its installer also installs the Command Line Tools, without which git only offers to install them. install_homebrew() { [ -x /opt/homebrew/bin/brew ] && return echo 'Installing homebrew...' sudo -v # with NONINTERACTIVE, Homebrew's installer aborts instead of asking for the password NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" echo 'done' } accept_xcode_license() { [[ "$(/usr/bin/xcrun clang 2>&1 || true)" =~ 'license' ]] || return 0 echo 'Agreeing to Xcode license...' sudo xcodebuild -license accept echo 'done' } install_nix() { [ -x "$NIX" ] && return echo 'Installing Nix...' curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --no-confirm echo 'done' } clone_or_update_repo() { if [ ! -d "$DOTFILES_PATH" ]; then echo 'Cloning repo...' git clone https://github.com/creasty/dotfiles.git "$DOTFILES_PATH" echo 'done' return fi if [ "$(git -C "$DOTFILES_PATH" branch --show-current)" != master ]; then echo 'Info: working on a non-master branch' return fi git -C "$DOTFILES_PATH" diff --quiet HEAD || return 0 # the switch warns that the tree is dirty echo 'Updating repo...' git -C "$DOTFILES_PATH" pull origin master echo 'done' } # Applies the configuration of flake.nix for this user (nix-darwin and home-manager) switch_configuration() { echo 'Switching to the configuration...' sudo -H "$NIX" run "$DOTFILES_PATH#darwin-rebuild" -- switch --flake "$DOTFILES_PATH#$(id -un)" "$@" echo 'done' } # Runs the behavioral tests of what was just applied verify_configuration() { [ "$DOTFILES_NOVERIFY" -eq 0 ] || return 0 "$DOTFILES_PATH/verify" } main() { print_header check_os install_homebrew accept_xcode_license install_nix clone_or_update_repo switch_configuration "$@" verify_configuration } main "$@"