I have a flake nixos configurations, where networking.hostName="" so hostname is obtain from the DHCP server. My problem is that I want to use the hostname as default value for an option in a module, but I can not find a way to get the hostname. Have tried:
config.networking.hostName but then get "", that we can expect.
builtins.getEnv "HOSTNAME but you get "" since we are using flakes
builtins.readFile /etc/hostname not allowed when using flakes
I can get getEnv or readFile to work with the --impure flag, but will try to avoid it.
Any idea to what I can do. Maybe it is possible to say that it is allowed to call environment variable HOSTNAME or read file /etc/hostname?
This is inherently impure, because you’re trying to get information from the running system that can change dynamically.
The best way to do this is to have the application you’re configuring query the host name at run time, rather than setting it in configuration. How this is done, if possible, depends on what you’re configuring, can’t say without more info.
If that’s not possible, everything you do will basically just be hard-coding anyway (since it will take the value at build time, not whenever you run the config in the future), so just set the value to the string you’re expecting or use --impure - both will break in the same situations, the former lets you skip the flag and at least is reproducible.