Spin up Postgres for testing and connect to it during build/test

I’m interested in transitioning to Nix to build my Rust crate with cargo2nix. I managed to get the project building with Nix. But my integration tests (usually in Rust crates they are placed in a top-level tests directory) need a local Postgres instance. Is it possible to spin up an instance when I run nix build .#tests and tear it down right after?

I’m new to Nix ecosystem so it would be great if you can point me to a function I should be using.

I’d be curious wether this works inside stdenv.mkDerivation, but from
what I know you’d need a NixOS integration test to achieve this task. In
the end it’s another Nix derivation, but you configure a whole NixOS
system in it, including a running Postgresql instance.


for insipiration.