Network test is not supported inside a nix-build except localhost.
How to use localhost and random port?
And is it better to use a network namespace?
This is an interesting question! I suspect the reason that no one tried an answer yet suggests that it is also a hard one. Could you elaborate what exactly you are trying to achieve, maybe with some sample code?
If you’re trying to test network protocols and the like,try
What if you’re trying to leverage SQLx’s (Rust sql framework) compile-time verification?
Normally people spin up a PGDB in a docker container so as to allow this during compile-time (read: cargo build).
Wait I totally misunderstood thew whole stuff here.
SQLx does not use docker at all to create DBs during tests.
It uses the DATABASE_URL var you provide.
I was getting
message: "permission denied to create database"
But it was only because I did not set the right permissions for postgres user.
Working example here, in a nix flake, using crane + SQLx:
craneLib.buildPackage (commonArgs // {
[...]
preBuild = ''
export DATABASE_URL=postgresql://postgres:postgres@localhost/test-db
${pkgs.postgresql}/bin/initdb -D .tmp/test-db
${pkgs.postgresql}/bin/pg_ctl -D .tmp/test-db -l .tmp/test-db.log -o "--unix_socket_directories='$PWD'" start
${pkgs.postgresql}/bin/createuser postgres -d -s -h $PWD
${pkgs.postgresql}/bin/createdb test-db -h $PWD --owner=postgres
${pkgs.sqlx-cli}/bin/sqlx migrate run
'';
[...]
}
Test works in rust with that, for me, no need for network access.
(Notice createuser postgres -d -s which adds the superuser -s and createdb -d roles)