1 { config, pkgs, lib, ... }:
3 cfg = config.services.odoo;
4 format = pkgs.formats.ini {};
9 enable = lib.mkEnableOption "odoo, an open source ERP and CRM system";
11 package = lib.mkPackageOption pkgs "odoo" { };
13 addons = lib.mkOption {
14 type = with lib.types; listOf package;
16 example = lib.literalExpression "[ pkgs.odoo_enterprise ]";
17 description = "Odoo addons.";
20 autoInit = lib.mkEnableOption "automatically initialize the DB";
22 autoInitExtraFlags = lib.mkOption {
23 type = with lib.types; listOf str;
25 example = lib.literalExpression /*nix*/ ''
26 [ "--without-demo=all" ]
28 description = "Extra flags passed to odoo when run for the first time by autoInit";
31 settings = lib.mkOption {
35 Odoo configuration settings. For more details see <https://www.odoo.com/documentation/15.0/administration/install/deploy.html>
37 example = lib.literalExpression ''
45 domain = lib.mkOption {
46 type = with lib.types; nullOr str;
47 description = "Domain to host Odoo with nginx";
53 config = lib.mkIf (cfg.enable) (let
54 cfgFile = format.generate "odoo.cfg" cfg.settings;
56 services.nginx = lib.mkIf (cfg.domain != null) {
59 "127.0.0.1:8069" = {};
63 "127.0.0.1:8072" = {};
67 virtualHosts."${cfg.domain}" = {
69 proxy_read_timeout 720s;
70 proxy_connect_timeout 720s;
71 proxy_send_timeout 720s;
73 proxy_set_header X-Forwarded-Host $host;
74 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
75 proxy_set_header X-Forwarded-Proto $scheme;
76 proxy_set_header X-Real-IP $remote_addr;
81 proxyPass = "http://odoochat";
85 proxyPass = "http://odoo";
94 services.odoo.settings.options = {
95 data_dir = "/var/lib/private/odoo/data";
96 proxy_mode = cfg.domain != null;
97 } // (lib.optionalAttrs (cfg.addons != []) {
98 addons_path = lib.concatMapStringsSep "," lib.escapeShellArg cfg.addons;
105 users.groups.odoo = {};
107 systemd.services.odoo = {
108 wantedBy = [ "multi-user.target" ];
109 after = [ "network.target" "postgresql.service" ];
112 path = [ config.services.postgresql.package ];
114 requires = [ "postgresql.service" ];
117 ExecStart = "${cfg.package}/bin/odoo";
118 ExecStartPre = pkgs.writeShellScript "odoo-start-pre.sh" (
122 cd "$STATE_DIRECTORY"
124 # Auto-migrate old deployments
125 if [[ -d .local/share/Odoo ]]; then
126 echo "pre-start: migrating state directory from $STATE_DIRECTORY/.local/share/Odoo to $STATE_DIRECTORY/data"
127 mv .local/share/Odoo ./data
132 + (lib.optionalString cfg.autoInit
134 echo "pre-start: auto-init"
135 INITIALIZED="${cfg.settings.options.data_dir}/.odoo.initialized"
136 if [ ! -e "$INITIALIZED" ]; then
137 ${cfg.package}/bin/odoo --init=INIT --database=odoo --db_user=odoo --stop-after-init ${lib.concatStringsSep " " cfg.autoInitExtraFlags}
141 + "echo pre-start: OK"
145 StateDirectory = "odoo";
152 services.postgresql = {
155 ensureDatabases = [ "odoo" ];
158 ensureDBOwnership = true;