From b6ef9ffdfd54e91cce180c306852489f5ebc3098 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Wed, 10 Apr 2024 10:04:43 +0200 Subject: nixos/udev: compress firmware with zstd if possible Closes #267442 $ nix path-info -Sh /nix/store/qj1dm7wfw5m3mxf1gn3fdm0az9y1h5ny-linux-firmware-20240312-xz /nix/store/qj1dm7wfw5m3mxf1gn3fdm0az9y1h5ny-linux-firmware-20240312-xz 440.3M $ nix path-info -Sh /nix/store/c3szcjxb3g990dbiz7llwmkaf0bi98j2-linux-firmware-20240312-zstd /nix/store/c3szcjxb3g990dbiz7llwmkaf0bi98j2-linux-firmware-20240312-zstd 460.6M This is an increase of 4.4%, but OTOH zstd has a significantly higher decompression speed[1]. [1] https://gregoryszorc.com/blog/2017/03/07/better-compression-with-zstandard/ --- pkgs/build-support/kernel/compress-firmware-xz.nix | 29 --------------- pkgs/build-support/kernel/compress-firmware.nix | 43 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 29 deletions(-) delete mode 100644 pkgs/build-support/kernel/compress-firmware-xz.nix create mode 100644 pkgs/build-support/kernel/compress-firmware.nix (limited to 'pkgs/build-support/kernel') diff --git a/pkgs/build-support/kernel/compress-firmware-xz.nix b/pkgs/build-support/kernel/compress-firmware-xz.nix deleted file mode 100644 index cb9ce7a713389..0000000000000 --- a/pkgs/build-support/kernel/compress-firmware-xz.nix +++ /dev/null @@ -1,29 +0,0 @@ -{ runCommand, lib }: - -firmware: - -let - args = { - allowedRequisites = []; - } // lib.optionalAttrs (firmware ? meta) { inherit (firmware) meta; }; -in - -runCommand "${firmware.name}-xz" args '' - mkdir -p $out/lib - (cd ${firmware} && find lib/firmware -type d -print0) | - (cd $out && xargs -0 mkdir -v --) - (cd ${firmware} && find lib/firmware -type f -print0) | - (cd $out && xargs -0rtP "$NIX_BUILD_CORES" -n1 \ - sh -c 'xz -9c -T1 -C crc32 --lzma2=dict=2MiB "${firmware}/$1" > "$1.xz"' --) - (cd ${firmware} && find lib/firmware -type l) | while read link; do - target="$(readlink "${firmware}/$link")" - if [ -f "${firmware}/$link" ]; then - ln -vs -- "''${target/^${firmware}/$out}.xz" "$out/$link.xz" - else - ln -vs -- "''${target/^${firmware}/$out}" "$out/$link" - fi - done - - echo "Checking for broken symlinks:" - find -L $out -type l -print -execdir false -- '{}' '+' -'' diff --git a/pkgs/build-support/kernel/compress-firmware.nix b/pkgs/build-support/kernel/compress-firmware.nix new file mode 100644 index 0000000000000..0949036d5127f --- /dev/null +++ b/pkgs/build-support/kernel/compress-firmware.nix @@ -0,0 +1,43 @@ +{ runCommand, lib, type ? "zstd", zstd }: + +firmware: + +let + compressor = { + xz = { + ext = "xz"; + nativeBuildInputs = [ ]; + cmd = file: target: ''xz -9c -T1 -C crc32 --lzma2=dict=2MiB "${file}" > "${target}"''; + }; + zstd = { + ext = "zst"; + nativeBuildInputs = [ zstd ]; + cmd = file: target: ''zstd -T1 -19 --long --check -f "${file}" -o "${target}"''; + }; + }.${type} or (throw "Unsupported compressor type for firmware."); + + args = { + allowedRequisites = []; + inherit (compressor) nativeBuildInputs; + } // lib.optionalAttrs (firmware ? meta) { inherit (firmware) meta; }; +in + +runCommand "${firmware.name}-${type}" args '' + mkdir -p $out/lib + (cd ${firmware} && find lib/firmware -type d -print0) | + (cd $out && xargs -0 mkdir -v --) + (cd ${firmware} && find lib/firmware -type f -print0) | + (cd $out && xargs -0rtP "$NIX_BUILD_CORES" -n1 \ + sh -c '${compressor.cmd "${firmware}/$1" "$1.${compressor.ext}"}' --) + (cd ${firmware} && find lib/firmware -type l) | while read link; do + target="$(readlink "${firmware}/$link")" + if [ -f "${firmware}/$link" ]; then + ln -vs -- "''${target/^${firmware}/$out}.${compressor.ext}" "$out/$link.${compressor.ext}" + else + ln -vs -- "''${target/^${firmware}/$out}" "$out/$link" + fi + done + + echo "Checking for broken symlinks:" + find -L $out -type l -print -execdir false -- '{}' '+' +'' -- cgit 1.4.1