Optimise `dockerTools.buildLayeredImage` size (`__pycache__`?)

I figured since I was testing ansible deployment scripts to be executed in gitlab-ci in a devShell, I might as well automatically produce docker images with the same functionality as said devShell, so any “docker-executor” (gitlab-runner) could use such image.

A PoC is here

The produced images are quite big though: tgz: ~200M, uncompressed: ~750M, and that’s “from scratch”…

I saw this related post, which indeed I can confirm, with buildImage there’s an extra 525M under /lib.

However, the image is still quite big, so I wondered whether there are things I can do to further optimise the size.

One thing that came to mind is to prevent the pythonPackage (?) builder from producing bytecode (rationale: why should a container image have caches?), but I don’t know how to achieve that (some stdEnv override?) or whether it is at all sensible to do anyway.