From e7eba9cc46547ae86642ad3c6a9a4fb22c07bc26 Mon Sep 17 00:00:00 2001 From: Kamal Al Marhubi Date: Tue, 29 Nov 2022 16:26:00 -0500 Subject: [PATCH] home-manager: refine flake URI lookup Depending on DHCP settings you might end up with different output from running `hostname`. Eg, your local hostname is `mylaptop`, and your home router is configured with a local domain of `.hoome.arpa`. In this case: $ hostname mylaptop.home.arpa $ hostname -s mylaptop If you then go to cafe which has its router configured with `.lan` as its local domain. Then, if your DHCP settings accept the local domain from the router, $ hostname myalaptop.lan $ hostname -s mylaptop With the pre-existing behaviour, if you had a `"me@mylaptop.home.arpa"` entry in `outputs.homeConfigurations`, running `home-manager switch` would fail: $ home-manager switch error: flake 'git+file:///home/me/.config/nixpkgs' does not provide attribute 'packages.aarch64-darwin.homeConfigurations."me".activationPackage', 'legacyPackages.aarch64-darwin.homeConfigurations."me".activationPackage' or 'homeConfigurations."me".activationPackage' After this commit, you can put configuration in a `"me@mylaptop"` entry in `outputs.homeConfigurations`, and everything will work on either network. --- home-manager/home-manager | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/home-manager/home-manager b/home-manager/home-manager index 9a5af6d93..459908f2e 100644 --- a/home-manager/home-manager +++ b/home-manager/home-manager @@ -106,10 +106,17 @@ function setFlakeAttribute() { local name="${FLAKE_ARG#*#}" ;; *) - local name="$USER@$(hostname)" - if [ "$(nix eval "$flake#homeConfigurations" --apply "x: x ? \"$name\"")" = "false" ]; then - name="$USER" - fi + local name="$USER" + # Check both long and short hostnames; long first to preserve + # pre-existing behaviour in case both happen to be defined. + for n in "$USER@$(hostname)" "$USER@$(hostname -s)"; do + if [[ "$(nix eval "$flake#homeConfigurations" --apply "x: x ? \"$n\"")" == "true" ]]; then + name="$n" + if [[ -v VERBOSE ]]; then + echo "Using flake homeConfiguration for $name" + fi + fi + done ;; esac export FLAKE_CONFIG_URI="$flake#homeConfigurations.\"$name\""