1 { config, lib, pkgs, ... }:
18 cfg = config.services.movim;
21 "output_buffering" = 0;
22 "error_reporting" = "E_ALL & ~E_DEPRECATED & ~E_STRICT";
23 "opcache.enable_cli" = 1;
24 "opcache.interned_strings_buffer" = 8;
25 "opcache.max_accelerated_files" = 6144;
26 "opcache.memory_consumption" = 128;
27 "opcache.revalidate_freq" = 2;
28 "opcache.fast_shutdown" = 1;
31 phpCfg = generators.toKeyValue
32 { mkKeyValue = generators.mkKeyValueDefault { } " = "; }
33 (defaultPHPCfg // cfg.phpCfg);
37 bevalue = a: lib.escapeShellArg (generators.mkValueStringDefault { } a);
39 lib.concatStringsSep " "
40 (lib.attrsets.foldlAttrs
41 (acc: k: v: acc ++ lib.optional (v != null) "--${k}=${bevalue v}")
47 p = cfg.package.override
50 withPgsql = cfg.database.type == "pgsql";
51 withMysql = cfg.database.type == "mysql";
52 inherit (cfg) minifyStaticFiles;
53 } // lib.optionalAttrs (lib.isAttrs cfg.minifyStaticFiles) (with cfg.minifyStaticFiles; {
54 esbuild = esbuild.package;
55 lightningcss = lightningcss.package;
56 scour = scour.package;
59 p.overrideAttrs (finalAttrs: prevAttrs:
61 appDir = "$out/share/php/${finalAttrs.pname}";
64 # Symlinking in our state directories
65 rm -rf $out/.env $out/cache ${appDir}/public/cache
66 ln -s ${cfg.dataDir}/.env ${appDir}/.env
67 ln -s ${cfg.dataDir}/public/cache ${appDir}/public/cache
68 ln -s ${cfg.logDir} ${appDir}/log
69 ln -s ${cfg.runtimeDir}/cache ${appDir}/cache
73 # Expose PHP Composer for scripts
75 echo "#!${lib.getExe pkgs.dash}" > $out/bin/movim-composer
76 echo "${finalAttrs.php.packages.composer}/bin/composer --working-dir="${appDir}" \"\$@\"" >> $out/bin/movim-composer
77 chmod +x $out/bin/movim-composer
80 podConfigInputDisableReplace = lib.optionalString (podConfigFlags != "")
81 (lib.concatStringsSep "\n"
82 (lib.attrsets.foldlAttrs
84 acc ++ lib.optional (v != null)
85 # Disable all Admin panel options that were set in the
86 # `cfg.podConfig` to prevent confusing situtions where the
87 # values are rewritten on server reboot
89 substituteInPlace ${appDir}/app/Widgets/AdminMain/adminmain.tpl \
90 --replace-warn 'name="${k}"' 'name="${k}" readonly'
95 precompressStaticFilesJobs =
97 inherit (cfg.precompressStaticFiles) brotli gzip;
99 findTextFileNames = lib.concatStringsSep " -o "
100 (builtins.map (n: ''-iname "*.${n}"'')
101 [ "css" "ini" "js" "json" "manifest" "mjs" "svg" "webmanifest" ]);
103 lib.concatStringsSep "\n" [
104 (lib.optionalString brotli.enable ''
105 echo -n "Precompressing static files with Brotli …"
106 find ${appDir}/public -type f ${findTextFileNames} -print0 \
107 | xargs -0 -n 1 -P $NIX_BUILD_CORES ${pkgs.writeShellScript "movim_precompress_broti" ''
109 ${lib.getExe brotli.package} --keep --quality=${builtins.toString brotli.compressionLevel} --output=$file.br $file
113 (lib.optionalString gzip.enable ''
114 echo -n "Precompressing static files with Gzip …"
115 find ${appDir}/public -type f ${findTextFileNames} -print0 \
116 | xargs -0 -n 1 -P $NIX_BUILD_CORES ${pkgs.writeShellScript "movim_precompress_broti" ''
118 ${lib.getExe gzip.package} -c -${builtins.toString gzip.compressionLevel} $file > $file.gz
125 postInstall = lib.concatStringsSep "\n\n" [
126 prevAttrs.postInstall
129 podConfigInputDisableReplace
130 precompressStaticFilesJobs
134 configFile = pipe cfg.settings [
135 (filterAttrsRecursive (_: v: v != null))
136 (generators.toKeyValue { })
137 (pkgs.writeText "movim-env")
141 fpm = config.services.phpfpm.pools.${pool};
142 phpExecutionUnit = "phpfpm-${pool}";
145 "postgresql" = "postgresql.service";
146 "mysql" = "mysql.service";
147 }.${cfg.database.type};
152 enable = mkEnableOption "a Movim instance";
153 package = mkPackageOption pkgs "movim" { };
154 phpPackage = mkPackageOption pkgs "php" { };
157 type = with types; attrsOf (oneOf [ int str bool ]);
158 defaultText = literalExpression (generators.toPretty { } defaultPHPCfg);
160 description = "Extra PHP INI options such as `memory_limit`, `max_execution_time`, etc.";
164 type = types.nonEmptyStr;
166 description = "User running Movim service";
170 type = types.nonEmptyStr;
172 description = "Group running Movim service";
176 type = types.nonEmptyStr;
177 default = "/var/lib/movim";
178 description = "State directory of the `movim` user which holds the application’s state & data.";
182 type = types.nonEmptyStr;
183 default = "/var/log/movim";
184 description = "Log directory of the `movim` user which holds the application’s logs.";
187 runtimeDir = mkOption {
188 type = types.nonEmptyStr;
189 default = "/run/movim";
190 description = "Runtime directory of the `movim` user which holds the application’s caches & temporary files.";
194 type = types.nonEmptyStr;
195 description = "Fully-qualified domain name (FQDN) for the Movim instance.";
201 description = "Movim daemon port.";
207 description = "Debugging logs.";
213 description = "Verbose logs.";
216 minifyStaticFiles = mkOption {
217 type = with types; either bool (submodule {
220 type = types.submodule {
222 enable = mkEnableOption "Script minification";
223 package = mkPackageOption pkgs "esbuild" { };
225 type = with types; nullOr nonEmptyStr;
232 type = types.submodule {
234 enable = mkEnableOption "Script minification";
235 package = mkPackageOption pkgs "lightningcss" { };
237 type = with types; nullOr nonEmptyStr;
244 type = types.submodule {
246 enable = mkEnableOption "SVG minification";
247 package = mkPackageOption pkgs "scour" { };
254 description = "Do minification on public static files";
257 precompressStaticFiles = mkOption {
258 type = with types; submodule {
261 enable = mkEnableOption "Brotli precompression";
262 package = mkPackageOption pkgs "brotli" { };
263 compressionLevel = mkOption {
264 type = types.ints.between 0 11;
266 description = "Brotli compression level";
270 enable = mkEnableOption "Gzip precompression";
271 package = mkPackageOption pkgs "gzip" { };
272 compressionLevel = mkOption {
273 type = types.ints.between 1 9;
275 description = "Gzip compression level";
281 brotli.enable = true;
284 description = "Aggressively precompress static files";
287 podConfig = mkOption {
288 type = types.submodule {
291 type = with types; nullOr str;
293 description = "Content of the info box on the login page";
296 description = mkOption {
297 type = with types; nullOr str;
299 description = "General description of the instance";
302 timezone = mkOption {
303 type = with types; nullOr str;
305 description = "The server timezone";
308 restrictsuggestions = mkOption {
309 type = with types; nullOr bool;
311 description = "Only suggest chatrooms, Communities and other contents that are available on the user XMPP server and related services";
314 chatonly = mkOption {
315 type = with types; nullOr bool;
317 description = "Disable all the social feature (Communities, Blog…) and keep only the chat ones";
320 disableregistration = mkOption {
321 type = with types; nullOr bool;
323 description = "Remove the XMPP registration flow and buttons from the interface";
326 loglevel = mkOption {
327 type = with types; nullOr (ints.between 0 3);
329 description = "The server loglevel";
333 type = with types; nullOr str;
335 description = "The server main locale";
338 xmppdomain = mkOption {
339 type = with types; nullOr str;
341 description = "The default XMPP server domain";
344 xmppdescription = mkOption {
345 type = with types; nullOr str;
347 description = "The default XMPP server description";
350 xmppwhitelist = mkOption {
351 type = with types; nullOr str;
353 description = "The allowlisted XMPP servers";
359 Pod configuration (values from `php daemon.php config --help`).
360 Note that these values will now be disabled in the admin panel.
364 settings = mkOption {
365 type = with types; attrsOf (nullOr (oneOf [ int str bool ]));
367 description = ".env settings for Movim. Secrets should use `secretFile` option instead. `null`s will be culled.";
370 secretFile = mkOption {
371 type = with types; nullOr path;
373 description = "The secret file to be sourced for the .env settings.";
378 type = types.enum [ "mysql" "postgresql" ];
380 default = "postgresql";
381 description = "Database engine to use.";
387 description = "Database name.";
393 description = "Database username.";
396 createLocally = mkOption {
399 description = "local database using UNIX socket authentication";
404 type = with types; nullOr (submodule
405 (import ../web-servers/nginx/vhost-options.nix {
409 example = lib.literalExpression /* nginx */ ''
412 "pics.''${config.networking.domain}"
419 With this option, you can customize an nginx virtual host which already has sensible defaults for Movim.
420 Set to `{ }` if you do not need any customization to the virtual host.
421 If enabled, then by default, the {option}`serverName` is `''${domain}`,
422 If this is set to null (the default), no nginx virtualHost will be configured.
426 poolConfig = mkOption {
427 type = with types; attrsOf (oneOf [ int str bool ]);
429 description = "Options for Movim’s PHP-FPM pool.";
434 config = mkIf cfg.enable {
435 environment.systemPackages = [ cfg.package ];
439 movim = mkIf (cfg.user == "movim") {
443 "${config.services.nginx.user}".extraGroups = [ cfg.group ];
454 DAEMON_URL = "//${cfg.domain}";
455 DAEMON_PORT = cfg.port;
456 DAEMON_INTERFACE = "127.0.0.1";
457 DAEMON_DEBUG = cfg.debug;
458 DAEMON_VERBOSE = cfg.verbose;
460 (mkIf cfg.database.createLocally {
462 "postgresql" = "pgsql";
464 }.${cfg.database.type};
465 DB_HOST = "localhost";
466 DB_PORT = config.services.${cfg.database.type}.settings.port;
467 DB_DATABASE = cfg.database.name;
468 DB_USERNAME = cfg.database.user;
473 poolConfig = lib.mapAttrs' (n: v: lib.nameValuePair n (lib.mkDefault v)) {
475 "php_admin_value[error_log]" = "stderr";
476 "php_admin_flag[log_errors]" = true;
477 "catch_workers_output" = true;
478 "pm.max_children" = 32;
479 "pm.start_servers" = 2;
480 "pm.min_spare_servers" = 2;
481 "pm.max_spare_servers" = 8;
482 "pm.max_requests" = 500;
486 nginx = mkIf (cfg.nginx != null) {
488 recommendedOptimisation = true;
489 recommendedGzipSettings = true;
490 recommendedBrotliSettings = true;
491 recommendedProxySettings = true;
492 # TODO: recommended cache options already in Nginx⁇
493 appendHttpConfig = /* nginx */ ''
494 fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=nginx_cache:100m inactive=60m;
495 fastcgi_cache_key "$scheme$request_method$host$request_uri";
497 virtualHosts."${cfg.domain}" = mkMerge [
500 root = lib.mkForce "${package}/share/php/movim/public";
504 extraConfig = /* nginx */ ''
511 extraConfig = /* nginx */ ''
516 "~ /\\.(?!well-known).*" = {
518 extraConfig = /* nginx */ ''
522 # Ask nginx to cache every URL starting with "/picture"
525 tryFiles = "$uri $uri/ /index.php$is_args$args";
526 extraConfig = /* nginx */ ''
527 set $no_cache 0; # Enable cache only there
532 tryFiles = "$uri $uri/ /index.php$is_args$args";
533 extraConfig = /* nginx */ ''
534 # https://github.com/movim/movim/issues/314
535 add_header Content-Security-Policy "default-src 'self'; img-src 'self' aesgcm: https:; media-src 'self' aesgcm: https:; script-src 'self' 'unsafe-eval' 'unsafe-inline'; style-src 'self' 'unsafe-inline';";
541 tryFiles = "$uri =404";
542 extraConfig = /* nginx */ ''
543 include ${config.services.nginx.package}/conf/fastcgi.conf;
544 add_header X-Cache $upstream_cache_status;
545 fastcgi_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
546 fastcgi_cache nginx_cache;
547 fastcgi_cache_valid any 7d;
548 fastcgi_cache_bypass $no_cache;
549 fastcgi_no_cache $no_cache;
550 fastcgi_split_path_info ^(.+\.php)(/.+)$;
551 fastcgi_index index.php;
552 fastcgi_pass unix:${fpm.socket};
557 proxyPass = "http://${cfg.settings.DAEMON_INTERFACE}:${builtins.toString cfg.port}/";
558 proxyWebsockets = true;
559 recommendedProxySettings = true;
560 extraConfig = /* nginx */ ''
561 proxy_set_header X-Forwarded-Proto $scheme;
566 extraConfig = /* ngnix */ ''
573 mysql = mkIf (cfg.database.createLocally && cfg.database.type == "mysql") {
574 enable = mkDefault true;
575 package = mkDefault pkgs.mariadb;
576 ensureDatabases = [ cfg.database.name ];
579 ensureDBOwnership = true;
583 postgresql = mkIf (cfg.database.createLocally && cfg.database.type == "postgresql") {
584 enable = mkDefault true;
585 ensureDatabases = [ cfg.database.name ];
588 ensureDBOwnership = true;
591 host ${cfg.database.name} ${cfg.database.user} localhost trust
595 phpfpm.pools.${pool} =
598 if (cfg.nginx != null)
599 then config.services.nginx.user
603 phpPackage = package.php;
613 "listen.owner" = socketOwner;
614 "listen.group" = cfg.group;
615 "listen.mode" = "0660";
616 "catch_workers_output" = true;
622 services.movim-data-setup = {
623 description = "Movim setup: .env file, databases init, cache reload";
624 wantedBy = [ "multi-user.target" ];
625 requiredBy = [ "${phpExecutionUnit}.service" ];
626 before = [ "${phpExecutionUnit}.service" ];
627 after = lib.optional cfg.database.createLocally dbService;
628 requires = lib.optional cfg.database.createLocally dbService;
635 } // lib.optionalAttrs (cfg.secretFile != null) {
636 LoadCredential = "env-secrets:${cfg.secretFile}";
641 rm -f ${cfg.dataDir}/.env
642 cp --no-preserve=all ${configFile} ${cfg.dataDir}/.env
643 echo -e '\n' >> ${cfg.dataDir}/.env
644 if [[ -f "$CREDENTIALS_DIRECTORY/env-secrets" ]]; then
645 cat "$CREDENTIALS_DIRECTORY/env-secrets" >> ${cfg.dataDir}/.env
646 echo -e '\n' >> ${cfg.dataDir}/.env
650 mkdir -p ${cfg.dataDir}/public/cache ${cfg.logDir} ${cfg.runtimeDir}/cache
651 chmod -R ug+rw ${cfg.dataDir}/public/cache
652 chmod -R ug+rw ${cfg.logDir}
653 chmod -R ug+rwx ${cfg.runtimeDir}/cache
656 MOVIM_VERSION="${package.version}"
657 if [[ ! -f "${cfg.dataDir}/.migration-version" ]] || [[ "$MOVIM_VERSION" != "$(<${cfg.dataDir}/.migration-version)" ]]; then
658 ${package}/bin/movim-composer movim:migrate && echo $MOVIM_VERSION > ${cfg.dataDir}/.migration-version
661 + lib.optionalString (podConfigFlags != "") (
663 flags = lib.concatStringsSep " "
664 ([ "--no-interaction" ]
665 ++ lib.optional cfg.debug "-vvv"
666 ++ lib.optional (!cfg.debug && cfg.verbose) "-v");
669 ${lib.getExe package} config ${podConfigFlags}
675 description = "Movim daemon";
676 wantedBy = [ "multi-user.target" ];
677 after = [ "movim-data-setup.service" ];
678 requires = [ "movim-data-setup.service" ]
679 ++ lib.optional cfg.database.createLocally dbService;
681 PUBLIC_URL = "//${cfg.domain}";
682 WS_PORT = builtins.toString cfg.port;
688 WorkingDirectory = "${package}/share/php/movim";
689 ExecStart = "${lib.getExe package} start";
693 services.${phpExecutionUnit} = {
694 after = [ "movim-data-setup.service" ];
695 requires = [ "movim-data-setup.service" ]
696 ++ lib.optional cfg.database.createLocally dbService;
699 tmpfiles.settings."10-movim" = with cfg; {
700 "${dataDir}".d = { inherit user group; mode = "0710"; };
701 "${dataDir}/public".d = { inherit user group; mode = "0750"; };
702 "${dataDir}/public/cache".d = { inherit user group; mode = "0750"; };
703 "${runtimeDir}".d = { inherit user group; mode = "0700"; };
704 "${runtimeDir}/cache".d = { inherit user group; mode = "0700"; };
705 "${logDir}".d = { inherit user group; mode = "0700"; };