nixos-rebuild: only use sudo when necessary

This commit is contained in:
Michael Hoang 2023-12-30 13:14:54 +11:00
parent b09de27cd6
commit 5aad97fcff
2 changed files with 22 additions and 14 deletions

View File

@ -363,11 +363,9 @@ is also set. This is useful when the target-host connection to cache.nixos.org
is faster than the connection between hosts. is faster than the connection between hosts.
. .
.It Fl -use-remote-sudo .It Fl -use-remote-sudo
When set, nixos-rebuild prefixes remote commands that run on the When set, nixos-rebuild prefixes activation commands that run on the
.Fl -build-host
and
.Fl -target-host .Fl -target-host
systems with system with
.Ic sudo Ns .Ic sudo Ns
\&. Setting this option allows deploying as a non-root user. \&. Setting this option allows deploying as a non-root user.
. .

View File

@ -157,8 +157,10 @@ while [ "$#" -gt 0 ]; do
esac esac
done done
if [[ -n "$SUDO_USER" || -n $remoteSudo ]]; then sudoCommand=(sudo --preserve-env="$preservedSudoVars" --)
maybeSudo=(sudo --preserve-env="$preservedSudoVars" --)
if [[ -n "$SUDO_USER" ]]; then
useSudo=1
fi fi
# log the given argument to stderr if verbose mode is on # log the given argument to stderr if verbose mode is on
@ -178,17 +180,25 @@ buildHostCmd() {
if [ -z "$buildHost" ]; then if [ -z "$buildHost" ]; then
runCmd "$@" runCmd "$@"
elif [ -n "$remoteNix" ]; then elif [ -n "$remoteNix" ]; then
runCmd ssh $SSHOPTS "$buildHost" "${maybeSudo[@]}" env PATH="$remoteNix":'$PATH' "$@" runCmd ssh $SSHOPTS "$buildHost" "${useSudo:+${sudoCommand[@]}}" env PATH="$remoteNix":'$PATH' "$@"
else else
runCmd ssh $SSHOPTS "$buildHost" "${maybeSudo[@]}" "$@" runCmd ssh $SSHOPTS "$buildHost" "${useSudo:+${sudoCommand[@]}}" "$@"
fi fi
} }
targetHostCmd() { targetHostCmd() {
if [ -z "$targetHost" ]; then if [ -z "$targetHost" ]; then
runCmd "${maybeSudo[@]}" "$@" runCmd "${useSudo:+${sudoCommand[@]}}" "$@"
else else
runCmd ssh $SSHOPTS "$targetHost" "${maybeSudo[@]}" "$@" runCmd ssh $SSHOPTS "$targetHost" "${useSudo:+${sudoCommand[@]}}" "$@"
fi
}
targetHostSudoCmd() {
if [ -n "$remoteSudo" ]; then
useSudo=1 targetHostCmd "$@"
else
targetHostCmd "$@"
fi fi
} }
@ -667,7 +677,7 @@ if [ -z "$rollback" ]; then
pathToConfig="$(nixFlakeBuild "$flake#$flakeAttr.config.system.build.toplevel" "${extraBuildFlags[@]}" "${lockFlags[@]}")" pathToConfig="$(nixFlakeBuild "$flake#$flakeAttr.config.system.build.toplevel" "${extraBuildFlags[@]}" "${lockFlags[@]}")"
fi fi
copyToTarget "$pathToConfig" copyToTarget "$pathToConfig"
targetHostCmd nix-env -p "$profile" --set "$pathToConfig" targetHostSudoCmd nix-env -p "$profile" --set "$pathToConfig"
elif [[ "$action" = test || "$action" = build || "$action" = dry-build || "$action" = dry-activate ]]; then elif [[ "$action" = test || "$action" = build || "$action" = dry-build || "$action" = dry-activate ]]; then
if [[ -z $flake ]]; then if [[ -z $flake ]]; then
pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}")" pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}")"
@ -695,7 +705,7 @@ if [ -z "$rollback" ]; then
fi fi
else # [ -n "$rollback" ] else # [ -n "$rollback" ]
if [[ "$action" = switch || "$action" = boot ]]; then if [[ "$action" = switch || "$action" = boot ]]; then
targetHostCmd nix-env --rollback -p "$profile" targetHostSudoCmd nix-env --rollback -p "$profile"
pathToConfig="$profile" pathToConfig="$profile"
elif [[ "$action" = test || "$action" = build ]]; then elif [[ "$action" = test || "$action" = build ]]; then
systemNumber=$( systemNumber=$(
@ -740,7 +750,7 @@ if [[ "$action" = switch || "$action" = boot || "$action" = test || "$action" =
if [[ -n "$NIXOS_SWITCH_USE_DIRTY_ENV" ]]; then if [[ -n "$NIXOS_SWITCH_USE_DIRTY_ENV" ]]; then
log "warning: skipping systemd-run since NIXOS_SWITCH_USE_DIRTY_ENV is set. This environment variable will be ignored in the future" log "warning: skipping systemd-run since NIXOS_SWITCH_USE_DIRTY_ENV is set. This environment variable will be ignored in the future"
cmd=() cmd=()
elif ! targetHostCmd "${cmd[@]}" true &>/dev/null; then elif ! targetHostSudoCmd "${cmd[@]}" true &>/dev/null; then
logVerbose "Skipping systemd-run to switch configuration since it is not working in target host." logVerbose "Skipping systemd-run to switch configuration since it is not working in target host."
cmd=( cmd=(
"env" "env"
@ -762,7 +772,7 @@ if [[ "$action" = switch || "$action" = boot || "$action" = test || "$action" =
fi fi
fi fi
if ! targetHostCmd "${cmd[@]}" "$action"; then if ! targetHostSudoCmd "${cmd[@]}" "$action"; then
log "warning: error(s) occurred while switching to the new configuration" log "warning: error(s) occurred while switching to the new configuration"
exit 1 exit 1
fi fi