How to preinstall packages in nix docker image?

Hi. How to install packages to nixos/nix docker image?

The typical workflow for example with ubuntu is to:

  1. update package list
  2. install packages
  3. clean up packge list

How to these steps in nixos/nix docker image?

FROM nixos/nix
RUN set -x && \
  # hungs for like 2 minutes, bad for testing
  nix-channel --update  && \
  # will this work? Additionally, this is interably slow, I am still waiting...
  nix-env -iA nixpkgs.terraform  && \
  # how to clean up to reduce image size?

Thanks.

NixOS has a declarative configuration:

  1. author a NixOS configuration
  2. build it into a docker image (nixos-generators might help)

I recommend going through as much of https://nix.dev/ as you can.

Hi. Thank you for your response.

  1. While nixos has declarative configuration, the image nixos/nix has nix, not nixos. In fact, there is no /etc/nixos/configuration.nix inside the image and nixpkgs, not nixos, channel is added. Does that mean anything? Is this significant?

  2. While it is great to use nixos-generate to generate one docker image, the intention here is to use nix as part of CI/CD pipeline with all the required tools prepared and fetched beforehand, so to utilize docker caching. The idea is that devs would use docker build && docker run and utility nix to do the work. There is no nix on the host. Does it make sense?

  3. I am trying to understand nix for the past 3 months and I have several problems with my understanding. I couldn’t find on nix.dev how to use nix from docker (not how to use docker from nix, that’s the other way round). The only documentation for nixos/nix docker image I found is Using Nix within Docker - Nix Reference Manual . I am still curious as to how to use that image.

Does this do what you want?