nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at devShellTools-shell 145 lines 4.0 kB view raw
1{ 2 brotli, 3 compressDrv, 4 lib, 5 zopfli, 6 zstd, 7}: 8/** 9 compressDrvWeb compresses a derivation for common web server use. 10 11 Useful when one wants to pre-compress certain static assets and pass them to 12 the web server. 13 14 # Inputs 15 16 `formats` ([String]) 17 18 : List of file extensions to compress. 19 20 Defaults to common formats that compress well. 21 22 `extraFindOperands` (String) 23 24 : See compressDrv for details. 25 26 `extraFormats` ([ String ]) 27 28 : Extra extensions to compress in addition to `formats`. 29 30 `compressors` ( { ${fileExtension} :: String }) 31 32 : Map a desired extension (e.g. `gz`) to a compress program. 33 34 # Type 35 36 ``` 37 compressDrvWeb :: Derivation -> { formats :: [ String ]; extraFormats :: [ String ]; compressors :: { ${fileExtension} :: String; } } -> Derivation 38 ``` 39 40 # Examples 41 :::{.example} 42 ## `pkgs.compressDrvWeb` full usage example with `pkgs.gamja` and a webserver 43 ```nix 44 45 For example, building `pkgs.gamja` produces the following output: 46 47 /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/ 48 index.2fd01148.js 49 index.2fd01148.js.map 50 index.37aa9a8a.css 51 index.37aa9a8a.css.map 52 index.html 53 manifest.webmanifest 54 55 With `pkgs.compressDrvWeb`, one can compress these files: 56 57 ```nix 58 pkgs.compressDrvWeb pkgs.gamja {} 59 => 60 «derivation /nix/store/...-gamja-compressed.drv» 61 ``` 62 63 ```bash 64 /nix/store/f5ryid7zrw2hid7h9kil5g5j29q5r2f7-gamja-1.0.0-beta.9-compressed 65 index.2fd01148.js -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/index.2fd01148.js 66 index.2fd01148.js.br 67 index.2fd01148.js.gz 68 index.2fd01148.js.map -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/index.2fd01148.js.map 69 index.2fd01148.js.map.br 70 index.2fd01148.js.map.gz 71 index.37aa9a8a.css -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/index.37aa9a8a.css 72 index.37aa9a8a.css.br 73 index.37aa9a8a.css.gz 74 index.37aa9a8a.css.map -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/index.37aa9a8a.css.map 75 index.37aa9a8a.css.map.br 76 index.37aa9a8a.css.map.gz 77 index.html -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/index.html 78 index.html.br 79 index.html.gz 80 manifest.webmanifest -> /nix/store/2wn1qbk8gp4y2m8xvafxv1b2dcdqj8fz-gamja-1.0.0-beta.9/manifest.webmanifest 81 manifest.webmanifest.br 82 manifest.webmanifest.gz 83 ``` 84 85 When the `-compressed` derivation is passed to a properly configured web server, 86 it enables direct serving of the pre-compressed files. 87 88 ```shell-session 89 $ curl -I -H 'Accept-Encoding: br' https://irc.example.org/ 90 <...> 91 content-encoding: br 92 <...> 93 ``` 94 95 For example, a caddy configuration snippet for gamja to serve 96 the static assets (JS, CSS files) pre-compressed: 97 98 ```nix 99 { 100 virtualHosts."irc.example.org".extraConfig = '' 101 root * ${pkgs.compressDrvWeb pkgs.gamja {}} 102 file_server browse { 103 precompressed br gzip 104 } 105 ''; 106 } 107 ``` 108 109 This feature is also available in nginx via `ngx_brotli` and 110 `ngx_http_gzip_static_module`. 111 ::: 112*/ 113drv: 114{ 115 formats ? [ 116 "css" 117 "eot" 118 "htm" 119 "html" 120 "js" 121 "json" 122 "map" 123 "otf" 124 "svg" 125 "ttf" 126 "txt" 127 "webmanifest" 128 "xml" 129 ], 130 extraFormats ? [ ], 131 compressors ? { 132 br = "${lib.getExe brotli} --keep --no-copy-stat {}"; 133 gz = "${lib.getExe zopfli} --keep {}"; 134 # --force is required to not fail on symlinks 135 # for details on the compression level see 136 # https://github.com/NixOS/nixpkgs/pull/332752#issuecomment-2275110390 137 zstd = "${lib.getExe zstd} --force --keep --quiet -19 {}"; 138 }, 139 extraFindOperands ? "", 140}: 141compressDrv drv { 142 formats = formats ++ extraFormats; 143 compressors = compressors; 144 inherit extraFindOperands; 145}