Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I apply a nix-shell config in a docker image?

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, in my CI config I then use nixos/nix:latest as my docker image, and surround every command with nix-shell --run "xxx" that all works. That nix-shell all over the place is tedious to maintain and hard to read what is actually going on. What I'd like to do is apply that configuration to the docker image somehow as the first command, so that the environment is available to all the subsequent build commands.

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”.

Thanks in advance.

like image 275
WillW Avatar asked Oct 16 '19 20:10

WillW


People also ask

What is shell command in docker?

The SHELL instruction allows the default shell used for the shell form of commands to be overridden. The default shell on Linux is ["/bin/sh", "-c"] , and on Windows is ["cmd", "/S", "/C"] . The SHELL instruction must be written in JSON form in a Dockerfile.

What is shell Nix?

Description. The command nix-shell will build the dependencies of the specified derivation, but not the derivation itself. It will then start an interactive shell in which all environment variables defined by the derivation path have been set to their corresponding values, and the script $stdenv/setup has been sourced.

What is the command to execute a container from an image?

To run an image inside of a container, we use the docker run command. The docker run command requires one parameter and that is the image name. Let's start our image and make sure it is running correctly.


1 Answers

You can use buildEnv to combine your tools into a single derivation.

default.nix:

{ pkgs ? import <nixpkgs> {} }:

pkgs.buildEnv {
  name = "my-tools";
  paths = [
    pkgs.hello
    pkgs.figlet
  ];
}

Now you can install your tools in the docker image:

nix-env -i -f default.nix

And use it in your shell.nix:

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  name = "shell-dummy";
  buildInputs = [ (import ./default.nix { inherit pkgs; }) ];
}

See also the Declarative Package Management section in the Nixpkgs manual.

like image 121
Robert Hensing Avatar answered Nov 07 '22 09:11

Robert Hensing