Skip to content

Instantly share code, notes, and snippets.

@byrongibson
Last active July 7, 2021 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byrongibson/9e59de23f7d69c83a5e4264a4cfda850 to your computer and use it in GitHub Desktop.
Save byrongibson/9e59de23f7d69c83a5e4264a4cfda850 to your computer and use it in GitHub Desktop.
Rebuild Nixos with ZFS snapshot
#!/usr/bin/env bash
# Rebuild NixOS. Snapshots system before rebuilds that are not dry-run,
# dry-activate, or rollback. Expects one required argument to nixos-rebuild:
# 'switch', 'boot', 'test', 'build',
# 'build-vm', 'build-vm-with-bootloader'
# 'dry-rebuild', 'dry-activate'
# optional arguments begin with --. Ex: --upgrade, --upgrade-all, --rollback, etc.
#error handling on
set -e
pprint () {
local cyan="\e[96m"
local default="\e[39m"
# ISO8601 timestamp + ms
local timestamp
#timestamp=$(date +%FT%T.%3NZ)
timestamp=$(date +%Y%m%d-%T-%Z)
echo -e "${cyan}${timestamp} $1${default}" 1>&2
}
argv="$@"
#snapshot before every nixos-rebuild, but not dry runs or rollbacks
if [[ "$@" =~ 'dry' || "$@" =~ 'rollback' ]]
then
pprint "Dry run or rollback, skipping ZFS snapshot."
else
pprint "nixos-rebuild $@ requested, creating zfs snapshot -r rpool@pre-rebuild-snap-$timestamp ... ";
zfs snapshot -r rpool@pre-rebuild-snap-$(date +%Y%m%d-%T-%Z);
sleep 5
fi
pprint "Starting nixos-rebuild -v --show-trace $@ ... ";
sleep 5;
nixos-rebuild -v --show-trace $argv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment