Skip to content

Commit acee95f

Browse files
committed
also support sudo password in terraform
1 parent 7a178fa commit acee95f

File tree

9 files changed

+57
-5
lines changed

9 files changed

+57
-5
lines changed

docs/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Options:
2121
* --env-password
2222
set a password used by ssh-copy-id, the password should be set by
2323
the environment variable SSHPASS. Additionally, sudo password can be set
24-
via SUDO_PASSWORD environment variable for remote sudo operations
24+
via SUDO_PASSWORD environment variable for remote sudo operations
2525
(only supported with sudo, not doas)
2626
* -s, --store-paths <disko-script> <nixos-system>
2727
set the store paths to the disko-script and nixos-system directly

docs/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Options:
4242
* --env-password
4343
set a password used by ssh-copy-id, the password should be set by
4444
the environment variable SSHPASS. Additionally, sudo password can be set
45-
via SUDO_PASSWORD environment variable for remote sudo operations
45+
via SUDO_PASSWORD environment variable for remote sudo operations
4646
(only supported with sudo, not doas)
4747
* -s, --store-paths <disko-script> <nixos-system>
4848
set the store paths to the disko-script and nixos-system directly

terraform/all-in-one.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ module "deploy" {
2828
# debug_logging = true
2929
# build the closure on the remote machine instead of locally
3030
# build_on_remote = true
31+
# Optional: SSH password for initial installation
32+
# install_pass = "your-ssh-password"
33+
# Optional: Sudo password for remote operations during installation
34+
# install_sudo_pass = "your-sudo-password"
3135
# script is below
3236
extra_files_script = "${path.module}/decrypt-ssh-secrets.sh"
3337
disk_encryption_key_scripts = [{
@@ -139,7 +143,7 @@ locals {
139143
resource "local_file" "nixos_vars" {
140144
content = jsonencode(local.nixos_vars) # Converts variables to JSON
141145
filename = local.nixos_vars_file # Specifies the output file path
142-
file_permission = "600"
146+
file_permission = "600"
143147
144148
# Automatically adds the generated file to Git
145149
provisioner "local-exec" {

terraform/all-in-one/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module "install" {
2727
target_user = local.install_user
2828
target_host = var.target_host
2929
target_port = local.install_port
30+
target_pass = var.install_pass
31+
target_sudo_pass = var.install_sudo_pass
3032
nixos_partitioner = module.partitioner-build.result.out
3133
nixos_system = module.system-build.result.out
3234
ssh_private_key = var.install_ssh_key

terraform/all-in-one/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,17 @@ variable "install_bootloader" {
149149
description = "Install/re-install the bootloader"
150150
default = false
151151
}
152+
153+
variable "install_pass" {
154+
type = string
155+
description = "Password used to connect to the target_host during installation"
156+
default = null
157+
sensitive = true
158+
}
159+
160+
variable "install_sudo_pass" {
161+
type = string
162+
description = "Sudo password for remote sudo operations during installation. Only supported with sudo, not doas."
163+
default = null
164+
sensitive = true
165+
}

terraform/install.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ module "install" {
3434
nixos_system = module.system-build.result.out
3535
nixos_partitioner = module.disko.result.out
3636
target_host = local.ipv4
37+
# Optional: SSH password authentication
38+
# target_pass = "your-ssh-password"
39+
# Optional: Sudo password for remote operations
40+
# target_sudo_pass = "your-sudo-password"
3741
}
3842
```
3943

terraform/install/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ locals {
1212
target_host = var.target_host
1313
target_port = var.target_port
1414
target_pass = var.target_pass
15+
target_sudo_pass = var.target_sudo_pass
1516
extra_files_script = var.extra_files_script
1617
build_on_remote = var.build_on_remote
1718
flake = var.flake

terraform/install/run-nixos-anywhere.sh

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ args=()
1313

1414
if [[ ${input[debug_logging]} == "true" ]]; then
1515
set -x
16-
declare -p input
16+
# Print input variables but filter out sensitive passwords
17+
for key in "${!input[@]}"; do
18+
if [[ $key == *"pass"* ]]; then
19+
echo "input[$key]='[FILTERED]'"
20+
else
21+
echo "input[$key]='${input[$key]}'"
22+
fi
23+
done
1724
args+=("--debug")
1825
fi
1926
if [[ ${input[kexec_tarball_url]} != "null" ]]; then
@@ -40,9 +47,22 @@ args+=(--phases "${input[phases]}")
4047
if [[ ${input[ssh_private_key]} != null ]]; then
4148
export SSH_PRIVATE_KEY="${input[ssh_private_key]}"
4249
fi
50+
if [[ ${input[target_pass]} != null || ${input[target_sudo_pass]} != null ]]; then
51+
args+=("--env-password")
52+
fi
53+
# Temporarily disable debug output when exporting sensitive variables
54+
if [[ ${input[debug_logging]} == "true" ]]; then
55+
set +x
56+
fi
4357
if [[ ${input[target_pass]} != null ]]; then
4458
export SSHPASS=${input[target_pass]}
45-
args+=("--env-password")
59+
fi
60+
if [[ ${input[target_sudo_pass]} != null ]]; then
61+
export SUDO_PASSWORD=${input[target_sudo_pass]}
62+
fi
63+
# Re-enable debug output if it was enabled
64+
if [[ ${input[debug_logging]} == "true" ]]; then
65+
set -x
4666
fi
4767

4868
tmpdir=$(mktemp -d)

terraform/install/variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ variable "target_pass" {
4141
default = null
4242
}
4343

44+
variable "target_sudo_pass" {
45+
type = string
46+
description = "Sudo password for remote sudo operations on target_host. Only supported with sudo, not doas."
47+
default = null
48+
sensitive = true
49+
}
50+
4451
variable "ssh_private_key" {
4552
type = string
4653
description = "Content of private key used to connect to the target_host"

0 commit comments

Comments
 (0)