-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·420 lines (344 loc) · 10.7 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·420 lines (344 loc) · 10.7 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env bash
set -euo pipefail
TARGET_USER="${1:-OpenClaw}"
NIXOS_DIR="/etc/nixos"
NIX_MODULES_DIR="/etc/nix-openclaw-vm-modules"
SYSTEM_MODULES_DIR="$NIX_MODULES_DIR/systemModules"
HOME_DIR="/home/$TARGET_USER"
HOME_MANAGER_DIR="$HOME_DIR/nix-openclaw-vm-home"
CONFIG_FILE="$NIXOS_DIR/configuration.nix"
HOME_MODULES_FILE="$NIX_MODULES_DIR/home-modules.nix"
export NIX_CONFIG="experimental-features = nix-command flakes"
declare -A CURRENT_PICK_DEFAULTS=()
discover_enable_options() {
local root_dir="$1"
[[ -d "$root_dir" ]] || return 0
grep -RhoE '[[:space:]]*(options\.)?[A-Za-z0-9_.-]+\.enable[[:space:]]*=[[:space:]]*lib\.mkEnableOption' "$root_dir" 2>/dev/null \
| sed -E 's/^[[:space:]]*//; s/[[:space:]]*=.*$//; s/^options\.//' \
| sed -E 's/\.enable$//' \
| sort -u
}
discover_source_defaults() {
local root_dir="$1"
local defaults_map_name="$2"
local exclude_modules_file="${3:-false}"
local file line opt value
local in_option_block=0
local current_option=""
local -n defaults_ref="$defaults_map_name"
[[ -d "$root_dir" ]] || return 0
while IFS= read -r -d '' file; do
while IFS= read -r line; do
if [[ "$line" =~ ^[[:space:]]*([A-Za-z0-9_.-]+)\.enable[[:space:]]*=[[:space:]]*lib\.mkDefault[[:space:]]*(true|false)[[:space:]]*\; ]]; then
opt="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
defaults_ref["$opt"]="$value"
fi
if [[ "$line" =~ ^[[:space:]]*options\.([A-Za-z0-9_.-]+)\.enable[[:space:]]*=[[:space:]]*lib\.mkEnableOption ]]; then
in_option_block=1
current_option="${BASH_REMATCH[1]}"
if [[ "$line" =~ default[[:space:]]*=[[:space:]]*(true|false)[[:space:]]*\; ]]; then
value="${BASH_REMATCH[1]}"
defaults_ref["$current_option"]="$value"
fi
if [[ "$line" == *";" ]]; then
in_option_block=0
current_option=""
fi
continue
fi
if [[ "$in_option_block" -eq 1 ]]; then
if [[ "$line" =~ default[[:space:]]*=[[:space:]]*(true|false)[[:space:]]*\; ]]; then
value="${BASH_REMATCH[1]}"
defaults_ref["$current_option"]="$value"
fi
if [[ "$line" == *"};"* || "$line" == *";"* ]]; then
in_option_block=0
current_option=""
fi
fi
done < "$file"
done < <(
if [[ "$exclude_modules_file" == "true" ]]; then
find "$root_dir" -type f -name '*.nix' ! -name 'modules.nix' ! -name 'local.nix' -print0
else
find "$root_dir" -type f -name '*.nix' -print0
fi
)
}
pick_modules_gum() {
local title="$1"
shift
local options=("$@")
local selected=""
local opt
local gum_args=(
choose
--no-limit
--cursor-prefix '>'
--selected-prefix '[x] '
--unselected-prefix '[ ] '
)
[[ ${#options[@]} -eq 0 ]] && return 0
echo
echo "$title"
for opt in "${options[@]}"; do
if [[ "${CURRENT_PICK_DEFAULTS[$opt]:-false}" == "true" ]]; then
gum_args+=(--selected "$opt")
fi
done
selected="$(gum "${gum_args[@]}" "${options[@]}" || true)"
printf '%s\n' "$selected" | sed '/^$/d'
}
pick_modules_whiptail() {
local title="$1"
local prompt="$2"
shift 2
local options=("$@")
local items=()
local opt status current
local out=""
[[ ${#options[@]} -eq 0 ]] && return 0
for opt in "${options[@]}"; do
current="${CURRENT_PICK_DEFAULTS[$opt]:-false}"
status="OFF"
[[ "$current" == "true" ]] && status="ON"
items+=("$opt" "" "$status")
done
out="$(whiptail --title "$title" --checklist "$prompt" 22 90 14 "${items[@]}" 3>&1 1>&2 2>&3 || true)"
out="${out//\"/}"
printf '%s\n' "$out" | tr ' ' '\n' | sed '/^$/d'
}
pick_modules_native() {
local title="$1"
shift
local options=("$@")
local total="${#options[@]}"
local cursor=0
local key key2 key3 i opt mark pointer
local -a selected=()
local tty="/dev/tty"
if [[ ! -r "$tty" || ! -w "$tty" ]]; then
echo "Native TUI requires an interactive terminal (/dev/tty unavailable)." >&2
exit 1
fi
[[ "$total" -eq 0 ]] && return 0
for ((i = 0; i < total; i++)); do
opt="${options[$i]}"
if [[ "${CURRENT_PICK_DEFAULTS[$opt]:-false}" == "true" ]]; then
selected[$i]=1
else
selected[$i]=0
fi
done
while true; do
clear > "$tty"
echo "$title" > "$tty"
echo "Use Up/Down to move, Space to toggle, Enter to submit." > "$tty"
echo > "$tty"
for ((i = 0; i < total; i++)); do
if [[ "${selected[$i]}" -eq 1 ]]; then
mark="[x]"
else
mark="[ ]"
fi
if [[ "$i" -eq "$cursor" ]]; then
pointer=">"
else
pointer=" "
fi
printf "%s %s %s\n" "$pointer" "$mark" "${options[$i]}" > "$tty"
done
IFS= read -rsn1 key < "$tty"
if [[ -z "$key" ]]; then
break
elif [[ "$key" == " " ]]; then
if [[ "${selected[$cursor]}" -eq 1 ]]; then
selected[$cursor]=0
else
selected[$cursor]=1
fi
elif [[ "$key" == $'\x1b' ]]; then
IFS= read -rsn1 key2 < "$tty"
IFS= read -rsn1 key3 < "$tty"
if [[ "$key2" == "[" ]]; then
case "$key3" in
A)
((cursor--))
if ((cursor < 0)); then
cursor=$((total - 1))
fi
;;
B)
((cursor++))
if ((cursor >= total)); then
cursor=0
fi
;;
esac
fi
fi
done
clear > "$tty"
for ((i = 0; i < total; i++)); do
if [[ "${selected[$i]}" -eq 1 ]]; then
printf '%s\n' "${options[$i]}"
fi
done
}
pick_modules() {
local title="$1"
local prompt="$2"
local defaults_map_name="$3"
shift 3
local options=("$@")
local opt
local -n defaults_ref="$defaults_map_name"
CURRENT_PICK_DEFAULTS=()
for opt in "${options[@]}"; do
CURRENT_PICK_DEFAULTS["$opt"]="${defaults_ref[$opt]:-false}"
done
if command -v gum >/dev/null 2>&1; then
pick_modules_gum "$title (toggle with space, confirm with enter)" "${options[@]}"
return 0
fi
if command -v whiptail >/dev/null 2>&1; then
pick_modules_whiptail "$title" "$prompt" "${options[@]}"
return 0
fi
pick_modules_native "$title" "${options[@]}"
}
build_bool_map() {
local selected_lines="$1"
shift
local options=("$@")
local out=""
local opt val
for opt in "${options[@]}"; do
val="false"
if grep -Fxq "$opt" <<< "$selected_lines"; then
val="true"
fi
out+="$opt=$val"$'\n'
done
printf '%s' "$out"
}
get_state_version() {
local cfg="$1"
local line version
if [[ -f "$cfg" ]]; then
line="$(grep -E '^[[:space:]]*system\.stateVersion[[:space:]]*=' "$cfg" | head -n 1 || true)"
if [[ "$line" =~ \"([0-9]{2}\.[0-9]{2})\" ]]; then
version="${BASH_REMATCH[1]}"
printf '%s\n' "$version"
return 0
fi
fi
if command -v nixos-version >/dev/null 2>&1; then
line="$(nixos-version || true)"
if [[ "$line" =~ ([0-9]{2}\.[0-9]{2}) ]]; then
printf '%s\n' "${BASH_REMATCH[1]}"
return 0
fi
fi
printf '25.05\n'
}
render_system_config() {
local state_version="$1"
local bool_map="$2"
local line key value
cat <<EOF
{ config, pkgs, ... }:
{
imports = [
./hardware-configuration.nix
];
system.stateVersion = "$state_version";
EOF
while IFS= read -r line; do
[[ -z "$line" ]] && continue
key="${line%%=*}"
value="${line##*=}"
printf ' %s.enable = %s;\n' "$key" "$value"
done <<< "$bool_map"
cat <<'EOF'
}
EOF
}
render_home_modules() {
local bool_map="$1"
local line key value
cat <<'EOF'
{
EOF
while IFS= read -r line; do
[[ -z "$line" ]] && continue
key="${line%%=*}"
value="${line##*=}"
printf ' %s.enable = %s;\n' "$key" "$value"
done <<< "$bool_map"
cat <<'EOF'
}
EOF
}
generate_system_config_content() {
local module_root="$1"
local system_cfg_path="$2"
local state_version selected bool_map
local -a system_options=()
local -A system_defaults=()
mapfile -t system_options < <(discover_enable_options "$module_root")
discover_source_defaults "$module_root" system_defaults false
if [[ ${#system_options[@]} -eq 0 ]]; then
echo "Warning: no system enable options discovered in $module_root" >&2
fi
selected="$(pick_modules "System Modules" "Select system modules to enable" system_defaults "${system_options[@]}")"
bool_map="$(build_bool_map "$selected" "${system_options[@]}")"
state_version="$(get_state_version "$system_cfg_path")"
render_system_config "$state_version" "$bool_map"
}
generate_home_modules_content() {
local home_root="$1"
local selected bool_map
local -a home_options=()
local -A home_defaults=()
mapfile -t home_options < <(discover_enable_options "$home_root")
discover_source_defaults "$home_root" home_defaults true
if [[ ${#home_options[@]} -eq 0 ]]; then
echo "Warning: no home enable options discovered in $home_root" >&2
fi
selected="$(pick_modules "Home Manager Modules" "Select home modules to enable" home_defaults "${home_options[@]}")"
bool_map="$(build_bool_map "$selected" "${home_options[@]}")"
render_home_modules "$bool_map"
}
main() {
local SYSTEM_CONFIG_CONTENT HOME_MODULES_CONTENT
sudo rm -rf "$NIX_MODULES_DIR"
sudo mkdir -p "$NIX_MODULES_DIR"
echo Downloading system config...
SYSTEM_TAR="$(mktemp)"
curl --fail --location https://github.com/gusjengis/nix-openclaw-vm-modules/archive/refs/heads/main.tar.gz -o "$SYSTEM_TAR"
sudo tar -xzf "$SYSTEM_TAR" --strip-components=1 -C "$NIX_MODULES_DIR"
rm -f "$SYSTEM_TAR"
echo Generating system module selections...
SYSTEM_CONFIG_CONTENT="$(generate_system_config_content "$SYSTEM_MODULES_DIR" "$CONFIG_FILE")"
printf '%s\n' "$SYSTEM_CONFIG_CONTENT" > "$CONFIG_FILE"
echo Installing system config...
sudo env NIX_CONFIG="experimental-features = nix-command flakes" nixos-rebuild switch --impure --flake /etc/nix-modules/nixosModules/
echo Replacing temporary system config with git clone...
sudo rm -rf "$NIX_MODULES_DIR"
sudo git clone https://github.com/gusjengis/nix-openclaw-vm-modules.git "$NIX_MODULES_DIR"
sudo printf '%s\n' "$SYSTEM_CONFIG_CONTENT" > "$CONFIG_FILE"
rm -rf "$HOME_MANAGER_DIR"
mkdir -p "$HOME_MANAGER_DIR"
echo Downloading home config...
git clone https://github.com/gusjengis/nx-openclaw-vm-home.git "$HOME_MANAGER_DIR"
echo Generating home-manager module selections...
HOME_MODULES_CONTENT="$(generate_home_modules_content "$HOME_MANAGER_DIR")"
printf '%s\n' "$HOME_MODULES_CONTENT" > "$HOME_MODULES_FILE"
TARGET_GROUP="$(id -gn "$TARGET_USER")"
sudo chown -R "$TARGET_USER:$TARGET_GROUP" "$HOME_DIR"
echo Installing home config...
sudo -u "$TARGET_USER" env NIX_CONFIG="experimental-features = nix-command flakes" home-manager switch --impure --flake "$HOME_MANAGER_DIR/"
}
main "$@"