This is a simple article explaining how to manage entries in /etc/hosts in a NixOS system. Modifying this file is quite useful when you need to make tests on a remote server while its domain name is still not updated so you can force a domain name to be resolved by a given IP address, bypassing DNS queries.
NixOS being what is is, you can't modify the /etc/hosts file directly.
In your /etc/nixos/configuration.nix file, you have to declare the variable networking.extraHosts and use "\n" as separator for entries.
networking.extraHosts = "18.104.22.168 foobar.perso.pw\n22.214.171.124 foo.perso.pw";
or as suggested by @email@example.com on Mastodon, you can use multiple lines in the string as follow (using two single quotes character):
networking.extraHosts = '' 126.96.36.199 foobar.perso.pw 188.8.131.52 foo.perso.pw '';
The previous pieces of configuration will associate "foobar.perso.pw" to IP 184.108.40.206 and "foo.perso.pw" to IP 220.127.116.11.
Now, I need to rebuild my system configuration and use it, this can be done with the command `nixos-rebuild switch` as root.