Closed al0git closed 6 months ago
$HOST
(as well as $HOSTNAME
) is not standard variable. In fact, there is no environment variables corresponding to hostname
I don't know why do you have it in your environment, but others don't have it. So I suggest to not use $HOST
in chezmoi
Exactly, you can't set hostnames through environment variables. If you want the value of an environment variable then you can use the env
template function.
Yes the description is very misleading. Sorry for that. The problem has nothing to do with $HOST or $HOSTNAME. I know that some people rely on it to avoid calling hostname too often, but let's forget these variables.
The problem is that cm execute-template '{{ .chezmoi.hostname }}'
just does not return the name of the machine. That is, it does not return the same result that hostname
does in some conditions. Theses conditions depending on /etc/hosts contents.
That is correct. It turns out that getting the hostname of a machine in a reliable way that works across multiple operating systems is hard.
If you want to use the output of hostname
on your machines then you can use the template:
{{ output "hostname" | trim }}
If you want to avoid calling hostname
every time then put this in your config file template:
[data]
hostname = {{ output "hostname" | trim | quote }}
and then use
{{ .hostname }}
in your templates.
First thanks for the trick, config file template are very powerful. I even cheated using chezmoi.hostname =
instead of hostname =
to avoid touching my templates and it is working.
For the bug, it seems this is a regression between version 2.31.0 and 2.46.1. I'll try to narrow it when I have a little bit more time (not a go expert). Maybe the order of the methods changed ...
Thanks for your help.
For the bug, it seems this is a regression between version 2.31.0 and 2.46.1. I'll try to narrow it when I have a little bit more time (not a go expert). Maybe the order of the methods changed ...
This is not going to be a regression, but an algorithmic change because of other bugs. There are some explicit changes made between those two versions (#3055, #3082, #3104).
As Tom says, this is hard and every source system does this slightly differently — even different Linux distributions do it slightly differently or may have different results in some contexts (e.g., inside Docker).
First thanks for the trick, config file template are very powerful. I even cheated using
chezmoi.hostname =
instead ofhostname =
to avoid touching my templates and it is working.
Don't do this. It’s not a supported behaviour and the contents of .chezmoi
should be treated as read only data and not updated during templating. We do not guarantee that this will work in the future and may make changes in the future that rebind the chezmoi runtime data before template execution that will break this.
That's clear, thanks.
Describe the bug
.chezmoi.hostname has sometimes a bad value on linux. It infers a value from /etc/hosts which is incorrect instead of using HOST when defined.
To reproduce
Add a line in /etc/hosts like this one 127.0.0.10 www.localdomain.tld
Check your hostname
Expected behavior
chezmoi
should not be sensitive to/etc/hosts
contents and usehostname
or$HOST
as value.Output of command with the
--verbose
flagOutput of
chezmoi doctor
Additional context