2018-07-20 22:56:59 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2017-04-23 15:51:21 +02:00
|
|
|
with lib;
|
|
|
|
let
|
2018-01-16 18:44:27 +01:00
|
|
|
cfg = config.services.matomo;
|
2019-09-24 10:03:06 +02:00
|
|
|
fpm = config.services.phpfpm.pools.${pool};
|
2017-04-23 15:51:21 +02:00
|
|
|
|
2018-01-16 18:44:27 +01:00
|
|
|
user = "matomo";
|
2017-04-23 15:51:21 +02:00
|
|
|
dataDir = "/var/lib/${user}";
|
2018-01-16 18:44:27 +01:00
|
|
|
deprecatedDataDir = "/var/lib/piwik";
|
2017-04-23 15:51:21 +02:00
|
|
|
|
|
|
|
pool = user;
|
|
|
|
phpExecutionUnit = "phpfpm-${pool}";
|
|
|
|
databaseService = "mysql.service";
|
|
|
|
|
2018-01-17 11:00:15 +01:00
|
|
|
fqdn =
|
|
|
|
let
|
|
|
|
join = hostName: domain: hostName + optionalString (domain != null) ".${domain}";
|
|
|
|
in join config.networking.hostName config.networking.domain;
|
|
|
|
|
2017-04-23 15:51:21 +02:00
|
|
|
in {
|
2019-12-10 02:51:19 +01:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "services" "piwik" "enable" ] [ "services" "matomo" "enable" ])
|
|
|
|
(mkRenamedOptionModule [ "services" "piwik" "webServerUser" ] [ "services" "matomo" "webServerUser" ])
|
|
|
|
(mkRemovedOptionModule [ "services" "piwik" "phpfpmProcessManagerConfig" ] "Use services.phpfpm.pools.<name>.settings")
|
|
|
|
(mkRemovedOptionModule [ "services" "matomo" "phpfpmProcessManagerConfig" ] "Use services.phpfpm.pools.<name>.settings")
|
|
|
|
(mkRenamedOptionModule [ "services" "piwik" "nginx" ] [ "services" "matomo" "nginx" ])
|
2021-07-07 18:49:44 +02:00
|
|
|
(mkRenamedOptionModule [ "services" "matomo" "periodicArchiveProcessingUrl" ] [ "services" "matomo" "hostname" ])
|
2019-12-10 02:51:19 +01:00
|
|
|
];
|
|
|
|
|
2017-04-23 15:51:21 +02:00
|
|
|
options = {
|
2018-01-16 18:44:27 +01:00
|
|
|
services.matomo = {
|
2017-04-23 15:51:21 +02:00
|
|
|
# NixOS PR for database setup: https://github.com/NixOS/nixpkgs/pull/6963
|
2018-05-13 16:52:37 +02:00
|
|
|
# Matomo issue for automatic Matomo setup: https://github.com/matomo-org/matomo/issues/10257
|
|
|
|
# TODO: find a nice way to do this when more NixOS MySQL and / or Matomo automatic setup stuff is implemented.
|
2017-04-23 15:51:21 +02:00
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
2018-05-13 16:52:37 +02:00
|
|
|
Enable Matomo web analytics with php-fpm backend.
|
2017-08-30 15:14:52 +02:00
|
|
|
Either the nginx option or the webServerUser option is mandatory.
|
2017-04-23 15:51:21 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-05-15 15:30:54 +02:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
2018-05-13 16:52:37 +02:00
|
|
|
description = ''
|
|
|
|
Matomo package for the service to use.
|
|
|
|
This can be used to point to newer releases from nixos-unstable,
|
|
|
|
as they don't get backported if they are not security-relevant.
|
|
|
|
'';
|
2018-05-15 15:30:54 +02:00
|
|
|
default = pkgs.matomo;
|
2021-10-03 18:06:03 +02:00
|
|
|
defaultText = literalExpression "pkgs.matomo";
|
2018-05-15 15:30:54 +02:00
|
|
|
};
|
|
|
|
|
2017-04-23 15:51:21 +02:00
|
|
|
webServerUser = mkOption {
|
2017-08-30 15:14:52 +02:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "lighttpd";
|
2017-04-23 15:51:21 +02:00
|
|
|
description = ''
|
2019-09-24 10:03:06 +02:00
|
|
|
Name of the web server user that forwards requests to <option>services.phpfpm.pools.<name>.socket</option> the fastcgi socket for Matomo if the nginx
|
2017-08-30 15:14:52 +02:00
|
|
|
option is not used. Either this option or the nginx option is mandatory.
|
2017-04-23 15:51:21 +02:00
|
|
|
If you want to use another webserver than nginx, you need to set this to that server's user
|
2019-02-10 23:29:48 +01:00
|
|
|
and pass fastcgi requests to `index.php`, `matomo.php` and `piwik.php` (legacy name) to this socket.
|
2017-04-23 15:51:21 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-05-13 16:52:00 +02:00
|
|
|
periodicArchiveProcessing = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Enable periodic archive processing, which generates aggregated reports from the visits.
|
|
|
|
|
|
|
|
This means that you can safely disable browser triggers for Matomo archiving,
|
|
|
|
and safely enable to delete old visitor logs.
|
|
|
|
Before deleting visitor logs,
|
|
|
|
make sure though that you run <literal>systemctl start matomo-archive-processing.service</literal>
|
|
|
|
at least once without errors if you have already collected data before.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-07-07 18:49:44 +02:00
|
|
|
hostname = mkOption {
|
2021-02-10 14:57:59 +01:00
|
|
|
type = types.str;
|
|
|
|
default = "${user}.${fqdn}";
|
|
|
|
example = "matomo.yourdomain.org";
|
|
|
|
description = ''
|
|
|
|
URL of the host, without https prefix. By default, this is ${user}.${fqdn}, but you may want to change it if you
|
|
|
|
run Matomo on a different URL than matomo.yourdomain.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-04-23 15:51:21 +02:00
|
|
|
nginx = mkOption {
|
2017-08-31 16:47:54 +02:00
|
|
|
type = types.nullOr (types.submodule (
|
|
|
|
recursiveUpdate
|
|
|
|
(import ../web-servers/nginx/vhost-options.nix { inherit config lib; })
|
|
|
|
{
|
|
|
|
# enable encryption by default,
|
2018-05-13 16:52:37 +02:00
|
|
|
# as sensitive login and Matomo data should not be transmitted in clear text.
|
2017-08-31 16:47:54 +02:00
|
|
|
options.forceSSL.default = true;
|
|
|
|
options.enableACME.default = true;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
2017-04-23 15:51:21 +02:00
|
|
|
default = null;
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
serverAliases = [
|
|
|
|
"matomo.''${config.networking.domain}"
|
|
|
|
"stats.''${config.networking.domain}"
|
|
|
|
];
|
|
|
|
enableACME = false;
|
|
|
|
}
|
|
|
|
'';
|
2017-04-23 15:51:21 +02:00
|
|
|
description = ''
|
2018-05-13 16:52:37 +02:00
|
|
|
With this option, you can customize an nginx virtualHost which already has sensible defaults for Matomo.
|
2017-08-30 15:14:52 +02:00
|
|
|
Either this option or the webServerUser option is mandatory.
|
2017-08-30 15:01:43 +02:00
|
|
|
Set this to {} to just enable the virtualHost if you don't need any customization.
|
2018-01-17 11:00:15 +01:00
|
|
|
If enabled, then by default, the <option>serverName</option> is
|
2019-09-17 02:27:13 +02:00
|
|
|
<literal>''${user}.''${config.networking.hostName}.''${config.networking.domain}</literal>,
|
2018-01-17 11:00:15 +01:00
|
|
|
SSL is active, and certificates are acquired via ACME.
|
2017-08-30 15:01:43 +02:00
|
|
|
If this is set to null (the default), no nginx virtualHost will be configured.
|
2017-04-23 15:51:21 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2017-08-30 15:14:52 +02:00
|
|
|
warnings = mkIf (cfg.nginx != null && cfg.webServerUser != null) [
|
2018-01-16 18:44:27 +01:00
|
|
|
"If services.matomo.nginx is set, services.matomo.nginx.webServerUser is ignored and should be removed."
|
2017-08-30 15:14:52 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
assertions = [ {
|
|
|
|
assertion = cfg.nginx != null || cfg.webServerUser != null;
|
2018-01-16 18:44:27 +01:00
|
|
|
message = "Either services.matomo.nginx or services.matomo.nginx.webServerUser is mandatory";
|
2017-08-30 15:14:52 +02:00
|
|
|
}];
|
2017-04-23 15:51:21 +02:00
|
|
|
|
2018-06-30 01:58:35 +02:00
|
|
|
users.users.${user} = {
|
2017-04-23 15:51:21 +02:00
|
|
|
isSystemUser = true;
|
|
|
|
createHome = true;
|
|
|
|
home = dataDir;
|
2019-06-29 03:47:43 +02:00
|
|
|
group = user;
|
2017-04-23 15:51:21 +02:00
|
|
|
};
|
2019-06-29 03:47:43 +02:00
|
|
|
users.groups.${user} = {};
|
2017-04-23 15:51:21 +02:00
|
|
|
|
2018-05-13 16:52:37 +02:00
|
|
|
systemd.services.matomo-setup-update = {
|
|
|
|
# everything needs to set up and up to date before Matomo php files are executed
|
2017-04-23 15:51:21 +02:00
|
|
|
requiredBy = [ "${phpExecutionUnit}.service" ];
|
|
|
|
before = [ "${phpExecutionUnit}.service" ];
|
|
|
|
# the update part of the script can only work if the database is already up and running
|
|
|
|
requires = [ databaseService ];
|
|
|
|
after = [ databaseService ];
|
2018-05-15 15:30:54 +02:00
|
|
|
path = [ cfg.package ];
|
2018-05-13 16:52:00 +02:00
|
|
|
environment.PIWIK_USER_PATH = dataDir;
|
2017-04-23 15:51:21 +02:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = user;
|
|
|
|
# hide especially config.ini.php from other
|
|
|
|
UMask = "0007";
|
2018-01-16 18:44:27 +01:00
|
|
|
# TODO: might get renamed to MATOMO_USER_PATH in future versions
|
2017-04-23 15:51:21 +02:00
|
|
|
# chown + chmod in preStart needs root
|
|
|
|
PermissionsStartOnly = true;
|
|
|
|
};
|
2018-05-13 16:52:00 +02:00
|
|
|
|
2017-04-23 15:51:21 +02:00
|
|
|
# correct ownership and permissions in case they're not correct anymore,
|
|
|
|
# e.g. after restoring from backup or moving from another system.
|
|
|
|
# Note that ${dataDir}/config/config.ini.php might contain the MySQL password.
|
|
|
|
preStart = ''
|
2018-05-13 16:52:37 +02:00
|
|
|
# migrate data from piwik to Matomo folder
|
2018-01-16 18:44:27 +01:00
|
|
|
if [ -d ${deprecatedDataDir} ]; then
|
|
|
|
echo "Migrating from ${deprecatedDataDir} to ${dataDir}"
|
|
|
|
mv -T ${deprecatedDataDir} ${dataDir}
|
|
|
|
fi
|
2019-06-29 03:47:43 +02:00
|
|
|
chown -R ${user}:${user} ${dataDir}
|
2017-04-23 15:51:21 +02:00
|
|
|
chmod -R ug+rwX,o-rwx ${dataDir}
|
2021-10-12 18:30:09 +02:00
|
|
|
|
|
|
|
if [ -e ${dataDir}/current-package ]; then
|
|
|
|
CURRENT_PACKAGE=$(readlink ${dataDir}/current-package)
|
|
|
|
NEW_PACKAGE=${cfg.package}
|
|
|
|
if [ "$CURRENT_PACKAGE" != "$NEW_PACKAGE" ]; then
|
|
|
|
# keeping tmp arround between upgrades seems to bork stuff, so delete it
|
|
|
|
rm -rf ${dataDir}/tmp
|
|
|
|
fi
|
|
|
|
elif [ -e ${dataDir}/tmp ]; then
|
|
|
|
# upgrade from 4.4.1
|
|
|
|
rm -rf ${dataDir}/tmp
|
|
|
|
fi
|
|
|
|
ln -sfT ${cfg.package} ${dataDir}/current-package
|
2017-04-23 15:51:21 +02:00
|
|
|
'';
|
|
|
|
script = ''
|
2018-05-13 16:52:37 +02:00
|
|
|
# Use User-Private Group scheme to protect Matomo data, but allow administration / backup via 'matomo' group
|
2017-04-23 15:51:21 +02:00
|
|
|
# Copy config folder
|
|
|
|
chmod g+s "${dataDir}"
|
2019-08-27 11:44:34 +02:00
|
|
|
cp -r "${cfg.package}/share/config" "${dataDir}/"
|
2017-04-23 15:51:21 +02:00
|
|
|
chmod -R u+rwX,g+rwX,o-rwx "${dataDir}"
|
|
|
|
|
|
|
|
# check whether user setup has already been done
|
|
|
|
if test -f "${dataDir}/config/config.ini.php"; then
|
|
|
|
# then execute possibly pending database upgrade
|
2018-01-16 18:44:27 +01:00
|
|
|
matomo-console core:update --yes
|
2017-04-23 15:51:21 +02:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-05-13 16:52:00 +02:00
|
|
|
# If this is run regularly via the timer,
|
|
|
|
# 'Browser trigger archiving' can be disabled in Matomo UI > Settings > General Settings.
|
|
|
|
systemd.services.matomo-archive-processing = {
|
|
|
|
description = "Archive Matomo reports";
|
|
|
|
# the archiving can only work if the database is already up and running
|
|
|
|
requires = [ databaseService ];
|
|
|
|
after = [ databaseService ];
|
|
|
|
|
|
|
|
# TODO: might get renamed to MATOMO_USER_PATH in future versions
|
|
|
|
environment.PIWIK_USER_PATH = dataDir;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = user;
|
|
|
|
UMask = "0007";
|
|
|
|
CPUSchedulingPolicy = "idle";
|
|
|
|
IOSchedulingClass = "idle";
|
2021-07-07 18:49:44 +02:00
|
|
|
ExecStart = "${cfg.package}/bin/matomo-console core:archive --url=https://${cfg.hostname}";
|
2018-05-13 16:52:00 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.timers.matomo-archive-processing = mkIf cfg.periodicArchiveProcessing {
|
|
|
|
description = "Automatically archive Matomo reports every hour";
|
|
|
|
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = "hourly";
|
|
|
|
Persistent = "yes";
|
|
|
|
AccuracySec = "10m";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-04-23 15:51:21 +02:00
|
|
|
systemd.services.${phpExecutionUnit} = {
|
2018-05-13 16:52:37 +02:00
|
|
|
# stop phpfpm on package upgrade, do database upgrade via matomo-setup-update, and then restart
|
2018-05-15 15:30:54 +02:00
|
|
|
restartTriggers = [ cfg.package ];
|
2017-04-23 15:51:21 +02:00
|
|
|
# stop config.ini.php from getting written with read permission for others
|
|
|
|
serviceConfig.UMask = "0007";
|
|
|
|
};
|
|
|
|
|
2019-07-03 18:11:38 +02:00
|
|
|
services.phpfpm.pools = let
|
2017-08-30 15:14:52 +02:00
|
|
|
# workaround for when both are null and need to generate a string,
|
|
|
|
# which is illegal, but as assertions apparently are being triggered *after* config generation,
|
|
|
|
# we have to avoid already throwing errors at this previous stage.
|
|
|
|
socketOwner = if (cfg.nginx != null) then config.services.nginx.user
|
|
|
|
else if (cfg.webServerUser != null) then cfg.webServerUser else "";
|
|
|
|
in {
|
2019-07-03 18:11:38 +02:00
|
|
|
${pool} = {
|
2019-09-24 10:03:06 +02:00
|
|
|
inherit user;
|
|
|
|
phpOptions = ''
|
|
|
|
error_log = 'stderr'
|
|
|
|
log_errors = on
|
2019-07-03 18:11:38 +02:00
|
|
|
'';
|
2019-09-24 10:03:06 +02:00
|
|
|
settings = mapAttrs (name: mkDefault) {
|
|
|
|
"listen.owner" = socketOwner;
|
|
|
|
"listen.group" = "root";
|
|
|
|
"listen.mode" = "0660";
|
|
|
|
"pm" = "dynamic";
|
|
|
|
"pm.max_children" = 75;
|
|
|
|
"pm.start_servers" = 10;
|
|
|
|
"pm.min_spare_servers" = 5;
|
|
|
|
"pm.max_spare_servers" = 20;
|
|
|
|
"pm.max_requests" = 500;
|
|
|
|
"catch_workers_output" = true;
|
|
|
|
};
|
|
|
|
phpEnv.PIWIK_USER_PATH = dataDir;
|
2019-07-03 18:11:38 +02:00
|
|
|
};
|
2017-04-23 15:51:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
services.nginx.virtualHosts = mkIf (cfg.nginx != null) {
|
|
|
|
# References:
|
|
|
|
# https://fralef.me/piwik-hardening-with-nginx-and-php-fpm.html
|
|
|
|
# https://github.com/perusio/piwik-nginx
|
2021-07-07 18:49:44 +02:00
|
|
|
"${cfg.hostname}" = mkMerge [ cfg.nginx {
|
2018-05-13 16:52:37 +02:00
|
|
|
# don't allow to override the root easily, as it will almost certainly break Matomo.
|
2017-08-31 16:47:54 +02:00
|
|
|
# disadvantage: not shown as default in docs.
|
2018-05-15 15:30:54 +02:00
|
|
|
root = mkForce "${cfg.package}/share";
|
2017-08-30 15:01:43 +02:00
|
|
|
|
2017-08-31 16:47:54 +02:00
|
|
|
# define locations here instead of as the submodule option's default
|
|
|
|
# so that they can easily be extended with additional locations if required
|
2018-05-13 16:52:37 +02:00
|
|
|
# without needing to redefine the Matomo ones.
|
2017-08-31 16:47:54 +02:00
|
|
|
# disadvantage: not shown as default in docs.
|
2017-04-23 15:51:21 +02:00
|
|
|
locations."/" = {
|
|
|
|
index = "index.php";
|
|
|
|
};
|
|
|
|
# allow index.php for webinterface
|
|
|
|
locations."= /index.php".extraConfig = ''
|
2019-09-24 10:03:06 +02:00
|
|
|
fastcgi_pass unix:${fpm.socket};
|
2017-04-23 15:51:21 +02:00
|
|
|
'';
|
2019-02-10 23:29:48 +01:00
|
|
|
# allow matomo.php for tracking
|
|
|
|
locations."= /matomo.php".extraConfig = ''
|
2019-09-24 10:03:06 +02:00
|
|
|
fastcgi_pass unix:${fpm.socket};
|
2019-02-10 23:29:48 +01:00
|
|
|
'';
|
|
|
|
# allow piwik.php for tracking (deprecated name)
|
2017-04-23 15:51:21 +02:00
|
|
|
locations."= /piwik.php".extraConfig = ''
|
2019-09-24 10:03:06 +02:00
|
|
|
fastcgi_pass unix:${fpm.socket};
|
2017-04-23 15:51:21 +02:00
|
|
|
'';
|
|
|
|
# Any other attempt to access any php files is forbidden
|
2019-09-17 02:22:11 +02:00
|
|
|
locations."~* ^.+\\.php$".extraConfig = ''
|
2017-04-23 15:51:21 +02:00
|
|
|
return 403;
|
|
|
|
'';
|
|
|
|
# Disallow access to unneeded directories
|
|
|
|
# config and tmp are already removed
|
|
|
|
locations."~ ^/(?:core|lang|misc)/".extraConfig = ''
|
|
|
|
return 403;
|
|
|
|
'';
|
|
|
|
# Disallow access to several helper files
|
2019-09-17 02:22:11 +02:00
|
|
|
locations."~* \\.(?:bat|git|ini|sh|txt|tpl|xml|md)$".extraConfig = ''
|
2017-04-23 15:51:21 +02:00
|
|
|
return 403;
|
|
|
|
'';
|
|
|
|
# No crawling of this site for bots that obey robots.txt - no useful information here.
|
|
|
|
locations."= /robots.txt".extraConfig = ''
|
|
|
|
return 200 "User-agent: *\nDisallow: /\n";
|
|
|
|
'';
|
2019-02-10 23:29:48 +01:00
|
|
|
# let browsers cache matomo.js
|
|
|
|
locations."= /matomo.js".extraConfig = ''
|
|
|
|
expires 1M;
|
|
|
|
'';
|
|
|
|
# let browsers cache piwik.js (deprecated name)
|
2017-04-23 15:51:21 +02:00
|
|
|
locations."= /piwik.js".extraConfig = ''
|
|
|
|
expires 1M;
|
|
|
|
'';
|
2017-08-30 15:01:43 +02:00
|
|
|
}];
|
2017-04-23 15:51:21 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
meta = {
|
2018-01-16 18:44:27 +01:00
|
|
|
doc = ./matomo-doc.xml;
|
2018-07-09 06:45:52 +02:00
|
|
|
maintainers = with lib.maintainers; [ florianjacob ];
|
2017-04-23 15:51:21 +02:00
|
|
|
};
|
|
|
|
}
|