config/pkgs/compress-drv/default.nix

64 lines
1.5 KiB
Nix
Raw Normal View History

2024-07-06 22:45:14 +03:00
/*
2024-07-29 15:39:54 +03:00
compressDrv compresses files in a given derivation.
2024-07-06 22:45:14 +03:00
2024-07-29 15:39:54 +03:00
Inputs:
2024-07-06 22:45:14 +03:00
2024-07-29 15:39:54 +03:00
- formats :: [String]
2024-07-06 22:45:14 +03:00
2024-07-29 15:39:54 +03:00
List of file extensions to compress.
2024-07-06 22:45:14 +03:00
2024-07-29 15:39:54 +03:00
Example: ["txt" "svg" "xml"]
2024-07-06 22:45:14 +03:00
2024-07-29 15:39:54 +03:00
- compressors :: {String -> String}
2024-07-06 22:45:14 +03:00
2024-07-29 15:39:54 +03:00
Map a desired extension (e.g. `gz`) to a compress program.
2024-07-06 22:45:14 +03:00
2024-07-29 15:39:54 +03: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-07-06 22:45:14 +03:00
2024-07-29 15:39:54 +03:00
Compressor must:
- read symlinks (thus --force is needed to gzip, zstd, xz).
- keep the original file in place (--keep).
2024-07-06 22:45:14 +03:00
2024-07-29 15:39:54 +03:00
Example:
2024-07-06 22:45:14 +03:00
2024-07-29 15:39:54 +03:00
{
xz = "${xz}/bin/xz --force --keep {}";
}
2024-07-06 22:45:14 +03:00
2024-07-29 15:39:54 +03:00
See compressDrvWeb, which is a wrapper on top of compressDrv, for broader
use examples.
2024-07-06 22:45:14 +03:00
*/
{
lib,
2024-07-29 15:19:49 +03:00
xorg,
2024-07-06 22:45:14 +03:00
runCommand,
2024-07-29 15:39:54 +03:00
}:
drv:
2024-08-07 08:57:36 +03:00
{ formats, compressors }:
2024-07-29 15:39:54 +03: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-07-09 07:51:29 +03:00
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
2024-07-29 15:39:54 +03:00
runCommand "${drv.name}-compressed" { } ''
mkdir $out
(cd $out; ${xorg.lndir}/bin/lndir ${drv})
2024-07-06 22:54:08 +03:00
2024-07-29 15:39:54 +03:00
${lib.concatStringsSep "\n\n" (lib.mapAttrsToList mkCmd compressors)}
''