about summary refs log tree commit diff stats
path: root/pkgs/by-name/fu/fupdate-sys/update-sys.sh
blob: 38debf445bf2dfca7029f0384dcdc48b3103e582 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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