I like to have the machine name in my prompt. I also predicate some startup configuration on the host name. Lastly, I have some scripts that alter their behavior based on the host name. What I do now is set a HOST var that is set to the output of the hostname command. It would be nice if this, or the output of uname -n, was automatically exposed by the platform module. Possibly in two forms: the FQDN and the machine name.
Note that elvish already runs hostname to populate the default right prompt (as well as running whoami).
Is it desirable to make this settable (requires root privileges of course)?
Also, should elvish query the hostname before every prompt, in case it has changed? Or perhaps on every invocation? It might easily change, at least in a world of DHCP provided network addresses.
Further to complicate things, note that macs have _three_ hostnames. You can query and set them with the scutil program. They are: HostName, ComputerName, and LocalHostName. The first of these is the one known by us unix geeks; the second one is the “user friendly” hostname, whatever that means; and the last one is the Bonjour name.
No, to allowing the value to be settable -- it should be readonly. For one thing changing it normally requires root privileges so would only be possible from an elvish instance run by root. More important it seems to me administrative changes to the machine configuration are outside the purview of Elvish.
Whether the value should be cached was discussed in the fish shell community ~4 years ago. The consensus was that it should be determined once and the value cached. For an interactive shell it's likely harmless if the hostname changes due to changing networks and getting a new DHCP lease. Scripts that use the value will be okay assuming they're not long running; that is, not acting like a daemon. Having said that, it might be worthwhile to provide a mechanism for forcing the value to be refreshed.
I think we can safely ignore OS specific features like macOS Bonjour names. And while it's possible for the network hostname to differ from the computer (machine) name reported by the uname API that is unlikely. But if the two values are different we probably don't care since the most salient name is the hostname; i.e., the DNS host name.
Also, note that the default Elvish right prompt caches the output of hostname via the constantly command. So far no one has complained :smile:
Agreed on the value being readonly. I merely asked to have the option on the table, the better to think about it. Also the other hostnames, for those who care they can call out to scutil themselves. But the fact that Macs can have three different hostnames could suitably be (briefly!) mentioned in the documentation of the variable.
Read-only hostname variable sounds good to me, but care needs to be taken regarding platform differences.
care needs to be taken regarding platform differences.
I've decided to simply defer to Go's os.Hostname. Which actually uses the OS uname() API by default. If and when someone tells us that is suboptimal on their system we can revisit the question.
I've verified that on all my systems I use for exploring elvish behavior (Linux, FreeBSD, macOS, WSL, Windows10) this produces reasonable results.
Also, I haven't provided a way to invalidate the cached information. Doing so is trivial but should remain unimplemented until we have a good reason to implement it.
If the hostname changed and I wished the shell to reflect that, I would probably just exec elvish. Of course that blows away any information I have collected in variables, but such data is usually short-lived anyway.
Most helpful comment
If the hostname changed and I wished the shell to reflect that, I would probably just exec elvish. Of course that blows away any information I have collected in variables, but such data is usually short-lived anyway.