0xdevalias / devalias.net

Source for devalias.net
http://www.devalias.net/
49 stars 10 forks source link

[Blog] Setting up a new computer/laptop (macOS) #79

Open 0xdevalias opened 4 years ago

0xdevalias commented 4 years ago

It would be cool to write about how I set up a new laptop, encoding some of my currently more manual processes, and the order that makes most sense in doing so.

Setting up a new laptop

Pre-Bootstrap

Go through the macOS installer, configure any settings as desired.

Once you've booted into macOS for the first time, it probably makes sense to check for any system updates (particularly major OS updates) and install those before continuing.

(Optional) At this stage, it may also be useful to get a snapshot of the default configurations on a freshly installed version of macOS. This can be useful for updating the scripts that configure macOS defaults (see 'dotfiles' section below)

Bootstrap

(Optional) Install command line development tools.

This will generally be performed as part of the Homebrew install script (see below), but if you want to do it manually, you can do this now (it's required for the default system installations of tools such as git, etc)

If you try to run a command line tool such as git before installing this, you will see a message similar to the following:

xcode-select: note: no developer tools were found at '/Applications/Xcode.app', requesting install. Choose an option in the dialog to download the command line developer tools.

image

Install Homebrew

Homebrew is a package manager that makes it easy to install all of the other applications we want on our machine.

It can be easily installed by opening Terminal and running the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once this is done, you probably also want enable the homebrew 'cask' 'tap', to be able to instal non CLI applications as well:

brew tap homebrew/cask

Dotfiles, defaults and core applications

Once I have my basic bootstrap tools installed, I can clone my dotfiles repo, and run some more automated setup scripts to get my environment exactly how I like it.

Open Terminal and clone my dotfiles repo. Note that we use https (rather than ssh) to clone for now, as we don't have ssh keys setup on our new machine yet:

git clone https://github.com/0xdevalias/dotfiles.git ~/.dotfiles

Make any customisations you may need before running. At a minimum, I would suggest checking that the latest versions are setup in:


Note: I hit a number of issues when running this that we should figure how to fix:

› /Users/devalias/.dotfiles/homebrew/install.sh
Already up-to-date.
› /Users/devalias/.dotfiles/antigen/install.sh
  › antigen update
Loading antigen..
  [ERROR] Antigen loader requires homebrew to be installed. Exiting.
› /Users/devalias/.dotfiles/zsh/install.sh
/Users/devalias/.dotfiles/zsh/install.sh: line 15: default-user-shell: command not found
/Users/devalias/.dotfiles/zsh/install.sh: line 16: default-user-shell: command not found
Setting default shell to zsh (homebrew) from ''
Changing shell for devalias.

After running the bootstrap, and restarting my terminal, it also seems as though it won't run all of the 'init scripts' correctly, due to missing programs, etc; which eventually cause the shell to exit with an error. These should either be included as part of a 'core required tools' step, or be smart enough to exit cleanly without running if those tools aren't already present:

Loading antigen..
  [ERROR] Antigen loader requires homebrew to be installed. Exiting.
/Users/devalias/.dotfiles/go/path.zsh:1: command not found: go
/Users/devalias/.dotfiles/ruby/path.zsh:9: command not found: rbenv
Loading gpg-agent..
[ERROR] Couldn't find 'gpg'. Try:
  brew install gpg

[brew-info]
  gnupg: stable 2.2.20 (bottled)
  GNU Pretty Good Privacy (PGP) package
  https://gnupg.org/
  Not installed
  From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/gnupg.rb
  ==> Dependencies
  Build: pkg-config
  Required: adns, gettext, gnutls, libassuan, libgcrypt, libgpg-error, libksba, libusb, npth, pinentry
  ==> Analytics
  install: 36,942 (30 days), 123,847 (90 days), 522,421 (365 days)
  install-on-request: 31,731 (30 days), 105,365 (90 days), 435,775 (365 days)
  build-error: 0 (30 days)
[ERROR] Couldn't find 'pinentry-mac'. Try:
  brew install pinentry-mac

[brew-info]
  pinentry-mac: stable 0.9.4 (bottled), HEAD
  Pinentry for GPG on Mac
  https://github.com/GPGTools/pinentry-mac
  Not installed
  From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/pinentry-mac.rb
  ==> Requirements
  Build: xcode
  ==> Options
  --HEAD
    Install HEAD version
  ==> Caveats
  You can now set this as your pinentry program like

  ~/.gnupg/gpg-agent.conf
      pinentry-program /usr/local/bin/pinentry-mac
  ==> Analytics
  install: 2,450 (30 days), 7,469 (90 days), 29,599 (365 days)
  install-on-request: 2,352 (30 days), 7,257 (90 days), 28,411 (365 days)
  build-error: 0 (30 days)
/Users/devalias/.dotfiles/gpg/gpg_agent.zsh:20: command not found: gpg2

  [ERROR] failed to parse gpg-config, not starting gpg-agent

[Process completed]
zsh compinit: insecure directories, run compaudit for list.
Ignore insecure directories and continue [y] or abort compinit [n]?

If you see this, you can say n for the time being, then once your shell starts, run the following command to ensure the directories aren't world writeable:


Once everything is updated as desired, run script/bootstrap to install things:

cd ~/.dotfiles
script/bootstrap

At time of writing, this will:

Note that the automatic running of installs scripts is a little manual/haphazard at the moment, mostly because I don't necessarily want to automatically install everything on a new machine. I would suggest checking the code to see what will be run, and sanity checking that each is going to do what you want it to. A lot of these scripts end up being run once per new machine, so they can drift/become outdated from my 'ideal setup' over time.

Eventually I would like to clean this up and standardise it a bit more, to be a 'bare minimal' set (eg. core development environments for node, python, ruby, etc. Though I think when that happens, it may make sense to do so as part of adopting a dotfiles management tool (see https://github.com/0xdevalias/dotfiles/issues/8)

For now, refer to the below sections for a more 'manual' list of core installs/setup tasks for a new dev machine. I've roughly attempted to document them in the order I would follow, but this isn't necessarily a hard requirement.

Computer Name

Basic text/code editor

Generate SSH key for GitHub, GitLab, Bitbucket, etc

Terminal/Shell (iTerm2)

GPG (git commit signing, etc)

Keybase

Development Tools (Java, Node, Python, Ruby, Golang, Swift, Xcode, etc)

Web Browser

Alfred

BetterTouchTool

Bartender

Hyperdock

Unsorted

The following don't have any complex setup scripts/environment required, so we can just manually install them with HomeBrew if desired:

Unsorted

App Store

We also want to open the App Store, and install any past purchases we want on this machine:

Launchpad

Next we want to organise applications in Launchpad into appropriate folders. While this is controlled by a sqlite database, I'm not currently aware of any tools that allow us to do this automatically, so we'll do it manually for now:

Cleaning up an old laptop before selling/disposing

Unsorted

Legacy

0xdevalias commented 1 year ago

Setup steps - MacBook Pro 16" (2023) M2 Max (April 2023)

macOS Ventura 13.2.1

Pre-Bootstrap

macOS Installer

Go through the macOS installer, configure any settings as desired:

Install System / OS Updates

Once you've booted into macOS for the first time, it probably makes sense to check for any system updates (particularly major OS updates) and install those before continuing.

(Optional) Save snapshot of macOS default config

At this stage, it may also be useful to get a snapshot of the default configurations on a freshly installed version of macOS. This can be useful for updating the scripts that configure macOS defaults (see 'dotfiles' section below)

Open Terminal and run the following:

mkdir ~/Desktop/macOS-defaults/
mkdir ~/Desktop/macOS-defaults/domains

defaults read > ~/Desktop/macOS-defaults/defaults-read.txt
defaults domains > ~/Desktop/macOS-defaults/defaults-domains.txt
defaults export -globalDomain - > ~/Desktop/macOS-defaults/defaults-export-globalDomain.xml

Then to export each individual domain's settings as .xml files, we can use the following script:

#!/bin/bash

# ChatGPT Ref: https://chat.openai.com/chat/c03c1acf-5ba2-46a1-878e-020bb4715df6

# Define the output directory
output_dir="$HOME/Desktop/macOS-defaults/domains"

# Get a list of all domains
domains=$(defaults domains)

echo "Exporting domains..."

# Loop through each domain
IFS=", "
for domain in $domains; do
  # Export the domain to an XML file
  echo "  $domain"
  defaults export "$domain" - > "${output_dir}/${domain}.xml"
done

Bootstrap

(Optional) Install command line development tools.

This will generally be performed as part of the Homebrew install script (see below), but if you want to do it manually, you can do this now (it's required for the default system installations of tools such as git, etc)

If you try to run a command line tool such as git before installing this, you will see a message similar to the following:

xcode-select: note: no developer tools were found at '/Applications/Xcode.app', requesting install. Choose an option in the dialog to download the command line developer tools.

image

Install Homebrew

Homebrew is a package manager that makes it easy to install all of the other applications we want on our machine.

It can be easily installed by opening Terminal and running the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

We used to have to manually set up the homebrew/cask tap separately, but this is automatically included and enabled by default these days.

(Optional) Universal Control

If you're setting up your new system while you still have your old one running, you can make use of macOS' Universal Control feature to share the same mouse/keyboard between both devices:

You can enable this by:

To make things easier, you probably also want to arrange your devices similar to how they are actually positioned in real life:

Once this is configured (potentially on both devices), you should be able to 'push' your pointer 'through' the edge of the screen from one device to the other.

Dotfiles, defaults, and core applications

Clone 0xdevalias/dotfiles

Once I have my basic bootstrap tools installed, I can clone my dotfiles repo, and run some more automated setup scripts to get my environment exactly how I like it.

Open Terminal and clone my dotfiles repo. Note that we use https (rather than ssh) to clone for now, as we don't have ssh keys setup on our new machine yet:

git clone https://github.com/0xdevalias/dotfiles.git ~/.dotfiles

Bootstrap dotfiles (~/.dotfiles/scripts/bootstrap)

Within Terminal, run the dotfiles bootstrap script to configure git and symlink all of the dotfiles configuration into place:

~/.dotfiles/script/bootstrap
Example output ```shell % ~/.dotfiles/script/bootstrap [ .. ] setup gitconfig.local [ ?? ] - What is your github author name? Glenn 'devalias' Grant [ ?? ] - What is your github author email? REDACTED@REDACTED.REDACTED TODO: Setup commit signing automagically https://github.com/pstadler/keybase-gpg-github TODO: Setup commit signing https://github.com/pstadler/keybase-gpg-github [ OK ] gitconfig [ .. ] installing dotfiles [ OK ] linked /Users/devalias/.dotfiles/vim/vimrc.symlink to /Users/devalias/.vimrc [ OK ] linked /Users/devalias/.dotfiles/asdf/asdfrc.symlink to /Users/devalias/.asdfrc [ OK ] linked /Users/devalias/.dotfiles/asdf/tool-versions.symlink to /Users/devalias/.tool-versions [ OK ] linked /Users/devalias/.dotfiles/go/cobra.yaml.symlink to /Users/devalias/.cobra.yaml [ OK ] linked /Users/devalias/.dotfiles/gpg/gnupg.symlink to /Users/devalias/.gnupg [ OK ] linked /Users/devalias/.dotfiles/config.symlink to /Users/devalias/.config [ OK ] linked /Users/devalias/.dotfiles/ssh/ssh.symlink to /Users/devalias/.ssh [ OK ] linked /Users/devalias/.dotfiles/iterm2/iterm2.symlink to /Users/devalias/.iterm2 [ OK ] linked /Users/devalias/.dotfiles/iterm2/iterm2_shell_integration.zsh.symlink to /Users/devalias/.iterm2_shell_integration.zsh [ OK ] linked /Users/devalias/.dotfiles/localrc.example.symlink to /Users/devalias/.localrc.example [ OK ] linked /Users/devalias/.dotfiles/zsh/zshrc.symlink to /Users/devalias/.zshrc [ OK ] linked /Users/devalias/.dotfiles/atom.symlink to /Users/devalias/.atom [ OK ] linked /Users/devalias/.dotfiles/ruby/gemrc.symlink to /Users/devalias/.gemrc [ OK ] linked /Users/devalias/.dotfiles/ruby/pryrc.symlink to /Users/devalias/.pryrc [ OK ] linked /Users/devalias/.dotfiles/ruby/irbrc.symlink to /Users/devalias/.irbrc [ OK ] linked /Users/devalias/.dotfiles/git/gitconfig.symlink to /Users/devalias/.gitconfig [ OK ] linked /Users/devalias/.dotfiles/git/gitconfig.local.symlink to /Users/devalias/.gitconfig.local [ OK ] linked /Users/devalias/.dotfiles/git/gitignore_global.symlink to /Users/devalias/.gitignore_global [ OK ] linked /Users/devalias/.dotfiles/starship.symlink to /Users/devalias/.starship All installed! ```
TODO: Computer Name

TODO: This should probably get moved into the bootstrap script so it happens automagically

TODO: Generate SSH key for GitHub, GitLab, Bitbucket, etc

TODO: This should probably get moved into the bootstrap script so it happens automagically

TODO: We should leverage the GitHub CLI (gh) tool as much as possible here

TODO: GPG (git commit signing, etc)

TODO: This should probably get moved into the bootstrap script so it happens automagically, since it's sort of a requirement for fully configuring git + commit signing properly

TODO: Keybase

TODO: This should probably get moved into the bootstrap script so it happens automagically, since it's sort of a semi-requirement for fully configuring git + commit signing properly I think


At this point it's worth closing and re-opening Terminal so that the configuration from the dotfiles can take effect.

TODO: Can we reload the shell automagically so we don't have to do it manually here..?

When the Terminal restarts, you might see some errors like the following, these are ok to ignore for now:

Loading antigen..
Error: No such keg: /opt/homebrew/Cellar/antigen
  ERROR: Antigen not found. Was it installed with homebrew? Skipping. (/opt/homebrew/share/antigen/antigen.zsh, )

Shell prompt customisations (~/.dotfiles/starship.symlink/install.sh)

Our terminal isn't quite looking right yet as we're missing Starship, which manages our shell prompt customisations. We can install that now as follows:

~/.dotfiles/starship.symlink/install.sh 
Example output ```shell › ~/.dotfiles/starship.symlink/install.sh [starship::install] [require_installed_brew::starship] Required command 'starship' missing. Installing.. ==> Fetching starship ==> Downloading https://ghcr.io/v2/homebrew/core/starship/manifests/1.13.1 ==> Downloading https://ghcr.io/v2/homebrew/core/starship/blobs/sha256:9d12ac9dcb05ca2ea86b943be41eaa32c2850ae429b6fc576c280f1e78514613 ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:9d12ac9dcb05ca2ea86b943be41eaa32c2850ae429b6fc576c280f1e78514613?se=2023-04-11T07%3A45%3A00Z&sig=Zek7y3tfm5IZ9G0Aqdq6mJVCKzfkpJk2JeZ0ha43Geg%3D&sp=r&spr=https&sr=b&sv=2019-12-12 ==> Pouring starship--1.13.1.arm64_ventura.bottle.tar.gz ==> Caveats zsh completions have been installed to: /opt/homebrew/share/zsh/site-functions ==> Summary 🍺 /opt/homebrew/Cellar/starship/1.13.1: 11 files, 5.7MB ==> Running `brew cleanup starship`... Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP. Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`). Install log: /tmp/brew-install-starship.log ```

Once Starship is installed, close and re-open Terminal again, and now things should look more normal:

image

Basic text/code editor (~/.dotfiles/sublimetext/install.sh)

Some of the next steps will be easier with a basic code/text editor installed, so we'll install Sublime Text next:

~/.dotfiles/sublimetext/install.sh
Example output ```shell ⇒ ~/.dotfiles/sublimetext/install.sh [sublimetext::install] [require_installed_brew_cask::sublime-text] Required cask 'sublime-text' missing. Installing.. ==> Downloading https://download.sublimetext.com/sublime_text_build_4143_mac.zip ==> Installing Cask sublime-text ==> Moving App 'Sublime Text.app' to '/Applications/Sublime Text.app' ==> Linking Binary 'subl' to '/opt/homebrew/bin/subl' 🍺 sublime-text was successfully installed! Install log: /tmp/brew-cask-install-sublime-text.log [sublimetext] Making settings folder.. (/Users/devalias/Library/Application Support/Sublime Text/Packages/User) [sublimetext] User settings found, moving into dotfiles.. (/Users/devalias/.dotfiles/sublimetext/SublimeSettings) [sublimetext] Symlinking settings path to dotfiles.. (/Users/devalias/Library/Application Support/Sublime Text/Packages/User) ```

TODO: Install Node, Python, Ruby, etc

Make any customisations you may need before running. At a minimum, I would suggest checking that the latest versions are setup in:

TODO: continue things here

Misc

TODO

Continue setting up new laptop as per the main config steps detailed above