Skip to content

Commit 3fdabe0

Browse files
committed
nixos/dolibarr: add H2O web server option
1 parent 38f3a23 commit 3fdabe0

File tree

1 file changed

+112
-14
lines changed

1 file changed

+112
-14
lines changed

nixos/modules/services/web-apps/dolibarr.nix

Lines changed: 112 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ let
2525
package = cfg.package.override { inherit (cfg) stateDir; };
2626

2727
cfg = config.services.dolibarr;
28-
vhostCfg = lib.optionalAttrs (cfg.nginx != null) config.services.nginx.virtualHosts."${cfg.domain}";
28+
29+
forcedTLS =
30+
if cfg.h2o != null then
31+
cfg.h2o.tls != null && cfg.h2o.tls.policy == "force"
32+
else if cfg.nginx != null then
33+
cfg.nginx.forceSSL
34+
else
35+
false;
2936

3037
mkConfigFile =
3138
filename: settings:
@@ -72,6 +79,17 @@ let
7279
else
7380
cfg.database.port;
7481

82+
# exclusivity asserted in `assertions`
83+
webServerService =
84+
if cfg.h2o != null then
85+
"h2o.service"
86+
else if cfg.nginx != null then
87+
"nginx.service"
88+
else
89+
null;
90+
91+
socketOwner = if cfg.h2o != null then config.services.h2o.user else cfg.user;
92+
7593
# see https://github.com/Dolibarr/dolibarr/blob/develop/htdocs/install/install.forced.sample.php for all possible values
7694
install = {
7795
force_install_noedit = 2;
@@ -90,7 +108,7 @@ let
90108
force_install_database = cfg.database.name;
91109
force_install_databaselogin = cfg.database.user;
92110

93-
force_install_mainforcehttps = vhostCfg.forceSSL or false;
111+
force_install_mainforcehttps = forcedTLS;
94112
force_install_createuser = false;
95113
force_install_dolibarrlogin = null;
96114
}
@@ -204,6 +222,29 @@ in
204222
description = "Dolibarr settings, see <https://github.com/Dolibarr/dolibarr/blob/develop/htdocs/conf/conf.php.example> for details.";
205223
};
206224

225+
h2o = mkOption {
226+
type = types.nullOr (
227+
types.submodule (import ../web-servers/h2o/vhost-options.nix { inherit config lib; })
228+
);
229+
default = null;
230+
example =
231+
lib.literalExpression # nix
232+
''
233+
{
234+
acme.enable = true;
235+
tls.policy = "force";
236+
compress = "ON";
237+
}
238+
'';
239+
description = ''
240+
With this option, you can customize an H2O virtual host which already
241+
has sensible defaults for Dolibarr. Set to `{ }` if you do not need any
242+
customization to the virtual host. If enabled, then by default, the
243+
{option}`serverName` is `''${domain}`, If this is set to `null` (the
244+
default), no H2O `hosts` will be configured.
245+
'';
246+
};
247+
207248
nginx = mkOption {
208249
type = types.nullOr (
209250
types.submodule (
@@ -267,6 +308,22 @@ in
267308
assertion = cfg.database.createLocally -> cfg.database.user == cfg.user;
268309
message = "services.dolibarr.database.user must match services.dolibarr.user if the database is to be automatically provisioned";
269310
}
311+
(
312+
let
313+
webServers = [
314+
"h2o"
315+
"nginx"
316+
];
317+
checkConfigs = lib.concatMapStringsSep ", " (ws: "services.dolibarr.${ws}") webServers;
318+
in
319+
{
320+
assertion = builtins.length (lib.lists.filter (ws: cfg.${ws} != null) webServers) <= 1;
321+
message = ''
322+
At most 1 web server virtual host configuration should be enabled
323+
for Dolibarr at a time. Check ${checkConfigs}.
324+
'';
325+
}
326+
)
270327
];
271328

272329
services.dolibarr.settings = {
@@ -297,7 +354,7 @@ in
297354

298355
# Security settings
299356
dolibarr_main_prod = true;
300-
dolibarr_main_force_https = vhostCfg.forceSSL or false;
357+
dolibarr_main_force_https = forcedTLS;
301358
dolibarr_main_restrict_os_commands =
302359
{
303360
"mysql" = "${pkgs.mariadb}/bin/mysqldump, ${pkgs.mariadb}/bin/mysql";
@@ -350,6 +407,39 @@ in
350407
'';
351408
};
352409

410+
services.h2o = mkIf (cfg.h2o != null) {
411+
enable = true;
412+
hosts."${cfg.domain}" = mkMerge [
413+
{
414+
settings = {
415+
paths = {
416+
"/" = {
417+
"file.dir" = "${package}/htdocs";
418+
"file.index" = [
419+
"index.php"
420+
"index.html"
421+
];
422+
redirect = {
423+
url = "/index.php/";
424+
internal = "YES";
425+
status = 307;
426+
};
427+
};
428+
};
429+
"file.custom-handler" = {
430+
extension = [ ".php" ];
431+
"fastcgi.document_root" = "${package}/htdocs";
432+
"fastcgi.connect" = {
433+
port = config.services.phpfpm.pools.dolibarr.socket;
434+
type = "unix";
435+
};
436+
};
437+
};
438+
}
439+
cfg.h2o
440+
];
441+
};
442+
353443
services.nginx.enable = mkIf (cfg.nginx != null) true;
354444
services.nginx.virtualHosts."${cfg.domain}" = mkIf (cfg.nginx != null) (
355445
lib.mkMerge [
@@ -368,6 +458,8 @@ in
368458
);
369459

370460
systemd.services."phpfpm-dolibarr" = {
461+
wantedBy = lib.optional (webServerService != null) webServerService;
462+
before = lib.optional (webServerService != null) webServerService;
371463
after = lib.optional cfg.database.createLocally dbUnit;
372464
requires = lib.optional cfg.database.createLocally dbUnit;
373465
};
@@ -388,7 +480,7 @@ in
388480

389481
settings = {
390482
"listen.mode" = "0660";
391-
"listen.owner" = cfg.user;
483+
"listen.owner" = socketOwner;
392484
"listen.group" = cfg.group;
393485
}
394486
// cfg.poolConfig;
@@ -427,17 +519,23 @@ in
427519
};
428520
};
429521

430-
users.users.dolibarr = mkIf (cfg.user == "dolibarr") {
431-
isSystemUser = true;
432-
group = cfg.group;
433-
};
434-
435-
users.groups = optionalAttrs (cfg.group == "dolibarr") {
436-
dolibarr = { };
522+
users = {
523+
users = {
524+
dolibarr = mkIf (cfg.user == "dolibarr") {
525+
isSystemUser = true;
526+
group = cfg.group;
527+
};
528+
}
529+
// lib.optionalAttrs (cfg.h2o != null) {
530+
"${config.services.h2o.user}".extraGroups = [ cfg.group ];
531+
}
532+
// lib.optionalAttrs (cfg.nginx != null) {
533+
"${config.services.nginx.user}".extraGroups = [ cfg.group ];
534+
};
535+
groups = optionalAttrs (cfg.group == "dolibarr") {
536+
dolibarr = { };
537+
};
437538
};
438539
}
439-
(mkIf (cfg.nginx != null) {
440-
users.users."${config.services.nginx.group}".extraGroups = mkIf (cfg.nginx != null) [ cfg.group ];
441-
})
442540
]);
443541
}

0 commit comments

Comments
 (0)