Merge pull request #12324 from rickynils/nixos-rebuild-remote-try2

Fix NixOS installer tests failures introduced by nixos-rebuild changes

+160 -27
+160 -27
nixos/modules/installer/tools/nixos-rebuild.sh
··· 19 upgrade= 20 repair= 21 profile=/nix/var/nix/profiles/system 22 23 while [ "$#" -gt 0 ]; do 24 i="$1"; shift 1 ··· 73 fi 74 shift 1 75 ;; 76 *) 77 echo "$0: unknown option \`$i'" 78 exit 1 79 ;; 80 esac 81 done 82 83 if [ -z "$action" ]; then showSyntax; fi 84 ··· 128 129 130 tmpDir=$(mktemp -t -d nixos-rebuild.XXXXXX) 131 - trap 'rm -rf "$tmpDir"' EXIT 132 133 134 # If the Nix daemon is running, then use it. This allows us to use ··· 150 buildNix= 151 fi 152 153 if [ -n "$buildNix" ]; then 154 echo "building Nix..." >&2 155 - if ! nix-build '<nixpkgs/nixos>' -A config.nix.package -o $tmpDir/nix "${extraBuildFlags[@]}" > /dev/null; then 156 - if ! nix-build '<nixpkgs/nixos>' -A nixFallback -o $tmpDir/nix "${extraBuildFlags[@]}" > /dev/null; then 157 - if ! nix-build '<nixpkgs>' -A nix -o $tmpDir/nix "${extraBuildFlags[@]}" > /dev/null; then 158 - machine="$(uname -m)" 159 - if [ "$machine" = x86_64 ]; then 160 - nixStorePath=/nix/store/xryr9g56h8yjddp89d6dw12anyb4ch7c-nix-1.10 161 - elif [[ "$machine" =~ i.86 ]]; then 162 - nixStorePath=/nix/store/2w92k5wlpspf0q2k9mnf2z42prx3bwmv-nix-1.10 163 - else 164 - echo "$0: unsupported platform" 165 - exit 1 166 - fi 167 if ! nix-store -r $nixStorePath --add-root $tmpDir/nix --indirect \ 168 --option extra-binary-caches https://cache.nixos.org/; then 169 echo "warning: don't know how to get latest Nix" >&2 170 fi 171 # Older version of nix-store -r don't support --add-root. 172 [ -e $tmpDir/nix ] || ln -sf $nixStorePath $tmpDir/nix 173 fi 174 fi 175 fi 176 - PATH=$tmpDir/nix/bin:$PATH 177 fi 178 179 ··· 200 if [ -z "$rollback" ]; then 201 echo "building the system configuration..." >&2 202 if [ "$action" = switch -o "$action" = boot ]; then 203 - nix-env "${extraBuildFlags[@]}" -p "$profile" -f '<nixpkgs/nixos>' --set -A system 204 - pathToConfig="$profile" 205 elif [ "$action" = test -o "$action" = build -o "$action" = dry-build -o "$action" = dry-activate ]; then 206 - nix-build '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}" > /dev/null 207 - pathToConfig=./result 208 elif [ "$action" = build-vm ]; then 209 - nix-build '<nixpkgs/nixos>' -A vm -k "${extraBuildFlags[@]}" > /dev/null 210 - pathToConfig=./result 211 elif [ "$action" = build-vm-with-bootloader ]; then 212 - nix-build '<nixpkgs/nixos>' -A vmWithBootLoader -k "${extraBuildFlags[@]}" > /dev/null 213 - pathToConfig=./result 214 else 215 showSyntax 216 fi 217 else # [ -n "$rollback" ] 218 if [ "$action" = switch -o "$action" = boot ]; then 219 - nix-env --rollback -p "$profile" 220 pathToConfig="$profile" 221 elif [ "$action" = test -o "$action" = build ]; then 222 systemNumber=$( 223 - nix-env -p "$profile" --list-generations | 224 sed -n '/current/ {g; p;}; s/ *\([0-9]*\).*/\1/; h' 225 ) 226 - ln -sT "$profile"-${systemNumber}-link ./result 227 - pathToConfig=./result 228 else 229 showSyntax 230 fi ··· 234 # If we're not just building, then make the new configuration the boot 235 # default and/or activate it now. 236 if [ "$action" = switch -o "$action" = boot -o "$action" = test -o "$action" = dry-activate ]; then 237 - if ! $pathToConfig/bin/switch-to-configuration "$action"; then 238 echo "warning: error(s) occurred while switching to the new configuration" >&2 239 exit 1 240 fi
··· 19 upgrade= 20 repair= 21 profile=/nix/var/nix/profiles/system 22 + buildHost= 23 + targetHost= 24 25 while [ "$#" -gt 0 ]; do 26 i="$1"; shift 1 ··· 75 fi 76 shift 1 77 ;; 78 + --build-host|h) 79 + buildHost="$1" 80 + shift 1 81 + ;; 82 + --target-host|t) 83 + targetHost="$1" 84 + shift 1 85 + ;; 86 *) 87 echo "$0: unknown option \`$i'" 88 exit 1 89 ;; 90 esac 91 done 92 + 93 + 94 + if [ -z "$buildHost" -a -n "$targetHost" ]; then 95 + buildHost="$targetHost" 96 + fi 97 + if [ "$targetHost" = localhost ]; then 98 + targetHost= 99 + fi 100 + if [ "$buildHost" = localhost ]; then 101 + buildHost= 102 + fi 103 + 104 + buildHostCmd() { 105 + if [ -z "$buildHost" ]; then 106 + "$@" 107 + elif [ -n "$remoteNix" ]; then 108 + ssh $SSHOPTS "$buildHost" PATH="$remoteNix:$PATH" "$@" 109 + else 110 + ssh $SSHOPTS "$buildHost" "$@" 111 + fi 112 + } 113 + 114 + targetHostCmd() { 115 + if [ -z "$targetHost" ]; then 116 + "$@" 117 + else 118 + ssh $SSHOPTS "$targetHost" "$@" 119 + fi 120 + } 121 + 122 + copyToTarget() { 123 + if ! [ "$targetHost" = "$buildHost" ]; then 124 + if [ -z "$targetHost" ]; then 125 + NIX_SSHOPTS=$SSH_OPTS nix-copy-closure --from "$buildHost" "$1" 126 + elif [ -z "$buildHost" ]; then 127 + NIX_SSHOPTS=$SSH_OPTS nix-copy-closure --to "$targetHost" "$1" 128 + else 129 + buildHostCmd nix-copy-closure --to "$targetHost" "$1" 130 + fi 131 + fi 132 + } 133 + 134 + nixBuild() { 135 + if [ -z "$buildHost" ]; then 136 + nix-build "$@" 137 + else 138 + local instArgs=() 139 + local buildArgs=() 140 + 141 + while [ "$#" -gt 0 ]; do 142 + local i="$1"; shift 1 143 + case "$i" in 144 + -o) 145 + local out="$1"; shift 1 146 + buildArgs+=("--add-root" "$out" "--indirect") 147 + ;; 148 + -A) 149 + local j="$1"; shift 1 150 + instArgs+=("$i" "$j") 151 + ;; 152 + -I) 153 + # We don't want this in buildArgs 154 + shift 1 155 + ;; 156 + "<"*) # nix paths 157 + instArgs+=("$i") 158 + ;; 159 + *) 160 + buildArgs+=("$i") 161 + ;; 162 + esac 163 + done 164 + 165 + local drv="$(nix-instantiate "${instArgs[@]}" "${extraBuildFlags[@]}")" 166 + if [ -a "$drv" ]; then 167 + NIX_SSHOPTS=$SSH_OPTS nix-copy-closure --to "$buildHost" "$drv" 168 + buildHostCmd nix-store -r "$drv" "${buildArgs[@]}" 169 + else 170 + echo "nix-instantiate failed" 171 + exit 1 172 + fi 173 + fi 174 + } 175 + 176 177 if [ -z "$action" ]; then showSyntax; fi 178 ··· 222 223 224 tmpDir=$(mktemp -t -d nixos-rebuild.XXXXXX) 225 + SSHOPTS="$NIX_SSHOPTS -o ControlMaster=auto -o ControlPath=$tmpDir/ssh-%n -o ControlPersist=60" 226 + 227 + cleanup() { 228 + for ctrl in "$tmpDir"/ssh-*; do 229 + ssh -o ControlPath="$ctrl" -O exit dummyhost 2>/dev/null || true 230 + done 231 + rm -rf "$tmpDir" 232 + } 233 + trap cleanup EXIT 234 + 235 236 237 # If the Nix daemon is running, then use it. This allows us to use ··· 253 buildNix= 254 fi 255 256 + prebuiltNix() { 257 + machine="$1" 258 + if [ "$machine" = x86_64 ]; then 259 + return /nix/store/xryr9g56h8yjddp89d6dw12anyb4ch7c-nix-1.10 260 + elif [[ "$machine" =~ i.86 ]]; then 261 + return /nix/store/2w92k5wlpspf0q2k9mnf2z42prx3bwmv-nix-1.10 262 + else 263 + echo "$0: unsupported platform" 264 + exit 1 265 + fi 266 + } 267 + 268 + remotePATH= 269 + 270 if [ -n "$buildNix" ]; then 271 echo "building Nix..." >&2 272 + nixDrv= 273 + if ! nixDrv="$(nix-instantiate '<nixpkgs/nixos>' --add-root $tmpDir/nix.drv --indirect -A config.nix.package "${extraBuildFlags[@]}")"; then 274 + if ! nixDrv="$(nix-instantiate '<nixpkgs/nixos>' --add-root $tmpDir/nix.drv --indirect -A nixFallback "${extraBuildFlags[@]}")"; then 275 + if ! nixDrv="$(nix-instantiate '<nixpkgs>' --add-root $tmpDir/nix.drv --indirect -A nix "${extraBuildFlags[@]}")"; then 276 + nixStorePath="$(prebuiltNix "$(uname -m)")" 277 if ! nix-store -r $nixStorePath --add-root $tmpDir/nix --indirect \ 278 --option extra-binary-caches https://cache.nixos.org/; then 279 echo "warning: don't know how to get latest Nix" >&2 280 fi 281 # Older version of nix-store -r don't support --add-root. 282 [ -e $tmpDir/nix ] || ln -sf $nixStorePath $tmpDir/nix 283 + if [ -n "$buildHost" ]; then 284 + remoteNixStorePath="$(prebuiltNix "$(buildHostCmd uname -m)")" 285 + remoteNix="$remoteNixStorePath/bin" 286 + if ! buildHostCmd nix-store -r $remoteNixStorePath \ 287 + --option extra-binary-caches https://cache.nixos.org/ >/dev/null; then 288 + remoteNix= 289 + echo "warning: don't know how to get latest Nix" >&2 290 + fi 291 + fi 292 fi 293 fi 294 fi 295 + if [ -a "$nixDrv" ]; then 296 + nix-store -r "$nixDrv"'!'"out" --add-root $tmpDir/nix --indirect >/dev/null 297 + if [ -n "$buildHost" ]; then 298 + nix-copy-closure --to "$buildHost" "$nixDrv" 299 + # The nix build produces multiple outputs, we add them all to the remote path 300 + for p in $(buildHostCmd nix-store -r "$(readlink "$nixDrv")" "${buildArgs[@]}"); do 301 + remoteNix="$remoteNix${remoteNix:+:}$p/bin" 302 + done 303 + fi 304 + fi 305 + PATH="$tmpDir/nix/bin:$PATH" 306 fi 307 308 ··· 329 if [ -z "$rollback" ]; then 330 echo "building the system configuration..." >&2 331 if [ "$action" = switch -o "$action" = boot ]; then 332 + pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A system "${extraBuildFlags[@]}")" 333 + copyToTarget "$pathToConfig" 334 + targetHostCmd nix-env -p "$profile" --set "$pathToConfig" 335 elif [ "$action" = test -o "$action" = build -o "$action" = dry-build -o "$action" = dry-activate ]; then 336 + pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}")" 337 elif [ "$action" = build-vm ]; then 338 + pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A vm -k "${extraBuildFlags[@]}")" 339 elif [ "$action" = build-vm-with-bootloader ]; then 340 + pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A vmWithBootLoader -k "${extraBuildFlags[@]}")" 341 else 342 showSyntax 343 fi 344 + # Copy build to target host if we haven't already done it 345 + if ! [ "$action" = switch -o "$action" = boot ]; then 346 + copyToTarget "$pathToConfig" 347 + fi 348 else # [ -n "$rollback" ] 349 if [ "$action" = switch -o "$action" = boot ]; then 350 + targetHostCmd nix-env --rollback -p "$profile" 351 pathToConfig="$profile" 352 elif [ "$action" = test -o "$action" = build ]; then 353 systemNumber=$( 354 + targetHostCmd nix-env -p "$profile" --list-generations | 355 sed -n '/current/ {g; p;}; s/ *\([0-9]*\).*/\1/; h' 356 ) 357 + pathToConfig="$profile"-${systemNumber}-link 358 + if [ -z "$targetHost" ]; then 359 + ln -sT "$pathToConfig" ./result 360 + fi 361 else 362 showSyntax 363 fi ··· 367 # If we're not just building, then make the new configuration the boot 368 # default and/or activate it now. 369 if [ "$action" = switch -o "$action" = boot -o "$action" = test -o "$action" = dry-activate ]; then 370 + if ! targetHostCmd $pathToConfig/bin/switch-to-configuration "$action"; then 371 echo "warning: error(s) occurred while switching to the new configuration" >&2 372 exit 1 373 fi