How to use docker contents during the image build?

I’m on a macOS and trying to use Nix to build a Docker image for a project I’m working on. I’m starting Nix inside a makefile:

.PHONY: docker-ci-build
docker-ci-build:
	@rm -Rf tmp/nix
	@mkdir -p tmp/nix
	@cp makefile tmp/nix
	@cp -R {app,plugin} tmp/nix
	@docker run --rm -v $(PWD):/tyk -w /tyk nixos/nix:2.3.6 nix-build --no-out-link misc/nix/docker-ci.nix

This is the Nix file:

let
  pkgs = import (fetchTarball
    "https://github.com/NixOS/nixpkgs/archive/0fd4e3c87705d16a694b7feba0e3d593b8b0c3ac.tar.gz")
    { };

in pkgs.dockerTools.buildImage {
  name = "tyk";
  config = { WorkingDir = "/tyk"; };
  contents =
    [ pkgs.golangci-lint pkgs.go pkgs.docker pkgs.jq pkgs.bash /tyk/tmp/nix ];
  extraCommands = ''
    substituteInPlace makefile --replace "SHELL := /bin/bash" "SHELL=$BASH"
    make tyk-build
  '';
}

And this is the output:

Adding contents...
Adding /nix/store/10kid82ax97hq0ab3sj309iwfs532ggr-golangci-lint-1.22.2
Adding /nix/store/fkvrggd8q3b543h6bal3clg9arzq6qvq-go-1.14.8
Adding /nix/store/723mrv250hc2rh12rrlw0f0aapmm3ji5-docker-19.03.12
Adding /nix/store/pdg3mr2vsm7g8sh0q3ysac9dw0pl3c1i-jq-1.6-bin
Adding /nix/store/hrpvwkjz04s9i4nmli843hyw9z4pwhww-bash-4.4-p23
Adding /nix/store/k6yqvl824r4gp0jzr2hhj6v9wiwm6vr5-nix
/nix/store/hrpvwkjz04s9i4nmli843hyw9z4pwhww-bash-4.4-p23/bin/bash: go: command not found
make: *** [makefile:55: filter-cors-build] Error 127
builder for '/nix/store/8xsfsp8g2zqigk2nq5bxlgvbsqsmbfj2-docker-layer-tyk-ci.drv' failed with exit code 2
cannot build derivation '/nix/store/dy3dwy0qq7awn0j1phalphglwcncw83p-docker-image-tyk-ci.tar.gz.drv': 1 dependencies couldn't be built
error: build of '/nix/store/dy3dwy0qq7awn0j1phalphglwcncw83p-docker-image-tyk-ci.tar.gz.drv' failed
make: *** [docker-ci-build] Error 100

I’m using go inside the make tyk-build call. The question is, how can I use go there? I can’t use a derive to build the project because I don’t want to duplicate the makefile logic at Nix and the makefile is required for the developers.

Nevermind, just realised that that’s not possible and I had to add a derivation to do what I had to do.

You can actually do what you want.

extraCommands = ''
  PATH=${pkgs.gnumake}/bin:${pkgs.go}/bin:$PATH make tyk-build
'';

Or something like that.

Also, you might be even able to drop go from the contents then, as contents only requires to contain runtime dependencies.