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/
This commit is contained in:
Maximilian Bosch 2024-04-10 10:04:43 +02:00
parent 378177d269
commit b6ef9ffdfd
No known key found for this signature in database
5 changed files with 60 additions and 34 deletions

View File

@ -53,6 +53,10 @@ Use `services.pipewire.extraConfig` or `services.pipewire.configPackages` for Pi
- A new option `system.etc.overlay.enable` was added. If enabled, `/etc` is
mounted via an overlayfs instead of being created by a custom perl script.
- For each supporting version of the Linux kernel firmware blobs and kernel modules
are compressed with zstd. For firmware blobs this means an increase of 4.4% in size, however
a significantly higher decompression speed.
- NixOS AMIs are now uploaded regularly to a new AWS Account.
Instructions on how to use them can be found on <https://nixos.github.io/amis>.
We are working on integration the data into the NixOS homepage.

View File

@ -167,10 +167,16 @@ let
mv etc/udev/hwdb.bin $out
'';
compressFirmware = firmware: if (config.boot.kernelPackages.kernelAtLeast "5.3" && (firmware.compressFirmware or true)) then
pkgs.compressFirmwareXz firmware
compressFirmware = firmware:
let
inherit (config.boot.kernelPackages) kernelAtLeast;
in
if ! (firmware.compressFirmware or true) then
firmware
else
id firmware;
if kernelAtLeast "5.19" then pkgs.compressFirmwareZstd firmware
else if kernelAtLeast "5.3" then pkgs.compressFirmwareXz firmware
else firmware;
# Udev has a 512-character limit for ENV{PATH}, so create a symlink
# tree to work around this.

View File

@ -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 -- '{}' '+'
''

View File

@ -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 -- '{}' '+'
''

View File

@ -1314,7 +1314,9 @@ with pkgs;
makeBinaryWrapper = callPackage ../build-support/setup-hooks/make-binary-wrapper { };
compressFirmwareXz = callPackage ../build-support/kernel/compress-firmware-xz.nix { };
compressFirmwareXz = callPackage ../build-support/kernel/compress-firmware.nix { type = "xz"; };
compressFirmwareZstd = callPackage ../build-support/kernel/compress-firmware.nix { type = "zstd"; };
makeModulesClosure = { kernel, firmware, rootModules, allowMissing ? false }:
callPackage ../build-support/kernel/modules-closure.nix {