Skip to content

Instantly share code, notes, and snippets.

@mgreenly
Last active April 28, 2025 02:52
Show Gist options
  • Save mgreenly/3d65e1336b8c8adb417afe0ae8b636c4 to your computer and use it in GitHub Desktop.
Save mgreenly/3d65e1336b8c8adb417afe0ae8b636c4 to your computer and use it in GitHub Desktop.
nix-docker
FROM nixos/nix AS build
RUN nix-channel --update
# Build a profile and package store in the /scratch directory. Only the files we install
# during this docker build will end up in this profile.
RUN mkdir -p /scratch/store
# Install all desired packages into the scratch profile. It's important to know that the
# packages themselve are installed in /nix/store during this step and the /scratch/profile
# simply symlinks to them.
RUN nix-env --profile /scratch/profile -i cowsay
# Now copy all the files associated with /scratch/profile into /scratch/store.
RUN cp -va $(nix-store -qR scratch/profile) /scratch/store
FROM scratch
# Copy the /scratch directory created above to /nix, fixing the symlinks.
COPY --from=build /scratch/profile /nix/profile
COPY --from=build /scratch/store /nix/store
ENV PATH=/nix/profile/bin:$PATH
CMD ["cowsay", "moooooo!!!"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment