{ config, pkgs, lib, ... }:
let
cfg = config.services.anuko-time-tracker;
configFile = let
smtpPassword = if cfg.settings.email.smtpPasswordFile == null
then "''"
else "trim(file_get_contents('${cfg.settings.email.smtpPasswordFile}'))";
in pkgs.writeText "config.php" ''
";
};
mode = lib.mkOption {
type = lib.types.str;
description = lib.mdDoc "Mail sending mode. Can be 'mail' or 'smtp'.";
default = "smtp";
};
smtpHost = lib.mkOption {
type = lib.types.str;
description = lib.mdDoc "MTA hostname.";
default = "localhost";
};
smtpPort = lib.mkOption {
type = lib.types.int;
description = lib.mdDoc "MTA port.";
default = 25;
};
smtpUser = lib.mkOption {
type = lib.types.str;
description = lib.mdDoc "MTA authentication username.";
default = "";
};
smtpAuth = lib.mkOption {
type = lib.types.bool;
default = false;
description = lib.mdDoc "MTA requires authentication.";
};
smtpPasswordFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
example = "/var/lib/anuko-time-tracker/secrets/smtp-password";
description = lib.mdDoc ''
Path to file containing the MTA authentication password.
'';
};
smtpDebug = lib.mkOption {
type = lib.types.bool;
default = false;
description = lib.mdDoc "Debug mail sending.";
};
};
defaultLanguage = lib.mkOption {
type = lib.types.str;
description = lib.mdDoc ''
Defines Anuko Time Tracker default language. It is used on Time Tracker login page.
After login, a language set for user group is used.
Empty string means the language is defined by user browser.
'';
default = "";
example = "nl";
};
defaultCurrency = lib.mkOption {
type = lib.types.str;
description = lib.mdDoc ''
Defines a default currency symbol for new groups.
Use €, £, a more specific dollar like US$, CAD, etc.
'';
default = "$";
example = "€";
};
exportDecimalDuration = lib.mkOption {
type = lib.types.bool;
default = true;
description = lib.mdDoc ''
Defines whether time duration values are decimal in CSV and XML data
exports (1.25 vs 1:15).
'';
};
reportFooter = lib.mkOption {
type = lib.types.bool;
default = true;
description = lib.mdDoc "Defines whether to use a footer on reports.";
};
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.database.createLocally -> cfg.database.passwordFile == null;
message = ''
cannot be specified if
is set to true.
'';
}
{
assertion = cfg.settings.email.smtpAuth -> (cfg.settings.email.smtpPasswordFile != null);
message = ''
needs to be set if
is enabled.
'';
}
];
services.phpfpm = {
pools.anuko-time-tracker = {
inherit (cfg) user;
group = config.services.nginx.group;
settings = {
"listen.owner" = config.services.nginx.user;
"listen.group" = config.services.nginx.group;
} // cfg.poolConfig;
};
};
services.nginx = {
enable = lib.mkDefault true;
recommendedTlsSettings = true;
recommendedOptimisation = true;
recommendedGzipSettings = true;
virtualHosts."${cfg.hostname}" = lib.mkMerge [
cfg.nginx
{
root = lib.mkForce "${package}";
locations = {
"/".index = "index.php";
"~ [^/]\\.php(/|$)" = {
extraConfig = ''
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass unix:${config.services.phpfpm.pools.anuko-time-tracker.socket};
'';
};
};
}
];
};
services.mysql = lib.mkIf cfg.database.createLocally {
enable = lib.mkDefault true;
package = lib.mkDefault pkgs.mariadb;
ensureDatabases = [ cfg.database.name ];
ensureUsers = [{
name = cfg.database.user;
ensurePermissions = {
"${cfg.database.name}.*" = "ALL PRIVILEGES";
};
}];
};
systemd = {
services = {
anuko-time-tracker-setup-database = lib.mkIf cfg.database.createLocally {
description = "Set up Anuko Time Tracker database";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
wantedBy = [ "phpfpm-anuko-time-tracker.service" ];
after = [ "mysql.service" ];
script =
let
mysql = "${config.services.mysql.package}/bin/mysql";
in
''
if [ ! -f ${cfg.dataDir}/.dbexists ]; then
# Load database schema provided with package
${mysql} ${cfg.database.name} < ${cfg.package}/mysql.sql
touch ${cfg.dataDir}/.dbexists
fi
'';
};
};
tmpfiles.rules = [
"d ${cfg.dataDir} 0750 ${cfg.user} ${config.services.nginx.group} -"
"d ${cfg.dataDir}/templates_c 0750 ${cfg.user} ${config.services.nginx.group} -"
];
};
users.users."${cfg.user}" = {
isSystemUser = true;
group = config.services.nginx.group;
};
};
meta.maintainers = with lib.maintainers; [ michaelshmitty ];
}