diff options
Diffstat (limited to 'pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh')
-rwxr-xr-x | pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh b/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh index f9bda1a64b622..4439487a9301a 100755 --- a/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh +++ b/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh @@ -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" "${useSudo:+${sudoCommand[@]}}" "$@" + fi +} + +targetHostSudoCmd() { + if [ -n "$remoteSudo" ]; then + useSudo=1 targetHostCmd "$@" else - runCmd ssh $SSHOPTS "$targetHost" "${maybeSudo[@]}" "$@" + targetHostCmd "$@" fi } @@ -426,7 +436,7 @@ if [ "$action" = edit ]; then exit 1 fi -SSHOPTS="$NIX_SSHOPTS -o ControlMaster=auto -o ControlPath=$tmpDir/ssh-%n -o ControlPersist=60" +SSHOPTS="$NIX_SSHOPTS -o ControlMaster=auto -o ControlPath=$tmpDir/ssh-%n -o ControlPersist=60 -t" # First build Nix, since NixOS may require a newer version than the # current one. @@ -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; 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 |