Skip to content

Instantly share code, notes, and snippets.

@ento
Last active April 30, 2023 04:19
Show Gist options
  • Save ento/641ad9dd18db0fabd4fca1a3dfd78efb to your computer and use it in GitHub Desktop.
Save ento/641ad9dd18db0fabd4fca1a3dfd78efb to your computer and use it in GitHub Desktop.
WIP: Dockerfile that might be able to build Nix dependencies first in a separate layer. Ref: https://mitchellh.com/writing/nix-with-dockerfiles#user-content-fnref-2
FROM nixos/nix:latest AS builder
RUN nix-env --install jq
COPY . /tmp/build
WORKDIR /tmp/build
RUN drvpath=$(nix --extra-experimental-features "nix-command flakes" derivation show | jq -r 'to_entries[0] | .key') \
&& echo $drvpath is the path \
&& closure=$(nix-store --query --requisites $drvpath | grep -v -F $drvpath) \
&& nix-store -r $closure
RUN nix \
--extra-experimental-features "nix-command flakes" \
--option filter-syscalls false \
build
RUN mkdir /tmp/nix-store-closure
RUN cp -R $(nix-store -qR result/) /tmp/nix-store-closure
FROM scratch
WORKDIR /app
COPY --from=builder /tmp/nix-store-closure /nix/store
COPY --from=builder /tmp/build/result /app
CMD ["/app/bin/app"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment