How to reference hostname in configuration.nix?

Basically, how do I do something like this?

services.nullmailer.config.allmailfrom = “${hostname}@domain.com”;

I feel like this should be pretty simple, but I’m only finding discussions about this in different contexts, like how to configure something using flakes or with home manager. I’m just looking to reference the hostname in a vanilla nixos configuration. I’m sure this must be possible, but… how?

Does "${config.networking.hostName}@domain.com" work?

2 Likes

It does! Thanks! I knew it had to be something simple. I had tried ${networking.hostName}, but didn’t realize I needed to prefix it with config.

Very much appreciated. Thank you.

1 Like

So where did you find this information or how did you know that these environment variables (assuming that’s what they are) were available?

My intention is not rudeness. I’m trying to learn ‘how to fish’.

1 Like

Great question!

Every configuration.nix file (every NixOS module in general, of which configuration.nix is an instance) is a function from an attrset of standard parameters to another attrset of configuration bits and pieces. One of those standard parameters is config, which is a reference to the ‘final’ configuration of the entire system (it’s just a regular Nix value, not some special keyword for accessing variables). From config, you can have access to any option defined in NixOS. If I didn’t happen to know that networking.hostName was the name of the desired NixOS option, I might have been able to find it in NixOS Search.

The module system—including what config is and what other input parameters are available—is documented here: NixOS modules - NixOS Wiki.

3 Likes