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 | 2a12e5f | 2023-01-21 18:54:50 +0100 | [diff] [blame^] | 15 | .PHONY: all images clean init gc update #$(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 | |
Matthias Andreas Benkard | 2a12e5f | 2023-01-21 18:54:50 +0100 | [diff] [blame^] | 33 | update: |
| 34 | $(NIX_DOCKER) sh -c 'nix-channel --update' |
| 35 | |
Matthias Andreas Benkard | 53f3767 | 2021-02-04 19:06:55 +0100 | [diff] [blame] | 36 | # Builds and loads all known Docker images. |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 37 | images: $(IMAGES_LOAD) |
| 38 | |
| 39 | # The following approaches were all considered and discarded: |
| 40 | # |
| 41 | # Takes ~240 seconds (nextcloud image): |
| 42 | # |
Matthias Andreas Benkard | 656b426 | 2022-12-28 21:28:10 +0100 | [diff] [blame] | 43 | #%.load: images.nix |
| 44 | # $(NIX_DOCKER) sh -c '$$(nix-build --no-out-link images.nix -A $(patsubst %.load,%,$@).streamed)' | $(DOCKER) load |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 45 | # |
| 46 | # Takes ~60 seconds (nextcloud image): |
| 47 | # |
Matthias Andreas Benkard | 656b426 | 2022-12-28 21:28:10 +0100 | [diff] [blame] | 48 | #%.load: images.nix |
| 49 | # $(NIX_DOCKER) sh -c 'cat $$(nix-build --no-out-link images.nix -A $(patsubst %.load,%,$@).layered)' | $(DOCKER) load |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 50 | # |
| 51 | # The final approach takes ~20 seconds to build the nextcloud image. |
| 52 | |
Matthias Andreas Benkard | 53f3767 | 2021-02-04 19:06:55 +0100 | [diff] [blame] | 53 | # Builds a Docker image as a .bin file that you can load with 'docker |
| 54 | # load'. |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 55 | %.bin: images.nix |
Matthias Andreas Benkard | 656b426 | 2022-12-28 21:28:10 +0100 | [diff] [blame] | 56 | $(NIX_DOCKER) sh -c 'cp $$(nix-build --no-out-link images.nix -A $(patsubst %.bin,%,$@).layered) $@' |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 57 | |
Matthias Andreas Benkard | 53f3767 | 2021-02-04 19:06:55 +0100 | [diff] [blame] | 58 | # Builds and loads a Docker image into your local Docker daemon. |
Matthias Andreas Benkard | c55bfae | 2021-01-02 07:35:21 +0100 | [diff] [blame] | 59 | %.load: %.bin |
| 60 | $(DOCKER) load --input=$< |