1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.odoo;
7 format = pkgs.formats.ini {};
8in
9{
10 options = {
11 services.odoo = {
12 enable = mkEnableOption (lib.mdDoc "odoo");
13
14 package = mkOption {
15 type = types.package;
16 default = pkgs.odoo;
17 defaultText = literalExpression "pkgs.odoo";
18 description = lib.mdDoc "Odoo package to use.";
19 };
20
21 addons = mkOption {
22 type = with types; listOf package;
23 default = [];
24 example = literalExpression "[ pkgs.odoo_enterprise ]";
25 description = lib.mdDoc "Odoo addons.";
26 };
27
28 settings = mkOption {
29 type = format.type;
30 default = {};
31 description = lib.mdDoc ''
32 Odoo configuration settings. For more details see <https://www.odoo.com/documentation/15.0/administration/install/deploy.html>
33 '';
34 example = literalExpression ''
35 options = {
36 db_user = "odoo";
37 db_password="odoo";
38 };
39 '';
40 };
41
42 domain = mkOption {
43 type = with types; nullOr str;
44 description = lib.mdDoc "Domain to host Odoo with nginx";
45 default = null;
46 };
47 };
48 };
49
50 config = mkIf (cfg.enable) (let
51 cfgFile = format.generate "odoo.cfg" cfg.settings;
52 in {
53 services.nginx = mkIf (cfg.domain != null) {
54 upstreams = {
55 odoo.servers = {
56 "127.0.0.1:8069" = {};
57 };
58
59 odoochat.servers = {
60 "127.0.0.1:8072" = {};
61 };
62 };
63
64 virtualHosts."${cfg.domain}" = {
65 extraConfig = ''
66 proxy_read_timeout 720s;
67 proxy_connect_timeout 720s;
68 proxy_send_timeout 720s;
69
70 proxy_set_header X-Forwarded-Host $host;
71 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
72 proxy_set_header X-Forwarded-Proto $scheme;
73 proxy_set_header X-Real-IP $remote_addr;
74 '';
75
76 locations = {
77 "/longpolling" = {
78 proxyPass = "http://odoochat";
79 };
80
81 "/" = {
82 proxyPass = "http://odoo";
83 extraConfig = ''
84 proxy_redirect off;
85 '';
86 };
87 };
88 };
89 };
90
91 services.odoo.settings.options = {
92 proxy_mode = cfg.domain != null;
93 };
94
95 users.users.odoo = {
96 isSystemUser = true;
97 group = "odoo";
98 };
99 users.groups.odoo = {};
100
101 systemd.services.odoo = {
102 wantedBy = [ "multi-user.target" ];
103 after = [ "network.target" "postgresql.service" ];
104
105 # pg_dump
106 path = [ config.services.postgresql.package ];
107
108 requires = [ "postgresql.service" ];
109 script = "HOME=$STATE_DIRECTORY ${cfg.package}/bin/odoo ${optionalString (cfg.addons != []) "--addons-path=${concatMapStringsSep "," escapeShellArg cfg.addons}"} -c ${cfgFile}";
110
111 serviceConfig = {
112 DynamicUser = true;
113 User = "odoo";
114 StateDirectory = "odoo";
115 };
116 };
117
118 services.postgresql = {
119 enable = true;
120
121 ensureDatabases = [ "odoo" ];
122 ensureUsers = [{
123 name = "odoo";
124 ensureDBOwnership = true;
125 }];
126 };
127 });
128}