2024-08-18 21:40:18 +02:00
|
|
|
{ lib, runCommand }:
|
2024-02-29 12:53:30 +01:00
|
|
|
/**
|
2024-08-05 14:04:09 +02:00
|
|
|
Compresses files of a given derivation, and returns a new derivation with
|
|
|
|
compressed files
|
2024-02-29 12:53:30 +01:00
|
|
|
|
2024-08-05 14:04:09 +02:00
|
|
|
# Inputs
|
2024-02-29 12:53:30 +01:00
|
|
|
|
|
|
|
`formats` ([String])
|
|
|
|
|
|
|
|
: List of file extensions to compress. Example: `["txt" "svg" "xml"]`.
|
|
|
|
|
2024-08-06 17:58:13 +02:00
|
|
|
`extraFindOperands` (String)
|
|
|
|
|
|
|
|
: Extra command line parameters to pass to the find command.
|
|
|
|
This can be used to exclude certain files.
|
|
|
|
For example: `-not -iregex ".*(\/apps\/.*\/l10n\/).*"`
|
|
|
|
|
2024-08-05 14:04:09 +02:00
|
|
|
`compressors` ( { ${fileExtension} :: String })
|
2024-02-29 12:53:30 +01:00
|
|
|
|
|
|
|
: Map a desired extension (e.g. `gz`) to a compress program.
|
|
|
|
|
2024-08-05 14:04:09 +02:00
|
|
|
The compressor program that will be executed to get the `COMPRESSOR` extension.
|
|
|
|
The program should have a single " {}", which will be the replaced with the
|
|
|
|
target filename.
|
2024-02-29 12:53:30 +01:00
|
|
|
|
2024-08-05 14:04:09 +02:00
|
|
|
Compressor must:
|
2024-02-29 12:53:30 +01:00
|
|
|
|
2024-08-05 14:04:09 +02:00
|
|
|
- read symlinks (thus --force is needed to gzip, zstd, xz).
|
|
|
|
- keep the original file in place (--keep).
|
2024-02-29 12:53:30 +01:00
|
|
|
|
2024-08-05 14:04:09 +02:00
|
|
|
# Type
|
|
|
|
|
|
|
|
```
|
|
|
|
compressDrv :: Derivation -> { formats :: [ String ]; compressors :: { ${fileExtension} :: String; } } -> Derivation
|
|
|
|
```
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `pkgs.compressDrv` usage example
|
2024-02-29 12:53:30 +01:00
|
|
|
```
|
2024-08-05 14:04:09 +02:00
|
|
|
compressDrv pkgs.spdx-license-list-data.json {
|
|
|
|
formats = ["json"];
|
|
|
|
compressors = {
|
2024-08-21 20:52:00 +02:00
|
|
|
gz = "${zopfli}/bin/zopfli --keep {}";
|
2024-08-05 14:04:09 +02:00
|
|
|
};
|
2024-02-29 12:53:30 +01:00
|
|
|
}
|
2024-08-05 14:04:09 +02:00
|
|
|
=>
|
|
|
|
«derivation /nix/store/...-spdx-license-list-data-3.24.0-compressed.drv»
|
2024-02-29 12:53:30 +01:00
|
|
|
```
|
|
|
|
|
2024-08-05 14:04:09 +02:00
|
|
|
See also pkgs.compressDrvWeb, which is a wrapper on top of compressDrv, for broader usage
|
2024-02-29 12:53:30 +01:00
|
|
|
examples.
|
2024-08-05 14:04:09 +02:00
|
|
|
:::
|
2024-02-29 12:53:30 +01:00
|
|
|
*/
|
|
|
|
drv:
|
2024-08-06 17:58:13 +02:00
|
|
|
{
|
|
|
|
formats,
|
|
|
|
compressors,
|
|
|
|
extraFindOperands ? "",
|
|
|
|
}:
|
2024-02-29 12:53:30 +01:00
|
|
|
let
|
|
|
|
validProg =
|
|
|
|
ext: prog:
|
|
|
|
let
|
|
|
|
matches = (builtins.length (builtins.split "\\{}" prog) - 1) / 2;
|
|
|
|
in
|
|
|
|
lib.assertMsg (
|
|
|
|
matches == 1
|
|
|
|
) "compressor ${ext} needs to have exactly one '{}', found ${builtins.toString matches}";
|
|
|
|
mkCmd =
|
|
|
|
ext: prog:
|
|
|
|
assert validProg ext prog;
|
|
|
|
''
|
2024-08-06 17:58:13 +02:00
|
|
|
find -L $out -type f -regextype posix-extended -iregex '.*\.(${formatsPipe})' ${extraFindOperands} -print0 \
|
2024-02-29 12:53:30 +01:00
|
|
|
| xargs -0 -P$NIX_BUILD_CORES -I{} ${prog}
|
|
|
|
'';
|
2024-08-06 17:58:13 +02:00
|
|
|
formatsPipe = lib.concatStringsSep "|" formats;
|
2024-02-29 12:53:30 +01:00
|
|
|
in
|
2024-08-06 17:59:18 +02:00
|
|
|
runCommand "${drv.name}-compressed"
|
|
|
|
(
|
|
|
|
(lib.optionalAttrs (drv ? pname) { inherit (drv) pname; })
|
|
|
|
// (lib.optionalAttrs (drv ? version) { inherit (drv) version; })
|
|
|
|
)
|
|
|
|
''
|
|
|
|
mkdir $out
|
|
|
|
|
2024-08-19 13:19:27 +02:00
|
|
|
# cannot use lndir here, because it stop recursing at symlinks that point to directories
|
2024-08-06 17:59:18 +02:00
|
|
|
(cd ${drv}; find -L -type d -exec mkdir -p $out/{} ';')
|
|
|
|
(cd ${drv}; find -L -type f -exec ln -s ${drv}/{} $out/{} ';')
|
|
|
|
|
|
|
|
${lib.concatStringsSep "\n\n" (lib.mapAttrsToList mkCmd compressors)}
|
|
|
|
''
|