1# Create an initramfs containing the closure of the specified
2# file system objects. An initramfs is used during the initial
3# stages of booting a Linux system. It is loaded by the boot loader
4# along with the kernel image. It's supposed to contain everything
5# (such as kernel modules) necessary to allow us to mount the root
6# file system. Once the root file system is mounted, the `real' boot
7# script can be called.
8#
9# An initramfs is a cpio archive, and may be compressed with a number
10# of algorithms.
11let
12 # Some metadata on various compression programs, relevant to naming
13 # the initramfs file and, if applicable, generating a u-boot image
14 # from it.
15 compressors = import ./initrd-compressor-meta.nix;
16 # Get the basename of the actual compression program from the whole
17 # compression command, for the purpose of guessing the u-boot
18 # compression type and filename extension.
19 compressorName = fullCommand: builtins.elemAt (builtins.match "([^ ]*/)?([^ ]+).*" fullCommand) 1;
20in
21{
22 stdenvNoCC,
23 cpio,
24 ubootTools,
25 lib,
26 pkgsBuildHost,
27 # Name of the derivation (not of the resulting file!)
28 name ? "initrd",
29
30 # Program used to compress the cpio archive; use "cat" for no compression.
31 # This can also be a function which takes a package set and returns the path to the compressor,
32 # such as `pkgs: "${pkgs.lzop}/bin/lzop"`.
33 compressor ? "gzip",
34 _compressorFunction ?
35 if lib.isFunction compressor then
36 compressor
37 else if !builtins.hasContext compressor && builtins.hasAttr compressor compressors then
38 compressors.${compressor}.executable
39 else
40 _: compressor,
41 _compressorExecutable ? _compressorFunction pkgsBuildHost,
42 _compressorName ? compressorName _compressorExecutable,
43 _compressorMeta ? compressors.${_compressorName} or { },
44
45 # List of arguments to pass to the compressor program, or null to use its defaults
46 compressorArgs ? null,
47 _compressorArgsReal ?
48 if compressorArgs == null then _compressorMeta.defaultArgs or [ ] else compressorArgs,
49
50 # Filename extension to use for the compressed initramfs. This is
51 # included for clarity, but $out/initrd will always be a symlink to
52 # the final image.
53 # If this isn't guessed, you may want to complete the metadata above and send a PR :)
54 extension ?
55 _compressorMeta.extension
56 or (throw "Unrecognised compressor ${_compressorName}, please specify filename extension"),
57
58 # List of { object = path_or_derivation; symlink = "/path"; }
59 # The paths are copied into the initramfs in their nix store path
60 # form, then linked at the root according to `symlink`.
61 contents,
62
63 # List of uncompressed cpio files to prepend to the initramfs. This
64 # can be used to add files in specified paths without them becoming
65 # symlinks to store paths.
66 prepend ? [ ],
67
68 # Whether to wrap the initramfs in a u-boot image.
69 makeUInitrd ? stdenvNoCC.hostPlatform.linux-kernel.target or "dummy" == "uImage",
70
71 # If generating a u-boot image, the architecture to use. The default
72 # guess may not align with u-boot's nomenclature correctly, so it can
73 # be overridden.
74 # See https://gitlab.denx.de/u-boot/u-boot/-/blob/9bfb567e5f1bfe7de8eb41f8c6d00f49d2b9a426/common/image.c#L81-106 for a list.
75 uInitrdArch ? stdenvNoCC.hostPlatform.linuxArch,
76
77 # The name of the compression, as recognised by u-boot.
78 # See https://gitlab.denx.de/u-boot/u-boot/-/blob/9bfb567e5f1bfe7de8eb41f8c6d00f49d2b9a426/common/image.c#L195-204 for a list.
79 # If this isn't guessed, you may want to complete the metadata above and send a PR :)
80 uInitrdCompression ?
81 _compressorMeta.ubootName
82 or (throw "Unrecognised compressor ${_compressorName}, please specify uInitrdCompression"),
83}:
84let
85 # !!! Move this into a public lib function, it is probably useful for others
86 toValidStoreName =
87 x: with builtins; lib.concatStringsSep "-" (filter (x: !(isList x)) (split "[^a-zA-Z0-9_=.?-]+" x));
88
89in
90stdenvNoCC.mkDerivation (
91 rec {
92 inherit
93 name
94 makeUInitrd
95 extension
96 uInitrdArch
97 prepend
98 ;
99
100 builder = ./make-initrd.sh;
101
102 nativeBuildInputs = [
103 cpio
104 ]
105 ++ lib.optional makeUInitrd ubootTools;
106
107 compress = "${_compressorExecutable} ${lib.escapeShellArgs _compressorArgsReal}";
108
109 # Pass the function through, for reuse in append-initrd-secrets. The
110 # function is used instead of the string, in order to support
111 # cross-compilation (append-initrd-secrets running on a different
112 # architecture than what the main initramfs is built on).
113 passthru = {
114 compressorExecutableFunction = _compressorFunction;
115 compressorArgs = _compressorArgsReal;
116 };
117
118 # !!! should use XML.
119 objects = map (x: x.object) contents;
120 symlinks = map (x: x.symlink) contents;
121 suffices = map (x: if x ? suffix then x.suffix else "none") contents;
122
123 closureInfo = "${pkgsBuildHost.closureInfo { rootPaths = objects; }}";
124 }
125 // lib.optionalAttrs makeUInitrd {
126 uInitrdCompression = uInitrdCompression;
127 }
128)