Skip to content

Instantly share code, notes, and snippets.

@flisk
Last active June 27, 2022 11:46
Show Gist options
  • Save flisk/312092150ac43c32500fbd1b583caf56 to your computer and use it in GitHub Desktop.
Save flisk/312092150ac43c32500fbd1b583caf56 to your computer and use it in GitHub Desktop.
macOS shell script to upgrade multi-user nix installs
#!/bin/sh
# see https://nixos.org/manual/nix/stable/installation/upgrading.html
set -eu
if [ "$(id -u)" != 0 ]; then
script="$(basename "$0")"
>&2 echo "$script: root required, re-executing through sudo"
exec sudo --login "$0"
fi
cmd() {
printf "[#] \033[36m%s\033[0m\n" "$*"
"$@"
}
cmd nix-channel --update
cmd nix-env -iA nixpkgs.nix
# the documentation on post-upgrade nix-daemon restarts seems to be slightly
# wrong; `launchctl remove` won't wait until jobs have actually stopped. if i
# follow the documentation exactly, `launchctl load` will sometimes run before
# the old nix-daemon is gone. the unload-load-method i'm using here appears to
# be working for me as of macOS 12.4.
plist=/Library/LaunchDaemons/org.nixos.nix-daemon.plist
cmd launchctl unload "$plist"
cmd launchctl load -w "$plist"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment