2020-04-24 23:36:52 +00:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.ihatemoney;
|
|
|
|
user = "ihatemoney";
|
|
|
|
group = "ihatemoney";
|
|
|
|
db = "ihatemoney";
|
|
|
|
python3 = config.services.uwsgi.package.python3;
|
|
|
|
pkg = python3.pkgs.ihatemoney;
|
|
|
|
toBool = x: if x then "True" else "False";
|
|
|
|
configFile = pkgs.writeText "ihatemoney.cfg" ''
|
|
|
|
from secrets import token_hex
|
|
|
|
# load a persistent secret key
|
|
|
|
SECRET_KEY_FILE = "/var/lib/ihatemoney/secret_key"
|
|
|
|
SECRET_KEY = ""
|
|
|
|
try:
|
|
|
|
with open(SECRET_KEY_FILE) as f:
|
|
|
|
SECRET_KEY = f.read()
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
if not SECRET_KEY:
|
|
|
|
print("ihatemoney: generating a new secret key")
|
|
|
|
SECRET_KEY = token_hex(50)
|
|
|
|
with open(SECRET_KEY_FILE, "w") as f:
|
|
|
|
f.write(SECRET_KEY)
|
|
|
|
del token_hex
|
|
|
|
del SECRET_KEY_FILE
|
|
|
|
|
|
|
|
# "normal" configuration
|
|
|
|
DEBUG = False
|
|
|
|
SQLALCHEMY_DATABASE_URI = '${
|
|
|
|
if cfg.backend == "sqlite"
|
|
|
|
then "sqlite:////var/lib/ihatemoney/ihatemoney.sqlite"
|
|
|
|
else "postgresql:///${db}"}'
|
|
|
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
2021-12-06 16:07:01 +00:00
|
|
|
MAIL_DEFAULT_SENDER = (r"${cfg.defaultSender.name}", r"${cfg.defaultSender.email}")
|
2020-04-24 23:36:52 +00:00
|
|
|
ACTIVATE_DEMO_PROJECT = ${toBool cfg.enableDemoProject}
|
2021-12-06 16:07:01 +00:00
|
|
|
ADMIN_PASSWORD = r"${toString cfg.adminHashedPassword /*toString null == ""*/}"
|
2020-04-24 23:36:52 +00:00
|
|
|
ALLOW_PUBLIC_PROJECT_CREATION = ${toBool cfg.enablePublicProjectCreation}
|
|
|
|
ACTIVATE_ADMIN_DASHBOARD = ${toBool cfg.enableAdminDashboard}
|
2021-12-06 16:07:01 +00:00
|
|
|
SESSION_COOKIE_SECURE = ${toBool cfg.secureCookie}
|
|
|
|
ENABLE_CAPTCHA = ${toBool cfg.enableCaptcha}
|
|
|
|
LEGAL_LINK = r"${toString cfg.legalLink}"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.ihatemoney = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "ihatemoney webapp. Note that this will set uwsgi to emperor mode");
|
2020-04-24 23:36:52 +00:00
|
|
|
backend = mkOption {
|
|
|
|
type = types.enum [ "sqlite" "postgresql" ];
|
|
|
|
default = "sqlite";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The database engine to use for ihatemoney.
|
2022-08-12 12:06:08 +00:00
|
|
|
If `postgresql` is selected, then a database called
|
|
|
|
`${db}` will be created. If you disable this option,
|
2020-04-24 23:36:52 +00:00
|
|
|
it will however not be removed.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
adminHashedPassword = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The hashed password of the administrator. To obtain it, run `ihatemoney generate_password_hash`";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
uwsgiConfig = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
example = {
|
|
|
|
http = ":8000";
|
|
|
|
};
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Additionnal configuration of the UWSGI vassal running ihatemoney. It should notably specify on which interfaces and ports the vassal should listen.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
defaultSender = {
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "Budget manager";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The display name of the sender of ihatemoney emails";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
email = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "ihatemoney@${config.networking.hostName}";
|
2021-12-06 16:07:01 +00:00
|
|
|
defaultText = literalExpression ''"ihatemoney@''${config.networking.hostName}"'';
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The email of the sender of ihatemoney emails";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
2021-12-06 16:07:01 +00:00
|
|
|
secureCookie = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Use secure cookies. Disable this when ihatemoney is served via http instead of https";
|
2021-12-06 16:07:01 +00:00
|
|
|
};
|
2022-09-09 14:08:57 +00:00
|
|
|
enableDemoProject = mkEnableOption (lib.mdDoc "access to the demo project in ihatemoney");
|
|
|
|
enablePublicProjectCreation = mkEnableOption (lib.mdDoc "permission to create projects in ihatemoney by anyone");
|
|
|
|
enableAdminDashboard = mkEnableOption (lib.mdDoc "ihatemoney admin dashboard");
|
|
|
|
enableCaptcha = mkEnableOption (lib.mdDoc "a simplistic captcha for some forms");
|
2021-12-06 16:07:01 +00:00
|
|
|
legalLink = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The URL to a page explaining legal statements about your service, eg. GDPR-related information.";
|
2021-12-06 16:07:01 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Extra configuration appended to ihatemoney's configuration file. It is a python file, so pay attention to indentation.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
services.postgresql = mkIf (cfg.backend == "postgresql") {
|
|
|
|
enable = true;
|
|
|
|
ensureDatabases = [ db ];
|
|
|
|
ensureUsers = [ {
|
|
|
|
name = user;
|
|
|
|
ensurePermissions = {
|
|
|
|
"DATABASE ${db}" = "ALL PRIVILEGES";
|
|
|
|
};
|
|
|
|
} ];
|
|
|
|
};
|
|
|
|
systemd.services.postgresql = mkIf (cfg.backend == "postgresql") {
|
|
|
|
wantedBy = [ "uwsgi.service" ];
|
|
|
|
before = [ "uwsgi.service" ];
|
|
|
|
};
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d /var/lib/ihatemoney 770 ${user} ${group}"
|
|
|
|
];
|
|
|
|
users = {
|
|
|
|
users.${user} = {
|
|
|
|
isSystemUser = true;
|
|
|
|
inherit group;
|
|
|
|
};
|
|
|
|
groups.${group} = {};
|
|
|
|
};
|
|
|
|
services.uwsgi = {
|
|
|
|
enable = true;
|
|
|
|
plugins = [ "python3" ];
|
|
|
|
instance = {
|
|
|
|
type = "emperor";
|
|
|
|
vassals.ihatemoney = {
|
|
|
|
type = "normal";
|
|
|
|
strict = true;
|
2021-01-15 22:18:51 +00:00
|
|
|
immediate-uid = user;
|
|
|
|
immediate-gid = group;
|
2020-04-24 23:36:52 +00:00
|
|
|
# apparently flask uses threads: https://github.com/spiral-project/ihatemoney/commit/c7815e48781b6d3a457eaff1808d179402558f8c
|
|
|
|
enable-threads = true;
|
|
|
|
module = "wsgi:application";
|
|
|
|
chdir = "${pkg}/${pkg.pythonModule.sitePackages}/ihatemoney";
|
|
|
|
env = [ "IHATEMONEY_SETTINGS_FILE_PATH=${configFile}" ];
|
|
|
|
pythonPackages = self: [ self.ihatemoney ];
|
|
|
|
} // cfg.uwsgiConfig;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|