nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, runCommand }:
2/**
3 Compresses files of a given derivation, and returns a new derivation with
4 compressed files
5
6 # Inputs
7
8 `formats` ([String])
9
10 : List of file extensions to compress. Example: `["txt" "svg" "xml"]`.
11
12 `extraFindOperands` (String)
13
14 : Extra command line parameters to pass to the find command.
15 This can be used to exclude certain files.
16 For example: `-not -iregex ".*(\/apps\/.*\/l10n\/).*"`
17
18 `compressors` ( { ${fileExtension} :: String })
19
20 : Map a desired extension (e.g. `gz`) to a compress program.
21
22 The compressor program that will be executed to get the `COMPRESSOR` extension.
23 The program should have a single " {}", which will be the replaced with the
24 target filename.
25
26 Compressor must:
27
28 - read symlinks (thus --force is needed to gzip, zstd, xz).
29 - keep the original file in place (--keep).
30
31 # Type
32
33 ```
34 compressDrv :: Derivation -> { formats :: [ String ]; compressors :: { ${fileExtension} :: String; } } -> Derivation
35 ```
36
37 # Examples
38 :::{.example}
39 ## `pkgs.compressDrv` usage example
40 ```
41 compressDrv pkgs.spdx-license-list-data.json {
42 formats = ["json"];
43 compressors = {
44 gz = "${zopfli}/bin/zopfli --keep {}";
45 };
46 }
47 =>
48 «derivation /nix/store/...-spdx-license-list-data-3.24.0-compressed.drv»
49 ```
50
51 See also pkgs.compressDrvWeb, which is a wrapper on top of compressDrv, for broader usage
52 examples.
53 :::
54*/
55drv:
56{
57 formats,
58 compressors,
59 extraFindOperands ? "",
60}:
61let
62 validProg =
63 ext: prog:
64 let
65 matches = (builtins.length (builtins.split "\\{}" prog) - 1) / 2;
66 in
67 lib.assertMsg (
68 matches == 1
69 ) "compressor ${ext} needs to have exactly one '{}', found ${toString matches}";
70 mkCmd =
71 ext: prog:
72 assert validProg ext prog;
73 ''
74 find -L $out -type f -regextype posix-extended -iregex '.*\.(${formatsPipe})' ${extraFindOperands} -print0 \
75 | xargs -0 -P$NIX_BUILD_CORES -I{} ${prog}
76 '';
77 formatsPipe = lib.concatStringsSep "|" formats;
78in
79runCommand "${drv.name}-compressed"
80 (
81 (lib.optionalAttrs (drv ? pname) { inherit (drv) pname; })
82 // (lib.optionalAttrs (drv ? version) { inherit (drv) version; })
83 // (lib.optionalAttrs (drv ? passthru) { inherit (drv) passthru; })
84 // (lib.optionalAttrs (drv ? meta) { inherit (drv) meta; })
85 )
86 ''
87 mkdir $out
88
89 # cannot use lndir here, because it stop recursing at symlinks that point to directories
90 (cd ${drv}; find -L -type d -exec mkdir -p $out/{} ';')
91 (cd ${drv}; find -L -type f -exec ln -s ${drv}/{} $out/{} ';')
92
93 ${lib.concatStringsSep "\n\n" (lib.mapAttrsToList mkCmd compressors)}
94 ''