config/pkgs/compress-drv/default.nix

65 lines
1.5 KiB
Nix
Raw Normal View History

2024-07-06 22:45:14 +03:00
/*
compressDrv compresses files in a given derivation.
Inputs:
- formats :: [String]
List of file extensions to compress.
Example: ["txt" "svg" "xml"]
2024-07-29 15:19:49 +03:00
- compressors :: {String -> String}
2024-07-06 22:45:14 +03:00
Map a desired extension (e.g. `gz`) to a compress program.
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.
Compressor must:
- read symlinks (thus --force is needed to gzip, zstd, xz).
- keep the original file in place (--keep).
2024-07-29 15:19:49 +03:00
Example:
2024-07-06 22:45:14 +03:00
2024-07-29 15:19:49 +03:00
{
xz = "${xz}/bin/xz --force --keep {}";
}
2024-07-06 22:45:14 +03:00
See compressDrvWeb, which is a wrapper on top of compressDrv, for broader
use examples.
*/
{
lib,
2024-07-29 15:19:49 +03:00
xorg,
2024-07-06 22:45:14 +03:00
runCommand,
}: drv: {
formats,
compressors,
...
2024-07-29 15:19:49 +03:00
}: let
2024-07-06 22:45:14 +03:00
validProg = ext: prog: let
matches = (builtins.length (builtins.split "\\{}" prog) - 1) / 2;
in
lib.assertMsg
(matches == 1)
2024-07-29 15:19:49 +03:00
"compressor ${ext} needs to have exactly one '{}', found ${builtins.toString matches}";
2024-07-09 07:51:29 +03:00
mkCmd = ext: prog:
assert validProg ext prog; ''
find -L $out -type f -regextype posix-extended -iregex '.*\.(${formatsPipe})' -print0 \
| xargs -0 -P$NIX_BUILD_CORES -I{} ${prog}
'';
2024-07-06 22:45:14 +03:00
formatsPipe = builtins.concatStringsSep "|" formats;
in
runCommand "${drv.name}-compressed" {} ''
mkdir $out
2024-07-29 15:19:49 +03:00
(cd $out; ${xorg.lndir}/bin/lndir ${drv})
2024-07-06 22:54:08 +03:00
2024-07-09 07:51:29 +03:00
${
2024-07-29 15:19:49 +03:00
lib.concatStringsSep
2024-07-09 07:51:29 +03:00
"\n\n"
2024-07-29 15:19:49 +03:00
(lib.mapAttrsToList mkCmd compressors)
2024-07-06 22:45:14 +03:00
}
''