From 89670e27e101b9b30f5900fc1eb6530258d316b1 Mon Sep 17 00:00:00 2001 From: Gaurav Juvekar Date: Sun, 28 Jul 2024 20:42:18 -0700 Subject: [PATCH] home-manager: ignore hostname -f lookup errors `hostname -f` could fail depending on the resolver. Discard any stderr and test for the exit status before using the value for flake attribute lookup. I was unable to repro the exact bad exit status in #5665. With - nscd disabled, - nsswitch.conf pointing to 'files', - hostname entry removed from /etc/hosts `hostname -f` from inetutils-2.5 fell back to showing just the nodename from `uname(2)`. Injecting an empty string into the `(struct utsname).nodename` field of `uname(2)` using strace still exited with empty output and 0 exit-status. Fixes #5665 --- home-manager/home-manager | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/home-manager/home-manager b/home-manager/home-manager index 5bf796b4..ab4de538 100644 --- a/home-manager/home-manager +++ b/home-manager/home-manager @@ -198,9 +198,19 @@ function setFlakeAttribute() { ;; *) local name="$USER" + + local hostnameArray=() + # FQDN lookup can fail depending on the resolver. + local fqdn + fqdn="$(hostname -f 2> /dev/null)" + if [[ $? -eq 0 ]]; then + hostnameArray+=( "$USER@$fqdn" ) + fi # Check FQDN, long, and short hostnames; long first to preserve # pre-existing behaviour in case both happen to be defined. - for n in "$USER@$(hostname -f)" "$USER@$(hostname)" "$USER@$(hostname -s)"; do + hostnameArray+=( "$USER@$(hostname)" "$USER@$(hostname -s)" ) + + for n in "${hostnameArray[@]}"; do if [[ "$(nix eval "$flake#homeConfigurations" --apply "x: x ? \"$n\"")" == "true" ]]; then name="$n" if [[ -v VERBOSE ]]; then