Unverified Commit 4cc5dee0 authored by Sandro Jäckel's avatar Sandro Jäckel
Browse files

compress-drv: allow passing extra arguments to find

This is useful for eg. nextcloud to prevent compressing thousands of
later unused files which are actually not used by the web server.
parent 0ca4bfa7
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -9,6 +9,12 @@

  : List of file extensions to compress. Example: `["txt" "svg" "xml"]`.

  `extraFindOperands` (String)

  : Extra command line parameters to pass to the find command.
    This can be used to exclude certain files.
    For example: `-not -iregex ".*(\/apps\/.*\/l10n\/).*"`

  `compressors` ( { ${fileExtension} :: String })

  : Map a desired extension (e.g. `gz`) to a compress program.
@@ -47,7 +53,11 @@
  :::
*/
drv:
{ formats, compressors }:
{
  formats,
  compressors,
  extraFindOperands ? "",
}:
let
  validProg =
    ext: prog:
@@ -61,10 +71,10 @@ let
    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})' ${extraFindOperands} -print0 \
        | xargs -0 -P$NIX_BUILD_CORES -I{} ${prog}
    '';
  formatsPipe = builtins.concatStringsSep "|" formats;
  formatsPipe = lib.concatStringsSep "|" formats;
in
runCommand "${drv.name}-compressed" { } ''
  mkdir $out
+6 −0
Original line number Diff line number Diff line
@@ -19,6 +19,10 @@

    Defaults to common formats that compress well.

  `extraFindOperands` (String)

  : See compressDrv for details.

  `extraFormats` ([ String ])

  : Extra extensions to compress in addition to `formats`.
@@ -132,8 +136,10 @@ drv:
    # https://github.com/NixOS/nixpkgs/pull/332752#issuecomment-2275110390
    zstd = "${lib.getExe zstd} --force --keep --quiet -19 {}";
  },
  extraFindOperands ? "",
}:
compressDrv drv {
  formats = formats ++ extraFormats;
  compressors = compressors;
  inherit extraFindOperands;
}