Fix matrix start race condition

When starting matrix, keycloak and nginx need to be up or matrix
will crash due to failing to initialize its OIDC connection. By
waiting in the prestart, we can prevent this

Change-Id: I04bcace7ba96f6fb1e330c08677de2a2dd252c1c
Reviewed-on: https://git.clicks.codes/c/Clicks/NixFiles/+/190
Tested-by: Skyler Grey <minion@clicks.codes>
Reviewed-by: Samuel Shuert <coded@clicks.codes>
diff --git a/modules/common/matrix.nix b/modules/common/matrix.nix
index 26b2c26..015c3f4 100644
--- a/modules/common/matrix.nix
+++ b/modules/common/matrix.nix
@@ -160,7 +160,8 @@
     };
   };
 
-  systemd.services.matrix-synapse.requires = [ "postgresql.service" "nginx.service" "keycloak.service" ];
+  systemd.services.matrix-synapse.requires = [ "postgresql.service" ];
+  systemd.services.matrix-synapse.after = [ "nginx.service" "keycloak.service" ];
 
 } (let isDerived = base != null;
 in if isDerived
@@ -186,9 +187,10 @@
         "${base.config.systemd.services.matrix-synapse.serviceConfig.ExecStart}");
 
     systemd.services.matrix-synapse.preStart = lib.mkForce
+      ("while [[ \"$(${pkgs.curl}/bin/curl -s -o /dev/null -w ''%{http_code}'' https://login.clicks.codes)\" != \"200\" ]]; do sleep 5; done; " +
       (builtins.replaceStrings [ "${synapse_cfgfile}" ]
         [ "${config.scalpel.trafos."synapse.yaml".destination}" ]
-        "${base.config.systemd.services.matrix-synapse.preStart}");
+        "${base.config.systemd.services.matrix-synapse.preStart}"));
 
     systemd.services.matrix-synapse.restartTriggers = [ synapse_cfgfile ];