-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·230 lines (189 loc) · 6.19 KB
/
run.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
# Simple task runner that's not Make so syntax highlighting and shellcheck, shfmt, etc work.
# https://gist.github.com/tvalladon/e316bee4b58ca082d2190be023565949
debug() { ## Debug REPL
echo "Stopped in REPL. Press ^D to resume, or ^C to abort."
local line
while read -r -p "> " line; do
eval "$line"
done
echo
}
## START NIX
nix-extra() { ## Create nix-extra with any sensitive values
mkdir -p ~/src/nix-extra
password=$(mkpasswd -m sha-512)
cat <<EOF >~/src/nix-extra/nixos.nix
{ config, pkgs, ... }: {
users.users.karl.hashedPassword = "${password}";
}
EOF
}
nix-displaylink() { ## Setup DisplayLink driver in nix store
local yyyymm="2024-05"
local displaylink_version=600
local ubuntu_version="6.0"
# https://nixos.wiki/wiki/Displaylink
# 6.0 - https://github.com/NixOS/nixpkgs/pull/317292
curl -Lo "$HOME"/Downloads/displaylink-"${displaylink_version}".zip \
https://www.synaptics.com/sites/default/files/exe_files/"${yyyymm}"/DisplayLink%20USB%20Graphics%20Software%20for%20Ubuntu"${ubuntu_version}"-EXE.zip
# TODO: confirm `--name` helps, or need to use `$PWD` in path like "Wiki".
nix-prefetch-url \
file://"$HOME"/Downloads/displaylink-"${displaylink_version}".zip
nix-prefetch-url \
--name displaylink.zip \
file://"$HOME"/Downloads/displaylink-"${displaylink_version}".zip
}
# TODO: only needed during initial install
# --extra-experimental-features nix-command \
# --extra-experimental-features flakes \
build_darwin() {
# Hack to supply real hostname, as friendly hostname "ABC-Macbook-Air" not found.
nix run nix-darwin \
-- build --impure --flake .#"${HOSTNAME%%.*}"
}
build_nixos() {
# update nix-extra reference if first time after install
nix flake update nix-extra
# rebuild configuration per --flake .#${hostname}
nixos-rebuild build --impure --flake .# --show-trace
}
build() { ## Build latest NixOS & home-manager configuration
if [ "$(uname)" == "Darwin" ]; then
echo "darwin detected, building..."
build_darwin
else
echo "nixos detected, building..."
build_nixos
fi
}
diff() { ## Build and diff
build
nvd diff /nix/var/nix/profiles/system/ result/
echo "see also: 'nvd list | grep asahi'"
echo "see also: 'nix-diff /run/current-system ./result'"
}
switch_darwin() {
nix run nix-darwin \
-- switch --impure --flake .#"${HOSTNAME%%.*}"
}
switch_linux() {
# Workaround CVE mitigation issue: https://github.com/NixOS/nixpkgs/pull/173170
sudo git config --global --add safe.directory "${PWD}"
sudo nixos-rebuild switch --impure --flake .#
sudo ./result/activate
sudo /run/current-system/bin/switch-to-configuration boot
echo 'Tree-sitter may have parsers built for previous gcc version and require reinstalling parsers, consider:
rm -rf ~/.local/share/nvim/lazy/nvim-treesitter/parser/*'
# clean
}
switch() { ## Build latest and switch
build
if [ $(uname) == "Darwin" ]; then
echo "darwin detected, switching..."
switch_darwin
else
echo "nixos detected, switching..."
switch_linux
fi
}
install() { ## Install NixOS for the first time
nix-extra
sed -i 's@home/karl/src/nix-extra@home/nixos/src/nix-extra@' flake.nix
nix --extra-experimental-features "nix-command flakes" flake update nix-extra
sudo hostname "$(read -rp 'hostname: ' temp && echo "$temp")"
nixos-rebuild build --flake .#"$(hostname)"
sudo nixos-install --impure --root /mnt/ --flake .#"$(hostname)"
}
clean() { ## Clean old generations
# home manager
nix-collect-garbage -d
# nixos generations
sudo nix-env -p /nix/var/nix/profiles/system --list-generations
sudo nix-collect-garbage -d
sudo nix-env -p /nix/var/nix/profiles/system --list-generations
# remove old entries from boot loader
sudo /run/current-system/bin/switch-to-configuration boot
# optimise store, soon nix.autoOptimise?
nix-store --optimise
}
update() { ## Update packages
nix flake update
build
}
## END Nix
## START Extras
goutils() { ## Install go utils
go install -v golang.org/x/tools/gopls@latest
go install -v mvdan.cc/gofumpt@latest
go install -v golang.org/x/tools/cmd/godoc@latest
go install -v golang.org/x/perf/cmd/benchstat@latest
go install -v honnef.co/go/tools/cmd/staticcheck@latest
go install -v github.com/pressly/goose/v3/cmd/goose@latest # TODO here or elsewhere
}
nvim_deps() { ## Install neovim dependencies with Mason
# grep this repo for comment convention: 'MasonInstall: <app1> <app2>'
apps="$(grep ':MasonInstall' dotfiles/nvim/lua/user/*.lua | cut -d ' ' -f 3- | xargs)"
echo "nvim -c 'MasonInstall ${apps}'"
}
tree-sitter() { ## Clear TreeSitter parsers
echo 'Tree-sitter may have parsers built for previous gcc version and require reinstalling parsers, removing...'
rm -rf ~/.local/share/nvim/lazy/nvim-treesitter/parser/*
}
fmt() { ## Format *.{lua,nix,sh}
fd --extension nix | xargs nixfmt
fd --extension lua | xargs stylua -g ./**/*.lua
fd --extension sh | xargs shfmt -w
}
mikrotik() { ## Backup Mikrotik router config
ssh 192.168.1.1 export terse >../mikrotik_r1_backup_"$(date -Iseconds)".rsc
}
tiny() { ## Tiny ZFS Unlock
local host="192.168.1.5"
ssh -v -p 2222 root@"${host}" "zpool import -a; zfs load-key -a && killall zfs"
}
guestdisk() { ## Create a guest qcow2 file from base qcow2 file <input.img> <output.qcow2>
if [ $# -ne 2 ]; then
echo 1>&2 "Usage: $0 ${FUNCNAME[0]} <arg> <arg>"
exit 3
fi
qemu-img create \
-b "${1}" \
-F qcow2 \
-f qcow2 \
"${2}"
echo "Useful disk size commands:
qemu-img info ubuntu-0.qcow2
qemu-img resize ubuntu-0.qcow2 +18G
"
}
# nix-shell -p cdrkit
userdata() { ## Generate NoCloud user-data cdrom image <directory>
if [ $# -ne 1 ]; then
echo 1>&2 "Usage: $0 ${FUNCNAME[0]} <arg>"
exit 3
fi
genisoimage \
-output "${1}"-cidata.iso \
-V cidata \
-r \
-J "${1}"/meta-data "${1}"/user-data
}
## END Extras
one-arg-that-is-very-long() { ## Example that requires 1 arg <arg>
if [ $# -ne 1 ]; then
echo 1>&2 "Usage: $0 ${FUNCNAME[0]} <arg>"
exit 3
fi
echo "Doing onearg thing with $1"
}
help() { ## Display usage for this application
echo "$0 <task> <args>"
grep -E '^[a-zA-Z_-]+\(\) { ## .*$' "$0" |
sed -E 's/\(\)//' |
sort |
awk 'BEGIN {FS = "{.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $1, $2}'
}
TIMEFORMAT="Task completed in %3lR"
time "${@:-help}"