compressDrv: update with "upstream"

This commit is contained in:
Motiejus Jakštys 2024-07-29 15:19:49 +03:00
parent bf589d3463
commit 9557e1d9ca
3 changed files with 39 additions and 55 deletions

View File

@ -9,14 +9,7 @@ Inputs:
Example: ["txt" "svg" "xml"]
- compressors :: [String]
A list of compressor names to use. Each element will need to have
an associated compressor in the same arguments (see below).
Example: ["gz" "zstd"]
- compressor-<EXTENSION> :: String
- compressors :: {String -> String}
Map a desired extension (e.g. `gz`) to a compress program.
@ -28,28 +21,30 @@ Inputs:
- read symlinks (thus --force is needed to gzip, zstd, xz).
- keep the original file in place (--keep).
Example compressor:
Example:
compressor-xz = "${xz}/bin/xz --force --keep {}";
{
xz = "${xz}/bin/xz --force --keep {}";
}
See compressDrvWeb, which is a wrapper on top of compressDrv, for broader
use examples.
*/
{
lib,
xorg,
runCommand,
}: drv: {
formats,
compressors,
...
} @ args: let
}: 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}";
compressorMap = lib.filterAttrs (k: _: (lib.hasPrefix "compressor-" k)) args;
"compressor ${ext} needs to have exactly one '{}', found ${builtins.toString matches}";
mkCmd = ext: prog:
assert validProg ext prog; ''
find -L $out -type f -regextype posix-extended -iregex '.*\.(${formatsPipe})' -print0 \
@ -59,13 +54,11 @@ Inputs:
in
runCommand "${drv.name}-compressed" {} ''
mkdir $out
(cd ${drv}; find -L -type d -exec mkdir -p $out/{} ';')
(cd ${drv}; find -L -type f -exec ln -s ${drv}/{} $out/{} ';')
(cd $out; ${xorg.lndir}/bin/lndir ${drv})
${
lib.concatMapStringsSep
lib.concatStringsSep
"\n\n"
(ext: mkCmd ext (builtins.getAttr "compressor-${ext}" compressorMap))
compressors
(lib.mapAttrsToList mkCmd compressors)
}
''

View File

@ -6,22 +6,36 @@
example = runCommand "sample-drv" {} ''
mkdir $out
echo 42 > $out/1.txt
echo 43 > $out/1.md
touch $out/2.png
'';
drv = compressDrv example {
formats = ["txt"];
compressors = ["gz"];
compressor-gz = "${gzip}/bin/gzip --force --keep --fast {}";
compressors.gz = "${gzip}/bin/gzip --force --keep --fast {}";
};
wrapped = compressDrv drv {
formats = ["md"];
compressors.gz = "${gzip}/bin/gzip --force --keep --fast {}";
};
in
runCommand "test-compressDrv" {} ''
set -ex
find ${drv}
ls -l ${drv}
test -h ${drv}/1.txt
test -f ${drv}/1.txt.gz
cmp ${drv}/1.txt <(${gzip}/bin/zcat ${drv}/1.txt.gz)
test -h ${drv}/2.png
test ! -a ${drv}/2.png.gz
# compressDrv always points to the final file, no matter how many times
# it's been wrapped
cmp <(readlink -e ${drv}/1.txt) <(readlink -e ${wrapped}/1.txt)
test -f ${wrapped}/1.txt.gz
test -f ${wrapped}/1.md.gz
test ! -f ${drv}/1.md.gz
mkdir $out
''

View File

@ -67,47 +67,24 @@ Inputs:
Extra extensions to compress in addition to `formats`.
- compressors :: [String]
- compressors :: {String -> String}
A list of compressor names to use. Each element will need to have
an associated compressor in the same arguments (see below).
Default: ["gz" "br"]
- extraCompressors :: [String]
Extra compressors in addition to `compressors`.
- compressor-<COMPRESSOR> :: String
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 is passed to xargs like this:
xargs -I{} -n1 ${prog}
Default:
compressor-gz = "${zopfli}/bin/zopfli --keep {}";
compressor-br = "${brotli}/bin/brotli --keep --no-copy-stat {}";
See parameter `compressors` of compressDrv.
*/
{
lib,
zopfli,
brotli,
compressDrv,
}: drv: {
formats ? ["css" "js" "svg" "ttf" "eot" "txt" "xml" "map" "html" "json" "webmanifest"],
extraFormats ? [],
compressors ? ["gz" "br"],
extraCompressors ? [],
compressors ? {
"gz" = "${zopfli}/bin/zopfli --keep {}";
"br" = "${brotli}/bin/brotli --keep --no-copy-stat {}";
},
...
} @ args:
compressDrv drv ({
formats = formats ++ extraFormats;
compressors = compressors ++ extraCompressors;
compressor-gz = "${zopfli}/bin/zopfli --keep {}";
compressor-br = "${brotli}/bin/brotli --keep --no-copy-stat {}";
}
// lib.filterAttrs (k: _: (lib.hasPrefix "compressor-" k)) args)
}:
compressDrv drv {
formats = formats ++ extraFormats;
compressors = compressors;
}