aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/fu/fupdate-sys
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-04-14 11:56:32 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-04-14 11:56:32 +0200
commitcf94ffb40e6afbeeea4b3737faf3eaef972d869f (patch)
treebe3f8f6cd0db965a738b4e7e0a3632521a3221f8 /pkgs/by-name/fu/fupdate-sys
parentpkgs/stamp: Migrate to `writeShellApplication` (diff)
downloadnixos-config-cf94ffb40e6afbeeea4b3737faf3eaef972d869f.zip
pkgs/update-sys: Migrate to `writeShellApplication` and rename to fupdate-sys
Diffstat (limited to 'pkgs/by-name/fu/fupdate-sys')
-rw-r--r--pkgs/by-name/fu/fupdate-sys/package.nix29
-rwxr-xr-xpkgs/by-name/fu/fupdate-sys/update-sys.sh161
2 files changed, 190 insertions, 0 deletions
diff --git a/pkgs/by-name/fu/fupdate-sys/package.nix b/pkgs/by-name/fu/fupdate-sys/package.nix
new file mode 100644
index 00000000..be692d12
--- /dev/null
+++ b/pkgs/by-name/fu/fupdate-sys/package.nix
@@ -0,0 +1,29 @@
+{
+ writeShellApplication,
+ # Dependencies
+ git,
+ nixos-rebuild,
+ sudo,
+ openssh,
+ coreutils,
+ mktemp,
+ gnugrep,
+ gnused,
+ systemd,
+}:
+writeShellApplication {
+ name = "fupdate-sys";
+ text = builtins.readFile ./fupdate-sys.sh;
+ inheritPath = false;
+ runtimeInputs = [
+ git
+ nixos-rebuild
+ sudo
+ openssh
+ coreutils
+ mktemp
+ gnugrep
+ gnused
+ systemd
+ ];
+}
diff --git a/pkgs/by-name/fu/fupdate-sys/update-sys.sh b/pkgs/by-name/fu/fupdate-sys/update-sys.sh
new file mode 100755
index 00000000..38debf44
--- /dev/null
+++ b/pkgs/by-name/fu/fupdate-sys/update-sys.sh
@@ -0,0 +1,161 @@
+#!/usr/bin/env dash
+
+# FIXME(@bpeetz): Ideally I could replace this script with a deployment tool. Thus we
+# would have the same tool on the server as I use in my config. <2025-04-14>
+
+# Shell library {{{
+die() {
+ error "$1"
+ if [ -n "$2" ]; then
+ exit "$2"
+ else
+ exit 1
+ fi
+}
+print() {
+ printf "%s" "$*"
+}
+println() {
+ printf "%s\n" "$*"
+}
+eprint() {
+ >&2 print "$@"
+}
+eprintln() {
+ >&2 println "$@"
+}
+if [ -n "$NO_COLOR" ]; then
+ error() {
+ eprintln "==> ERROR:" "$*"
+ }
+ warning() {
+ eprintln "==> WARNING:" "$*"
+ }
+ debug() {
+ [ -n "$SHELL_LIBRARY_DEBUG" ] && eprintln "==> [Debug:]" "$*"
+ }
+ debug2() {
+ [ -n "$SHELL_LIBRARY_DEBUG" ] && eprintln " -> [Debug:]" "$*"
+ }
+ msg() {
+ eprintln "==>" "$*"
+ }
+ msg2() {
+ eprintln " ->" "$*"
+ }
+ prompt() {
+ eprint "..>" "$*"
+ }
+else
+ error() {
+ eprintln "\033[1;91m==> ERROR:\033[0m" "\033[1;93m$*\033[0m"
+ }
+ warning() {
+ eprintln "\033[1;91m==> WARNING:\033[0m" "\033[1;93m$*\033[0m"
+ }
+ debug() {
+ [ -n "$SHELL_LIBRARY_DEBUG" ] && eprintln "\033[1;94m==> [Debug:]\033[0m" "\033[1;93m$*\033[0m"
+ }
+ debug2() {
+ [ -n "$SHELL_LIBRARY_DEBUG" ] && eprintln "\033[1;94m -> [Debug:]\033[0m" "\033[1;93m$*\033[0m"
+ }
+ msg() {
+ eprintln "\033[1;96m==>\033[0m" "\033[1;93m$*\033[0m"
+ }
+ msg2() {
+ eprintln "\033[1;96m ->\033[0m" "\033[1;93m$*\033[0m"
+ }
+ prompt() {
+ eprint "\033[1;96m..>\033[0m" "\033[1;93m$*\033[0m"
+ }
+fi
+# }}}
+
+
+NAME="update-sys"
+help() {
+ cat <<EOF
+This is a NixOS System flake update manager.
+
+USAGE:
+ $NAME [--branch <branchname>] [--help]
+
+OPTIONS:
+ --branch | -b BRANCHNAME
+ select a branch to update from.
+
+ --mode | -m MODE
+ select a mode to update with
+
+ --help | -h
+ output this help.
+ARGUMENTS:
+ BRANCHNAME := [[ git branch --list --format '%(refname:short)' ]]
+ The name of the branch to deploy the config from
+
+ MODE := switch|boot|test|build|dry-build|dry-activate|edit|repl|build-vm|build-vm-with-bootloader
+ See the 'nixos-rebuild' manpage for more information about these modes.
+EOF
+ exit "$1"
+}
+BRANCH=""
+
+while [ "$#" -gt 0 ]; do
+ case "$1" in
+ "--help" | "-h")
+ help 0
+ ;;
+ "--branch" | "-b")
+ if [ -n "$2" ]; then
+ BRANCH="$2"
+ else
+ error "$1 requires an argument"
+ help 1
+ fi
+ shift 2
+ ;;
+ "--mode" | "-m")
+ if [ -n "$2" ]; then
+ MODE="$2"
+ else
+ error "$1 requires an argument"
+ help 1
+ fi
+ shift 2
+ ;;
+ *)
+ error "the option $1 does not exist!"
+ help 1
+ ;;
+ esac
+done
+
+cd /etc/nixos || die "No /etc/nixos"
+msg "Starting system update..."
+git remote update origin --prune >/dev/null 2>&1
+if ! [ "$BRANCH" = "" ]; then
+ git switch "$BRANCH" >/dev/null 2>&1 && msg2 "Switched to branch '$BRANCH'"
+fi
+msg2 "Updating git repository..."
+git pull --rebase
+
+# We use a tempfile, to make this truly async.
+default_branch=$(mktemp)
+cleanup() {
+ rm "$default_branch"
+}
+trap cleanup EXIT
+
+git remote show origin | grep 'HEAD' | cut -d':' -f2 | sed -e 's/^ *//g' -e 's/ *$//g' >"$default_branch" &
+
+msg2 "Updating system..."
+if [ -n "$MODE" ]; then
+ nixos-rebuild "$MODE"
+else
+ nixos-rebuild switch
+fi
+
+git switch "$(cat "$default_branch")" >/dev/null 2>&1 && msg2 "Switched to branch '$(cat "$default_branch")'"
+msg "Finished Update!"
+
+# vim: ft=sh