make-initrd-ng: fix reproducibility problems

The previous find invocation didn't match the root directory, so the
root directory's access and modification time wasn't set to a
deterministic value and the build time leaked into the output.

`make-initrd` replaced `cpio` with `bsdtar` in #165892 because the
former includes the number of hardlinks in the created archive, which
depends on the filesystem (and can also be influenced by `nix-store
--optimise`). The same problem applies to `make-initrd-ng`, so this
commit replaces `cpio` with `libarchive`'s `bsdtar`.
This commit is contained in:
Andreas Stührk 2023-12-04 18:36:24 +01:00
parent 216b111fb8
commit 4084ee0cd5

View file

@ -8,7 +8,7 @@ let
# compression type and filename extension. # compression type and filename extension.
compressorName = fullCommand: builtins.elemAt (builtins.match "([^ ]*/)?([^ ]+).*" fullCommand) 1; compressorName = fullCommand: builtins.elemAt (builtins.match "([^ ]*/)?([^ ]+).*" fullCommand) 1;
in in
{ stdenvNoCC, perl, cpio, ubootTools, lib, pkgsBuildHost, makeInitrdNGTool, binutils, runCommand { stdenvNoCC, libarchive, ubootTools, lib, pkgsBuildHost, makeInitrdNGTool, binutils, runCommand
# Name of the derivation (not of the resulting file!) # Name of the derivation (not of the resulting file!)
, name ? "initrd" , name ? "initrd"
@ -74,18 +74,18 @@ in
passAsFile = ["contents"]; passAsFile = ["contents"];
contents = lib.concatMapStringsSep "\n" ({ object, symlink, ... }: "${object}\n${lib.optionalString (symlink != null) symlink}") contents + "\n"; contents = lib.concatMapStringsSep "\n" ({ object, symlink, ... }: "${object}\n${lib.optionalString (symlink != null) symlink}") contents + "\n";
nativeBuildInputs = [makeInitrdNGTool cpio] ++ lib.optional makeUInitrd ubootTools ++ lib.optional strip binutils; nativeBuildInputs = [makeInitrdNGTool libarchive] ++ lib.optional makeUInitrd ubootTools ++ lib.optional strip binutils;
STRIP = if strip then "${pkgsBuildHost.binutils.targetPrefix}strip" else null; STRIP = if strip then "${pkgsBuildHost.binutils.targetPrefix}strip" else null;
}) '' }) ''
mkdir -p ./root/var/empty mkdir -p ./root/var/empty
make-initrd-ng "$contentsPath" ./root make-initrd-ng "$contentsPath" ./root
mkdir "$out" mkdir "$out"
(cd root && find * .[^.*] -exec touch -h -d '@1' '{}' +) (cd root && find . -exec touch -h -d '@1' '{}' +)
for PREP in $prepend; do for PREP in $prepend; do
cat $PREP >> $out/initrd cat $PREP >> $out/initrd
done done
(cd root && find . -print0 | sort -z | cpio -o -H newc -R +0:+0 --reproducible --null | eval -- $compress >> "$out/initrd") (cd root && find . -print0 | sort -z | bsdtar --uid 0 --gid 0 -cnf - -T - | bsdtar --null -cf - --format=newc @- | eval -- $compress >> "$out/initrd")
if [ -n "$makeUInitrd" ]; then if [ -n "$makeUInitrd" ]; then
mkimage -A "$uInitrdArch" -O linux -T ramdisk -C "$uInitrdCompression" -d "$out/initrd" $out/initrd.img mkimage -A "$uInitrdArch" -O linux -T ramdisk -C "$uInitrdCompression" -d "$out/initrd" $out/initrd.img