Last active
July 7, 2021 18:37
-
-
Save byrongibson/9e59de23f7d69c83a5e4264a4cfda850 to your computer and use it in GitHub Desktop.
Rebuild Nixos with ZFS snapshot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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