blob: ff7e8f51e18b26ce02df3eb9a1b7818ff8266da7 [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 Benkard2a12e5f2023-01-21 18:54:50 +010015.PHONY: all images clean init gc update #$(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
Matthias Andreas Benkard2a12e5f2023-01-21 18:54:50 +010033update:
34 $(NIX_DOCKER) sh -c 'nix-channel --update'
35
Matthias Andreas Benkard53f37672021-02-04 19:06:55 +010036# Builds and loads all known Docker images.
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +010037images: $(IMAGES_LOAD)
38
39# The following approaches were all considered and discarded:
40#
41# Takes ~240 seconds (nextcloud image):
42#
Matthias Andreas Benkard656b4262022-12-28 21:28:10 +010043#%.load: images.nix
44# $(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 +010045#
46# Takes ~60 seconds (nextcloud image):
47#
Matthias Andreas Benkard656b4262022-12-28 21:28:10 +010048#%.load: images.nix
49# $(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 +010050#
51# The final approach takes ~20 seconds to build the nextcloud image.
52
Matthias Andreas Benkard53f37672021-02-04 19:06:55 +010053# Builds a Docker image as a .bin file that you can load with 'docker
54# load'.
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +010055%.bin: images.nix
Matthias Andreas Benkard656b4262022-12-28 21:28:10 +010056 $(NIX_DOCKER) sh -c 'cp $$(nix-build --no-out-link images.nix -A $(patsubst %.bin,%,$@).layered) $@'
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +010057
Matthias Andreas Benkard53f37672021-02-04 19:06:55 +010058# Builds and loads a Docker image into your local Docker daemon.
Matthias Andreas Benkardc55bfae2021-01-02 07:35:21 +010059%.load: %.bin
60 $(DOCKER) load --input=$<