-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjustfile
More file actions
245 lines (207 loc) · 6.93 KB
/
Copy pathjustfile
File metadata and controls
245 lines (207 loc) · 6.93 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
set unstable
set ignore-comments
# utility recipes
mod utils
sudo_cmd := "run0 --description='gensokyo system manager'"
true := "true"
mkIf(k, v) := if k == true { v } else { "" }
# get help
help recipe="":
#!/usr/bin/env sh
if test "{{recipe}}" = ""; then
just --list
echo
echo 'this justfile heavily utilizes nested `just` invocations,'
echo 'there may be hidden params needed by the underlying recipe.'
echo
echo 'in general, see `just help (general|remote)`.'
else
just --usage {{recipe}}
echo
echo "some recipes may have additional help messages. if a help param exists, try passing --help."
fi
# general nixos-rebuild operations, add -h for help
[arg("op", help="nixos-rebuild operations")]
[arg("system", short='s', long, help="the target system")]
[arg("nom", short='n', long="nom", value="true", help="use nix-output-monitor")]
[arg("sudo", long, value="true", help="use sudo")]
[arg("help", short='h', long, value="true", help="show help message")]
[arg("fast", long, value="true", help="add --no-reexec")]
[arg("args", long, help="args to pass to nixos-rebuild")]
[arg("extra-args", help="more args to pass to nixos-rebuild")]
general op system=`hostname` nom="" sudo="" help="" fast="" args="" *extra-args="":
#!/usr/bin/env sh
set -euo pipefail
if test "{{help}}" = "true" || test {{op}} = "help"; then
just --usage general
echo
echo 'op == ("help" OR any nixos-rebuild ops)'
echo 'system defaults to the current host if not specified'
exit 0
fi
set -x
{{mkIf(sudo, sudo_cmd)}} nixos-rebuild \
{{op}} \
-v -L \
--accept-flake-config \
--flake ".#{{system}}" \
{{args}}\
{{extra-args}}\
{{mkIf(fast, "--no-reexec")}}\
{{mkIf(nom, "--log-format internal-json |& nom --json")}}
# do something to a remote
[arg("op", help="nixos-rebuild operations")]
[arg("system", help="the target system")]
[arg("remote-build", long, value="true", help="build on the remote")]
[arg("args", help="params passed to the general recipe")]
remote op system remote-build="" *args:
just general {{op}} \
--system {{system}} \
--args " \
--target-host {{system}} \
--use-remote-sudo \
{{mkIf(remote-build, f"--build-host {{system}}")}} \
" \
{{args}}
# eval a command on all systems matching a filter
[arg("help", short='h', long, value="true", help="show help message")]
[arg("cmd", help="a command, or `help`")]
[arg("filter", long, help="nix expression. see --help")]
[arg("fail-fast", short='F', long, value="true", help="fail and quit on first error")]
forall filter="true" help="" fail-fast="" +cmd="help":
#!/usr/bin/env sh
set -euo pipefail
expr="configs: \
builtins.map \
(system: system.config.networking.hostName) \
(builtins.filter \
(system: {{filter}}) \
(builtins.attrValues configs) \
)"
if test '{{cmd}}' = "help"; then
just --usage forall
echo
echo '`filter` is a nix expression passed to `builtins.filter`.'
echo 'a `system` parameter is provided, which contains the nixos config as accessed with .#nixosConfigurations.${system}.'
echo '`--impure` is passed to `nix eval`, so things like `builtins.currentSystem` work.'
echo
echo '`cmd` can be any valid bash syntax to be run for all systems. the ${sys} env var is set to the current one.'
exit
fi
echo "evaluating hosts matching filter..."
systems=""
read -r systems < <(nix eval --impure --json \
--apply "${expr}" \
'.#nixosConfigurations' \
| jq '.[]' \
| xargs
)
test "$systems" = "" && exit 1
echo '{{GREEN}}will run the command{{NORMAL}}'
echo ' {{CYAN}}{{cmd}}'
echo
echo '{{GREEN}}for the following systems:'
for system in $systems; do echo " * $system"; done
printf '{{NORMAL}}continue? [Y/n] '
read -r continue
case $continue in
''|y*|Y*) echo '{{GREEN}}continuing...{{NORMAL}}' ;;
*) echo '{{RED}}aborted.{{NORMAL}}'; exit 1 ;;
esac
# status report
stat_success="$(mktemp)"
stat_fail="$(mktemp)"
cleanup() {
echo "{{GREEN}}* success:$(< $stat_success)"
echo "{{RED}}* failure:$(< $stat_fail)"
echo "{{NORMAL}}"
rm $stat_success $stat_fail
}
trap cleanup EXIT ERR
for sys in $systems; do
set +e
{{cmd}}
exit="$?"
test "$exit" -eq 130 && exit 1 # immediate exit on sigint/^C
set -e
if [ "$exit" -ne 0 ]; then
if [ '{{fail-fast}}' = 'true' ]; then
echo "{{RED}} fail-fast requested, terminating{{NORMAL}}"
exit "$exit"
else
printf " ${sys}" >> $stat_fail
fi
else
printf " ${sys}" >> $stat_success
fi
echo "------------------------ >8 -----------------------"
done
# evaluate a system's configuration
[arg("system", short='s', long, help="the target system")]
eval system=`hostname` *args:
nix eval '.#nixosConfigurations.{{system}}.config.system.build.toplevel' {{args}}
# build the configuration for a system
build *args="--nom":
just general "build" {{args}}
# build the configuration for the current system
[default]
_build:
just general "build" --nom -- --keep-going
dix /run/current-system ./result
# build and cache the derivation for a system
cache system *args:
just build -s "{{system}}" {{args}}
niks3 push --pin "gensokyo-{{system}}" ./result
# switch the system to a configuration
switch *args:
just general "switch" {{args}}
# switch but quick, quickswitch, sw
sw:
just general "switch" --fast --sudo
# stage a configuration to a reboot
[arg("remote", short='r', long, value="true")]
stage remote="" *args:
just {{ if remote == true {"remote"} else {"general"} }} "boot" {{args}}
# deploy to a remote machine
deploy *args:
just remote "switch" {{args}}
[arg("boot", short='b', long, value="true", help="run the bootloader as well")]
[arg("run", short='r', long, value="true", help="run the vm after building")]
[arg("system", short='s', long, help="the target system")]
vm system=`hostname` boot="" run="" *args:
just general "build-vm{{mkIf(boot, "-with-bootloader")}}" \
--system {{system}} \
{{args}}
{{mkIf(run, f"./result/bin/run-{{system}}-vm")}}
# common filters
filterCurrentSystem := "system.config.nixpkgs.hostPlatform.system == builtins.currentSystem"
filterNonSensitive := "!system.config.gensokyo.traits.sensitive"
# evaluate all systems
# eval-all: (forall "true" "" "true" "just eval -s ${sys}")
eval-all:
just forall -F 'just eval -s ${sys}'
build-all:
just forall -F --filter '{{filterCurrentSystem}}' 'just build -s ${sys}'
# stage-all:
# deploy-all:
# build and cache systems
cache-all:
# TODO: fix remote building so we can build for everything
just forall --filter '{{filterCurrentSystem}}' 'just cache ${sys}'
# non-nixos-rebuild stuff
# delete old nixos generations and GC the store
[arg("days", help="only delete generations older than this many days")]
gc days="3":
{{sudo_cmd}} nix profile wipe-history \
--profile /nix/var/nix/profiles/system \
--older-than {{days}}d
{{sudo_cmd}} nix store gc -vL
# run flake checks
check:
nix flake check
# diff the current system and the one in ./result
diff:
dix /run/current-system ./result
# update everything in flake.lock and commit
flake-update:
nix flake update --commit-lock-file