Skip to content

Commit be2221a

Browse files
bors[bot]jfroche
andauthored
Merge #80
80: fix(rebuild): use sudo if rebuild with another user than root r=jfroche a=jfroche Co-authored-by: Jean-François Roche <jfroche@pyxel.be>
2 parents ac1eaaf + 4371932 commit be2221a

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

terraform/nixos-rebuild/deploy.sh

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
set -uex -o pipefail
44

5-
if [ "$#" -ne 3 ]; then
6-
echo "USAGE: $0 NIXOS_SYSTEM TARGET_HOST TARGET_PORT" >&2
5+
if [ "$#" -ne 4 ]; then
6+
echo "USAGE: $0 NIXOS_SYSTEM TARGET_USER TARGET_HOST TARGET_PORT" >&2
77
exit 1
88
fi
99

1010
NIXOS_SYSTEM=$1
11-
TARGET_HOST=$2
12-
TARGET_PORT=$3
11+
TARGET_USER=$2
12+
TARGET_HOST=$3
13+
TARGET_PORT=$4
1314
shift 3
1415

16+
TARGET="${TARGET_USER}@${TARGET_HOST}"
17+
1518
workDir=$(mktemp -d)
1619
trap 'rm -rf "$workDir"' EXIT
1720

@@ -31,7 +34,7 @@ if [[ -n ${SSH_KEY+x} && ${SSH_KEY} != "-" ]]; then
3134
fi
3235

3336
try=1
34-
until NIX_SSHOPTS="${sshOpts[*]}" nix copy -s --experimental-features nix-command --to "ssh://$TARGET_HOST" "$NIXOS_SYSTEM"; do
37+
until NIX_SSHOPTS="${sshOpts[*]}" nix copy -s --experimental-features nix-command --to "ssh://$TARGET" "$NIXOS_SYSTEM"; do
3538
if [[ $try -gt 10 ]]; then
3639
echo "retries exhausted" >&2
3740
exit 1
@@ -40,5 +43,9 @@ until NIX_SSHOPTS="${sshOpts[*]}" nix copy -s --experimental-features nix-comman
4043
try=$((try + 1))
4144
done
4245

46+
switchCommand="nix-env -p /nix/var/nix/profiles/system --set $(printf "%q" "$NIXOS_SYSTEM"); /nix/var/nix/profiles/system/bin/switch-to-configuration switch"
47+
if [[ $TARGET_USER != "root" ]]; then
48+
switchCommand="sudo bash -c '$switchCommand'"
49+
fi
4350
# shellcheck disable=SC2029
44-
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" || :
51+
ssh "${sshOpts[@]}" "$TARGET" "$switchCommand"

terraform/nixos-rebuild/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ resource "null_resource" "nixos-rebuild" {
77
SSH_KEY = var.ssh_private_key
88
}
99

10-
command = "${path.module}/deploy.sh ${var.nixos_system} ${var.target_user}@${var.target_host} ${var.target_port}"
10+
command = "${path.module}/deploy.sh ${var.nixos_system} ${var.target_user} ${var.target_host} ${var.target_port}"
1111
}
1212
}

0 commit comments

Comments
 (0)