diff --git a/pkgs/compress-drv/default.nix b/pkgs/compress-drv/default.nix index 6a3baf3..2a921e8 100644 --- a/pkgs/compress-drv/default.nix +++ b/pkgs/compress-drv/default.nix @@ -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- :: 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) } '' diff --git a/pkgs/compress-drv/test.nix b/pkgs/compress-drv/test.nix index b30e1b0..823a422 100644 --- a/pkgs/compress-drv/test.nix +++ b/pkgs/compress-drv/test.nix @@ -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 '' diff --git a/pkgs/compress-drv/web.nix b/pkgs/compress-drv/web.nix index e6aa26f..1a3b446 100644 --- a/pkgs/compress-drv/web.nix +++ b/pkgs/compress-drv/web.nix @@ -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- :: 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; +}