Skip to content

Instantly share code, notes, and snippets.

@vraravam
Last active April 29, 2024 10:56
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save vraravam/04fc2296b25090ea848642afbce4d168 to your computer and use it in GitHub Desktop.
Save vraravam/04fc2296b25090ea848642afbce4d168 to your computer and use it in GitHub Desktop.
Steps to get "up and running" with new Mac OSX
#!/usr/bin/env zsh
# This script can be used to setup a macos machine based on Vijay's configurations. As of now, this script is idempotent and will restore your local setup to the same state if run multiple times.
# If you have the same files already present, it will prompt you whether to override or not
# file location: <anywhere> (just need to invoke it from that location)
# You can run this script using this command:
# curl -L https://gist.githubusercontent.com/vraravam/04fc2296b25090ea848642afbce4d168/raw -o fresh-install.sh; chmod +x ./fresh-install.sh; ./fresh-install.sh
# BEFORE STARTING TO RUN THIS SCRIPT (for the first time on a new machine)
# 1. Login into Apple App store
# 2. Grant full disk access to Terminal app via System Preferences
# 3. Turn on File Vault encryption via System Preferences (if not, then this script will exit in the beginning with an appropriate message)
# 4. Turn off battery from showing in the Control Center (nice to have especially if you end up with the Stats app)
# 5. Change the built-in clock to show as analog to save horizontal space in the Control Center via System Preferences
# These env vars are defined by (duplicated intentionally) since this script would bootstrap the installation
USERNAME=${USERNAME:-$(whoami)}
PERSONAL_PROFILES_DIR=${PERSONAL_PROFILES_DIR:-"${HOME}/personal/${USERNAME}/profiles"}
# Set DNS of 8.8.8.8 before proceeding (in some cases, for eg Jio Wifi, gist.github doesn't resolve at all and times out)
sudo networksetup -setdnsservers Wi-Fi 8.8.8.8
##################################
# Install command line dev tools #
##################################
xcode-select -p 2>&1 >/dev/null
if [ $? != 0 ]; then
# install using the non-gui cmd-line alone
touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
softwareupdate -ia
rm /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
sudo xcodebuild -license accept || true
fi
###############################
# Do not allow rootless login #
###############################
# Note: Commented out since I am not sure if we need to do this on the office MBP or not
# ROOTLESS_STATUS=$(/usr/bin/csrutil status | awk '/status/ {print $5}' | sed 's/\.$//')
# if [[ ${ROOTLESS_STATUS} == "enabled" ]]; then
# echo "csrutil (\"rootless\") is enabled. please disable in boot screen and run again!"
# exit 1
# fi
#####################
# Turn on FileVault #
#####################
FILEVAULT_STATUS=$(fdesetup status)
if [[ ${FILEVAULT_STATUS} != "FileVault is On." ]]; then
echo "FileVault is not turned on. Please encrypt your hard disk!"
exit 1
fi
#################################
# Setup ssh scripts/directories #
#################################
mkdir -p "${HOME}/.ssh"
sudo chmod -R 600 "${HOME}"/.ssh/*
############################
# Disable macos gatekeeper #
############################
# sudo spectl --master-disable
# utility functions
blue() {
printf "\033[94m${1}\033[0m"
}
cyan() {
printf "\033[96m${1}\033[0m"
}
green() {
printf "\033[1;32m${1}\033[0m"
}
red() {
printf "\033[31m${1}\033[0m"
}
yellow() {
printf "\033[33m${1}\033[0m"
}
warn() (
echo $(yellow "**WARN** ${1}")
)
override_prompt() {
echo "$(blue 'Downloading') '${2}' : "
if [ ! -f "${2}" ]; then
echo " $(yellow ' since its not present already')"
curl -fsSL "${1}" -o "${2}"
else
read "yn?$(red ' already present'). Should I override [yn]? "
case $yn in
[Yy]*) curl -fsSL "${1}" -o "${2}" && echo " $(green "downloaded '${2}'")";;
[Nn]*) warn " skipping download of '${2}' since its already present";;
esac
fi
}
command_exists() {
type ${1} &> /dev/null 2>&1
}
######################
# vim configurations #
######################
override_prompt https://gist.githubusercontent.com/vraravam/2d8654cb21bfe506a64a05a49268d9de/raw "${HOME}/.vimrc"
######################################################################################
# Setup any sudo access password from cmd-line to also invoke the gui touchId prompt #
######################################################################################
curl -fsSL https://gist.githubusercontent.com/vraravam/0579373b2028e9b5b08ae87fb39c09d8/raw | sh
#####################
# Install oh-my-zsh #
#####################
export ZSH=
if [ ! -d "${HOME}/.oh-my-zsh" ]; then
curl -fsSL http://install.ohmyz.sh | sh
else
warn "skipping installation of oh-my-zsh since '${HOME}/.oh-my-zsh' is already present"
fi
override_prompt https://gist.githubusercontent.com/vraravam/a64d51fff250271aa7669438db5bd6a5/raw "${HOME}/.zshenv"
override_prompt https://gist.githubusercontent.com/vraravam/51e0516f5d7c6beec67668d1a31b7da8/raw "${HOME}/.zlogin"
override_prompt https://gist.githubusercontent.com/vraravam/ce8654afd65def117dc17e2b0a30156c/raw "${HOME}/.zprofile"
override_prompt https://gist.githubusercontent.com/vraravam/657c3b94d1b04bacd2b6a38c22d6ec56/raw "${HOME}/.zshrc"
override_prompt https://gist.githubusercontent.com/vraravam/9c8d9f335159f90c3fff890bcf3856d6/raw "${HOME}/.zshrc.custom"
override_prompt https://gist.githubusercontent.com/vraravam/ce0dda29bb07f0504df8686cfd2755ca/raw "${HOME}/.aliases"
override_prompt https://gist.githubusercontent.com/vraravam/93077cb859cdb64793cc500791da6dce/raw "${HOME}/.p10k.zsh"
override_prompt https://gist.githubusercontent.com/vraravam/d8c0a88d0046225e5700f6ba7258da65/raw "${HOME}/.gitignore"
override_prompt https://gist.githubusercontent.com/vraravam/eab090e70a545934f409/raw "${HOME}/.gitconfig"
override_prompt https://gist.githubusercontent.com/vraravam/e9676759db46950e1fd817e49e513394/raw "${HOME}/.gitconfig-template.inc"
##################################
# Install custom plugins for zsh #
##################################
ZSH_CUSTOM=${ZSH_CUSTOM:-${HOME}/.oh-my-zsh/custom}
mkdir -p "${ZSH_CUSTOM}/plugins"
clone_if_not_present() {
target_folder="${ZSH_CUSTOM}/plugins/$(basename ${1})"
if [ ! -d ${target_folder} ]; then
git clone --depth=1 ${1} ${target_folder}
else
warn "skipping cloning of '${1}' since '${target_folder}' is already present"
fi
}
clone_if_not_present https://github.com/mroth/evalcache
# Note: If you install these via homebrew, you will have to manually load them outside of the 'plugins' defn in '.zshrc'
clone_if_not_present https://github.com/zdharma-continuum/fast-syntax-highlighting
clone_if_not_present https://github.com/zsh-users/zsh-autosuggestions
################################
# Prep for installing homebrew #
################################
source "${HOME}/.zshenv"
source "${HOME}/.zprofile"
source "${HOME}/.zshrc"
sudo mkdir -p "${HOMEBREW_PREFIX}/tmp" "${HOMEBREW_PREFIX}/repository" "${HOMEBREW_PREFIX}/plugins" "${HOMEBREW_PREFIX}/bin"
sudo chown -fR "${USERNAME}":admin "${HOMEBREW_PREFIX}"
# sudo rm -rf ${HOME}/.gnupg # to delete gpg keys that might have been generated from an older version of gpg
#######################################
# Install homebrew (on empty machine) #
#######################################
command_exists brew || bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
override_prompt https://gist.githubusercontent.com/vraravam/8c9eae91a3750bed86b81e3a4711f842/raw "${HOME}/Brewfile"
brew bundle check || brew bundle --all || true
###########################################
# Link programs to open from the cmd-line #
###########################################
replace_executable_if_exists_and_is_not_symlinked() {
if [ -e "${1}" ]; then
rm -fv "${2}" && ln -sf "${1}" "${2}"
else
warn "executable '${1}' not found and so skipping symlinking"
fi
}
# Symlink from the embedded executable for codium-insiders
replace_executable_if_exists_and_is_not_symlinked "/Applications/VSCodium - Insiders.app/Contents/Resources/app/bin/codium-insiders" "${HOMEBREW_PREFIX}/bin/codium-insiders"
# if we are using 'vscodium-insiders' only, symlink it to 'codium' for ease of typing
replace_executable_if_exists_and_is_not_symlinked "${HOMEBREW_PREFIX}/bin/codium-insiders" "${HOMEBREW_PREFIX}/bin/codium"
# extra: also symlink for 'code'
replace_executable_if_exists_and_is_not_symlinked "${HOMEBREW_PREFIX}/bin/codium" "${HOMEBREW_PREFIX}/bin/code"
# Setup rider for use from the cmd-line
# replace_executable_if_exists_and_is_not_symlinked "/Applications/Rider.app/Contents/MacOS/rider" "${HOMEBREW_PREFIX}/bin/rider"
# Setup idea for use from the cmd-line
# replace_executable_if_exists_and_is_not_symlinked "/Applications/IntelliJ IDEA CE.app/Contents/MacOS/idea" "${HOMEBREW_PREFIX}/bin/idea"
# defaults write -g NSFileViewer -string org.yanex.marta
# To revert back to use Finder as default file manager you can enter
# defaults delete -g NSFileViewer
# ln -sf /Applications/Marta.app/Contents/Resources/launcher ${HOMEBREW_PREFIX}/bin/marta
# Setup login items
setup_login_item() {
if [ -d "/Applications/${1}" ]; then
echo $(green "Setting up '${1}' as a login item") && osascript -e "tell application \"System Events\" to make login item at end with properties {path:\"/Applications/${1}\", hidden:false}" 2>&1 > /dev/null
else
warn "Couldn't find application '/Applications/${1}' and so skipping setting up as a login item"
fi
}
setup_login_item "AlDente.app"
# setup_login_item "Bitwarden.app"
setup_login_item "Clocker.app"
setup_login_item "Cloudflare WARP.app"
setup_login_item "Command X.app"
setup_login_item "Flycut.app"
setup_login_item "iBar.app"
setup_login_item "Itsycal.app"
setup_login_item "KeepingYouAwake.app"
setup_login_item "Keybase.app"
setup_login_item "Rectangle.app"
setup_login_item "Stats.app"
echo "\n"
echo $(green "********** Finished auto installation process: MANUALLY QUIT AND RESTART iTerm2 and Terminal apps **********")
echo $(red "1. Use '${HOME}/.gitconfig-template.inc' as a template to create equivalent configuration files with your logins and make corresponding changes in ${HOME}/.gitconfig to reflect the same")
echo $(red "2. Go to Terminal > Preferences > Profiles > Basic > Text > Change Font to 'MesloLGS Nerd Font'")
echo $(red "3. Go to iTerm2 > Preferences > Profiles > Default > Text > Change Font to 'MesloLGS Nerd Font'")
echo $(red "4. Go to iTerm2 > Preferences > Profiles > Default > Keys > Key Mappings > Presets (and choose 'Natural Text Editing')")
echo $(red "5. Turn on 'Tap to click' in Trackpad prefs")
@vraravam
Copy link
Author

vraravam commented Jul 5, 2023

This script is now idempotent and will also prompt if the file already exists on your system. If this is the case and you have your own customization on top of my version in each file, you can now choose to skip the download (so, it will not force overwrite your local file). This should help especially in the case of customized Brewfile

@vraravam
Copy link
Author

vraravam commented Jul 6, 2023

Fixed curl command since directly piping to bash made it non-interactive and thus the prompts stopped working. Instead of that, we are now downloading to a local file and then executing that file.

@vraravam
Copy link
Author

Added 'Cloudflare Warp' to be setup as a login item.

@vraravam
Copy link
Author

Added AlDente

@vraravam
Copy link
Author

vraravam commented Mar 4, 2024

Fixed issue with oh-my-zsh complaining about being loaded from bash. Turned out, I was running this script via bash. Fixed the read command now to work with zsh, and that allowed me to switch to using zsh for this script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment