-
Notifications
You must be signed in to change notification settings - Fork 2
/
system-update.sh
132 lines (102 loc) · 3.63 KB
/
system-update.sh
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
source @bashLib@
nix_config="${HOME}/.nix-config"
_has_unit_enabled() {
[[ "$(systemctl is-enabled "${1}" 2> /dev/null)" == "enabled" ]]
}
_log() {
echo
echo -e "[${YELLOW}${BOLD}${1}${RESET}] ${BOLD}${2}${RESET}"
echo
}
_migration_remove() {
local file="${1}"
local ask="${2:-}"
if [[ -e "${file}" && -w "${file}" ]] && ( [[ "${ask}" != "1" ]] || _read_boolean "Remove ${file//"${HOME}"/"~"}?" ); then
_log "migration" "remove ${file//"${HOME}"/"~"}"
rm -vrf "${file}"
fi
}
_pull_changes() {
if [[ -d "${2}" && -w "${2}" ]]; then
_log "pull changes" "update ${1} project"
git -C "${2}" pull --prune
fi
}
_show_result_diff() {
echo
nvd diff "${1}" ./result
rm result
}
if _is_root; then
_log "Please don't run this script with root!"
exit 1
fi
# add key
_log "keychain" "add key"
keychain "${HOME}/.ssh/keys/id_rsa.vcs"
# update ubuntu
if _available apt && ! _is_darwin; then
_log "apt" "update"
sudo apt update
_log "apt" "upgrade"
sudo apt upgrade -y
_log "apt" "autoclean"
sudo apt autoclean -y
_log "apt" "autoremove"
sudo apt autoremove -y
fi
# update brew
if _available brew && _is_darwin; then
_log "brew" "update"
brew update
_log "brew" "upgrade"
brew upgrade
fi
# update projects
_pull_changes "nix-config" "${nix_config}"
_pull_changes "files" "${HOME}/.files"
_pull_changes "pass" "${HOME}/.password-store"
# nix updates
# TODO: use scripts defined in home/development/nix
if _is_nixos; then
_log "nix" "build nixos configuration"
nix build --log-format internal-json --verbose "${nix_config}#nixosConfigurations.$(hostname).config.system.build.toplevel" |& nom --json
_show_result_diff "/nix/var/nix/profiles/system"
_log "nix" "switch nixos configuration"
nixos-rebuild switch --use-remote-sudo --flake "${nix_config}"
fi
if [[ "${USER}" == "nix-on-droid" ]] && _available nix-on-droid; then
_log "nix" "build nix-on-droid configuration"
nix build --log-format internal-json --verbose "${nix_config}#nixOnDroidConfigurations.pixel7a.activationPackage" --impure |& nom --json
_show_result_diff "/nix/var/nix/profiles/nix-on-droid"
_log "nix" "switch nix-on-droid configuration"
nix-on-droid switch --flake "${nix_config}#pixel7a"
fi
if ! _is_nixos && _available home-manager; then
_log "nix" "build home-manager configuration"
nix build --log-format internal-json --verbose "${nix_config}#homeConfigurations.\"$(whoami)@$(hostname)\".activationPackage" |& nom --json
_show_result_diff "${HOME}/.local/state/nix/profiles/home-manager"
_log "nix" "switch home-manager configuration"
home-manager switch --flake "${nix_config}" -b hm-bak
fi
# general migrations
if [[ ! -f "${HOME}/.age/key.txt" || -L "${HOME}/.age" ]] && _read_boolean "Generate ~/.age/key.txt?"; then
if [[ -L "${HOME}/.age" ]]; then
rm -v "${HOME}/.age"
fi
mkdir -p "${HOME}/.age"
age-keygen -o "${HOME}/.age/key.txt" 2>&1 |
sed -e "s,^Public key: \(.*\)\$,\n# $(hostname)-${USER} = \"\1\"," |
tee -a "${nix_config}/.agenix.toml"
else
_migration_remove "${HOME}/.age-bak" 1
fi
_migration_remove "${HOME}/.ssh/id_rsa"
_migration_remove "${HOME}/.ssh/id_rsa.pub"
_migration_remove "${HOME}/.ssh/known_hosts.old"
_migration_remove "${HOME}/.gnupg-setup" 1
mapfile -t to_be_removed_pkgs < <(nix-env -q --json | jq -r ".[].pname" | grep -Ev '^(home-manager|nix-on-droid)-path$')
if [[ "${#to_be_removed_pkgs[@]}" -ne 0 ]]; then
_log "migration" "remove manual installed packages via nix-env"
nix-env --uninstall "${to_be_removed_pkgs[@]}"
fi