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.
This commit is contained in:
Kamal Al Marhubi 2022-11-29 16:26:00 -05:00 committed by Robert Helgesson
parent 37713c6b04
commit e7eba9cc46
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89

View file

@ -106,10 +106,17 @@ function setFlakeAttribute() {
local name="${FLAKE_ARG#*#}" local name="${FLAKE_ARG#*#}"
;; ;;
*) *)
local name="$USER@$(hostname)" local name="$USER"
if [ "$(nix eval "$flake#homeConfigurations" --apply "x: x ? \"$name\"")" = "false" ]; then # Check both long and short hostnames; long first to preserve
name="$USER" # 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
fi
done
;; ;;
esac esac
export FLAKE_CONFIG_URI="$flake#homeConfigurations.\"$name\"" export FLAKE_CONFIG_URI="$flake#homeConfigurations.\"$name\""