@@ -5,10 +5,10 @@ with lib;
55let
66 cfg = config.services.plausible;
77
8+ # FIXME consider using LoadCredential as soon as it actually works.
89 envSecrets = ''
910 export ADMIN_USER_PWD="$(<${cfg.adminUser.passwordFile})"
1011 export SECRET_KEY_BASE="$(<${cfg.server.secretKeybaseFile})"
11− export RELEASE_TMP=/var/lib/plausible/tmp
1212 ${optionalString (cfg.mail.smtp.passwordFile != null) ''
1313 export SMTP_USER_PWD="$(<${cfg.mail.smtp.passwordFile})"
1414 ''}
@@ -51,9 +51,7 @@ in {
5151 default = "http://localhost:8123/default";
5252 type = types.str;
5353 description = ''
54− The URL to be used to connect to <package>postgres</package>. The format
55− is described in <link xlink:href="https://hexdocs.pm/ecto/Ecto.Repo.html#module-urls">
56− the elixir docs</link>.
54+ The URL to be used to connect to <package>clickhouse</package>.
5755 '';
5856 };
5957 };
@@ -110,7 +108,7 @@ in {
110108
111109 mail = {
112110 email = mkOption {
113− default = " hello@plausible.local";
111+ default = "hello@plausible.local";
114112 type = types.str;
115113 description = ''
116114 The email id to use for as <emphasis>from</emphasis> address of all communications
@@ -146,7 +144,7 @@ in {
146144 The path to the file with the password in case SMTP auth is enabled.
147145 '';
148146 };
149− enableSSL = mkEnableOption "";
147+ enableSSL = mkEnableOption "SSL when connecting to the SMTP server";
150148 retries = mkOption {
151149 type = types.ints.unsigned;
152150 default = 2;
@@ -162,7 +160,7 @@ in {
162160 assertions = [
163161 { assertion = cfg.adminUser.activate -> cfg.database.postgres.setup;
164162 message = ''
165− Unable to automatically activate the admin-user if no local DB-managed for
163+ Unable to automatically activate the admin-user if no locally DB-managed for
166164 postgres (`services.plausible.database.postgres.setup') is enabled!
167165 '';
168166 }
@@ -181,10 +179,13 @@ in {
181179 plausible = {
182180 inherit (pkgs.plausible.meta) description;
183181 documentation = [ "https://plausible.io/docs/self-hosting" ];
184− wantedBy = [ "multi-user.target" ]
185− ++ optional cfg.database.clickhouse.setup "clickhouse.service"
186− ++ optional cfg.database.postgres.setup "postgresql.service";
182+ wantedBy = [ "multi-user.target" ];
187183 after = optional cfg.database.postgres.setup "plausible-postgres.service";
184+ requires = optional cfg.database.clickhouse.setup "clickhouse.service"
185+ ++ optionals cfg.database.postgres.setup [
186+ "postgresql.service"
187+ "plausible-postgres.service"
188+ ];
188189
189190 environment = {
190191 # NixOS specific option to avoid that it's trying to write into its store-path.
@@ -196,6 +197,8 @@ in {
196197 PORT = toString cfg.server.port;
197198 DISABLE_REGISTRATION = boolToString cfg.server.disableRegistration;
198199
200+ RELEASE_TMP = "/var/lib/plausible/tmp";
201+
199202 ADMIN_USER_NAME = cfg.adminUser.name;
200203 ADMIN_USER_EMAIL = cfg.adminUser.email;
201204
@@ -210,8 +213,11 @@ in {
210213 SMTP_HOST_PORT = toString cfg.mail.smtp.hostPort;
211214 SMTP_RETRIES = toString cfg.mail.smtp.retries;
212215 SMTP_HOST_SSL_ENABLED = boolToString cfg.mail.smtp.enableSSL;
213− ${if cfg.mail.smtp.user != null then "SMTP_USER_NAME" else null} = cfg.mail.smtp.user;
214− };
216+
217+ SELFHOST = "true";
218+ } // (optionalAttrs (cfg.mail.smtp.user != null) {
219+ SMTP_USER_NAME = cfg.mail.smtp.user;
220+ });
215221
216222 path = [ pkgs.plausible ]
217223 ++ optional cfg.database.postgres.setup config.services.postgresql.package;
@@ -239,26 +245,23 @@ in {
239245 };
240246 }
241247 (mkIf cfg.database.postgres.setup {
242− # Unfortunately `plausible' requires super-user permissions in postgresql, so this
243− # has to be done imperatively here.
248+ # `plausible' requires the `citext'-extension.
244249 plausible-postgres = {
245250 after = [ "postgresql.service" ];
246251 bindsTo = [ "postgresql.service" ];
247252 requiredBy = [ "plausible.service" ];
248253 partOf = [ "plausible.service" ];
249254 serviceConfig.Type = "oneshot";
255+ unitConfig.ConditionPathExists = "!/var/lib/plausible/.db-setup";
250256 script = ''
251− if [ ! -e /var/lib/plausible/.db-setup ]; then
252− mkdir -p /var/lib/plausible/
253− PSQL() {
254− /run/wrappers/bin/sudo -Hu postgres ${config.services.postgresql.package}/bin/psql --port=5432 "$@"
255− }
256− PSQL -tAc "CREATE EXTENSION IF NOT EXISTS citext;"
257− PSQL -tAc "CREATE ROLE plausible WITH LOGIN;"
258− PSQL -tAc "CREATE DATABASE plausible WITH OWNER plausible;"
259− PSQL -tAc "ALTER USER plausible WITH SUPERUSER;"
260− touch /var/lib/plausible/.db-setup
261− fi
257+ mkdir -p /var/lib/plausible/
258+ PSQL() {
259+ /run/wrappers/bin/sudo -Hu postgres ${config.services.postgresql.package}/bin/psql --port=5432 "$@"
260+ }
261+ PSQL -tAc "CREATE ROLE plausible WITH LOGIN;"
262+ PSQL -tAc "CREATE DATABASE plausible WITH OWNER plausible;"
263+ PSQL -d plausible -tAc "CREATE EXTENSION IF NOT EXISTS citext;"
264+ touch /var/lib/plausible/.db-setup
262265 '';
263266 };
264267 })