2021-06-04 09:07:49 +00:00
|
|
|
{ lib, pkgs, config, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.plausible;
|
|
|
|
|
|
|
|
in {
|
|
|
|
options.services.plausible = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "plausible");
|
2021-06-04 09:07:49 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
package = mkPackageOption pkgs "plausible" { };
|
2023-04-29 16:46:19 +00:00
|
|
|
|
2021-06-04 09:07:49 +00:00
|
|
|
adminUser = {
|
|
|
|
name = mkOption {
|
|
|
|
default = "admin";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-04 09:07:49 +00:00
|
|
|
Name of the admin user that plausible will created on initial startup.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
email = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "admin@localhost";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-04 09:07:49 +00:00
|
|
|
Email-address of the admin-user.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.either types.str types.path;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-04 09:07:49 +00:00
|
|
|
Path to the file which contains the password of the admin user.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
activate = mkEnableOption (lib.mdDoc "activating the freshly created admin-user");
|
2021-06-04 09:07:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
database = {
|
|
|
|
clickhouse = {
|
2022-09-09 14:08:57 +00:00
|
|
|
setup = mkEnableOption (lib.mdDoc "creating a clickhouse instance") // { default = true; };
|
2021-06-04 09:07:49 +00:00
|
|
|
url = mkOption {
|
|
|
|
default = "http://localhost:8123/default";
|
|
|
|
type = types.str;
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The URL to be used to connect to `clickhouse`.
|
2021-06-04 09:07:49 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
postgres = {
|
2022-09-09 14:08:57 +00:00
|
|
|
setup = mkEnableOption (lib.mdDoc "creating a postgresql instance") // { default = true; };
|
2021-06-04 09:07:49 +00:00
|
|
|
dbname = mkOption {
|
|
|
|
default = "plausible";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-04 09:07:49 +00:00
|
|
|
Name of the database to use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
socket = mkOption {
|
|
|
|
default = "/run/postgresql";
|
|
|
|
type = types.str;
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Path to the UNIX domain-socket to communicate with `postgres`.
|
2021-06-04 09:07:49 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
server = {
|
|
|
|
disableRegistration = mkOption {
|
|
|
|
default = true;
|
2023-11-16 04:20:00 +00:00
|
|
|
type = types.enum [true false "invite_only"];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2023-11-16 04:20:00 +00:00
|
|
|
Whether to prohibit creating an account in plausible's UI or allow on `invite_only`.
|
2021-06-04 09:07:49 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
secretKeybaseFile = mkOption {
|
|
|
|
type = types.either types.path types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Path to the secret used by the `phoenix`-framework. Instructions
|
2021-06-04 09:07:49 +00:00
|
|
|
how to generate one are documented in the
|
2022-08-12 12:06:08 +00:00
|
|
|
[
|
|
|
|
framework docs](https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.Secret.html#content).
|
2021-06-04 09:07:49 +00:00
|
|
|
'';
|
|
|
|
};
|
2023-11-16 04:20:00 +00:00
|
|
|
listenAddress = mkOption {
|
|
|
|
default = "127.0.0.1";
|
|
|
|
type = types.str;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
The IP address on which the server is listening.
|
|
|
|
'';
|
|
|
|
};
|
2021-06-04 09:07:49 +00:00
|
|
|
port = mkOption {
|
|
|
|
default = 8000;
|
|
|
|
type = types.port;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-04 09:07:49 +00:00
|
|
|
Port where the service should be available.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
baseUrl = mkOption {
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-04 09:07:49 +00:00
|
|
|
Public URL where plausible is available.
|
2021-07-24 12:14:16 +00:00
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
Note that `/path` components are currently ignored:
|
|
|
|
[
|
2021-07-24 12:14:16 +00:00
|
|
|
https://github.com/plausible/analytics/issues/1182
|
2022-08-12 12:06:08 +00:00
|
|
|
](https://github.com/plausible/analytics/issues/1182).
|
2021-06-04 09:07:49 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mail = {
|
|
|
|
email = mkOption {
|
|
|
|
default = "hello@plausible.local";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The email id to use for as *from* address of all communications
|
2021-06-04 09:07:49 +00:00
|
|
|
from Plausible.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
smtp = {
|
|
|
|
hostAddr = mkOption {
|
|
|
|
default = "localhost";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-04 09:07:49 +00:00
|
|
|
The host address of your smtp server.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
hostPort = mkOption {
|
|
|
|
default = 25;
|
|
|
|
type = types.port;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-04 09:07:49 +00:00
|
|
|
The port of your smtp server.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
user = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-04 09:07:49 +00:00
|
|
|
The username/email in case SMTP auth is enabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
passwordFile = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr (either str path);
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-04 09:07:49 +00:00
|
|
|
The path to the file with the password in case SMTP auth is enabled.
|
|
|
|
'';
|
|
|
|
};
|
2022-09-09 14:08:57 +00:00
|
|
|
enableSSL = mkEnableOption (lib.mdDoc "SSL when connecting to the SMTP server");
|
2021-06-04 09:07:49 +00:00
|
|
|
retries = mkOption {
|
|
|
|
type = types.ints.unsigned;
|
|
|
|
default = 2;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-04 09:07:49 +00:00
|
|
|
Number of retries to make until mailer gives up.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "plausible" "releaseCookiePath" ] "Plausible uses no distributed Erlang features, so this option is no longer necessary and was removed")
|
|
|
|
];
|
|
|
|
|
2021-06-04 09:07:49 +00:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{ assertion = cfg.adminUser.activate -> cfg.database.postgres.setup;
|
|
|
|
message = ''
|
|
|
|
Unable to automatically activate the admin-user if no locally managed DB for
|
|
|
|
postgres (`services.plausible.database.postgres.setup') is enabled!
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
services.postgresql = mkIf cfg.database.postgres.setup {
|
|
|
|
enable = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
services.clickhouse = mkIf cfg.database.clickhouse.setup {
|
|
|
|
enable = true;
|
|
|
|
};
|
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2021-12-06 16:07:01 +00:00
|
|
|
|
2021-06-04 09:07:49 +00:00
|
|
|
systemd.services = mkMerge [
|
|
|
|
{
|
|
|
|
plausible = {
|
2023-04-29 16:46:19 +00:00
|
|
|
inherit (cfg.package.meta) description;
|
2021-06-04 09:07:49 +00:00
|
|
|
documentation = [ "https://plausible.io/docs/self-hosting" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-08-21 13:32:41 +00:00
|
|
|
after = optional cfg.database.clickhouse.setup "clickhouse.service"
|
|
|
|
++ optionals cfg.database.postgres.setup [
|
|
|
|
"postgresql.service"
|
|
|
|
"plausible-postgres.service"
|
|
|
|
];
|
2021-06-04 09:07:49 +00:00
|
|
|
requires = optional cfg.database.clickhouse.setup "clickhouse.service"
|
|
|
|
++ optionals cfg.database.postgres.setup [
|
|
|
|
"postgresql.service"
|
|
|
|
"plausible-postgres.service"
|
|
|
|
];
|
|
|
|
|
|
|
|
environment = {
|
|
|
|
# NixOS specific option to avoid that it's trying to write into its store-path.
|
|
|
|
# See also https://github.com/lau/tzdata#data-directory-and-releases
|
2021-12-06 16:07:01 +00:00
|
|
|
STORAGE_DIR = "/var/lib/plausible/elixir_tzdata";
|
2021-06-04 09:07:49 +00:00
|
|
|
|
|
|
|
# Configuration options from
|
|
|
|
# https://plausible.io/docs/self-hosting-configuration
|
|
|
|
PORT = toString cfg.server.port;
|
2023-11-16 04:20:00 +00:00
|
|
|
LISTEN_IP = cfg.server.listenAddress;
|
|
|
|
|
|
|
|
# Note [plausible-needs-no-erlang-distributed-features]:
|
|
|
|
# Plausible does not use, and does not plan to use, any of
|
|
|
|
# Erlang's distributed features, see:
|
|
|
|
# https://github.com/plausible/analytics/pull/1190#issuecomment-1018820934
|
|
|
|
# Thus, disable distribution for improved simplicity and security:
|
|
|
|
#
|
|
|
|
# When distribution is enabled,
|
|
|
|
# Elixir spwans the Erlang VM, which will listen by default on all
|
|
|
|
# interfaces for messages between Erlang nodes (capable of
|
|
|
|
# remote code execution); it can be protected by a cookie; see
|
|
|
|
# https://erlang.org/doc/reference_manual/distributed.html#security).
|
|
|
|
#
|
|
|
|
# It would be possible to restrict the interface to one of our choice
|
|
|
|
# (e.g. localhost or a VPN IP) similar to how we do it with `listenAddress`
|
|
|
|
# for the Plausible web server; if distribution is ever needed in the future,
|
|
|
|
# https://github.com/NixOS/nixpkgs/pull/130297 shows how to do it.
|
|
|
|
#
|
|
|
|
# But since Plausible does not use this feature in any way,
|
|
|
|
# we just disable it.
|
|
|
|
RELEASE_DISTRIBUTION = "none";
|
|
|
|
# Additional safeguard, in case `RELEASE_DISTRIBUTION=none` ever
|
|
|
|
# stops disabling the start of EPMD.
|
|
|
|
ERL_EPMD_ADDRESS = "127.0.0.1";
|
|
|
|
|
|
|
|
DISABLE_REGISTRATION = if isBool cfg.server.disableRegistration then boolToString cfg.server.disableRegistration else cfg.server.disableRegistration;
|
2021-06-04 09:07:49 +00:00
|
|
|
|
|
|
|
RELEASE_TMP = "/var/lib/plausible/tmp";
|
2021-12-06 16:07:01 +00:00
|
|
|
# Home is needed to connect to the node with iex
|
|
|
|
HOME = "/var/lib/plausible";
|
2021-06-04 09:07:49 +00:00
|
|
|
|
|
|
|
ADMIN_USER_NAME = cfg.adminUser.name;
|
|
|
|
ADMIN_USER_EMAIL = cfg.adminUser.email;
|
|
|
|
|
|
|
|
DATABASE_SOCKET_DIR = cfg.database.postgres.socket;
|
|
|
|
DATABASE_NAME = cfg.database.postgres.dbname;
|
|
|
|
CLICKHOUSE_DATABASE_URL = cfg.database.clickhouse.url;
|
|
|
|
|
|
|
|
BASE_URL = cfg.server.baseUrl;
|
|
|
|
|
|
|
|
MAILER_EMAIL = cfg.mail.email;
|
|
|
|
SMTP_HOST_ADDR = cfg.mail.smtp.hostAddr;
|
|
|
|
SMTP_HOST_PORT = toString cfg.mail.smtp.hostPort;
|
|
|
|
SMTP_RETRIES = toString cfg.mail.smtp.retries;
|
|
|
|
SMTP_HOST_SSL_ENABLED = boolToString cfg.mail.smtp.enableSSL;
|
|
|
|
|
|
|
|
SELFHOST = "true";
|
|
|
|
} // (optionalAttrs (cfg.mail.smtp.user != null) {
|
|
|
|
SMTP_USER_NAME = cfg.mail.smtp.user;
|
|
|
|
});
|
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
path = [ cfg.package ]
|
2021-06-04 09:07:49 +00:00
|
|
|
++ optional cfg.database.postgres.setup config.services.postgresql.package;
|
2021-12-06 16:07:01 +00:00
|
|
|
script = ''
|
2023-11-16 04:20:00 +00:00
|
|
|
# Elixir does not start up if `RELEASE_COOKIE` is not set,
|
|
|
|
# even though we set `RELEASE_DISTRIBUTION=none` so the cookie should be unused.
|
|
|
|
# Thus, make a random one, which should then be ignored.
|
|
|
|
export RELEASE_COOKIE=$(tr -dc A-Za-z0-9 < /dev/urandom | head -c 20)
|
2023-07-15 17:15:38 +00:00
|
|
|
export ADMIN_USER_PWD="$(< $CREDENTIALS_DIRECTORY/ADMIN_USER_PWD )"
|
|
|
|
export SECRET_KEY_BASE="$(< $CREDENTIALS_DIRECTORY/SECRET_KEY_BASE )"
|
|
|
|
|
|
|
|
${lib.optionalString (cfg.mail.smtp.passwordFile != null)
|
|
|
|
''export SMTP_USER_PWD="$(< $CREDENTIALS_DIRECTORY/SMTP_USER_PWD )"''}
|
2022-02-10 20:34:41 +00:00
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
# setup
|
2023-04-29 16:46:19 +00:00
|
|
|
${cfg.package}/createdb.sh
|
|
|
|
${cfg.package}/migrate.sh
|
2023-10-09 19:29:22 +00:00
|
|
|
export IP_GEOLOCATION_DB=${pkgs.dbip-country-lite}/share/dbip/dbip-country-lite.mmdb
|
|
|
|
${cfg.package}/bin/plausible eval "(Plausible.Release.prepare() ; Plausible.Auth.create_user(\"$ADMIN_USER_NAME\", \"$ADMIN_USER_EMAIL\", \"$ADMIN_USER_PWD\"))"
|
2021-12-06 16:07:01 +00:00
|
|
|
${optionalString cfg.adminUser.activate ''
|
2023-10-09 19:29:22 +00:00
|
|
|
psql -d plausible <<< "UPDATE users SET email_verified=true where email = '$ADMIN_USER_EMAIL';"
|
2021-12-06 16:07:01 +00:00
|
|
|
''}
|
2022-02-10 20:34:41 +00:00
|
|
|
|
|
|
|
exec plausible start
|
2021-12-06 16:07:01 +00:00
|
|
|
'';
|
2021-06-04 09:07:49 +00:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
DynamicUser = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
WorkingDirectory = "/var/lib/plausible";
|
|
|
|
StateDirectory = "plausible";
|
2021-12-06 16:07:01 +00:00
|
|
|
LoadCredential = [
|
|
|
|
"ADMIN_USER_PWD:${cfg.adminUser.passwordFile}"
|
|
|
|
"SECRET_KEY_BASE:${cfg.server.secretKeybaseFile}"
|
2022-02-10 20:34:41 +00:00
|
|
|
] ++ lib.optionals (cfg.mail.smtp.passwordFile != null) [ "SMTP_USER_PWD:${cfg.mail.smtp.passwordFile}"];
|
2021-06-04 09:07:49 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
(mkIf cfg.database.postgres.setup {
|
|
|
|
# `plausible' requires the `citext'-extension.
|
|
|
|
plausible-postgres = {
|
|
|
|
after = [ "postgresql.service" ];
|
|
|
|
partOf = [ "plausible.service" ];
|
2021-12-06 16:07:01 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = config.services.postgresql.superUser;
|
|
|
|
RemainAfterExit = true;
|
|
|
|
};
|
|
|
|
script = with cfg.database.postgres; ''
|
2021-06-04 09:07:49 +00:00
|
|
|
PSQL() {
|
2021-12-06 16:07:01 +00:00
|
|
|
${config.services.postgresql.package}/bin/psql --port=5432 "$@"
|
2021-06-04 09:07:49 +00:00
|
|
|
}
|
2021-12-06 16:07:01 +00:00
|
|
|
# check if the database already exists
|
|
|
|
if ! PSQL -lqt | ${pkgs.coreutils}/bin/cut -d \| -f 1 | ${pkgs.gnugrep}/bin/grep -qw ${dbname} ; then
|
|
|
|
PSQL -tAc "CREATE ROLE plausible WITH LOGIN;"
|
|
|
|
PSQL -tAc "CREATE DATABASE ${dbname} WITH OWNER plausible;"
|
|
|
|
PSQL -d ${dbname} -tAc "CREATE EXTENSION IF NOT EXISTS citext;"
|
|
|
|
fi
|
2021-06-04 09:07:49 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2023-10-19 13:55:26 +00:00
|
|
|
meta.maintainers = with maintainers; [ ];
|
2023-02-02 18:25:31 +00:00
|
|
|
meta.doc = ./plausible.md;
|
2021-06-04 09:07:49 +00:00
|
|
|
}
|