Hi all,
We have encountered the following situation in a test that we are writing currently for the federated reddit clone Lemmy (https://github.com/ngi-nix/nixpkgs/blob/mob/lemmy-improvements-new/nixos/tests/lemmy.nix).
Our goal here is to setup a nixos test with a lemmy server such that we can, not only explore it in the terminal with the interactive test driver but also through our host machine browser. We have succeeded to do so in the following way, but it is conflicting a bit with how tests are typically setup:
First, it’s important that Lemmy gets served through the reverse proxy Caddy that then hands of requests to the backend, ui and other attached services. To route a request, Caddy listens on specific IP addresses and ports, and checks whether the hostname header in the http request corresponds to a pre-configured one. The hostname is passed to caddy in the lemmy module here.
As Caddy is currently setup, it is therefore not enough to just ping the correct IP address from a different hostname. E.g. if the hostname is set to http://lemmy.com
, we can launch the test driver from nixpkgs root directory with $(nix-build -A nixosTests.lemmy.driverInteractive)/bin/nixos-test-driver
, then start the virtual machines with start_all()
in the Python interpreter, forward a host port of the server node port 80 that caddy listens to in the test executing server.forward_port(8888, 80)
in the Python interpreter, and finally connect to it from outside of the test environment with curl -v -H Host:lemmy.com localhost:8888
successfully. But leaving out the -H
argument from curl won’t work and it’s also not possible to connect to Lemmy from the browser with the URL localhost:8888
without installing cumbersome browser extensions that set this host header.
The inability to examine the test machines with a browser is why we starting setting the hostname
to http://lemmy.localhost
. After running the interactive driver and forwarding the port in the same way as before, we can now simply do curl -v localhost:8888
and also access and use lemmy from the browser by just entering lemmy.localhost:8888
in the URL bar.
However, this hostname somewhat goes against the way nixos tests are setup at the moment where the default hostname is the same as the name that is used for the nixOS virtual machine. For example, checkout this tutorial on nix.dev which says: “Between the machines defined inside the nodes
attribute, hostnames are resolved based on their attribute names. In this case we have client
and server
.”.
We are now confronted with a few questions that we struggle to answer. The main question is, of course:
- Is there a simple way to enable the use case of connecting with a browser to a nixOS test that is consistent with the nixpkgs conventions?
- Are nixOS tests the right place for interactive browser testing? [I personally think it’s enormously valuable but would be happy to be convinced that an alternative is better]
Some subquestions that may lead to a solution:
- is there a simple way (without installing extensions or editing individual http requests) to modify the hostname header in a browser (e.g. on firefox and chrome)?
- is there a less invasive way to setup a temporary hostname on a system than editing the hosts file?
- is it bad practice to use a
*.localhost
hostname in a nixOS test? - is it bad practice for a reverse proxy like caddy to listen to several hostnames, one of them being
*.localhost
and the others referring to the internal nixos test hostnames?
ps: this is a followup of this question: