git.iko.soy/typeable/nixpkgs
on master help sign in
nixos-rebuild: Print commands as they are run on verbose flag

nixos-rebuild would forward all verbose flags to nix, but not actually increase its own verbosity. Which would make it hard to see what’s going on without resorting to tools like strace or adding `set -x` to the script. We add a simple exec wrapper around the most “interesting” commands, that is nix tool invocations. If any verbosity flag is given, it will set the verbosity flag of the nixos-rebuild script all commands wrapped into `runCmd` will start logging their invocation. This is done via a `logVerbose` function, which can also be used to print logging messages when in verbose mode, to aid debugging why nixos-rebuild does what it does. A few messages are added, but potentially we could log a lot more details. Now, testing all of the changed code paths turns out to be rather hard, so I am unsure how to verify that for all changed commands behaviour stays indeed the same.

commitcb7bc7c60f39e6ebe8bad32720ff3548618b4d97 authorProfpatsch <mail@profpatsch.de> dateTue 8 Mar 2022, 16:59 (5 years ago) parent575ab73
1 file changed+56−30
pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh+56−30
pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh+56−30
@@ -31,6 +31,7 @@ profile=/nix/var/nix/profiles/system
3131buildHost=localhost
3232targetHost=
3333remoteSudo=
34+verboseScript=
3435# comma separated list of vars to preserve when using sudo
3536preservedSudoVars=NIXOS_INSTALL_BOOTLOADER
3637
@@ -71,7 +72,11 @@ while [ "$#" -gt 0 ]; do
7172 j="$1"; shift 1
7273 extraBuildFlags+=("$i" "$j")
7374 ;;
74− --show-trace|--keep-failed|-K|--keep-going|-k|--verbose|-v|-vv|-vvv|-vvvv|-vvvvv|--fallback|--repair|--no-build-output|-Q|-j*|-L|--print-build-logs|--refresh|--no-net|--offline|--impure)
75+ --show-trace|--keep-failed|-K|--keep-going|-k|--fallback|--repair|--no-build-output|-Q|-j*|-L|--print-build-logs|--refresh|--no-net|--offline|--impure)
76+ extraBuildFlags+=("$i")
77+ ;;
78+ --verbose|-v|-vv|-vvv|-vvvv|-vvvvv)
79+ verboseScript="true"
7580 extraBuildFlags+=("$i")
7681 ;;
7782 --option)
@@ -143,30 +148,45 @@ if [ "$buildHost" = localhost ]; then
143148 buildHost=
144149fi
145150
151+# log the given argument to stderr if verbose mode is on
152+logVerbose() {
153+ if [ -n "$verboseScript" ]; then
154+ echo "$@" >&2
155+ fi
156+}
157+
158+# Run a command, logging it first if verbose mode is on
159+runCmd() {
160+ logVerbose "$" "$@"
161+ "$@"
162+}
163+
146164buildHostCmd() {
147165 if [ -z "$buildHost" ]; then
148− "$@"
166+ runCmd "$@"
149167 elif [ -n "$remoteNix" ]; then
150− ssh $SSHOPTS "$buildHost" "${maybeSudo[@]}" env PATH="$remoteNix":'$PATH' "$@"
168+ runCmd ssh $SSHOPTS "$buildHost" "${maybeSudo[@]}" env PATH="$remoteNix":'$PATH' "$@"
151169 else
152− ssh $SSHOPTS "$buildHost" "${maybeSudo[@]}" "$@"
170+ runCmd ssh $SSHOPTS "$buildHost" "${maybeSudo[@]}" "$@"
153171 fi
154172}
155173
156174targetHostCmd() {
157175 if [ -z "$targetHost" ]; then
158− "${maybeSudo[@]}" "$@"
176+ runCmd "${maybeSudo[@]}" "$@"
159177 else
160− ssh $SSHOPTS "$targetHost" "${maybeSudo[@]}" "$@"
178+ runCmd ssh $SSHOPTS "$targetHost" "${maybeSudo[@]}" "$@"
161179 fi
162180}
163181
164182copyToTarget() {
165183 if ! [ "$targetHost" = "$buildHost" ]; then
166184 if [ -z "$targetHost" ]; then
167− NIX_SSHOPTS=$SSHOPTS nix-copy-closure "${copyClosureFlags[@]}" --from "$buildHost" "$1"
185+ logVerbose "Running nix-copy-closure with these NIX_SSHOPTS: $SSHOPTS"
186+ NIX_SSHOPTS=$SSHOPTS runCmd nix-copy-closure "${copyClosureFlags[@]}" --from "$buildHost" "$1"
168187 elif [ -z "$buildHost" ]; then
169− NIX_SSHOPTS=$SSHOPTS nix-copy-closure "${copyClosureFlags[@]}" --to "$targetHost" "$1"
188+ logVerbose "Running nix-copy-closure with these NIX_SSHOPTS: $SSHOPTS"
189+ NIX_SSHOPTS=$SSHOPTS runCmd nix-copy-closure "${copyClosureFlags[@]}" --to "$targetHost" "$1"
170190 else
171191 buildHostCmd nix-copy-closure "${copyClosureFlags[@]}" --to "$targetHost" "$1"
172192 fi
@@ -174,9 +194,12 @@ copyToTarget() {
174194}
175195
176196nixBuild() {
197+ logVerbose "Building in legacy (non-flake) mode."
177198 if [ -z "$buildHost" ]; then
178− nix-build "$@"
199+ logVerbose "No --build-host given, running nix-build locally"
200+ runCmd nix-build "$@"
179201 else
202+ logVerbose "buildHost set to \"$buildHost\", running nix-build remotely"
180203 local instArgs=()
181204 local buildArgs=()
182205 local drv=
@@ -206,9 +229,10 @@ nixBuild() {
206229 esac
207230 done
208231
209− drv="$(nix-instantiate "${instArgs[@]}" "${extraBuildFlags[@]}")"
232+ drv="$(runCmd nix-instantiate "${instArgs[@]}" "${extraBuildFlags[@]}")"
210233 if [ -a "$drv" ]; then
211− NIX_SSHOPTS=$SSHOPTS nix-copy-closure --to "$buildHost" "$drv"
234+ logVerbose "Running nix-copy-closure with these NIX_SSHOPTS: $SSHOPTS"
235+ NIX_SSHOPTS=$SSHOPTS runCmd nix-copy-closure --to "$buildHost" "$drv"
212236 buildHostCmd nix-store -r "$drv" "${buildArgs[@]}"
213237 else
214238 echo "nix-instantiate failed"
@@ -218,12 +242,13 @@ nixBuild() {
218242}
219243
220244nixFlakeBuild() {
245+ logVerbose "Building in flake mode."
221246 if [[ -z "$buildHost" && -z "$targetHost" && "$action" != switch && "$action" != boot ]]
222247 then
223− nix "${flakeFlags[@]}" build "$@"
248+ runCmd nix "${flakeFlags[@]}" build "$@"
224249 readlink -f ./result
225250 elif [ -z "$buildHost" ]; then
226− nix "${flakeFlags[@]}" build "$@" --out-link "${tmpDir}/result"
251+ runCmd nix "${flakeFlags[@]}" build "$@" --out-link "${tmpDir}/result"
227252 readlink -f "${tmpDir}/result"
228253 else
229254 local attr="$1"
@@ -255,9 +280,10 @@ nixFlakeBuild() {
255280 esac
256281 done
257282
258− drv="$(nix "${flakeFlags[@]}" eval --raw "${attr}.drvPath" "${evalArgs[@]}" "${extraBuildFlags[@]}")"
283+ drv="$(runCmd nix "${flakeFlags[@]}" eval --raw "${attr}.drvPath" "${evalArgs[@]}" "${extraBuildFlags[@]}")"
259284 if [ -a "$drv" ]; then
260− NIX_SSHOPTS=$SSHOPTS nix "${flakeFlags[@]}" copy --derivation --to "ssh://$buildHost" "$drv"
285+ logVerbose "Running nix with these NIX_SSHOPTS: $SSHOPTS"
286+ NIX_SSHOPTS=$SSHOPTS runCmd nix "${flakeFlags[@]}" copy --derivation --to "ssh://$buildHost" "$drv"
261287 buildHostCmd nix-store -r "$drv" "${buildArgs[@]}"
262288 else
263289 echo "nix eval failed"
@@ -291,11 +317,11 @@ if [[ -n $upgrade && -z $_NIXOS_REBUILD_REEXEC && -z $flake ]]; then
291317 channel_name=$(basename "$channelpath")
292318
293319 if [[ "$channel_name" == "nixos" ]]; then
294− nix-channel --update "$channel_name"
320+ runCmd nix-channel --update "$channel_name"
295321 elif [ -e "$channelpath/.update-on-nixos-rebuild" ]; then
296− nix-channel --update "$channel_name"
322+ runCmd nix-channel --update "$channel_name"
297323 elif [[ -n $upgrade_all ]] ; then
298− nix-channel --update "$channel_name"
324+ runCmd nix-channel --update "$channel_name"
299325 fi
300326 done
301327fi
@@ -320,9 +346,9 @@ fi
320346# Re-execute nixos-rebuild from the Nixpkgs tree.
321347# FIXME: get nixos-rebuild from $flake.
322348if [[ -z $_NIXOS_REBUILD_REEXEC && -n $canRun && -z $fast && -z $flake ]]; then
323− if p=$(nix-build --no-out-link --expr 'with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild' "${extraBuildFlags[@]}"); then
349+ if p=$(runCmd nix-build --no-out-link --expr 'with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild' "${extraBuildFlags[@]}"); then
324350 export _NIXOS_REBUILD_REEXEC=1
325− exec "$p/bin/nixos-rebuild" "${origArgs[@]}"
351+ runCmd exec "$p/bin/nixos-rebuild" "${origArgs[@]}"
326352 exit 1
327353 fi
328354fi
@@ -348,13 +374,13 @@ fi
348374# Find configuration.nix and open editor instead of building.
349375if [ "$action" = edit ]; then
350376 if [[ -z $flake ]]; then
351− NIXOS_CONFIG=${NIXOS_CONFIG:-$(nix-instantiate --find-file nixos-config)}
377+ NIXOS_CONFIG=${NIXOS_CONFIG:-$(runCmd nix-instantiate --find-file nixos-config)}
352378 if [[ -d $NIXOS_CONFIG ]]; then
353379 NIXOS_CONFIG=$NIXOS_CONFIG/default.nix
354380 fi
355− exec ${EDITOR:-nano} "$NIXOS_CONFIG"
381+ runCmd exec ${EDITOR:-nano} "$NIXOS_CONFIG"
356382 else
357− exec nix "${flakeFlags[@]}" edit "${lockFlags[@]}" -- "$flake#$flakeAttr"
383+ runCmd exec nix "${flakeFlags[@]}" edit "${lockFlags[@]}" -- "$flake#$flakeAttr"
358384 fi
359385 exit 1
360386fi
@@ -403,19 +429,19 @@ prebuiltNix() {
403429if [[ -n $buildNix && -z $flake ]]; then
404430 echo "building Nix..." >&2
405431 nixDrv=
406− if ! nixDrv="$(nix-instantiate '<nixpkgs/nixos>' --add-root "$tmpDir/nix.drv" --indirect -A config.nix.package.out "${extraBuildFlags[@]}")"; then
407− if ! nixDrv="$(nix-instantiate '<nixpkgs>' --add-root "$tmpDir/nix.drv" --indirect -A nix "${extraBuildFlags[@]}")"; then
408− if ! nixStorePath="$(nix-instantiate --eval '<nixpkgs/nixos/modules/installer/tools/nix-fallback-paths.nix>' -A "$(nixSystem)" | sed -e 's/^"//' -e 's/"$//')"; then
432+ if ! nixDrv="$(runCmd nix-instantiate '<nixpkgs/nixos>' --add-root "$tmpDir/nix.drv" --indirect -A config.nix.package.out "${extraBuildFlags[@]}")"; then
433+ if ! nixDrv="$(runCmd nix-instantiate '<nixpkgs>' --add-root "$tmpDir/nix.drv" --indirect -A nix "${extraBuildFlags[@]}")"; then
434+ if ! nixStorePath="$(runCmd nix-instantiate --eval '<nixpkgs/nixos/modules/installer/tools/nix-fallback-paths.nix>' -A "$(nixSystem)" | sed -e 's/^"//' -e 's/"$//')"; then
409435 nixStorePath="$(prebuiltNix "$(uname -m)")"
410436 fi
411− if ! nix-store -r "$nixStorePath" --add-root "${tmpDir}/nix" --indirect \
437+ if ! runCmd nix-store -r "$nixStorePath" --add-root "${tmpDir}/nix" --indirect \
412438 --option extra-binary-caches https://cache.nixos.org/; then
413439 echo "warning: don't know how to get latest Nix" >&2
414440 fi
415441 # Older version of nix-store -r don't support --add-root.
416442 [ -e "$tmpDir/nix" ] || ln -sf "$nixStorePath" "$tmpDir/nix"
417443 if [ -n "$buildHost" ]; then
418− remoteNixStorePath="$(prebuiltNix "$(buildHostCmd uname -m)")"
444+ remoteNixStorePath="$(runCmd prebuiltNix "$(buildHostCmd uname -m)")"
419445 remoteNix="$remoteNixStorePath/bin"
420446 if ! buildHostCmd nix-store -r "$remoteNixStorePath" \
421447 --option extra-binary-caches https://cache.nixos.org/ >/dev/null; then
@@ -442,8 +468,8 @@ fi
442468# Update the version suffix if we're building from Git (so that
443469# nixos-version shows something useful).
444470if [[ -n $canRun && -z $flake ]]; then
445− if nixpkgs=$(nix-instantiate --find-file nixpkgs "${extraBuildFlags[@]}"); then
446− suffix=$($SHELL "$nixpkgs/nixos/modules/installer/tools/get-version-suffix" "${extraBuildFlags[@]}" || true)
471+ if nixpkgs=$(runCmd nix-instantiate --find-file nixpkgs "${extraBuildFlags[@]}"); then
472+ suffix=$(runCmd $SHELL "$nixpkgs/nixos/modules/installer/tools/get-version-suffix" "${extraBuildFlags[@]}" || true)
447473 if [ -n "$suffix" ]; then
448474 echo -n "$suffix" > "$nixpkgs/.version-suffix" || true
449475 fi