wlroots: 0.18.1 -> 0.18.2 (#364488)
[NixPkgs.git] / nixos / modules / services / finance / odoo.nix
blobec0c4992566e4dbdf795dc3731f13ca98031f651
2   config,
3   pkgs,
4   lib,
5   ...
6 }:
7 let
8   cfg = config.services.odoo;
9   format = pkgs.formats.ini { };
12   options = {
13     services.odoo = {
14       enable = lib.mkEnableOption "odoo, an open source ERP and CRM system";
16       package = lib.mkPackageOption pkgs "odoo" { };
18       addons = lib.mkOption {
19         type = with lib.types; listOf package;
20         default = [ ];
21         example = lib.literalExpression "[ pkgs.odoo_enterprise ]";
22         description = "Odoo addons.";
23       };
25       autoInit = lib.mkEnableOption "automatically initialize the DB";
27       autoInitExtraFlags = lib.mkOption {
28         type = with lib.types; listOf str;
29         default = [ ];
30         example =
31           lib.literalExpression # nix
32             ''
33               [ "--without-demo=all" ]
34             '';
35         description = "Extra flags passed to odoo when run for the first time by autoInit";
36       };
38       settings = lib.mkOption {
39         type = format.type;
40         default = { };
41         description = ''
42           Odoo configuration settings. For more details see <https://www.odoo.com/documentation/15.0/administration/install/deploy.html>
43         '';
44         example = lib.literalExpression ''
45           options = {
46             db_user = "odoo";
47             db_password="odoo";
48           };
49         '';
50       };
52       domain = lib.mkOption {
53         type = with lib.types; nullOr str;
54         description = "Domain to host Odoo with nginx";
55         default = null;
56       };
57     };
58   };
60   config = lib.mkIf (cfg.enable) (
61     let
62       cfgFile = format.generate "odoo.cfg" cfg.settings;
63     in
64     {
65       services.nginx = lib.mkIf (cfg.domain != null) {
66         upstreams = {
67           odoo.servers = {
68             "127.0.0.1:8069" = { };
69           };
71           odoochat.servers = {
72             "127.0.0.1:8072" = { };
73           };
74         };
76         virtualHosts."${cfg.domain}" = {
77           extraConfig = ''
78             proxy_read_timeout 720s;
79             proxy_connect_timeout 720s;
80             proxy_send_timeout 720s;
82             proxy_set_header X-Forwarded-Host $host;
83             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
84             proxy_set_header X-Forwarded-Proto $scheme;
85             proxy_set_header X-Real-IP $remote_addr;
86           '';
88           locations = {
89             "/longpolling" = {
90               proxyPass = "http://odoochat";
91             };
93             "/" = {
94               proxyPass = "http://odoo";
95               extraConfig = ''
96                 proxy_redirect off;
97               '';
98             };
99           };
100         };
101       };
103       services.odoo.settings.options =
104         {
105           data_dir = "/var/lib/private/odoo/data";
106           proxy_mode = cfg.domain != null;
107         }
108         // (lib.optionalAttrs (cfg.addons != [ ]) {
109           addons_path = lib.concatMapStringsSep "," lib.escapeShellArg cfg.addons;
110         });
112       users.users.odoo = {
113         isSystemUser = true;
114         group = "odoo";
115       };
116       users.groups.odoo = { };
118       systemd.services.odoo = {
119         wantedBy = [ "multi-user.target" ];
120         after = [
121           "network.target"
122           "postgresql.service"
123         ];
125         # pg_dump
126         path = [ config.services.postgresql.package ];
128         requires = [ "postgresql.service" ];
130         serviceConfig = {
131           ExecStart = "${cfg.package}/bin/odoo";
132           ExecStartPre = pkgs.writeShellScript "odoo-start-pre.sh" (
133             ''
134               set -euo pipefail
136               cd "$STATE_DIRECTORY"
138               # Auto-migrate old deployments
139               if [[ -d .local/share/Odoo ]]; then
140                 echo "pre-start: migrating state directory from $STATE_DIRECTORY/.local/share/Odoo to $STATE_DIRECTORY/data"
141                 mv .local/share/Odoo ./data
142                 rmdir .local/share
143                 rmdir .local
144               fi
145             ''
146             + (lib.optionalString cfg.autoInit ''
147               echo "pre-start: auto-init"
148               INITIALIZED="${cfg.settings.options.data_dir}/.odoo.initialized"
149               if [ ! -e "$INITIALIZED" ]; then
150                 ${cfg.package}/bin/odoo  --init=INIT --database=odoo --db_user=odoo --stop-after-init ${lib.concatStringsSep " " cfg.autoInitExtraFlags}
151                 touch "$INITIALIZED"
152               fi
153             '')
154             + "echo pre-start: OK"
155           );
156           DynamicUser = true;
157           User = "odoo";
158           StateDirectory = "odoo";
159           Environment = [
160             "ODOO_RC=${cfgFile}"
161           ];
162         };
163       };
165       services.postgresql = {
166         enable = true;
168         ensureDatabases = [ "odoo" ];
169         ensureUsers = [
170           {
171             name = "odoo";
172             ensureDBOwnership = true;
173           }
174         ];
175       };
176     }
177   );