nixos-rebuild: support sudo + --target-host

This adds support for deploying to remote hosts without being root:

  sudo nixos-rebuild --target-host non-root@host

Without this change, only root@host is able to deploy.

The idea is that if the local command is run with sudo, so should the
remote one, thus there is no need for adding any CLI options.
This commit is contained in:
Bjørn Forsman 2019-10-14 21:14:35 +02:00
parent 14803d8070
commit 263a81e285
1 changed files with 7 additions and 3 deletions

View File

@ -22,6 +22,7 @@ repair=
profile=/nix/var/nix/profiles/system profile=/nix/var/nix/profiles/system
buildHost= buildHost=
targetHost= targetHost=
maybeSudo=
while [ "$#" -gt 0 ]; do while [ "$#" -gt 0 ]; do
i="$1"; shift 1 i="$1"; shift 1
@ -96,6 +97,9 @@ while [ "$#" -gt 0 ]; do
esac esac
done done
if [ -n "$SUDO_USER" ]; then
maybeSudo="sudo "
fi
if [ -z "$buildHost" -a -n "$targetHost" ]; then if [ -z "$buildHost" -a -n "$targetHost" ]; then
buildHost="$targetHost" buildHost="$targetHost"
@ -111,9 +115,9 @@ buildHostCmd() {
if [ -z "$buildHost" ]; then if [ -z "$buildHost" ]; then
"$@" "$@"
elif [ -n "$remoteNix" ]; then elif [ -n "$remoteNix" ]; then
ssh $SSHOPTS "$buildHost" env PATH="$remoteNix:$PATH" "$@" ssh $SSHOPTS "$buildHost" env PATH="$remoteNix:$PATH" "$maybeSudo$@"
else else
ssh $SSHOPTS "$buildHost" "$@" ssh $SSHOPTS "$buildHost" "$maybeSudo$@"
fi fi
} }
@ -121,7 +125,7 @@ targetHostCmd() {
if [ -z "$targetHost" ]; then if [ -z "$targetHost" ]; then
"$@" "$@"
else else
ssh $SSHOPTS "$targetHost" "$@" ssh $SSHOPTS "$targetHost" "$maybeSudo$@"
fi fi
} }