numtide / devshell

Per project developer environments
https://numtide.github.io/devshell/
MIT License
1.22k stars 87 forks source link

modules: introduce userhosts #225

Closed bobvanderlinden closed 1 year ago

bobvanderlinden commented 1 year ago

The userhosts module allows changing DNS lookups within the shell. Where usually you'd change /etc/hosts, it is now possible to change these entries within the shell without root.

This uses LD_PRELOAD to let ld.so load the libuserhosts.so library and the HOSTS_FILE is set to a Nix-generated hosts file containing the hosts entries from the devshell configuration.

This PR is similar to https://github.com/numtide/devshell/pull/75, but is based on userhosts instead of hostctl.

Example:

pkgs.mkShell {
  imports = [
    ./extra/services/userhosts.nix
  ];

  services.userhosts.hosts = {
    "127.0.0.1" = [ "mydomain.test" "example.org" ];
  };
}

Which allows:

[devshell]$ nc -v example.org 8080
nc: connect to example.org (127.0.0.1) port 8080 (tcp) failed: Connection refused
bobvanderlinden commented 1 year ago

I've found that this solution isn't great. It'll crash programs that were linked to a different glibc version, so using any executables that aren't defined in the shell can be affected.

It showed up on Debian, which has an older version of glibc. git crashed because the LD_PRELOAD of userhosts resulted in an incompatible glibc version being loaded first.

flokli commented 1 year ago

@bobvanderlinden yes, NSS isn't great. I wrote about this stuff in https://flokli.de/posts/2022-11-18-nsncd/.

This however speaks about per-host NSS config.

I'd probably recommend using *.localhost hostnames when you want to have multiple vhosts pointing back to your own machine. That should work on most linux systems using nss-systemd.

https://serverfault.com/a/1103995 suggests it might work on MacOS too, but i didn't check.

bobvanderlinden commented 1 year ago

Yea, if I could change all applications at my workplace to use *.localhost I would, but it's not trivial to do so atm :sweat_smile: nsncd sounds good though, I'll enable it on my system.

flokli commented 1 year ago

Thanks! Please reach out if you encounter any issues, I'm very interested in them :-)