File tree Expand file tree Collapse file tree 2 files changed +14
-7
lines changed Expand file tree Collapse file tree 2 files changed +14
-7
lines changed Original file line number Diff line number Diff line change 2
2
3
3
set -uex -o pipefail
4
4
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
7
7
exit 1
8
8
fi
9
9
10
10
NIXOS_SYSTEM=$1
11
- TARGET_HOST=$2
12
- TARGET_PORT=$3
11
+ TARGET_USER=$2
12
+ TARGET_HOST=$3
13
+ TARGET_PORT=$4
13
14
shift 3
14
15
16
+ TARGET=" ${TARGET_USER} @${TARGET_HOST} "
17
+
15
18
workDir=$( mktemp -d)
16
19
trap ' rm -rf "$workDir"' EXIT
17
20
@@ -31,7 +34,7 @@ if [[ -n ${SSH_KEY+x} && ${SSH_KEY} != "-" ]]; then
31
34
fi
32
35
33
36
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
35
38
if [[ $try -gt 10 ]]; then
36
39
echo " retries exhausted" >&2
37
40
exit 1
@@ -40,5 +43,9 @@ until NIX_SSHOPTS="${sshOpts[*]}" nix copy -s --experimental-features nix-comman
40
43
try=$(( try + 1 ))
41
44
done
42
45
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
43
50
# 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 "
Original file line number Diff line number Diff line change @@ -7,6 +7,6 @@ resource "null_resource" "nixos-rebuild" {
7
7
SSH_KEY = var.ssh_private_key
8
8
}
9
9
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 } "
11
11
}
12
12
}
You can’t perform that action at this time.
0 commit comments