From 2c9f7408ca1eb0f61054d21c0d831df7eb9b2401 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Tue, 16 Jul 2024 15:21:46 +0200 Subject: [PATCH 1/8] odoo: add missing lxml-html-clean dependency Fixes this runtime error: lxml.html.clean module is now a separate project lxml_html_clean. Install lxml[html_clean] or lxml_html_clean directly. --- pkgs/by-name/od/odoo/package.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/by-name/od/odoo/package.nix b/pkgs/by-name/od/odoo/package.nix index bf86f819290e..e3b0c77ec7ec 100644 --- a/pkgs/by-name/od/odoo/package.nix +++ b/pkgs/by-name/od/odoo/package.nix @@ -58,6 +58,7 @@ in python.pkgs.buildPythonApplication rec { jinja2 libsass lxml + lxml-html-clean markupsafe num2words ofxparse From 77977ae4adab9d2957ee89489e3bd4fd90f7fae6 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Tue, 16 Jul 2024 17:51:34 +0200 Subject: [PATCH 2/8] odoo: add missing geoip2 dependency --- pkgs/by-name/od/odoo/package.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/by-name/od/odoo/package.nix b/pkgs/by-name/od/odoo/package.nix index e3b0c77ec7ec..fe3a7aed3681 100644 --- a/pkgs/by-name/od/odoo/package.nix +++ b/pkgs/by-name/od/odoo/package.nix @@ -52,6 +52,7 @@ in python.pkgs.buildPythonApplication rec { docutils-0_17 # sphinx has a docutils requirement >= 18 ebaysdk freezegun + geoip2 gevent greenlet idna From 750f1478fcbf20054b867ac2281bfc912768915a Mon Sep 17 00:00:00 2001 From: zimbatm Date: Tue, 16 Jul 2024 15:59:36 +0200 Subject: [PATCH 3/8] nixos/odoo: use env vars to configure Odoo This allows running multiple commands with the same settings. --- nixos/modules/services/finance/odoo.nix | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/nixos/modules/services/finance/odoo.nix b/nixos/modules/services/finance/odoo.nix index 45fb9c7c2397..6b7682c83cfe 100644 --- a/nixos/modules/services/finance/odoo.nix +++ b/nixos/modules/services/finance/odoo.nix @@ -85,7 +85,9 @@ in services.odoo.settings.options = { proxy_mode = cfg.domain != null; - }; + } // (lib.optionalAttrs (cfg.addons != []) { + addons_path = concatMapStringsSep "," escapeShellArg cfg.addons; + }); users.users.odoo = { isSystemUser = true; @@ -101,12 +103,16 @@ in path = [ config.services.postgresql.package ]; requires = [ "postgresql.service" ]; - script = "HOME=$STATE_DIRECTORY ${cfg.package}/bin/odoo ${optionalString (cfg.addons != []) "--addons-path=${concatMapStringsSep "," escapeShellArg cfg.addons}"} -c ${cfgFile}"; serviceConfig = { + ExecStart = "${cfg.package}/bin/odoo"; DynamicUser = true; User = "odoo"; StateDirectory = "odoo"; + Environment = [ + "HOME=%S/odoo" + "ODOO_RC=${cfgFile}" + ]; }; }; From c570314635abd5ce5be952d5992d5fae7d9910e1 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Tue, 16 Jul 2024 16:12:28 +0200 Subject: [PATCH 4/8] nixos/odoo: add autoInit option When enabled, Odoo will automatically initialize the database on startup. --- nixos/modules/services/finance/odoo.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/nixos/modules/services/finance/odoo.nix b/nixos/modules/services/finance/odoo.nix index 6b7682c83cfe..819d1a60ef81 100644 --- a/nixos/modules/services/finance/odoo.nix +++ b/nixos/modules/services/finance/odoo.nix @@ -20,6 +20,8 @@ in description = "Odoo addons."; }; + autoInit = mkEnableOption "automatically initialize the DB"; + settings = mkOption { type = format.type; default = {}; @@ -113,7 +115,9 @@ in "HOME=%S/odoo" "ODOO_RC=${cfgFile}" ]; - }; + } // (lib.optionalAttrs cfg.autoInit { + ExecStartPre = "${cfg.package}/bin/odoo --init=INIT --database=odoo --db_user=odoo --stop-after-init"; + }); }; services.postgresql = { From d11a54cb2ff0725f2fbcdaf73f51bf40d6c4ee8b Mon Sep 17 00:00:00 2001 From: zimbatm Date: Tue, 16 Jul 2024 17:57:37 +0200 Subject: [PATCH 5/8] nixos/odoo: set data_dir explicitly Don't hide the data is a private sub-folder. Before: * /var/lib/private/odoo/.local/share/Odoo/ After: * /var/lib/private/odoo/data --- nixos/modules/services/finance/odoo.nix | 26 +++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/nixos/modules/services/finance/odoo.nix b/nixos/modules/services/finance/odoo.nix index 819d1a60ef81..6bb8d4d71422 100644 --- a/nixos/modules/services/finance/odoo.nix +++ b/nixos/modules/services/finance/odoo.nix @@ -86,6 +86,7 @@ in }; services.odoo.settings.options = { + data_dir = "/var/lib/private/odoo/data"; proxy_mode = cfg.domain != null; } // (lib.optionalAttrs (cfg.addons != []) { addons_path = concatMapStringsSep "," escapeShellArg cfg.addons; @@ -108,16 +109,33 @@ in 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" + ${cfg.package}/bin/odoo --init=INIT --database=odoo --db_user=odoo --stop-after-init + '') + + "echo pre-start: OK" + ); DynamicUser = true; User = "odoo"; StateDirectory = "odoo"; Environment = [ - "HOME=%S/odoo" "ODOO_RC=${cfgFile}" ]; - } // (lib.optionalAttrs cfg.autoInit { - ExecStartPre = "${cfg.package}/bin/odoo --init=INIT --database=odoo --db_user=odoo --stop-after-init"; - }); + }; }; services.postgresql = { From 67bcb287eccb779f9a3fd9540f480a6671a6217e Mon Sep 17 00:00:00 2001 From: Sirio Balmelli Date: Thu, 18 Jul 2024 15:50:28 +0000 Subject: [PATCH 6/8] modules/odoo: execute autoInit only once Signed-off-by: Sirio Balmelli --- nixos/modules/services/finance/odoo.nix | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/nixos/modules/services/finance/odoo.nix b/nixos/modules/services/finance/odoo.nix index 6bb8d4d71422..6185695288d9 100644 --- a/nixos/modules/services/finance/odoo.nix +++ b/nixos/modules/services/finance/odoo.nix @@ -123,9 +123,14 @@ in rmdir .local fi '' - + (lib.optionalString cfg.autoInit '' - echo "pre-start: auto-init" - ${cfg.package}/bin/odoo --init=INIT --database=odoo --db_user=odoo --stop-after-init + + (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 + touch "$INITIALIZED" + fi '') + "echo pre-start: OK" ); From 85761ade56b0a99bee38cccb5c05f83077ab7521 Mon Sep 17 00:00:00 2001 From: Sirio Balmelli Date: Thu, 18 Jul 2024 15:50:29 +0000 Subject: [PATCH 7/8] modules/odoo: add autoInitExtraFlags Allow module user to specify additional flags to be passed on autoInit Signed-off-by: Sirio Balmelli --- nixos/modules/services/finance/odoo.nix | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/nixos/modules/services/finance/odoo.nix b/nixos/modules/services/finance/odoo.nix index 6185695288d9..074617d35e3d 100644 --- a/nixos/modules/services/finance/odoo.nix +++ b/nixos/modules/services/finance/odoo.nix @@ -22,6 +22,15 @@ in autoInit = mkEnableOption "automatically initialize the DB"; + autoInitExtraFlags = mkOption { + type = with types; listOf str; + default = [ ]; + example = literalExpression /*nix*/ '' + [ "--without-demo=all" ] + ''; + description = "Extra flags passed to odoo when run for the first time by autoInit"; + }; + settings = mkOption { type = format.type; default = {}; @@ -128,7 +137,7 @@ in 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 + ${cfg.package}/bin/odoo --init=INIT --database=odoo --db_user=odoo --stop-after-init ${concatStringsSep " " cfg.autoInitExtraFlags} touch "$INITIALIZED" fi '') From c2b4c0e51d84370db640e331a60888ddaaf13486 Mon Sep 17 00:00:00 2001 From: Sirio Balmelli Date: Thu, 18 Jul 2024 15:50:32 +0000 Subject: [PATCH 8/8] tests/odoo: replace db init workaround with services.odoo.autoInit Signed-off-by: Sirio Balmelli --- nixos/tests/odoo.nix | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/nixos/tests/odoo.nix b/nixos/tests/odoo.nix index 45ec7b7d7a6b..d3764cbc9f0b 100644 --- a/nixos/tests/odoo.nix +++ b/nixos/tests/odoo.nix @@ -12,20 +12,10 @@ import ./make-test-python.nix ({ pkgs, lib, package ? pkgs.odoo, ...} : { services.odoo = { enable = true; package = package; + autoInit = true; + autoInitExtraFlags = [ "--without-demo=all" ]; domain = "localhost"; }; - - # odoo does not automatically initialize its database, - # even if passing what _should_ be the equivalent of these options: - # settings = { - # options = { - # database = "odoo"; - # init = "base"; - # }; - # }; - systemd.services.odoo.preStart = '' - HOME=$STATE_DIRECTORY ${package}/bin/odoo -d odoo -i base --stop-after-init --without-demo all - ''; }; };