Skip to content

Commit

Permalink
nixos/movim: precompress static files
Browse files Browse the repository at this point in the history
  • Loading branch information
toastal committed Apr 10, 2024
1 parent deba739 commit 44c385a
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions nixos/modules/services/web-apps/movim.nix
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,45 @@ 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" [
prevAttrs.postInstall
stateDirectories
exposeComposer
podConfigInputDisableReplace
precompressStaticFilesJobs
];
});

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 44c385a

Please sign in to comment.