dockerTools.buildImage ADD/COPY equivalent

If, for example, I have a python script that I want to copy into a docker image how would I do that with dockerTools.buildImage? I’m looking for something like ADD or COPY in a normal Dockerfile.

You’re looking for contents https://nixos.org/manual/nixpkgs/stable/#ssec-pkgs-dockerTools-buildLayeredImage

I haven’t tested this, but from the description, this should work:

buildImage {
  ...
  contents = [
    ./myscript.py
  ];
}

and then myscript.py should exist at /myscript.py within the container

EDIT:
Paths will be added to the nixstore, (e.g. /nix/store/…-myscript.py) then included as an input for the derivation
https://nixos.wiki/wiki/Nix_Expression_Language

3 Likes

I solved by creating a new directory, putting my script there, then including ./directory in contents. Thanks!

Note to anyone in the future stuck on this: it needs to be in a directory, just specifying the filename will give you errors

2 Likes

I have just tried this approach (contents = [ ./dir ];) and got an error:

Executing pre-mount steps...
Adding contents...
Adding /absolute/path/to/dir
rsync: change_dir "/absolute/path/to/dir" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1189) [sender=3.1.3]

The path it shows is correct.