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

View File

@ -6,22 +6,36 @@
example = runCommand "sample-drv" {} '' example = runCommand "sample-drv" {} ''
mkdir $out mkdir $out
echo 42 > $out/1.txt echo 42 > $out/1.txt
echo 43 > $out/1.md
touch $out/2.png touch $out/2.png
''; '';
drv = compressDrv example { drv = compressDrv example {
formats = ["txt"]; formats = ["txt"];
compressors = ["gz"]; compressors.gz = "${gzip}/bin/gzip --force --keep --fast {}";
compressor-gz = "${gzip}/bin/gzip --force --keep --fast {}"; };
wrapped = compressDrv drv {
formats = ["md"];
compressors.gz = "${gzip}/bin/gzip --force --keep --fast {}";
}; };
in in
runCommand "test-compressDrv" {} '' runCommand "test-compressDrv" {} ''
set -ex set -ex
find ${drv}
ls -l ${drv}
test -h ${drv}/1.txt test -h ${drv}/1.txt
test -f ${drv}/1.txt.gz test -f ${drv}/1.txt.gz
cmp ${drv}/1.txt <(${gzip}/bin/zcat ${drv}/1.txt.gz) cmp ${drv}/1.txt <(${gzip}/bin/zcat ${drv}/1.txt.gz)
test -h ${drv}/2.png test -h ${drv}/2.png
test ! -a ${drv}/2.png.gz 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 mkdir $out
'' ''

View File

@ -67,47 +67,24 @@ Inputs:
Extra extensions to compress in addition to `formats`. 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 See parameter `compressors` of compressDrv.
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 {}";
*/ */
{ {
lib,
zopfli, zopfli,
brotli, brotli,
compressDrv, compressDrv,
}: drv: { }: drv: {
formats ? ["css" "js" "svg" "ttf" "eot" "txt" "xml" "map" "html" "json" "webmanifest"], formats ? ["css" "js" "svg" "ttf" "eot" "txt" "xml" "map" "html" "json" "webmanifest"],
extraFormats ? [], extraFormats ? [],
compressors ? ["gz" "br"], compressors ? {
extraCompressors ? [], "gz" = "${zopfli}/bin/zopfli --keep {}";
"br" = "${brotli}/bin/brotli --keep --no-copy-stat {}";
},
... ...
} @ args: }:
compressDrv drv ({ compressDrv drv {
formats = formats ++ extraFormats; formats = formats ++ extraFormats;
compressors = compressors ++ extraCompressors; compressors = compressors;
compressor-gz = "${zopfli}/bin/zopfli --keep {}"; }
compressor-br = "${brotli}/bin/brotli --keep --no-copy-stat {}";
}
// lib.filterAttrs (k: _: (lib.hasPrefix "compressor-" k)) args)