Skip to content

Commit

Permalink
wip: extend terraform module to support extra_files_script
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 committed Sep 5, 2023
1 parent 50b53e6 commit 9661aef
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions terraform/all-in-one/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module "install" {
ssh_private_key = var.install_ssh_key
debug_logging = var.debug_logging
stop_after_disko = var.stop_after_disko
extra_files_script = var.extra_files_script
instance_id = var.instance_id
}

Expand Down
6 changes: 6 additions & 0 deletions terraform/all-in-one/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,9 @@ variable "stop_after_disko" {
description = "Exit after disko formatting"
default = false
}

variable "extra_files_script" {
type = string
description = "A script that prepares extra files to be copied to the target host during installation. The script expected to write all its files to the current directory. This directory is rsynced to the target host during installation to the / directory."
default = null
}
14 changes: 9 additions & 5 deletions terraform/install/main.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
locals {
nixos_anywhere_flags = "${var.stop_after_disko ? "--stop-after-disko" : ""} ${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}"
}

resource "null_resource" "nixos-remote" {
triggers = {
instance_id = var.instance_id
}
provisioner "local-exec" {
environment = {
SSH_PRIVATE_KEY = var.ssh_private_key
stop_after_disko = var.stop_after_disko
debug_logging = var.debug_logging
kexec_tarball_url = var.kexec_tarball_url
nixos_partitioner = var.nixos_partitioner
nixos_system = var.nixos_system
target_user = var.target_user
target_host = var.target_host
extra_files_script = var.extra_files_script
}
command = "nix run --extra-experimental-features 'nix-command flakes' path:${path.module}/../..#nixos-anywhere -- ${local.nixos_anywhere_flags}"
command = "bash run-nixos-anywhere.sh"
quiet = var.debug_logging
}
}
31 changes: 31 additions & 0 deletions terraform/install/run-nixos-anywhere.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# shellcheck shell=bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
args=()

if [[ ${debug_logging-} == "true" ]]; then
set -x
args+=("--debug")
fi
if [[ ${stop_after_disko-} == "true" ]]; then
args+=("--stop-after-disko")
fi
if [[ ${kexec_tarball_url-} != "" ]]; then
args+=("--kexec" "${kexec_tarball_url}")
fi
args+=("--store-paths" "${nixos_partitioner} ${nixos_system}")
if [[ ${extra_files_script-} != "" ]]; then
tmpdir=$(mktemp -d)
cleanup() {
rm -rf "${tmpdir}"
}
trap cleanup EXIT
pushd "${tmpdir}"
${extra_files_script}
popd
args+=("--extra-files" "${tmpdir}")
fi
args+=("${target_user}@${target_host}")

nix run --extra-experimental-features 'nix-command flakes' "path:${SCRIPT_DIR}/../..#nixos-anywhere" -- "${args[@]}"
6 changes: 6 additions & 0 deletions terraform/install/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ variable "stop_after_disko" {
description = "Exit after disko formatting"
default = false
}

variable "extra_files_script" {
type = string
description = "A script that prepares extra files to be copied to the target host during installation. The script expected to write all its files to the current directory. This directory is rsynced to the target host during installation to the / directory."
default = null
}

0 comments on commit 9661aef

Please sign in to comment.