-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.sh
More file actions
232 lines (203 loc) · 5.56 KB
/
Copy pathbuild.sh
File metadata and controls
232 lines (203 loc) · 5.56 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
#!/usr/bin/env bash
export NIXPKGS_ALLOW_UNFREE=1
ICEDOS_DIR="/tmp/icedos"
CONFIG="$ICEDOS_DIR/configuration-location"
FLAKE="flake.nix"
cd "$(dirname "$(readlink -f "$0")")"
action="switch"
globalBuildArgs=()
nhBuildArgs=()
set -e
set -o pipefail
previous_arguments=("$@")
while [[ $# -gt 0 ]]; do
case $1 in
--boot)
action="boot"
shift
;;
--build)
action="build"
shift
;;
--build-vm)
action="build-vm"
shift
;;
--run-vm)
action="build-vm"
run_vm=1
shift
;;
--export-full-config)
export_full_config=1
shift
;;
--genflake-only)
genflake_only=1
shift
;;
--update)
update_all="1"
update_core="1"
update_nixpkgs="1"
update_repos="1"
update_repos_inputs="1"
shift
;;
--update-core)
update_core="1"
shift
;;
--update-nixpkgs)
update_nixpkgs="1"
shift
;;
--update-repos)
update_repos="1"
shift
;;
--update-repos-inputs)
update_repos_inputs="1"
shift
;;
--ask)
nhBuildArgs+=("-a")
shift
;;
--builder)
nhBuildArgs+=("--build-host")
nhBuildArgs+=("$2")
shift 2
;;
--target)
nhBuildArgs+=("--target-host")
nhBuildArgs+=("$2")
shift 2
;;
--nh-args)
shift
while [[ $# -gt 0 && "$1" != "--build-args" ]]; do
nhBuildArgs+=("$1")
shift
done
;;
--build-args)
shift
globalBuildArgs=("$@")
break
;;
--logs)
export ICEDOS_LOGGING=1
trace="--show-trace"
shift
;;
*)
echo "Unknown arg: $1" >&2
exit 1
;;
esac
done
export NIX_CONFIG="experimental-features = flakes nix-command pipe-operators"
# Refresh every `type: "path"` input in config/flake.lock so a local-
# core override (inputs.icedos.url = "path:...") lands on every plain
# rebuild without requiring --update-core. github / git inputs stay
# pinned. Skipped when --update-core is set since the block below
# does a full --refresh on all inputs anyway.
if [ "$update_core" != "1" ] \
&& [ -n "$ICEDOS_CONFIG_ROOT" ] \
&& [ -f "$ICEDOS_CONFIG_ROOT/flake.lock" ]; then
(
set -e
cd "$ICEDOS_CONFIG_ROOT"
for input in $(jq -r '.nodes | to_entries[] | select(.value.locked.type == "path") | .key' flake.lock 2>/dev/null); do
nix flake update "$input" 2>/dev/null || true
done
)
fi
if [[ "$update_core" == "1" && -z "$skip_update_core" ]]; then
cd "$ICEDOS_CONFIG_ROOT"
nix flake update --refresh
exec env skip_update_core=1 nix run path:. -- "${previous_arguments[@]}"
exit 0
fi
mkdir -p "$ICEDOS_DIR"
# Save current directory into a file
[ -f "$CONFIG" ] && rm -f "$CONFIG" || sudo rm -rf "$CONFIG"
printf '%s' "$ICEDOS_STATE_DIR" > "$CONFIG"
if [ "$update_repos" == "1" ]; then
refresh="--refresh"
fi
export ICEDOS_BUILD_DIR="$(mktemp -d -t icedos-build-XXXXXXX-0)"
mkdir -p "$ICEDOS_BUILD_DIR"
# Generate flake
ICEDOS_UPDATE="$update_repos" ICEDOS_STAGE="genflake" nix eval $refresh $trace --file "$ICEDOS_ROOT/lib/genflake.nix" --raw flakeFinal >"$ICEDOS_STATE_DIR/$FLAKE"
nixfmt "$ICEDOS_STATE_DIR/$FLAKE"
(
set -e
cd "$ICEDOS_STATE_DIR"
if [ ! -f flake.lock ] || [ -n "$update_core$update_nixpkgs$update_repos$update_repos_inputs" ]; then
nix flake prefetch-inputs
fi
# Refresh every `type: "path"` input on each build so local sibling-
# repo edits (e.g. overrideUrl = "path:..." in config.toml) land
# without requiring --update-repos. github / git inputs stay pinned
# to their lock entries so we don't pay a network roundtrip per
# rebuild.
for input in $(jq -r '.nodes | to_entries[] | select(.value.locked.type == "path") | .key' flake.lock 2>/dev/null); do
nix flake update "$input" 2>/dev/null || true
done
[ "$update_core" == "1" ] && nix flake update icedos-core --refresh 2>/dev/null || true
)
if [ "$update_all" == "1" ]; then
(
set -e
cd "$ICEDOS_STATE_DIR"
nix flake update --refresh
)
elif [ "$update_repos_inputs" == "1" ]; then
(
set -e
cd "$ICEDOS_STATE_DIR"
for input in $(jq -r '
.nodes.root.inputs
| to_entries[]
| select(.value | type == "string")
| .key
| select(startswith("icedos-"))
' flake.lock 2>/dev/null); do
nix flake update "$input" --refresh 2>/dev/null || true
done
)
fi
# Stop after the flake (and its lock) have been generated, without
# building anything. Lets callers evaluate the generated flake (e.g. to
# query per-package output paths) without realising the system closure.
if [ "$genflake_only" == "1" ]; then
exit 0
fi
if [ "$export_full_config" == "1" ]; then
(
cd "$ICEDOS_STATE_DIR"
ICEDOS_STAGE="genflake" nix eval $trace --file "$ICEDOS_ROOT/lib/genflake.nix" evaluatedConfig | nixfmt | jq -r . > .cache/full-config.json
jsonfmt .cache/full-config.json -w
toml2json "$ICEDOS_CONFIG_ROOT/config.toml" > .cache/config.json
jsonfmt .cache/config.json -w
)
exit 0
fi
[ "$update_nixpkgs" == "1" ] && [ "$update_all" != "1" ] && (
set -e
cd "$ICEDOS_STATE_DIR"
nix flake update nixpkgs
)
rsync -a --exclude=".cache" "$ICEDOS_STATE_DIR/" "$ICEDOS_BUILD_DIR"
echo "building from path $ICEDOS_BUILD_DIR..."
cd $ICEDOS_BUILD_DIR
nh os $action --no-update-lock-file path:. "${nhBuildArgs[@]}" --hostname "$(cat /etc/hostname)" -- $trace "${globalBuildArgs[@]}"
if [[ "$action" == "build-vm" ]]; then
echo "VM configuration stored in $PWD/result"
fi
if [ "$run_vm" == "1" ]; then
exec "result/bin/run-$(cat /etc/hostname)-vm"
fi