summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/finance/odoo.nix
blob: 6165e521bda732f29735ee9f911f976163abe90d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
{
  config,
  pkgs,
  lib,
  ...
}:
let
  cfg = config.services.odoo;
  format = pkgs.formats.ini { };

  inherit (config.system) stateVersion;
in
{
  options = {
    services.odoo = {
      enable = lib.mkEnableOption "odoo, an open source ERP and CRM system";

      package = lib.mkOption {
        type = lib.types.package;
        description = "Which package to use for the Odoo instance.";
        relatedPackages = [
          "odoo17"
          "odoo18"
          "odoo19"
        ];
      };

      addons = lib.mkOption {
        type = with lib.types; listOf package;
        default = [ ];
        example = lib.literalExpression "[ pkgs.odoo_enterprise ]";
        description = "Odoo addons.";
      };

      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 {
        type = lib.types.submodule {
          options = {
            options = {
              workers = lib.mkOption {
                type = lib.types.ints.unsigned;
                default = 0;
                description = "Values above 0 will enable the multi-processing HTTP server, this should be set for production setups. This needs to be set to >0 for real-time connections in the discuss app. For configuration recommendations see <https://www.odoo.com/documentation/19.0/administration/on_premise/deploy.html#builtin-server>";
              };
              proxy_mode = lib.mkOption {
                type = lib.types.bool;
                default = cfg.domain != null;
                defaultText = "services.odoo.domain != null";
                description = "Enables the use of X-Forwarded-* headers through Werkzeug’s proxy support. Must be enabled if reverse proxy is used.";
              };
            };
          };
          freeformType = format.type;
        };
        default = { };
        description = ''
          Odoo configuration settings. For more details see <https://www.odoo.com/documentation/15.0/administration/install/deploy.html>
        '';
        example = lib.literalExpression ''
          options = {
            db_user = "odoo";
            db_password="odoo";
          };
        '';
      };

      domain = lib.mkOption {
        type = with lib.types; nullOr str;
        description = "Domain to host Odoo with nginx";
        default = null;
      };
    };
  };

  config = lib.mkIf (cfg.enable) (
    let
      cfgFile = format.generate "odoo.cfg" cfg.settings;
    in
    {
      services.nginx = lib.mkIf (cfg.domain != null) {
        upstreams = {
          odoo.servers = {
            "127.0.0.1:8069" = { };
          };

          odoochat.servers = {
            "127.0.0.1:8072" = { };
          };
        };

        virtualHosts."${cfg.domain}" = {
          extraConfig = ''
            proxy_read_timeout 720s;
            proxy_connect_timeout 720s;
            proxy_send_timeout 720s;

            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;
          '';

          locations = {
            "/websocket" = {
              proxyPass = "http://odoochat";
            };

            "/" = {
              proxyPass = "http://odoo";
              extraConfig = ''
                proxy_redirect off;
              '';
            };
          };
        };
      };

      services.odoo = {
        package = lib.mkDefault (
          if pkgs ? odoo then
            throw ''
              The `pkgs.odoo`-attribute has been removed. If it's supposed to be the default
              odoo defined in an overlay, please set `services.odoo.package` to
              `pkgs.odoo`.
            ''
          else if lib.versionOlder stateVersion "25.05" then
            pkgs.odoo18
          else if lib.versionOlder stateVersion "25.11" then
            pkgs.odoo18
          else
            pkgs.odoo19
        );
        settings.options = {
          data_dir = "/var/lib/private/odoo/data";
          # Disable the database manager by default
          # https://www.odoo.com/documentation/master/administration/on_premise/deploy.html#database-manager-security
          list_db = lib.mkDefault false;
        }
        // (lib.optionalAttrs (cfg.addons != [ ]) {
          addons_path = lib.concatMapStringsSep "," lib.escapeShellArg cfg.addons;
        });
      };

      users.users.odoo = {
        isSystemUser = true;
        group = "odoo";
      };
      users.groups.odoo = { };

      systemd.services.odoo = {
        wantedBy = [ "multi-user.target" ];
        after = [
          "network.target"
          "postgresql.target"
        ];

        # pg_dump
        path = [ config.services.postgresql.package ];

        requires = [ "postgresql.target" ];

        serviceConfig = {
          ExecStart = "${cfg.package}/bin/odoo";
          ExecStartPre = pkgs.writeShellScript "odoo-start-pre.sh" (
            ''
              set -euo pipefail

              cd "$STATE_DIRECTORY"

              # 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"
              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
            '')
            + "echo pre-start: OK"
          );
          DynamicUser = true;
          User = "odoo";
          StateDirectory = "odoo";
          Environment = [
            "ODOO_RC=${cfgFile}"
          ];
        };
      };

      services.postgresql = {
        enable = true;

        ensureDatabases = [ "odoo" ];
        ensureUsers = [
          {
            name = "odoo";
            ensureDBOwnership = true;
          }
        ];
      };
    }
  );
}