diff --git a/nixos/modules/services/finance/odoo.nix b/nixos/modules/services/finance/odoo.nix index 45fb9c7c2397..074617d35e3d 100644 --- a/nixos/modules/services/finance/odoo.nix +++ b/nixos/modules/services/finance/odoo.nix @@ -20,6 +20,17 @@ in description = "Odoo addons."; }; + 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 = {}; @@ -84,8 +95,11 @@ 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; + }); users.users.odoo = { isSystemUser = true; @@ -101,12 +115,40 @@ 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"; + 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 ${concatStringsSep " " cfg.autoInitExtraFlags} + touch "$INITIALIZED" + fi + '') + + "echo pre-start: OK" + ); DynamicUser = true; User = "odoo"; StateDirectory = "odoo"; + Environment = [ + "ODOO_RC=${cfgFile}" + ]; }; }; 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 - ''; }; }; diff --git a/pkgs/by-name/od/odoo/package.nix b/pkgs/by-name/od/odoo/package.nix index bf86f819290e..fe3a7aed3681 100644 --- a/pkgs/by-name/od/odoo/package.nix +++ b/pkgs/by-name/od/odoo/package.nix @@ -52,12 +52,14 @@ in python.pkgs.buildPythonApplication rec { docutils-0_17 # sphinx has a docutils requirement >= 18 ebaysdk freezegun + geoip2 gevent greenlet idna jinja2 libsass lxml + lxml-html-clean markupsafe num2words ofxparse