Skip to content

Commit ff743f3

Browse files
bors[bot]Mic92
andauthored
Merge #72
72: Terraform module fixes r=Mic92 a=Mic92 Co-authored-by: Jörg Thalheim <joerg@thalheim.io>
2 parents a1d93f9 + ed67349 commit ff743f3

File tree

7 files changed

+48
-7
lines changed

7 files changed

+48
-7
lines changed

src/nixos-anywhere.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ if [[ -n ${extra_files-} ]]; then
308308
if [[ -d $extra_files ]]; then
309309
extra_files="$extra_files/"
310310
fi
311-
rsync -rlpv -FF -e "ssh -i \"$ssh_key_dir\"/nixos-remote -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" "$extra_files" "${ssh_connection}:/mnt/"
311+
rsync -rlpv -FF -e "ssh -i \"$ssh_key_dir\"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" "$extra_files" "${ssh_connection}:/mnt/"
312312
fi
313313

314314
ssh_ <<SSH

terraform/all-in-one/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module "install" {
1919
nixos_partitioner = module.partitioner-build.result.out
2020
nixos_system = module.system-build.result.out
2121
ssh_private_key = var.ssh_private_key
22+
debug_logging = var.debug_logging
23+
instance_id = var.instance_id
2224
}
2325

2426
module "nixos-rebuild" {

terraform/all-in-one/variables.tf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ variable "kexec_tarball_url" {
77
# To make this re-usuable we maybe should accept a store path here?
88
variable "nixos_partitioner_attr" {
99
type = string
10-
description = "nixos partitioner and mount script"
10+
description = "Nixos partitioner and mount script i.e. your-flake#nixosConfigurations.your-evaluated-nixos.config.system.build.diskoNoDeps or just your-evaluated.config.system.build.diskNoDeps. `config.system.build.diskNoDeps` is provided by the disko nixos module"
1111
}
1212

1313
# To make this re-usuable we maybe should accept a store path here?
1414
variable "nixos_system_attr" {
1515
type = string
16-
description = "The nixos system to deploy"
16+
description = "The nixos system to deploy i.e. your-flake#nixosConfigurations.your-evaluated-nixos.config.system.build.toplevel or just your-evaluated-nixos.config.system.build.toplevel if you are not using flakes"
1717
}
1818

1919
variable "file" {
2020
type = string
21-
description = "file to get the nixos_system_attr and nixos_partitioner_attr from if they are not flakes."
21+
description = "Nix file containing the nixos_system_attr and nixos_partitioner_attr. Use this if you are not using flake"
2222
default = null
2323
}
2424

@@ -39,8 +39,20 @@ variable "target_port" {
3939
default = 22
4040
}
4141

42+
variable "instance_id" {
43+
type = string
44+
description = "The instance id of the target_host, used to track when to reinstall the machine"
45+
default = null
46+
}
47+
4248
variable "ssh_private_key" {
4349
type = string
4450
description = "Content of private key used to connect to the target_host"
4551
default = null
4652
}
53+
54+
variable "debug_logging" {
55+
type = bool
56+
description = "Enable debug logging"
57+
default = false
58+
}

terraform/install/main.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
locals {
2+
nixos_anywhere_flags = "${var.debug_logging ? "--debug" : ""} ${var.kexec_tarball_url != null ? "--kexec ${var.kexec_tarball_url}" : "" } --store-paths ${var.nixos_partitioner} ${var.nixos_system} ${var.target_user}@${var.target_host}"
3+
}
4+
15
resource "null_resource" "nixos-remote" {
6+
triggers = {
7+
instance_id = var.instance_id
8+
}
29
provisioner "local-exec" {
310
environment = {
411
SSH_PRIVATE_KEY = var.ssh_private_key
512
}
6-
command = "nix run ${path.module}#nixos-remote -- --store-paths ${var.nixos_partitioner} ${var.nixos_system} ${var.target_user}@${var.target_host}"
13+
command = "nix run --extra-experimental-features 'nix-command flakes' ${path.module}#nixos-anywhere -- ${local.nixos_anywhere_flags}"
714
}
815
}

terraform/install/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ variable "ssh_private_key" {
3838
description = "Content of private key used to connect to the target_host"
3939
default = ""
4040
}
41+
42+
variable "instance_id" {
43+
type = string
44+
description = "The instance id of the target_host, used to track when to reinstall the machine"
45+
default = null
46+
}
47+
48+
variable "debug_logging" {
49+
type = bool
50+
description = "Enable debug logging"
51+
default = false
52+
}

terraform/nix-build/nix-build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -efu
33

44
declare file attribute
55
eval "$(jq -r '@sh "attribute=\(.attribute) file=\(.file)"')"
6-
if [[ -e ${file+x} ]]; then
6+
if [[ -n ${file-} ]] && [[ -e ${file-} ]]; then
77
out=$(nix build --no-link --json -f "$file" "$attribute")
88
printf '%s' "$out" | jq -c '.[].outputs'
99
else

terraform/nixos-rebuild/deploy.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ if [[ -n ${SSH_KEY+x} && ${SSH_KEY} != "-" ]]; then
2828
sshOpts+=(-o "IdentityFile=${sshPrivateKeyFile}")
2929
fi
3030

31-
NIX_SSHOPTS="${sshOpts[*]}" retry -t 10 -d 10 -- nix copy -s --experimental-features nix-command --to "ssh://$TARGET_HOST" "$NIXOS_SYSTEM"
31+
try=1
32+
until NIX_SSHOPTS="${sshOpts[*]}" nix copy -s --experimental-features nix-command --to "ssh://$TARGET_HOST" "$NIXOS_SYSTEM"; do
33+
if [[ $try -gt 10 ]]; then
34+
echo "retries exhausted" >&2
35+
exit 1
36+
fi
37+
sleep 10
38+
try=$((try + 1))
39+
done
3240

3341
# shellcheck disable=SC2029
3442
ssh "${sshOpts[@]}" "$TARGET_HOST" "nix-env -p /nix/var/nix/profiles/system --set $(printf "%q" "$NIXOS_SYSTEM"); /nix/var/nix/profiles/system/bin/switch-to-configuration switch" || :

0 commit comments

Comments
 (0)