git.iko.soy/typeable/nixpkgs
on master help sign in
plausible: first review fix iteration

* Most significant is probably the patching necessary to run plausible with postgres without superuser privilege. This change includes: * updating ecto_sql to 3.6 where `CREATE DATABASE` is only executed if it doesn't exist[1]. * patching a migration to only modify the `users.email` column (to use `citext` rather than creating the extension. `plausible-postgres` takes care of that). * Correctly declare dependencies in systemd. * A few minor fixes. [1] https://github.com/elixir-ecto/ecto_sql/commit/051baf669ef6ec92639ccc8b113d00b98047e927

commit02b15d0f5bc6bd821718d808e8dcdbc441449f72 authorMaximilian Bosch <maximilian@mbosch.me> dateSat 29 May 2021, 16:13 (5 years ago) parentba934e9
6 files changed+73−35
nixos/modules/services/web-apps/plausible.nix+28−25 nixos/modules/services/web-apps/plausible.xml+1−1 pkgs/servers/web-apps/plausible/default.nix+11−5 pkgs/servers/web-apps/plausible/ecto_sql-fix.patch+13 pkgs/servers/web-apps/plausible/skip-create-ext.patch+16 pkgs/servers/web-apps/plausible/update.sh+4−4
nixos/modules/services/web-apps/plausible.nix+28−25
@@ -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 })
nixos/modules/services/web-apps/plausible.xml+1−1
@@ -12,7 +12,7 @@
1212 <title>Basic Usage</title>
1313 <para>
1414 At first, a secret key is needed to be generated. This can be done with e.g.
15− <prompt>$ openssl rand -base64 64</prompt>
15+ <screen><prompt>$ </prompt>openssl rand -base64 64</screen>
1616 </para>
1717 <para>
1818 After that, <package>plausible</package> can be deployed like this:
pkgs/servers/web-apps/plausible/default.nix+11−5
@@ -14,10 +14,12 @@ let
1414 sha256 = "03lm1f29gwwixnhgjish5bhi3m73qyp71ns2sczdnwnbhrw61zps";
1515 };
1616
17− mixDeps = beamPackages.fetchMixDeps {
17+ # TODO consider using `mix2nix` as soon as it supports git dependencies.
18+ mixFodDeps = beamPackages.fetchMixDeps {
1819 pname = "${pname}-deps";
1920 inherit src version;
20− sha256 = "sha256-66zSdYmis3UnbdLkPi649RbPbMPI5gVeFlaMekOy5CQ=";
21+ sha256 = "sha256-pv/zXcku+ZgxV1804kIfDZN0jave2qG3rgZwm4yGA6I=";
22+ patches = [ ./ecto_sql-fix.patch ];
2123 };
2224
2325 yarnDeps = mkYarnModules {
@@ -28,15 +30,15 @@ let
2830 yarnLock = ./yarn.lock;
2931 preBuild = ''
3032 mkdir -p tmp/deps
31− cp -r ${mixDeps}/phoenix tmp/deps/phoenix
32− cp -r ${mixDeps}/phoenix_html tmp/deps/phoenix_html
33+ cp -r ${mixFodDeps}/phoenix tmp/deps/phoenix
34+ cp -r ${mixFodDeps}/phoenix_html tmp/deps/phoenix_html
3335 '';
3436 postBuild = ''
3537 echo 'module.exports = {}' > $out/node_modules/flatpickr/dist/postcss.config.js
3638 '';
3739 };
3840in beamPackages.mixRelease {
39− inherit pname version src mixDeps;
41+ inherit pname version src mixFodDeps;
4042
4143 nativeBuildInputs = [ nodejs ];
4244
@@ -47,6 +49,10 @@ in beamPackages.mixRelease {
4749 url = "https://github.com/Ma27/analytics/commit/f2ee5892a6c3e1a861d69ed30cac43e05e9cd36f.patch";
4850 sha256 = "sha256-JvJ7xlGw+tHtWje+jiQChVC4KTyqqdq2q+MIcOv/k1o=";
4951 })
52+
53+ # CREATE EXTENSION requires super-user privileges. To avoid that, we just skip
54+ # the responsible SQL statement here and take care of it in the module.
55+ ./skip-create-ext.patch
5056 ];
5157
5258 passthru = {
pkgs/servers/web-apps/plausible/ecto_sql-fix.patchnew file+13
@@ -0,0 +1,13 @@
1+diff --git a/mix.exs b/mix.exs
2+index f6e3b9a..67687d1 100644
3+--- a/mix.exs
4++++ b/mix.exs
5+@@ -52,7 +52,7 @@ defmodule Plausible.MixProject do
6+ [
7+ {:bcrypt_elixir, "~> 2.0"},
8+ {:cors_plug, "~> 1.5"},
9+- {:ecto_sql, "~> 3.0"},
10++ {:ecto_sql, "~> 3.6"},
11+ {:elixir_uuid, "~> 1.2"},
12+ {:gettext, "~> 0.11"},
13+ {:jason, "~> 1.0"},
pkgs/servers/web-apps/plausible/skip-create-ext.patchnew file+16
@@ -0,0 +1,16 @@
1+diff --git a/priv/repo/migrations/20190430140411_use_citext_for_email.exs b/priv/repo/migrations/20190430140411_use_citext_for_email.exs
2+index ecb6f80..f3bf12f 100644
3+--- a/priv/repo/migrations/20190430140411_use_citext_for_email.exs
4++++ b/priv/repo/migrations/20190430140411_use_citext_for_email.exs
5+@@ -2,10 +2,6 @@ defmodule Plausible.Repo.Migrations.UseCitextForEmail do
6+ use Ecto.Migration
7+
8+ def change do
9+- execute "CREATE EXTENSION IF NOT EXISTS citext;"
10+-
11+- alter table(:users) do
12+- modify :email, :citext, null: false
13+- end
14++ execute "ALTER TABLE users ALTER COLUMN email TYPE citext;"
15+ end
16+ end
pkgs/servers/web-apps/plausible/update.sh+4−4
@@ -45,17 +45,17 @@ fake_hash="$(nix-instantiate --eval -A lib.fakeSha256 | xargs echo)"
4545sed -i "$dir/default.nix" \
4646 -e 's,version = ".*",version = "'"$nix_version"'",' \
4747 -e '/^ src = fetchFromGitHub/,+4{;s/sha256 = "\(.*\)"/sha256 = "'"$tarball_hash"'"/}' \
48− -e '/^ mixDeps =/,+3{;s/sha256 = "\(.*\)"/sha256 = "'"$fake_hash"'"/}'
48+ -e '/^ mixFodDeps =/,+3{;s/sha256 = "\(.*\)"/sha256 = "'"$fake_hash"'"/}'
4949
50−mix_hash="$(nix-build -A plausible.mixDeps 2>&1 | tail -n3 | grep 'got:' | cut -d: -f2- | xargs echo)"
50+mix_hash="$(nix-build -A plausible.mixFodDeps 2>&1 | tail -n3 | grep 'got:' | cut -d: -f2- | xargs echo)"
5151
52−sed -i "$dir/default.nix" -e '/^ mixDeps =/,+3{;s/sha256 = "\(.*\)"/sha256 = "'"$mix_hash"'"/}'
52+sed -i "$dir/default.nix" -e '/^ mixFodDeps =/,+3{;s/sha256 = "\(.*\)"/sha256 = "'"$mix_hash"'"/}'
5353
5454tmp_setup_dir="$(mktemp -d)"
5555trap "rm -rf $tmp_setup_dir" EXIT
5656
5757cp -r $tarball_path/* $tmp_setup_dir/
58−cp -r "$(nix-build -A plausible.mixDeps)" "$tmp_setup_dir/deps"
58+cp -r "$(nix-build -A plausible.mixFodDeps)" "$tmp_setup_dir/deps"
5959chmod -R a+rwx "$tmp_setup_dir"
6060
6161pushd $tmp_setup_dir/assets