Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 1 | RM_F = rm -f |
| 2 | |
| 3 | DOCKER = docker |
Matthias Andreas Benkard | 53f3767 | 2021-02-04 19:06:55 +0100 | [diff] [blame^] | 4 | NIX_DOCKER_0 = $(DOCKER) run --rm --volume $$(pwd):$$(pwd) --workdir $$(pwd) |
| 5 | NIX_DOCKER_MAINT = $(NIX_DOCKER_0) --mount source=type=volume,source=nix,target=/nix2 nixos/nix |
| 6 | NIX_DOCKER = $(NIX_DOCKER_0) --mount source=type=volume,source=nix,target=/nix nixos/nix |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 7 | |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 8 | NIX_BUILD = $(NIX_DOCKER) nix-build |
| 9 | NIX_BUILD_OUT = $(NIX_DOCKER) sh -c '$$(nix-build)' |
| 10 | |
| 11 | IMAGES = mailcow nano nextcloud prosody samba vim webcron |
| 12 | IMAGES_BIN = $(patsubst %,%.bin,$(IMAGES)) |
| 13 | IMAGES_LOAD = $(patsubst %,%.load,$(IMAGES)) |
| 14 | |
Matthias Andreas Benkard | 53f3767 | 2021-02-04 19:06:55 +0100 | [diff] [blame^] | 15 | .PHONY: all images clean init gc #$(IMAGES_LOAD) |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 16 | |
| 17 | all: images |
| 18 | |
Matthias Andreas Benkard | 53f3767 | 2021-02-04 19:06:55 +0100 | [diff] [blame^] | 19 | # Deletes all build artifacts. |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 20 | clean: |
| 21 | $(RM_F) $(IMAGES_BIN) |
| 22 | |
Matthias Andreas Benkard | 53f3767 | 2021-02-04 19:06:55 +0100 | [diff] [blame^] | 23 | # Runs nix-collect-garbage in the Docker container. |
| 24 | gc: |
| 25 | $(NIX_DOCKER) nix-collect-garbage |
| 26 | |
| 27 | # Initializes the Docker volume with a Nix store. |
| 28 | # |
| 29 | # Run this before any other target of this makefile. |
| 30 | init: |
| 31 | $(NIX_DOCKER_MAINT) sh -c 'nix-env -iA nixpkgs.rsync && rsync --info=progress2 -a /nix/ /nix2/' |
| 32 | |
| 33 | # Builds and loads all known Docker images. |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 34 | images: $(IMAGES_LOAD) |
| 35 | |
| 36 | # The following approaches were all considered and discarded: |
| 37 | # |
| 38 | # Takes ~240 seconds (nextcloud image): |
| 39 | # |
| 40 | # $(IMAGES): |
| 41 | # $(NIX_DOCKER) sh -c '$$(nix-build images.nix -A $@.streamed)' | $(DOCKER) load |
| 42 | # |
| 43 | # Takes ~60 seconds (nextcloud image): |
| 44 | # |
| 45 | # $(IMAGES): |
| 46 | # $(NIX_DOCKER) sh -c 'cat $$(nix-build images.nix -A $@.layered)' | $(DOCKER) load |
| 47 | # |
| 48 | # The final approach takes ~20 seconds to build the nextcloud image. |
| 49 | |
Matthias Andreas Benkard | 53f3767 | 2021-02-04 19:06:55 +0100 | [diff] [blame^] | 50 | # Builds a Docker image as a .bin file that you can load with 'docker |
| 51 | # load'. |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 52 | %.bin: images.nix |
| 53 | $(NIX_DOCKER) sh -c 'cp $$(nix-build images.nix -A $(patsubst %.bin,%,$@).layered) $@' |
| 54 | |
Matthias Andreas Benkard | 53f3767 | 2021-02-04 19:06:55 +0100 | [diff] [blame^] | 55 | # Builds and loads a Docker image into your local Docker daemon. |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 56 | %.load: %.bin |
| 57 | $(DOCKER) load --input=$< |