Replacing nix-shell in CI script

I’m trying to use Nix as a way of unifying my local development environment and the docker based Gitlab CI build that I’m using.

I have a simple shell.nix that works perfectly - I can run nix-shell locally and get everything I need.

If I then use nixos/nix:latest as my docker image, I can then get everything working by surrounding every command I run in it with nix-shell --run "xxx". That’s tedious though - I want to run a command that will create that environment permanently in that docker container, and it’ll be available for every subsequent command.

I think what I want is something like nix-env -f shell.nix -i but that fails with “This derivation is not meant to be built, aborting”.

After some experimenting it appears that this is the magic command to use:

nix-env -f shell.nix -i -A 'buildInputs'

This will install into the environment the same things that nix-shell does.

Explanation for the uninitiated (me, yesterday):
nix-env - Manipulates user environments. In the same way nix-shell sets up a temporary environment that ends when you exit it, nix-env changes and existing user environment (i.e. the things that are available when using a shell as that user)

-f shell.nix Use derviations in shell.nix

-i Install some things

-A buildInputs this uses the buildInputs attribute to provide the list of derivations to install. buildInputs is the list provided to mkShell in shell.nix to specify the things that should be available in the environment.

4 Likes