blob: 44f6f8556143ef10f6f4a62fab69f3e53d220915 [file] [log] [blame]
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +01001RM_F = rm -f
2
3DOCKER = docker
Matthias Andreas Benkard53f37672021-02-04 19:06:55 +01004NIX_DOCKER_0 = $(DOCKER) run --rm --volume $$(pwd):$$(pwd) --workdir $$(pwd)
5NIX_DOCKER_MAINT = $(NIX_DOCKER_0) --mount source=type=volume,source=nix,target=/nix2 nixos/nix
6NIX_DOCKER = $(NIX_DOCKER_0) --mount source=type=volume,source=nix,target=/nix nixos/nix
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +01007
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +01008NIX_BUILD = $(NIX_DOCKER) nix-build
9NIX_BUILD_OUT = $(NIX_DOCKER) sh -c '$$(nix-build)'
10
11IMAGES = mailcow nano nextcloud prosody samba vim webcron
12IMAGES_BIN = $(patsubst %,%.bin,$(IMAGES))
13IMAGES_LOAD = $(patsubst %,%.load,$(IMAGES))
14
Matthias Andreas Benkard53f37672021-02-04 19:06:55 +010015.PHONY: all images clean init gc #$(IMAGES_LOAD)
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +010016
17all: images
18
Matthias Andreas Benkard53f37672021-02-04 19:06:55 +010019# Deletes all build artifacts.
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +010020clean:
21 $(RM_F) $(IMAGES_BIN)
22
Matthias Andreas Benkard53f37672021-02-04 19:06:55 +010023# Runs nix-collect-garbage in the Docker container.
24gc:
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.
30init:
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 Benkardc55bfae2021-01-02 07:35:21 +010034images: $(IMAGES_LOAD)
35
36# The following approaches were all considered and discarded:
37#
38# Takes ~240 seconds (nextcloud image):
39#
Matthias Andreas Benkard656b4262022-12-28 21:28:10 +010040#%.load: images.nix
41# $(NIX_DOCKER) sh -c '$$(nix-build --no-out-link images.nix -A $(patsubst %.load,%,$@).streamed)' | $(DOCKER) load
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +010042#
43# Takes ~60 seconds (nextcloud image):
44#
Matthias Andreas Benkard656b4262022-12-28 21:28:10 +010045#%.load: images.nix
46# $(NIX_DOCKER) sh -c 'cat $$(nix-build --no-out-link images.nix -A $(patsubst %.load,%,$@).layered)' | $(DOCKER) load
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +010047#
48# The final approach takes ~20 seconds to build the nextcloud image.
49
Matthias Andreas Benkard53f37672021-02-04 19:06:55 +010050# Builds a Docker image as a .bin file that you can load with 'docker
51# load'.
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +010052%.bin: images.nix
Matthias Andreas Benkard656b4262022-12-28 21:28:10 +010053 $(NIX_DOCKER) sh -c 'cp $$(nix-build --no-out-link images.nix -A $(patsubst %.bin,%,$@).layered) $@'
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +010054
Matthias Andreas Benkard53f37672021-02-04 19:06:55 +010055# Builds and loads a Docker image into your local Docker daemon.
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +010056%.load: %.bin
57 $(DOCKER) load --input=$<