From 241c986cdfc4e7f927d4696b688a2cab1deb8eb6 Mon Sep 17 00:00:00 2001
From: charliemirabile <46761267+charliemirabile@users.noreply.github.com>
Date: Mon, 24 Feb 2025 17:37:09 -0500
Subject: [PATCH 1/3] submatrix: add missing apk update

all of the othe containerfiles update the package list before installing
to ensure that they install the latest versions.
---
 submatrix/Containerfile | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/submatrix/Containerfile b/submatrix/Containerfile
index 91d8178c..d59b8f50 100644
--- a/submatrix/Containerfile
+++ b/submatrix/Containerfile
@@ -1,11 +1,13 @@
 FROM alpine:3.20 as submatrix
 
-RUN apk add \
-	synapse \
-	envsubst \
-	py3-curl \
-	tzdata \
-	;
+RUN apk update && \
+	apk add \
+		synapse \
+		envsubst \
+		py3-curl \
+		tzdata \
+	&& \
+	:
 
 COPY homeserver.yaml.template /etc/synapse/homeserver.yaml.template
 

From e716f8d7e7aa51917d12a4de01726a039cbc8854 Mon Sep 17 00:00:00 2001
From: charliemirabile <46761267+charliemirabile@users.noreply.github.com>
Date: Mon, 24 Feb 2025 17:31:34 -0500
Subject: [PATCH 2/3] submatrix: use multistage build to avoid cluttering final
 image

We do not need envsubst at runtime or the unpopulated template.

Have a build stage where where we create the homeserver.yaml and then copy
only the final resulting file into the submatrix image
---
 submatrix/Containerfile | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/submatrix/Containerfile b/submatrix/Containerfile
index d59b8f50..75cdb326 100644
--- a/submatrix/Containerfile
+++ b/submatrix/Containerfile
@@ -1,26 +1,35 @@
+FROM alpine:3.20 as build
+
+RUN apk update && \
+	apk add \
+		envsubst \
+	&& \
+	:
+
+COPY homeserver.yaml.template /etc/synapse/homeserver.yaml.template
+
+ARG MATRIX_HOSTNAME
+
+RUN envsubst '$MATRIX_HOSTNAME' < /etc/synapse/homeserver.yaml.template > /etc/synapse/homeserver.yaml
+
 FROM alpine:3.20 as submatrix
 
+COPY --from=build /etc/synapse/homeserver.yaml /etc/synapse/homeserver.yaml
+
 RUN apk update && \
 	apk add \
 		synapse \
-		envsubst \
 		py3-curl \
 		tzdata \
 	&& \
 	:
 
-COPY homeserver.yaml.template /etc/synapse/homeserver.yaml.template
-
 COPY homeserver.log.config /etc/synapse/homeserver.log.config
 
 COPY orbit_auth.py /usr/local/share/submatrix/
 
 COPY start.sh /usr/local/share/submatrix/
 
-ARG MATRIX_HOSTNAME=localhost
-
-RUN envsubst '$MATRIX_HOSTNAME' < /etc/synapse/homeserver.yaml.template > /etc/synapse/homeserver.yaml
-
 USER 100:100
 
 ENTRYPOINT ["/usr/local/share/submatrix/start.sh"]

From df6bfe4bea37764e7ef823ef36c2d130f5963693 Mon Sep 17 00:00:00 2001
From: charliemirabile <46761267+charliemirabile@users.noreply.github.com>
Date: Mon, 24 Feb 2025 17:32:08 -0500
Subject: [PATCH 3/3] submatrix: use .pth file to point to auth module

We can add our folder with the auth code to the list of places the module
import code will look by putting a `.pth` file in the site-packages folder
that has the correct path inside of it. The exact location this file goes
depends on the python version, but python itself knows, so we can just use
a one-liner to create the file with its contents in the appropriate place.
---
 submatrix/Containerfile | 8 ++++++--
 submatrix/start.sh      | 3 ---
 2 files changed, 6 insertions(+), 5 deletions(-)
 delete mode 100755 submatrix/start.sh

diff --git a/submatrix/Containerfile b/submatrix/Containerfile
index 75cdb326..ad73ebb6 100644
--- a/submatrix/Containerfile
+++ b/submatrix/Containerfile
@@ -28,9 +28,13 @@ COPY homeserver.log.config /etc/synapse/homeserver.log.config
 
 COPY orbit_auth.py /usr/local/share/submatrix/
 
-COPY start.sh /usr/local/share/submatrix/
+# forgive the __import__ garbage, you can't start a with block after a semicolon (i.e. import foo; with...)
+# this creates a file in the sytem site-packages folder (whose exact location depends on the python version
+# and can only be discovered by asking python) that points to our folder for the auth plugin so that our
+# folder is added to the python path and synapse will be able to import the module code located in there.
+RUN python3 -c 'with open(__import__("pathlib").Path(__import__("site").getsitepackages()[0]) / "custom-site-pkgs.pth", "w") as f: f.write("/usr/local/share/submatrix\n")'
 
 USER 100:100
 
-ENTRYPOINT ["/usr/local/share/submatrix/start.sh"]
+ENTRYPOINT ["synapse_homeserver", "-c", "/etc/synapse/homeserver.yaml"]
 
diff --git a/submatrix/start.sh b/submatrix/start.sh
deleted file mode 100755
index 0d908cdf..00000000
--- a/submatrix/start.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/sh
-export PYTHONPATH="/usr/local/share/submatrix:${PYTHONPATH}"
-exec synapse_homeserver -c /etc/synapse/homeserver.yaml
