1let
2 # Some metadata on various compression programs, relevant to naming
3 # the initramfs file and, if applicable, generating a u-boot image
4 # from it.
5 compressors = import ./initrd-compressor-meta.nix;
6 # Get the basename of the actual compression program from the whole
7 # compression command, for the purpose of guessing the u-boot
8 # compression type and filename extension.
9 compressorName = fullCommand: builtins.elemAt (builtins.match "([^ ]*/)?([^ ]+).*" fullCommand) 1;
10in
11{ stdenvNoCC, libarchive, ubootTools, lib, pkgsBuildHost, makeInitrdNGTool, binutils, runCommand
12# Name of the derivation (not of the resulting file!)
13, name ? "initrd"
14
15, strip ? true
16
17# Program used to compress the cpio archive; use "cat" for no compression.
18# This can also be a function which takes a package set and returns the path to the compressor,
19# such as `pkgs: "${pkgs.lzop}/bin/lzop"`.
20, compressor ? "gzip"
21, _compressorFunction ?
22 if lib.isFunction compressor then compressor
23 else if ! builtins.hasContext compressor && builtins.hasAttr compressor compressors then compressors.${compressor}.executable
24 else _: compressor
25, _compressorExecutable ? _compressorFunction pkgsBuildHost
26, _compressorName ? compressorName _compressorExecutable
27, _compressorMeta ? compressors.${_compressorName} or {}
28
29# List of arguments to pass to the compressor program, or null to use its defaults
30, compressorArgs ? null
31, _compressorArgsReal ? if compressorArgs == null then _compressorMeta.defaultArgs or [] else compressorArgs
32
33# Filename extension to use for the compressed initramfs. This is
34# included for clarity, but $out/initrd will always be a symlink to
35# the final image.
36# If this isn't guessed, you may want to complete the metadata above and send a PR :)
37, extension ? _compressorMeta.extension or
38 (throw "Unrecognised compressor ${_compressorName}, please specify filename extension")
39
40# List of { object = path_or_derivation; symlink = "/path"; }
41# The paths are copied into the initramfs in their nix store path
42# form, then linked at the root according to `symlink`.
43, contents
44
45# List of uncompressed cpio files to prepend to the initramfs. This
46# can be used to add files in specified paths without them becoming
47# symlinks to store paths.
48, prepend ? []
49
50# Whether to wrap the initramfs in a u-boot image.
51, makeUInitrd ? stdenvNoCC.hostPlatform.linux-kernel.target == "uImage"
52
53# If generating a u-boot image, the architecture to use. The default
54# guess may not align with u-boot's nomenclature correctly, so it can
55# be overridden.
56# See https://gitlab.denx.de/u-boot/u-boot/-/blob/9bfb567e5f1bfe7de8eb41f8c6d00f49d2b9a426/common/image.c#L81-106 for a list.
57, uInitrdArch ? stdenvNoCC.hostPlatform.ubootArch
58
59# The name of the compression, as recognised by u-boot.
60# See https://gitlab.denx.de/u-boot/u-boot/-/blob/9bfb567e5f1bfe7de8eb41f8c6d00f49d2b9a426/common/image.c#L195-204 for a list.
61# If this isn't guessed, you may want to complete the metadata above and send a PR :)
62, uInitrdCompression ? _compressorMeta.ubootName or
63 (throw "Unrecognised compressor ${_compressorName}, please specify uInitrdCompression")
64}: runCommand name ({
65 compress = "${_compressorExecutable} ${lib.escapeShellArgs _compressorArgsReal}";
66 passthru = {
67 compressorExecutableFunction = _compressorFunction;
68 compressorArgs = _compressorArgsReal;
69 };
70
71 inherit extension makeUInitrd uInitrdArch prepend;
72 ${if makeUInitrd then "uInitrdCompression" else null} = uInitrdCompression;
73
74 passAsFile = ["contents"];
75 contents = lib.concatMapStringsSep "\n" ({ object, symlink, ... }: "${object}\n${lib.optionalString (symlink != null) symlink}") contents + "\n";
76
77 nativeBuildInputs = [makeInitrdNGTool libarchive] ++ lib.optional makeUInitrd ubootTools ++ lib.optional strip binutils;
78
79 STRIP = if strip then "${pkgsBuildHost.binutils.targetPrefix}strip" else null;
80}) ''
81 mkdir -p ./root/var/empty
82 make-initrd-ng "$contentsPath" ./root
83 mkdir "$out"
84 (cd root && find . -exec touch -h -d '@1' '{}' +)
85 for PREP in $prepend; do
86 cat $PREP >> $out/initrd
87 done
88 (cd root && find . -print0 | sort -z | bsdtar --uid 0 --gid 0 -cnf - -T - | bsdtar --null -cf - --format=newc @- | eval -- $compress >> "$out/initrd")
89
90 if [ -n "$makeUInitrd" ]; then
91 mkimage -A "$uInitrdArch" -O linux -T ramdisk -C "$uInitrdCompression" -d "$out/initrd" $out/initrd.img
92 # Compatibility symlink
93 ln -sf "initrd.img" "$out/initrd"
94 else
95 ln -s "initrd" "$out/initrd$extension"
96 fi
97''