nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
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 perl,
24 cpio,
25 ubootTools,
26 lib,
27 pkgsBuildHost,
28 # Name of the derivation (not of the resulting file!)
29 name ? "initrd",
30
31 # Program used to compress the cpio archive; use "cat" for no compression.
32 # This can also be a function which takes a package set and returns the path to the compressor,
33 # such as `pkgs: "${pkgs.lzop}/bin/lzop"`.
34 compressor ? "gzip",
35 _compressorFunction ?
36 if lib.isFunction compressor then
37 compressor
38 else if !builtins.hasContext compressor && builtins.hasAttr compressor compressors then
39 compressors.${compressor}.executable
40 else
41 _: compressor,
42 _compressorExecutable ? _compressorFunction pkgsBuildHost,
43 _compressorName ? compressorName _compressorExecutable,
44 _compressorMeta ? compressors.${_compressorName} or { },
45
46 # List of arguments to pass to the compressor program, or null to use its defaults
47 compressorArgs ? null,
48 _compressorArgsReal ?
49 if compressorArgs == null then _compressorMeta.defaultArgs or [ ] else compressorArgs,
50
51 # Filename extension to use for the compressed initramfs. This is
52 # included for clarity, but $out/initrd will always be a symlink to
53 # the final image.
54 # If this isn't guessed, you may want to complete the metadata above and send a PR :)
55 extension ?
56 _compressorMeta.extension
57 or (throw "Unrecognised compressor ${_compressorName}, please specify filename extension"),
58
59 # List of { object = path_or_derivation; symlink = "/path"; }
60 # The paths are copied into the initramfs in their nix store path
61 # form, then linked at the root according to `symlink`.
62 contents,
63
64 # List of uncompressed cpio files to prepend to the initramfs. This
65 # can be used to add files in specified paths without them becoming
66 # symlinks to store paths.
67 prepend ? [ ],
68
69 # Whether to wrap the initramfs in a u-boot image.
70 makeUInitrd ? stdenvNoCC.hostPlatform.linux-kernel.target or "dummy" == "uImage",
71
72 # If generating a u-boot image, the architecture to use. The default
73 # guess may not align with u-boot's nomenclature correctly, so it can
74 # be overridden.
75 # See https://gitlab.denx.de/u-boot/u-boot/-/blob/9bfb567e5f1bfe7de8eb41f8c6d00f49d2b9a426/common/image.c#L81-106 for a list.
76 uInitrdArch ? stdenvNoCC.hostPlatform.linuxArch,
77
78 # The name of the compression, as recognised by u-boot.
79 # See https://gitlab.denx.de/u-boot/u-boot/-/blob/9bfb567e5f1bfe7de8eb41f8c6d00f49d2b9a426/common/image.c#L195-204 for a list.
80 # If this isn't guessed, you may want to complete the metadata above and send a PR :)
81 uInitrdCompression ?
82 _compressorMeta.ubootName
83 or (throw "Unrecognised compressor ${_compressorName}, please specify uInitrdCompression"),
84}:
85let
86 # !!! Move this into a public lib function, it is probably useful for others
87 toValidStoreName =
88 x: with builtins; lib.concatStringsSep "-" (filter (x: !(isList x)) (split "[^a-zA-Z0-9_=.?-]+" x));
89
90in
91stdenvNoCC.mkDerivation (
92 rec {
93 inherit
94 name
95 makeUInitrd
96 extension
97 uInitrdArch
98 prepend
99 ;
100
101 builder = ./make-initrd.sh;
102
103 nativeBuildInputs = [
104 perl
105 cpio
106 ] ++ lib.optional makeUInitrd ubootTools;
107
108 compress = "${_compressorExecutable} ${lib.escapeShellArgs _compressorArgsReal}";
109
110 # Pass the function through, for reuse in append-initrd-secrets. The
111 # function is used instead of the string, in order to support
112 # cross-compilation (append-initrd-secrets running on a different
113 # architecture than what the main initramfs is built on).
114 passthru = {
115 compressorExecutableFunction = _compressorFunction;
116 compressorArgs = _compressorArgsReal;
117 };
118
119 # !!! should use XML.
120 objects = map (x: x.object) contents;
121 symlinks = map (x: x.symlink) contents;
122 suffices = map (x: if x ? suffix then x.suffix else "none") contents;
123
124 # For obtaining the closure of `contents'.
125 # Note: we don't use closureInfo yet, as that won't build with nix-1.x.
126 # See #36268.
127 exportReferencesGraph = lib.zipListsWith (x: i: [
128 ("closure-${toValidStoreName (baseNameOf x.symlink)}-${toString i}")
129 x.object
130 ]) contents (lib.range 0 (lib.length contents - 1));
131 pathsFromGraph = ./paths-from-graph.pl;
132 }
133 // lib.optionalAttrs makeUInitrd {
134 uInitrdCompression = uInitrdCompression;
135 }
136)