Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
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{
12 stdenvNoCC,
13 cpio,
14 ubootTools,
15 lib,
16 pkgsBuildHost,
17 makeInitrdNGTool,
18 binutils,
19 runCommand,
20 # Name of the derivation (not of the resulting file!)
21 name ? "initrd",
22
23 # Program used to compress the cpio archive; use "cat" for no compression.
24 # This can also be a function which takes a package set and returns the path to the compressor,
25 # such as `pkgs: "${pkgs.lzop}/bin/lzop"`.
26 compressor ? "gzip",
27 _compressorFunction ?
28 if lib.isFunction compressor then
29 compressor
30 else if !builtins.hasContext compressor && builtins.hasAttr compressor compressors then
31 compressors.${compressor}.executable
32 else
33 _: compressor,
34 _compressorExecutable ? _compressorFunction pkgsBuildHost,
35 _compressorName ? compressorName _compressorExecutable,
36 _compressorMeta ? compressors.${_compressorName} or { },
37
38 # List of arguments to pass to the compressor program, or null to use its defaults
39 compressorArgs ? null,
40 _compressorArgsReal ?
41 if compressorArgs == null then _compressorMeta.defaultArgs or [ ] else compressorArgs,
42
43 # Filename extension to use for the compressed initramfs. This is
44 # included for clarity, but $out/initrd will always be a symlink to
45 # the final image.
46 # If this isn't guessed, you may want to complete the metadata above and send a PR :)
47 extension ?
48 _compressorMeta.extension
49 or (throw "Unrecognised compressor ${_compressorName}, please specify filename extension"),
50
51 # List of { source = path_or_derivation; target = "/path"; }
52 # The paths are copied into the initramfs in their nix store path
53 # form, then linked at the root according to `target`.
54 contents,
55
56 # List of uncompressed cpio files to prepend to the initramfs. This
57 # can be used to add files in specified paths without them becoming
58 # symlinks to store paths.
59 prepend ? [ ],
60
61 # Whether to wrap the initramfs in a u-boot image.
62 makeUInitrd ? stdenvNoCC.hostPlatform.linux-kernel.target == "uImage",
63
64 # If generating a u-boot image, the architecture to use. The default
65 # guess may not align with u-boot's nomenclature correctly, so it can
66 # be overridden.
67 # See https://gitlab.denx.de/u-boot/u-boot/-/blob/9bfb567e5f1bfe7de8eb41f8c6d00f49d2b9a426/common/image.c#L81-106 for a list.
68 uInitrdArch ? stdenvNoCC.hostPlatform.ubootArch,
69
70 # The name of the compression, as recognised by u-boot.
71 # See https://gitlab.denx.de/u-boot/u-boot/-/blob/9bfb567e5f1bfe7de8eb41f8c6d00f49d2b9a426/common/image.c#L195-204 for a list.
72 # If this isn't guessed, you may want to complete the metadata above and send a PR :)
73 uInitrdCompression ?
74 _compressorMeta.ubootName
75 or (throw "Unrecognised compressor ${_compressorName}, please specify uInitrdCompression"),
76}:
77runCommand name
78 ({
79 compress = "${_compressorExecutable} ${lib.escapeShellArgs _compressorArgsReal}";
80 passthru = {
81 compressorExecutableFunction = _compressorFunction;
82 compressorArgs = _compressorArgsReal;
83 };
84
85 inherit
86 extension
87 makeUInitrd
88 uInitrdArch
89 prepend
90 ;
91 ${if makeUInitrd then "uInitrdCompression" else null} = uInitrdCompression;
92
93 passAsFile = [ "contents" ];
94 contents = builtins.toJSON contents;
95
96 nativeBuildInputs = [
97 makeInitrdNGTool
98 cpio
99 ]
100 ++ lib.optional makeUInitrd ubootTools;
101 })
102 ''
103 mkdir -p ./root/var/empty
104 make-initrd-ng "$contentsPath" ./root
105 mkdir "$out"
106 (cd root && find . -exec touch -h -d '@1' '{}' +)
107 for PREP in $prepend; do
108 cat $PREP >> $out/initrd
109 done
110 (cd root && find . -print0 | sort -z | cpio --quiet -o -H newc -R +0:+0 --reproducible --null | eval -- $compress >> "$out/initrd")
111
112 if [ -n "$makeUInitrd" ]; then
113 mkimage -A "$uInitrdArch" -O linux -T ramdisk -C "$uInitrdCompression" -d "$out/initrd" $out/initrd.img
114 # Compatibility symlink
115 ln -sf "initrd.img" "$out/initrd"
116 else
117 ln -s "initrd" "$out/initrd$extension"
118 fi
119 ''