Building docker image with dockerTools.buildImage and Flake env

Hi all,

I am trying to build a simple docker image inside the “nixpkgs/nixos” and “nixpkgs/cachix-flakes” images with flake from, but I run into some difficulties.

{
  description = "Simple Docker Flake project";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let pkgs = nixpkgs.legacyPackages.${system};
      in {
        packages.hello = pkgs.dockerTools.buildImage {
          name = "hello";
          tag = "0.1.0";

          config = { Cmd = [ "${pkgs.hello}/bin/hello" ]; };

          created = "now";
        };

        devShells.default =
          pkgs.mkShell { buildInputs = with pkgs; [ bat vim ]; };
      });
}

When I run nix build .#hello command without using flake environment (direnv: use flake) everything works fine, but when I load flake environment and try to build docker image, I get the following error.

error: builder for
'/nix/store/kyfd16cspq8hrc0bjrpd846rkg213jxr-hello-config. json.drv' failed with exit code 1;
last 2 log lines:
> unpacking sources
- variable $src or $srcs should point to the source
For full logs, run 'nix log /nix/store/kyfd16cspq8hrcobjrpd846rkg213jxr-hello-config.json.drv'
error: 1 dependencies of derivation '/nix/store/r2502npbrcsvzips0px03d4xrv5k3i8-hello-config.ison.drv'failed to build
error: 1 dependencies of derivation '/nix/store/roza2pch58s573x615sfswrai60nav4x-docker-image-hello.tar.az.drv'failed to build

Can someone take a look at this and please tell me what I’m doing wrong?

The above Flake code works fine outside the docker environment.

I tried to reproduce this on NixOS unstable but I cannot help. It works on my system (as it does for you):

nix develop
nix build .\#hello

What do you mean with “I am trying to build a simple docker image inside the “nixpkgs/nixos” and “nixpkgs/cachix-flakes” images with flake from, but I run into some difficulties.”? Does this mean you trying to run this inside of container based on what image?

Please provide more details on how to reproduce this.

Pardon my clumsy instructions… here are the exact commands to reproduce the error.

docker run -it --name buildDockerImage -v /root/flake.nix:/root/flake.nix nixos/nix

cd /root && echo "use flake" > .envrc

echo "experimental-features = flakes nix-command" >> /etc/nix/nix.conf
nix profile install nixpkgs#direnv
direnv allow && eval "$(direnv export bash)"

nix build .#hello

It is unclear to me why this fails. It works fine if i omit direnv and simply do the build. I assume that something in the environment (from direnv) causes this.