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