-
Notifications
You must be signed in to change notification settings - Fork 0
/
justfile
327 lines (277 loc) · 9.64 KB
/
justfile
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
# This is a jusfile for the nix-config.
# Sections are separated by ## and recipes are documented with a single #
# on lines preceding the recipe.
## nix
## secrets
## CI/CD
# Default command when 'just' is run without arguments
# Run 'just <command>' to execute a command.
default: help
# Display help
help:
@printf "\nRun 'just -n <command>' to print what would be executed...\n\n"
@just --list --unsorted
@printf "\n...by running 'just <command>'.\n"
@printf "This message is printed by 'just help' and just 'just'.\n"
## nix
# Print nix flake inputs and outputs
[group('nix')]
io:
nix flake metadata
nix flake show
# Lint nix files
[group('nix')]
lint:
nix fmt
# Manually enter dev shell
[group('nix')]
dev:
nix develop
# Remove build output link (no garbage collection)
[group('nix')]
clean:
rm -f ./result
# Build nix flake
[group('nix')]
build profile: lint check
nix build --json --no-link --print-build-logs ".#{{ profile }}"
# Check nix flake
[group('nix')]
check:
nix flake check
# Run nix flake to execute `nix run .#activate` for the current host.
[group('nix')]
switch:
nix run
# Run nix flake to execute `nix run .#activate-home` for the current user.
[group('nix')]
switch-home:
nix run .#activate-home
# https://discourse.nixos.org/t/sudo-run-current-system-sw-bin-sudo-must-be-owned-by-uid-0-and-have-the-setuid-bit-set-and-cannot-chdir-var-cron-bailing-out-var-cron-permission-denied/20463
# sudo: /run/current-system/sw/bin/sudo must be owned by uid 0 and have the setuid bit set
# Run nix flake with explicit use of the sudo in `/run/wrappers`
[group('nix')]
switch-wrapper:
/run/wrappers/bin/sudo nix run
# Shell with bootstrap dependencies
[group('nix')]
bootstrap-shell:
nix \
--extra-experimental-features "nix-command flakes" \
shell \
"nixpkgs#git" \
"nixpkgs#just"
# nix run home-manager -- build --flake ".#{{ profile }}"
# Bootstrap build home-manager with flake
[group('nix-home-manager')]
home-manager-bootstrap-build profile="aarch64-linux":
nix \
--extra-experimental-features "nix-command flakes" \
run home-manager -- build \
--extra-experimental-features "nix-command flakes" \
--flake ".#{{ profile }}" \
--show-trace \
--print-build-logs
# nix run home-manager -- switch --flake ".#{{ profile }}"
# Bootstrap switch home-manager with flake
[group('nix-home-manager')]
home-manager-bootstrap-switch profile="aarch64-linux":
nix \
--extra-experimental-features "nix-command flakes" \
run home-manager -- switch \
--extra-experimental-features "nix-command flakes" \
--flake ".#{{ profile }}" \
--show-trace \
--print-build-logs
# Build home-manager with flake
[group('nix-home-manager')]
home-manager-build profile="aarch64-linux":
home-manager build --flake ".#{{ profile }}"
# Switch home-manager with flake
[group('nix-home-manager')]
home-manager-switch profile="aarch64-linux":
home-manager switch --flake ".#{{ profile }}"
# Bootstrap nix-darwin with flake
[group('nix-darwin')]
darwin-bootstrap profile="aarch64":
nix run nix-darwin -- switch --flake ".#{{ profile }}"
# Build darwin from flake
[group('nix-darwin')]
darwin-build profile="aarch64":
just build "darwinConfigurations.{{ profile }}.config.system.build.toplevel"
# Switch darwin from flake
[group('nix-darwin')]
darwin-switch profile="aarch64":
darwin-rebuild switch --flake ".#{{ profile }}"
# Test darwin from flake
[group('nix-darwin')]
darwin-test profile="aarch64":
darwin-rebuild check --flake ".#{{ profile }}"
# Bootstrap nixos
[group('nixos')]
nixos-bootstrap destination username publickey:
ssh \
-o PubkeyAuthentication=no \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
{{destination}} " \
parted /dev/nvme0n1 -- mklabel gpt; \
parted /dev/nvme0n1 -- mkpart primary 512MiB -8GiB; \
parted /dev/nvme0n1 -- mkpart primary linux-swap -8GiB 100\%; \
parted /dev/nvme0n1 -- mkpart ESP fat32 1MiB 512MiB; \
parted /dev/nvme0n1 -- set 3 esp on; \
sleep 1; \
mkfs.ext4 -L nixos /dev/nvme0n1p1; \
mkswap -L swap /dev/nvme0n1p2; \
mkfs.fat -F 32 -n boot /dev/nvme0n1p3; \
sleep 1; \
mount /dev/disk/by-label/nixos /mnt; \
mkdir -p /mnt/boot; \
mount /dev/disk/by-label/boot /mnt/boot; \
nixos-generate-config --root /mnt; \
sed --in-place '/system\.stateVersion = .*/a \
nix.extraOptions = \"experimental-features = nix-command flakes\";\n \
security.sudo.enable = true;\n \
security.sudo.wheelNeedsPassword = false;\n \
services.openssh.enable = true;\n \
services.openssh.settings.PasswordAuthentication = false;\n \
services.openssh.settings.PermitRootLogin = \"no\";\n \
users.mutableUsers = false;\n \
users.users.{{username}}.extraGroups = [ \"wheel\" ];\n \
users.users.{{username}}.initialPassword = \"{{username}}\";\n \
users.users.{{username}}.home = \"/home/{{username}}\";\n \
users.users.{{username}}.isNormalUser = true;\n \
users.users.{{username}}.openssh.authorizedKeys.keys = [ \"{{publickey}}\" ];\n \
' /mnt/etc/nixos/configuration.nix; \
nixos-install --no-root-passwd; \
reboot;"
# Copy flake to VM
[group('nixos')]
nixos-vm-sync user destination:
rsync -avz \
--exclude='.direnv' \
--exclude='result' \
. \
{{ user }}@{{ destination }}:~/nix-config
# Build nixos from flake
[group('nixos')]
nixos-build profile="aarch64":
just build "nixosConfigurations.{{ profile }}.config.system.build.toplevel"
# Test nixos from flake
[group('nixos')]
nixos-test profile="aarch64":
nixos-rebuild test --flake ".#{{ profile }}"
# Switch nixos from flake
[group('nixos')]
nixos-switch profile="aarch64":
nixos-rebuild switch --flake ".#{{ profile }}"
# Update nix flake
[group('nix')]
update:
nix flake update
## secrets
# Define the project variable
gcp_project_id := env_var_or_default('GCP_PROJECT_ID', 'development')
# Show existing secrets
[group('secrets')]
show:
@teller show
# Create a secret with the given name
[group('secrets')]
create-secret name:
@gcloud secrets create {{name}} --replication-policy="automatic" --project {{gcp_project_id}}
# Populate a single secret with the contents of a dotenv-formatted file
[group('secrets')]
populate-single-secret name path:
@gcloud secrets versions add {{name}} --data-file={{path}} --project {{gcp_project_id}}
# Populate each line of a dotenv-formatted file as a separate secret
[group('secrets')]
populate-separate-secrets path:
@while IFS= read -r line; do \
KEY=$(echo $line | cut -d '=' -f 1); \
VALUE=$(echo $line | cut -d '=' -f 2); \
gcloud secrets create $KEY --replication-policy="automatic" --project {{gcp_project_id}} 2>/dev/null; \
printf "$VALUE" | gcloud secrets versions add $KEY --data-file=- --project {{gcp_project_id}}; \
done < {{path}}
# Complete process: Create a secret and populate it with the entire contents of a dotenv file
[group('secrets')]
create-and-populate-single-secret name path:
@just create-secret {{name}}
@just populate-single-secret {{name}} {{path}}
# Complete process: Create and populate separate secrets for each line in the dotenv file
[group('secrets')]
create-and-populate-separate-secrets path:
@just populate-separate-secrets {{path}}
# Retrieve the contents of a given secret
[group('secrets')]
get-secret name:
@gcloud secrets versions access latest --secret={{name}} --project={{gcp_project_id}}
# Create empty dotenv from template
[group('secrets')]
seed-dotenv:
@cp .template.env .env
# Export unique secrets to dotenv format
[group('secrets')]
export:
@teller export env | sort | uniq | grep -v '^$' > .secrets.env
# Check secrets are available in teller shell.
[group('secrets')]
check-secrets:
@printf "Check teller environment for secrets\n\n"
@teller run -s -- env | grep -E 'GITHUB|CACHIX' | teller redact
# Save KUBECONFIG to file
[group('secrets')]
get-kubeconfig:
@teller run -s -- printenv KUBECONFIG > kubeconfig.yaml
## CI/CD
# Update github secrets for repo from environment variables
[group('CI/CD')]
ghsecrets repo="cameronraysmith/nix-config":
@echo "secrets before updates:"
@echo
PAGER=cat gh secret list --repo={{ repo }}
@echo
eval "$(teller sh)" && \
gh secret set CACHIX_AUTH_TOKEN --repo={{ repo }} --body="$CACHIX_AUTH_TOKEN"
@echo
@echo secrets after updates:
@echo
PAGER=cat gh secret list --repo={{ repo }}
# List available workflows and associated jobs.
[group('CI/CD')]
list-workflows:
@act -l
# Execute flake.yaml workflow.
[group('CI/CD')]
test-flake-workflow:
@teller run -s -- \
act workflow_dispatch \
-W '.github/workflows/ci.yaml' \
-j nixci \
-s GITHUB_TOKEN -s CACHIX_AUTH_TOKEN \
--matrix os:ubuntu-latest \
--container-architecture linux/amd64
# Command to run sethvargo/ratchet to pin GitHub Actions workflows version tags to commit hashes
# If not installed, you can use docker to run the command
# ratchet_base := "docker run -it --rm -v \"${PWD}:${PWD}\" -w \"${PWD}\" ghcr.io/sethvargo/ratchet:0.9.2"
ratchet_base := "ratchet"
# List of GitHub Actions workflows
gha_workflows := "./.github/workflows/flake.yaml"
# Pin all workflow versions to hash values (requires Docker)
[group('CI/CD')]
ratchet-pin:
@for workflow in {{gha_workflows}}; do \
eval "{{ratchet_base}} pin $workflow"; \
done
# Unpin hashed workflow versions to semantic values (requires Docker)
[group('CI/CD')]
ratchet-unpin:
@for workflow in {{gha_workflows}}; do \
eval "{{ratchet_base}} unpin $workflow"; \
done
# Update GitHub Actions workflows to the latest version (requires Docker)
[group('CI/CD')]
ratchet-update:
@for workflow in {{gha_workflows}}; do \
eval "{{ratchet_base}} update $workflow"; \
done