Trying to reduce netboot size as much as possible

Hello. I have been working intermittedly on a “recovery” solution which is just a NixOS netboot image that’s available on your bootloader of choice (It builds something “bespoke” to your machine for convenience sake).

Reason being that recovering a portable linux install (NixOS) with another usb drive sounds ridicolous (Why can’t you just use the same device?) and because I don’t happen to carry my flash drive every time I screw up my system big time.

However, initrd takes around 800M. That’s a little bit too much for my liking (I think NetworkManager might take a big part in this since it requires webkit and god knows what else). I don’t know what to do to reduce the size further given the packages (I even tried to override systemd with just the bare essentials to get this to run, but packages kept failing left and right) and modules that I desire within the image (NM is plug & play which is very desirable especially on this scenario when you’re dealing on a tty).

This is the code that I have, a little bit crude (especially since it’s not very modular and it’s just hardcoded to my needs and desires) but it does the job:

{
  pkgs,
  lib,
  modulesPath,
  config,
  system,
  ...
}:
let
  netboot = import ("${modulesPath}/../lib/eval-config.nix") rec {
    inherit system;

    modules = [
      (modulesPath + "/installer/netboot/netboot-minimal.nix")
      {
        networking = {
          networkmanager = {
            enable = true;
            wifi = {
              macAddress = "stable-ssid";
            };
            ethernet = {
              macAddress = "stable-ssid";
            };
          };

          modemmanager.enable = config.networking.modemmanager.enable;
          firewall.logRefusedConnections = false;
        };

        hardware = {
          enableRedistributableFirmware = config.hardware.enableRedistributableFirmware;
        };

        environment.systemPackages = with pkgs; [
          helix
          git
        ];

        boot = {
          loader.grub.enable = false;
          supportedFilesystems = lib.mkForce config.boot.supportedFilesystems;
          kernelParams = [ "drm.panic_screen=qr_code" ];

          kernelPackages = config.boot.kernelPackages;
          kernel.sysctl."kernel.sysrq" = 1;

          initrd = {
            kernelModules = lib.mkForce config.boot.initrd.kernelModules;

            systemd = {
              enable = true;
              strip = true;
            };

            compressor = "zstd";

            extraFiles = {
              "/etc/NetworkManager/system-connections" = "/etc/NetworkManager/system-connections";
            };
          };
        };

        console.keyMap = config.services.xserver.xkb.layout;

        systemd = {
          coredump.enable = false;
          oomd.enable = false;
        };

        services.gpm.enable = true;
        system.stateVersion = config.system.stateVersion;
        hardware.enableAllHardware = lib.mkForce false;
      }
    ];
  };

  title = "NixOS - Recovery";
  cfg = config.boot.recovery;
in
{
  config = lib.mkIf cfg.enable {
    boot.loader = {
      grub = {
        extraEntries = ''
          menuentry "${title}" {
            linux ($drive1)/rescue-kernel init=${netboot.config.system.build.toplevel}/init ${toString netboot.config.boot.kernelParams}
            initrd ($drive1)/rescue-initrd
          }
        '';
        extraFiles =
          if (builtins.hasAttr "/boot/EFI" config.fileSystems) then
            {
              "../rescue-kernel" = "${netboot.config.system.build.kernel}/bzImage";
              "../rescue-initrd" = "${netboot.config.system.build.netbootRamdisk}/initrd";
            }
          else
            {
              "rescue-kernel" = "${netboot.config.system.build.kernel}/bzImage";
              "rescue-initrd" = "${netboot.config.system.build.netbootRamdisk}/initrd";
            };

      };
      systemd-boot = {
        extraEntries = {
          "recovery.conf" = ''
            title ${title}
            linux rescuekrnl.img
            initrd rescueinit.img
            options init=${netboot.config.system.build.toplevel}/init ${toString netboot.config.boot.kernelParams}
          '';
        };
        extraFiles = {
          "rescuekrnl.img" = "${netboot.config.system.build.kernel}/bzImage";
          "rescueinit.img" = "${netboot.config.system.build.netbootRamdisk}/initrd";
        };
      };
    };
  };

  options = {
    boot.recovery.enable = lib.mkEnableOption "Embed a recovery ISO";
  };
}

(It’s supposed to be imported as a module in your config.)

It’s a crude setup and it’s based off of nixos-configs/rescue_boot.nix at 1611ff749a46b3fee37b91ecd5e3ec50887aa13f · cleverca22/nixos-configs · GitHub