<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-booting-via-kexec">
  <title><quote>Booting</quote> into NixOS via kexec</title>
  <para>
    In some cases, your system might already be booted into/preinstalled
    with another Linux distribution, and booting NixOS by attaching an
    installation image is quite a manual process.
  </para>
  <para>
    This is particularly useful for (cloud) providers where you can’t
    boot a custom image, but get some Debian or Ubuntu installation.
  </para>
  <para>
    In these cases, it might be easier to use <literal>kexec</literal>
    to <quote>jump into NixOS</quote> from the running system, which
    only assumes <literal>bash</literal> and <literal>kexec</literal> to
    be installed on the machine.
  </para>
  <para>
    Note that kexec may not work correctly on some hardware, as devices
    are not fully re-initialized in the process. In practice, this
    however is rarely the case.
  </para>
  <para>
    To build the necessary files from your current version of nixpkgs,
    you can run:
  </para>
  <programlisting>
nix-build -A kexec.x86_64-linux '&lt;nixpkgs/nixos/release.nix&gt;'
</programlisting>
  <para>
    This will create a <literal>result</literal> directory containing
    the following:
  </para>
  <itemizedlist spacing="compact">
    <listitem>
      <para>
        <literal>bzImage</literal> (the Linux kernel)
      </para>
    </listitem>
    <listitem>
      <para>
        <literal>initrd</literal> (the initrd file)
      </para>
    </listitem>
    <listitem>
      <para>
        <literal>kexec-boot</literal> (a shellscript invoking
        <literal>kexec</literal>)
      </para>
    </listitem>
  </itemizedlist>
  <para>
    These three files are meant to be copied over to the other already
    running Linux Distribution.
  </para>
  <para>
    Note it’s symlinks pointing elsewhere, so <literal>cd</literal> in,
    and use <literal>scp * root@$destination</literal> to copy it over,
    rather than rsync.
  </para>
  <para>
    Once you finished copying, execute <literal>kexec-boot</literal>
    <emphasis>on the destination</emphasis>, and after some seconds, the
    machine should be booting into an (ephemeral) NixOS installation
    medium.
  </para>
  <para>
    In case you want to describe your own system closure to kexec into,
    instead of the default installer image, you can build your own
    <literal>configuration.nix</literal>:
  </para>
  <programlisting language="bash">
{ modulesPath, ... }: {
  imports = [
    (modulesPath + &quot;/installer/netboot/netboot-minimal.nix&quot;)
  ];

  services.openssh.enable = true;
  users.users.root.openssh.authorizedKeys.keys = [
    &quot;my-ssh-pubkey&quot;
  ];
}
</programlisting>
  <programlisting>
nix-build '&lt;nixpkgs/nixos&gt;' \
  --arg configuration ./configuration.nix
  --attr config.system.build.kexecTree
</programlisting>
  <para>
    Make sure your <literal>configuration.nix</literal> does still
    import <literal>netboot-minimal.nix</literal> (or
    <literal>netboot-base.nix</literal>).
  </para>
</section>
