nixpkgs/nixos/modules/services/finance/odoo.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

179 lines
4.7 KiB
Nix
Raw Permalink Normal View History

2021-11-02 01:45:19 +01:00
{
config,
pkgs,
lib,
...
}:
let
cfg = config.services.odoo;
format = pkgs.formats.ini { };
in
{
options = {
services.odoo = {
enable = lib.mkEnableOption "odoo, an open source ERP and CRM system";
2021-11-02 01:45:19 +01:00
package = lib.mkPackageOption pkgs "odoo" { };
2021-11-02 01:45:19 +01:00
addons = lib.mkOption {
type = with lib.types; listOf package;
2021-11-02 01:45:19 +01:00
default = [ ];
example = lib.literalExpression "[ pkgs.odoo_enterprise ]";
description = "Odoo addons.";
2021-11-02 01:45:19 +01:00
};
autoInit = lib.mkEnableOption "automatically initialize the DB";
autoInitExtraFlags = lib.mkOption {
type = with lib.types; listOf str;
default = [ ];
example =
lib.literalExpression # nix
''
[ "--without-demo=all" ]
'';
description = "Extra flags passed to odoo when run for the first time by autoInit";
};
settings = lib.mkOption {
2021-11-02 01:45:19 +01:00
type = format.type;
default = { };
description = ''
Odoo configuration settings. For more details see <https://www.odoo.com/documentation/15.0/administration/install/deploy.html>
2021-11-02 01:45:19 +01:00
'';
example = lib.literalExpression ''
2023-08-25 19:12:18 +02:00
options = {
db_user = "odoo";
db_password="odoo";
};
'';
2021-11-02 01:45:19 +01:00
};
domain = lib.mkOption {
type = with lib.types; nullOr str;
2021-11-02 01:45:19 +01:00
description = "Domain to host Odoo with nginx";
default = null;
};
};
};
config = lib.mkIf (cfg.enable) (
let
2021-11-02 01:45:19 +01:00
cfgFile = format.generate "odoo.cfg" cfg.settings;
in
{
services.nginx = lib.mkIf (cfg.domain != null) {
2021-11-02 01:45:19 +01:00
upstreams = {
odoo.servers = {
"127.0.0.1:8069" = { };
};
2021-11-02 01:45:19 +01:00
odoochat.servers = {
"127.0.0.1:8072" = { };
};
2021-11-02 01:45:19 +01:00
};
virtualHosts."${cfg.domain}" = {
extraConfig = ''
proxy_read_timeout 720s;
proxy_connect_timeout 720s;
proxy_send_timeout 720s;
2021-11-02 01:45:19 +01:00
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
'';
2021-11-02 01:45:19 +01:00
locations = {
"/longpolling" = {
proxyPass = "http://odoochat";
};
2021-11-02 01:45:19 +01:00
"/" = {
proxyPass = "http://odoo";
extraConfig = ''
proxy_redirect off;
'';
};
};
2021-11-02 01:45:19 +01:00
};
};
services.odoo.settings.options =
{
data_dir = "/var/lib/private/odoo/data";
proxy_mode = cfg.domain != null;
}
2021-11-02 01:45:19 +01:00
// (lib.optionalAttrs (cfg.addons != [ ]) {
addons_path = lib.concatMapStringsSep "," lib.escapeShellArg cfg.addons;
});
2021-11-02 01:45:19 +01:00
users.users.odoo = {
isSystemUser = true;
group = "odoo";
};
2021-11-02 01:45:19 +01:00
users.groups.odoo = { };
systemd.services.odoo = {
wantedBy = [ "multi-user.target" ];
after = [
"network.target"
"postgresql.service"
];
# pg_dump
path = [ config.services.postgresql.package ];
2021-11-02 01:45:19 +01:00
requires = [ "postgresql.service" ];
2021-11-02 01:45:19 +01:00
serviceConfig = {
ExecStart = "${cfg.package}/bin/odoo";
ExecStartPre = pkgs.writeShellScript "odoo-start-pre.sh" (
''
set -euo pipefail
cd "$STATE_DIRECTORY"
2021-11-02 01:45:19 +01:00
# Auto-migrate old deployments
if [[ -d .local/share/Odoo ]]; then
echo "pre-start: migrating state directory from $STATE_DIRECTORY/.local/share/Odoo to $STATE_DIRECTORY/data"
mv .local/share/Odoo ./data
rmdir .local/share
rmdir .local
fi
''
+ (lib.optionalString cfg.autoInit ''
echo "pre-start: auto-init"
2021-11-02 01:45:19 +01:00
INITIALIZED="${cfg.settings.options.data_dir}/.odoo.initialized"
if [ ! -e "$INITIALIZED" ]; then
${cfg.package}/bin/odoo --init=INIT --database=odoo --db_user=odoo --stop-after-init ${lib.concatStringsSep " " cfg.autoInitExtraFlags}
touch "$INITIALIZED"
fi
2021-11-02 01:45:19 +01:00
'')
+ "echo pre-start: OK"
);
2021-11-02 01:45:19 +01:00
DynamicUser = true;
User = "odoo";
StateDirectory = "odoo";
Environment = [
"ODOO_RC=${cfgFile}"
];
2021-11-02 01:45:19 +01:00
};
};
services.postgresql = {
enable = true;
2021-11-02 01:45:19 +01:00
ensureDatabases = [ "odoo" ];
ensureUsers = [
{
name = "odoo";
2021-11-02 01:45:19 +01:00
ensureDBOwnership = true;
}
];
};
2021-11-02 01:45:19 +01:00
}
);
}