How can I spawn a redis instance within a nix-build?

Hello to all users,

I am using the install-nix-action-v6 currently on github to have a build workflow for my haskell package sources over there.

This haskell package does not much for now but query a redis server, just pings it to be exact.

Locally everything was fine so far. I nix-env’d a redis and spawned the redis-server, which got discovered, when the nix-build was executed and the package got it’s ping resolved by the server answering pong.

On the github-ci workflow side I tried different approaches including nix-env as locally etc., using a docker-based redis as service and using some other third-party action that installed redis onto the ci-machine.
I even injected REDIS_HOST and REDIS_PORT to the environment with no change to the end that all options regarding building on github’s CI lead to the error message:

Network.Socket.connect: <socket: 16>: does not exist (Connection refused)

To be translated roughly as: redis-server can’t be found by the haskell app.

I have a slight clue about it all but am unable yet to solve the problem. So my question would be for pointers or help what to do here.

The clue or part of the above question would be: Is it related to the pure nature of nix-build, might be visible just on github-CI due to effects on my local system, that the redis instance cannot be found? And if so, how would I prepare my nix file to include redis directly to the build and also spawn the server instance therein ?

Hi @573, maybe it isn’t so clear from the manuals but Nix builders usually forbid access to sockets in order to ensure that no impure side effects could alter the build process. If you need to do integration tests where you test your application to correctly work with external services I would advise you to do that using the NixOS testing infrastructure. You can find many examples of those tests in the /nixos/tests subdirectory of the nixpkgs repository

1 Like

If you can start server with a socket interface, then you may go that way. I hope redis supports socket path in REDIS_HOST

I also believe it is possible to bind loopback interface in “fixed-output derivations”. For inspiration:

1 Like

Thanks @danbst, I will try with an explicit configuration file provided by parameter when starting the redis service. I do hope though that this does prevent from the impurity @azazel75 mentions.

@azazel75 thanks, that information was indeed missing on my side, though I suspected it to be the case that external services will break purity. This is definitly something to work with.
And yes, I do indeed want integration tests using nixpkgs.
I will try and figure out how NixOS does it at the moment and if that applies to cachix-action/install-nix-action as well somehow.
I do hope to stick with github for that.

Hello @azazel75, I tried and came up with a little server.nix setup for now using the redis.nix from the nixpkgs repository (/nixos/tests) and the nice article here as a reference.

What I asked myself now is it in any way possible to run this framework outside of NixOS as I have just an Archlinux instance with nixpkgs locally available.

This is the error I got:

error: a 'x86_64-linux' with features {kvm, nixos-test} is required to build '/nix/store/gy01r3fh9sm19blv7257yc7k7qhwx2f3-vm-test-run-redis.drv', but I am a 'x86_64-linux' with features {kvm}

You will need to add nixos-test to your system-features in /etc/nix/nix.conf.

1 Like

Wow @knedlsepp, that was the exact and faster than I dared hope for solution. Thanks!

P. S. I will add a comprehending gist later thus I will revoke the solution mark meanwhile, to be able to do so.

Have the proposed short writeup as a gist here.

Note that there is still a potential bug when running the test, but that is out of the scope of my initial question here. Has to do with permissions in the vm spawned by the test. If I come up with progress on the former I will open up a new topic and link it here.

Thanks again for helping!

To clarify my original question once more: I wanted to do an integration test for my haskell project.
Luckily I was able to do exactly that now w/o qemu or nixosTests even.
Just writing that addendum due to nixosTests foreseen to be testing service to service communication which on an OS level something like my little haskell project does not apply to.