2024-02-29 11:53:30 +00:00
|
|
|
{
|
|
|
|
brotli,
|
|
|
|
compressDrv,
|
2024-08-06 14:46:49 +00:00
|
|
|
lib,
|
|
|
|
zopfli,
|
2024-08-06 14:46:55 +00:00
|
|
|
zstd,
|
2024-02-29 11:53:30 +00:00
|
|
|
}:
|
|
|
|
/**
|
2024-08-05 12:04:09 +00:00
|
|
|
compressDrvWeb compresses a derivation for common web server use.
|
2024-02-29 11:53:30 +00:00
|
|
|
|
|
|
|
Useful when one wants to pre-compress certain static assets and pass them to
|
2024-08-05 12:04:09 +00:00
|
|
|
the web server.
|
2024-02-29 11:53:30 +00:00
|
|
|
|
2024-08-05 12:04:09 +00:00
|
|
|
# Inputs
|
2024-02-29 11:53:30 +00:00
|
|
|
|
2024-08-05 12:04:09 +00:00
|
|
|
`formats` ([String])
|
2024-02-29 11:53:30 +00:00
|
|
|
|
2024-08-05 12:04:09 +00:00
|
|
|
: List of file extensions to compress.
|
2024-02-29 11:53:30 +00:00
|
|
|
|
2024-08-05 12:04:09 +00:00
|
|
|
Defaults to common formats that compress well.
|
2024-02-29 11:53:30 +00:00
|
|
|
|
2024-08-05 12:04:09 +00:00
|
|
|
`extraFormats` ([ String ])
|
2024-02-29 11:53:30 +00:00
|
|
|
|
2024-08-05 12:04:09 +00:00
|
|
|
: Extra extensions to compress in addition to `formats`.
|
2024-02-29 11:53:30 +00:00
|
|
|
|
2024-08-05 12:04:09 +00:00
|
|
|
`compressors` ( { ${fileExtension} :: String })
|
|
|
|
|
|
|
|
: Map a desired extension (e.g. `gz`) to a compress program.
|
|
|
|
|
|
|
|
# Type
|
|
|
|
|
|
|
|
```
|
|
|
|
compressDrvWeb :: Derivation -> { formats :: [ String ]; extraFormats :: [ String ]; compressors :: { ${fileExtension} :: String; } } -> Derivation
|
|
|
|
```
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `pkgs.compressDrvWeb` full usage example with `pkgs.gamja` and a webserver
|
|
|
|
```nix
|
|
|
|
|
|
|
|
For example, building `pkgs.gamja` produces the following output:
|
|
|
|
|
|
|
|
/nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/
|
|
|
|
├── index.2fd01148.js
|
|
|
|
├── index.2fd01148.js.map
|
|
|
|
├── index.37aa9a8a.css
|
|
|
|
├── index.37aa9a8a.css.map
|
|
|
|
├── index.html
|
|
|
|
└── manifest.webmanifest
|
|
|
|
|
|
|
|
With `pkgs.compressDrvWeb`, one can compress these files:
|
|
|
|
|
|
|
|
```nix
|
|
|
|
pkgs.compressDrvWeb pkgs.gamja {}
|
|
|
|
=>
|
|
|
|
«derivation /nix/store/...-gamja-compressed.drv»
|
|
|
|
```
|
|
|
|
|
|
|
|
```bash
|
|
|
|
/nix/store/f5ryid7zrw2hid7h9kil5g5j29q5r2f7-gamja-1.0.0-beta.9-compressed
|
|
|
|
├── index.2fd01148.js -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/index.2fd01148.js
|
|
|
|
├── index.2fd01148.js.br
|
|
|
|
├── index.2fd01148.js.gz
|
|
|
|
├── index.2fd01148.js.map -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/index.2fd01148.js.map
|
|
|
|
├── index.2fd01148.js.map.br
|
|
|
|
├── index.2fd01148.js.map.gz
|
|
|
|
├── index.37aa9a8a.css -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/index.37aa9a8a.css
|
|
|
|
├── index.37aa9a8a.css.br
|
|
|
|
├── index.37aa9a8a.css.gz
|
|
|
|
├── index.37aa9a8a.css.map -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/index.37aa9a8a.css.map
|
|
|
|
├── index.37aa9a8a.css.map.br
|
|
|
|
├── index.37aa9a8a.css.map.gz
|
|
|
|
├── index.html -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/index.html
|
|
|
|
├── index.html.br
|
|
|
|
├── index.html.gz
|
|
|
|
├── manifest.webmanifest -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/manifest.webmanifest
|
|
|
|
├── manifest.webmanifest.br
|
|
|
|
└── manifest.webmanifest.gz
|
|
|
|
```
|
|
|
|
|
|
|
|
When the `-compressed` derivation is passed to a properly configured web server,
|
|
|
|
it enables direct serving of the pre-compressed files.
|
|
|
|
|
|
|
|
```shell-session
|
|
|
|
$ curl -I -H 'Accept-Encoding: br' https://irc.example.org/
|
|
|
|
<...>
|
|
|
|
content-encoding: br
|
|
|
|
<...>
|
|
|
|
```
|
2024-02-29 11:53:30 +00:00
|
|
|
|
2024-08-05 12:04:09 +00:00
|
|
|
For example, a caddy configuration snippet for gamja to serve
|
|
|
|
the static assets (JS, CSS files) pre-compressed:
|
2024-02-29 11:53:30 +00:00
|
|
|
|
2024-08-05 12:04:09 +00:00
|
|
|
```nix
|
|
|
|
{
|
|
|
|
virtualHosts."irc.example.org".extraConfig = ''
|
|
|
|
root * ${pkgs.compressDrvWeb pkgs.gamja {}}
|
|
|
|
file_server browse {
|
|
|
|
precompressed br gzip
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
```
|
2024-02-29 11:53:30 +00:00
|
|
|
|
2024-08-05 12:04:09 +00:00
|
|
|
This feature is also available in nginx via `ngx_brotli` and
|
|
|
|
`ngx_http_gzip_static_module`.
|
|
|
|
:::
|
2024-02-29 11:53:30 +00:00
|
|
|
*/
|
|
|
|
drv:
|
|
|
|
{
|
|
|
|
formats ? [
|
|
|
|
"css"
|
2024-08-06 14:29:27 +00:00
|
|
|
"eot"
|
2024-08-06 14:31:18 +00:00
|
|
|
"htm"
|
2024-08-06 14:29:27 +00:00
|
|
|
"html"
|
2024-02-29 11:53:30 +00:00
|
|
|
"js"
|
2024-08-06 14:29:27 +00:00
|
|
|
"json"
|
|
|
|
"map"
|
2024-08-06 14:31:18 +00:00
|
|
|
"otf"
|
2024-02-29 11:53:30 +00:00
|
|
|
"svg"
|
|
|
|
"ttf"
|
|
|
|
"txt"
|
|
|
|
"webmanifest"
|
2024-08-06 14:29:27 +00:00
|
|
|
"xml"
|
2024-02-29 11:53:30 +00:00
|
|
|
],
|
|
|
|
extraFormats ? [ ],
|
|
|
|
compressors ? {
|
2024-08-06 14:46:49 +00:00
|
|
|
br = "${lib.getExe brotli} --keep --no-copy-stat {}";
|
|
|
|
gz = "${lib.getExe zopfli} --keep {}";
|
2024-08-06 14:46:55 +00:00
|
|
|
# --force is required to not fail on symlinks
|
|
|
|
# for details on the compression level see
|
|
|
|
# https://github.com/NixOS/nixpkgs/pull/332752#issuecomment-2275110390
|
|
|
|
zstd = "${lib.getExe zstd} --force --keep --quiet -19 {}";
|
2024-02-29 11:53:30 +00:00
|
|
|
},
|
|
|
|
}:
|
|
|
|
compressDrv drv {
|
|
|
|
formats = formats ++ extraFormats;
|
|
|
|
compressors = compressors;
|
|
|
|
}
|