diff --git a/nixos/modules/services/web-apps/movim.nix b/nixos/modules/services/web-apps/movim.nix index c9314e28e949..d7f6ad5bb5ef 100644 --- a/nixos/modules/services/web-apps/movim.nix +++ b/nixos/modules/services/web-apps/movim.nix @@ -44,11 +44,17 @@ let package = let - p = cfg.package.override { - inherit phpCfg; - withPgsql = cfg.database.type == "pgsql"; - withMysql = cfg.database.type == "mysql"; - }; + p = cfg.package.override + ({ + inherit phpCfg; + withPgsql = cfg.database.type == "pgsql"; + withMysql = cfg.database.type == "mysql"; + inherit (cfg) minifyStaticFiles; + } // lib.optionalAttrs (lib.isAttrs cfg.minifyStaticFiles) (with cfg.minifyStaticFiles; { + esbuild = esbuild.package; + lightningcss = lightningcss.package; + scour = scour.package; + })); in p.overrideAttrs (finalAttrs: prevAttrs: let @@ -177,6 +183,47 @@ in description = "Verbose logs."; }; + minifyStaticFiles = mkOption { + type = with types; either bool (submodule { + options = { + script = mkOption { + type = types.submodule { + options = { + enable = mkEnableOption "Script minification"; + package = mkPackageOption pkgs "esbuild" { }; + target = mkOption { + type = with types; nullOr nonEmptyStr; + default = null; + }; + }; + }; + }; + style = mkOption { + type = types.submodule { + options = { + enable = mkEnableOption "Script minification"; + package = mkPackageOption pkgs "lightningcss" { }; + target = mkOption { + type = with types; nullOr nonEmptyStr; + default = null; + }; + }; + }; + }; + svg = mkOption { + type = types.submodule { + options = { + enable = mkEnableOption "SVG minification"; + package = mkPackageOption pkgs "scour" { }; + }; + }; + }; + }; + }); + default = true; + description = "Do minification on public static files"; + }; + podConfig = mkOption { type = types.submodule { options = { diff --git a/pkgs/by-name/mo/movim/package.nix b/pkgs/by-name/mo/movim/package.nix index 04695835710f..6380baf3d46f 100644 --- a/pkgs/by-name/mo/movim/package.nix +++ b/pkgs/by-name/mo/movim/package.nix @@ -6,9 +6,37 @@ , phpCfg ? null , withPgsql ? true # “strongly recommended” according to docs , withMysql ? false +, minifyStaticFiles ? false # default files are often not minified +, parallel +, esbuild +, lightningcss +, scour , nixosTests }: +let + defaultMinifyOpts = { + script = { + enable = false; + target = "es2021"; + }; + style = { + enable = false; + browserslist = "defaults, Firefox ESR, last 20 Firefox major versions, last 20 Chrome major versions, last 3 Safari major versions, last 1 KaiOS version, and supports css-variables"; + }; + svg = { + enable = false; + }; + }; + + minify = lib.recursiveUpdate defaultMinifyOpts + (if lib.isBool minifyStaticFiles && minifyStaticFiles then + { script.enable = true; style.enable = true; svg.enable = true; } + else if lib.isAttrs minifyStaticFiles then + lib.filterAttrsRecursive (_: v: v != null) minifyStaticFiles + else + { }); +in php.buildComposerProject (finalAttrs: { pname = "movim"; version = "0.23.0.20240328"; @@ -31,6 +59,12 @@ php.buildComposerProject (finalAttrs: { extraConfig = phpCfg; }); + nativeBuildInputs = + lib.optional (lib.any (x: x.enable) (lib.attrValues minify)) parallel + ++ lib.optional minify.script.enable esbuild + ++ lib.optional minify.style.enable lightningcss + ++ lib.optional minify.svg.enable scour; + # no listed license # pinned commonmark composerStrictValidation = false; @@ -59,6 +93,42 @@ php.buildComposerProject (finalAttrs: { --replace-fail "Imagick::ALPHACHANNEL_ACTIVATE" "Imagick::ALPHACHANNEL_ON" ''; + preBuild = lib.optionalString minify.script.enable '' + find ./public -type f -iname "*.js" \ + | parallel ${lib.escapeShellArgs [ + "--will-cite" + "-j $NIX_BUILD_CORES" + '' + tmp="$(mktemp)" + esbuild {} --minify --target=${lib.escapeShellArg minify.script.target} --outfile=$tmp + [[ "$(stat -c %s $tmp)" -lt "$(stat -c %s {})" ]] && mv $tmp {} + '' + ]} + '' + lib.optionalString minify.style.enable '' + export BROWSERLIST=${lib.escapeShellArg minify.style.browserslist} + find ./public -type f -iname "*.css" \ + | parallel ${lib.escapeShellArgs [ + "--will-cite" + "-j $NIX_BUILD_CORES" + '' + tmp="$(mktemp)" + lightningcss {} --minify --browserslist --output-file=$tmp + [[ "$(stat -c %s $tmp)" -lt "$(stat -c %s {})" ]] && mv $tmp {} + '' + ]} + '' + lib.optionalString minify.svg.enable '' + find ./public -type f -iname "*.svg" -a -not -path "*/emojis/*" \ + | parallel ${lib.escapeShellArgs [ + "--will-cite" + "-j $NIX_BUILD_CORES" + '' + tmp="$(mktemp)" + scour -i {} -o $tmp --disable-style-to-xml --enable-comment-stripping --enable-viewboxing --indent=tab + [[ "$(stat -c %s $tmp)" -lt "$(stat -c %s {})" ]] && mv $tmp {} + '' + ]} + ''; + postInstall = '' mkdir -p $out/bin echo "#!${lib.getExe dash}" > $out/bin/movim