nixos/movim: precompress static files

This commit is contained in:
toastal 2024-03-18 15:50:57 +07:00
parent 64b110589c
commit 0ba23300de
1 changed files with 62 additions and 0 deletions

View File

@ -91,6 +91,37 @@ let
'')
[ ]
cfg.podConfig));
precompressStaticFilesJobs =
let
inherit (cfg.precompressStaticFiles) brotli gzip;
findTextFileNames = lib.concatStringsSep " -o "
(builtins.map (n: ''-iname "*.${n}"'')
[ "css" "ini" "js" "json" "manifest" "mjs" "svg" "webmanifest" ]);
in
lib.concatStringsSep "\n" [
(lib.optionalString brotli.enable ''
echo -n "Precompressing static files with Brotli "
find ${appDir}/public -type f ${findTextFileNames} \
| ${lib.getExe pkgs.parallel} ${lib.escapeShellArgs [
"--will-cite"
"-j $NIX_BUILD_CORES"
"${lib.getExe brotli.package} --keep --quality=${builtins.toString brotli.compressionLevel} --output={}.br {}"
]}
echo " done."
'')
(lib.optionalString gzip.enable ''
echo -n "Precompressing static files with Gzip "
find ${appDir}/public -type f ${findTextFileNames} \
| ${lib.getExe pkgs.parallel} ${lib.escapeShellArgs [
"--will-cite"
"-j $NIX_BUILD_CORES"
"${lib.getExe gzip.package} -c -${builtins.toString gzip.compressionLevel} {} > {}.gz"
]}
echo " done."
'')
];
in
{
postInstall = lib.concatStringsSep "\n\n" [
@ -98,6 +129,7 @@ let
stateDirectories
exposeComposer
podConfigInputDisableReplace
precompressStaticFilesJobs
];
});
@ -224,6 +256,36 @@ in
description = "Do minification on public static files";
};
precompressStaticFiles = mkOption {
type = with types; submodule {
options = {
brotli = {
enable = mkEnableOption "Brotli precompression";
package = mkPackageOption pkgs "brotli" { };
compressionLevel = mkOption {
type = types.ints.between 0 11;
default = 11;
description = "Brotli compression level";
};
};
gzip = {
enable = mkEnableOption "Gzip precompression";
package = mkPackageOption pkgs "gzip" { };
compressionLevel = mkOption {
type = types.ints.between 1 9;
default = 9;
description = "Gzip compression level";
};
};
};
};
default = {
brotli.enable = true;
gzip.enable = false;
};
description = "Aggressively precompress static files";
};
podConfig = mkOption {
type = types.submodule {
options = {