Home Explore Blog Models CI



nixpkgs

1st chunk of `pkgs/build-support/kernel/make-initrd-ng/README.md`
b2f1151ee44e1235acafb1745d32350259cda401fedeccda0000000100000808
# What is this for?

NixOS's traditional initrd is generated by listing the paths that
should be included in initrd and copying the full runtime closure of
those paths into the archive. For most things, like almost any
executable, this involves copying the entirety of huge packages like
glibc, when only things like the shared library files are needed. To
solve this, NixOS does a variety of patchwork to edit the files being
copied in so they only refer to small, patched up paths. For instance,
executables and their shared library dependencies are copied into an
`extraUtils` derivation, and every ELF file is patched to refer to
files in that output.

The problem with this is that it is often difficult to correctly patch
some things. For instance, systemd bakes the path to the `mount`
command into the binary, so patchelf is no help. Instead, it's very
often easier to simply copy the desired files to their original store
locations in initrd and not copy their entire runtime closure. This
does mean that it is the burden of the developer to ensure that all
necessary dependencies are copied in, as closures won't be
consulted. However, it is rare that full closures are actually
desirable, so in the traditional initrd, the developer was likely to
do manual work on patching the dependencies explicitly anyway.

# How it works

This program is similar to its inspiration (`find-libs` from the
traditional initrd), except that it also handles symlinks and
directories according to certain rules. As input, it receives a
sequence of pairs of paths. The first path is an object to copy into
initrd. The second path (if not empty) is the path to a symlink that
should be placed in the initrd, pointing to that object. How that
object is copied depends on its type.

1. A regular file is copied directly to the same absolute path in the
   initrd.

   - If it is *also* an ELF file, then all of its direct shared
     library dependencies are also listed as objects to be copied.

   - If an unwrapped file exists as `.[filename]-wrapped`, then it is

Title: Optimizing NixOS Initrd Generation
Summary
The text discusses the challenges with NixOS's traditional `initrd` generation, which copies entire runtime closures, leading to large `initrd` sizes and complex, often difficult patching of binaries like `systemd`. It proposes an alternative where necessary files are copied directly to their original store locations instead of full closures, simplifying the process but shifting dependency management to the developer. The text then introduces a new program, inspired by `find-libs`, designed to improve this by handling symlinks and directories, copying regular files directly, and automatically including direct shared library dependencies for ELF files.