mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-01-24 13:53:24 +00:00
nixos-rebuild: only use sudo when necessary
This commit is contained in:
parent
b09de27cd6
commit
5aad97fcff
@ -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.
|
||||
.
|
||||
.It Fl -use-remote-sudo
|
||||
When set, nixos-rebuild prefixes remote commands that run on the
|
||||
.Fl -build-host
|
||||
and
|
||||
When set, nixos-rebuild prefixes activation commands that run on the
|
||||
.Fl -target-host
|
||||
systems with
|
||||
system with
|
||||
.Ic sudo Ns
|
||||
\&. Setting this option allows deploying as a non-root user.
|
||||
.
|
||||
|
@ -157,8 +157,10 @@ while [ "$#" -gt 0 ]; do
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -n "$SUDO_USER" || -n $remoteSudo ]]; then
|
||||
maybeSudo=(sudo --preserve-env="$preservedSudoVars" --)
|
||||
sudoCommand=(sudo --preserve-env="$preservedSudoVars" --)
|
||||
|
||||
if [[ -n "$SUDO_USER" ]]; then
|
||||
useSudo=1
|
||||
fi
|
||||
|
||||
# log the given argument to stderr if verbose mode is on
|
||||
@ -178,17 +180,25 @@ buildHostCmd() {
|
||||
if [ -z "$buildHost" ]; then
|
||||
runCmd "$@"
|
||||
elif [ -n "$remoteNix" ]; then
|
||||
runCmd ssh $SSHOPTS "$buildHost" "${maybeSudo[@]}" env PATH="$remoteNix":'$PATH' "$@"
|
||||
runCmd ssh $SSHOPTS "$buildHost" "${useSudo:+${sudoCommand[@]}}" env PATH="$remoteNix":'$PATH' "$@"
|
||||
else
|
||||
runCmd ssh $SSHOPTS "$buildHost" "${maybeSudo[@]}" "$@"
|
||||
runCmd ssh $SSHOPTS "$buildHost" "${useSudo:+${sudoCommand[@]}}" "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
targetHostCmd() {
|
||||
if [ -z "$targetHost" ]; then
|
||||
runCmd "${maybeSudo[@]}" "$@"
|
||||
runCmd "${useSudo:+${sudoCommand[@]}}" "$@"
|
||||
else
|
||||
runCmd ssh $SSHOPTS "$targetHost" "${maybeSudo[@]}" "$@"
|
||||
runCmd ssh $SSHOPTS "$targetHost" "${useSudo:+${sudoCommand[@]}}" "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
targetHostSudoCmd() {
|
||||
if [ -n "$remoteSudo" ]; then
|
||||
useSudo=1 targetHostCmd "$@"
|
||||
else
|
||||
targetHostCmd "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
@ -667,7 +677,7 @@ if [ -z "$rollback" ]; then
|
||||
pathToConfig="$(nixFlakeBuild "$flake#$flakeAttr.config.system.build.toplevel" "${extraBuildFlags[@]}" "${lockFlags[@]}")"
|
||||
fi
|
||||
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
|
||||
if [[ -z $flake ]]; then
|
||||
pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}")"
|
||||
@ -695,7 +705,7 @@ if [ -z "$rollback" ]; then
|
||||
fi
|
||||
else # [ -n "$rollback" ]
|
||||
if [[ "$action" = switch || "$action" = boot ]]; then
|
||||
targetHostCmd nix-env --rollback -p "$profile"
|
||||
targetHostSudoCmd nix-env --rollback -p "$profile"
|
||||
pathToConfig="$profile"
|
||||
elif [[ "$action" = test || "$action" = build ]]; then
|
||||
systemNumber=$(
|
||||
@ -740,7 +750,7 @@ if [[ "$action" = switch || "$action" = boot || "$action" = test || "$action" =
|
||||
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"
|
||||
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."
|
||||
cmd=(
|
||||
"env"
|
||||
@ -762,7 +772,7 @@ if [[ "$action" = switch || "$action" = boot || "$action" = test || "$action" =
|
||||
fi
|
||||
fi
|
||||
|
||||
if ! targetHostCmd "${cmd[@]}" "$action"; then
|
||||
if ! targetHostSudoCmd "${cmd[@]}" "$action"; then
|
||||
log "warning: error(s) occurred while switching to the new configuration"
|
||||
exit 1
|
||||
fi
|
||||
|
Loading…
Reference in New Issue
Block a user