Change etc/hosts using shell.nix

Is it possible to use lorri + shell.nix to modify /etc/hosts for a particular project on my machine?

1 Like


1 Like

Is it just me or does it not work in NixOS?

$ nixos-version
21.03pre244932.84d74ae9c9c (Okapi)
$ echo 'n' >> ~/.hosts
$ export HOSTALIASES=~/.hosts
$ curl -v n
* Could not resolve host: n
* Closing connection 0
curl: (6) Could not resolve host: n

Edit: added NixOS version.

1 Like

Yea you are correct. It does not seem to work on my end as well. :frowning:

@blaggacao has an interest in this topic


/etc is usually owned by root, which would mean you need to execute nix-shell as root. Which seems way outside of it’s normal bounds of responsibility.

Another option might be buildFHSUserEnv, but I’ve never used it with nix-shell.

I would try to see if there’s any user-level options that are available.

Found this:

It emulates per-user hosts using LD_PRELOAD. Just tested and it works fine for me.

for many reasons, I’m kinda of glad it doesn’t work.


:smiley: Indeed! I think you raised a valid point.

@j-k oh thanks! Indeed, I just prototyped this, fingers are still hot:

If someody has interest in promoting it’s inclusion, I (and certainly @zimbatm, too) would appreciate if you could expose your use case in the corresponding issue.

There is an ester egg on the related issue akin to figiel/hosts :wink:


I use buildFHSUserEnv with nix-shell all the time. It works fine (though I’d like more control over how much of /etc it carries over to ‘inside’ the userenv - right now it only selects the essentials, and I lose e.g. my zsh configuration).

I don’t see an obvious way to overwrite /etc/hosts in there, though.

@amanjeev You can now try out my branch, I just fixed the obvious things and it works now.

Thank you @blaggacao. I have been busy lately. Will try and let you know. Although I do not use devshell

mkDevShell is intended to be an improved successor of mkShell by the same author.