···11+<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-booting-via-kexec">
22+ <title><quote>Booting</quote> into NixOS via kexec</title>
33+ <para>
44+ In some cases, your system might already be booted into/preinstalled
55+ with another Linux distribution, and booting NixOS by attaching an
66+ installation image is quite a manual process.
77+ </para>
88+ <para>
99+ This is particularly useful for (cloud) providers where you can’t
1010+ boot a custom image, but get some Debian or Ubuntu installation.
1111+ </para>
1212+ <para>
1313+ In these cases, it might be easier to use <literal>kexec</literal>
1414+ to <quote>jump into NixOS</quote> from the running system, which
1515+ only assumes <literal>bash</literal> and <literal>kexec</literal> to
1616+ be installed on the machine.
1717+ </para>
1818+ <para>
1919+ Note that kexec may not work correctly on some hardware, as devices
2020+ are not fully re-initialized in the process. In practice, this
2121+ however is rarely the case.
2222+ </para>
2323+ <para>
2424+ To build the necessary files from your current version of nixpkgs,
2525+ you can run:
2626+ </para>
2727+ <programlisting>
2828+nix-build -A kexec.x86_64-linux '<nixpkgs/nixos/release.nix>'
2929+</programlisting>
3030+ <para>
3131+ This will create a <literal>result</literal> directory containing
3232+ the following:
3333+ </para>
3434+ <itemizedlist spacing="compact">
3535+ <listitem>
3636+ <para>
3737+ <literal>bzImage</literal> (the Linux kernel)
3838+ </para>
3939+ </listitem>
4040+ <listitem>
4141+ <para>
4242+ <literal>initrd</literal> (the initrd file)
4343+ </para>
4444+ </listitem>
4545+ <listitem>
4646+ <para>
4747+ <literal>kexec-boot</literal> (a shellscript invoking
4848+ <literal>kexec</literal>)
4949+ </para>
5050+ </listitem>
5151+ </itemizedlist>
5252+ <para>
5353+ These three files are meant to be copied over to the other already
5454+ running Linux Distribution.
5555+ </para>
5656+ <para>
5757+ Note it’s symlinks pointing elsewhere, so <literal>cd</literal> in,
5858+ and use <literal>scp * root@$destination</literal> to copy it over,
5959+ rather than rsync.
6060+ </para>
6161+ <para>
6262+ Once you finished copying, execute <literal>kexec-boot</literal>
6363+ <emphasis>on the destination</emphasis>, and after some seconds, the
6464+ machine should be booting into an (ephemeral) NixOS installation
6565+ medium.
6666+ </para>
6767+ <para>
6868+ In case you want to describe your own system closure to kexec into,
6969+ instead of the default installer image, you can build your own
7070+ <literal>configuration.nix</literal>:
7171+ </para>
7272+ <programlisting language="bash">
7373+{ modulesPath, ... }: {
7474+ imports = [
7575+ (modulesPath + "/installer/netboot/netboot-minimal.nix")
7676+ ];
7777+7878+ services.openssh.enable = true;
7979+ users.users.root.openssh.authorizedKeys.keys = [
8080+ "my-ssh-pubkey"
8181+ ];
8282+}
8383+</programlisting>
8484+ <programlisting>
8585+nix-build '<nixpkgs/nixos>' \
8686+ --arg configuration ./configuration.nix
8787+ --attr config.system.build.kexecTree
8888+</programlisting>
8989+ <para>
9090+ Make sure your <literal>configuration.nix</literal> does still
9191+ import <literal>netboot-minimal.nix</literal> (or
9292+ <literal>netboot-base.nix</literal>).
9393+ </para>
9494+</section>
···11+# "Booting" into NixOS via kexec {#sec-booting-via-kexec}
22+33+In some cases, your system might already be booted into/preinstalled with
44+another Linux distribution, and booting NixOS by attaching an installation
55+image is quite a manual process.
66+77+This is particularly useful for (cloud) providers where you can't boot a custom
88+image, but get some Debian or Ubuntu installation.
99+1010+In these cases, it might be easier to use `kexec` to "jump into NixOS" from the
1111+running system, which only assumes `bash` and `kexec` to be installed on the
1212+machine.
1313+1414+Note that kexec may not work correctly on some hardware, as devices are not
1515+fully re-initialized in the process. In practice, this however is rarely the
1616+case.
1717+1818+To build the necessary files from your current version of nixpkgs,
1919+you can run:
2020+2121+```ShellSession
2222+nix-build -A kexec.x86_64-linux '<nixpkgs/nixos/release.nix>'
2323+```
2424+2525+This will create a `result` directory containing the following:
2626+ - `bzImage` (the Linux kernel)
2727+ - `initrd` (the initrd file)
2828+ - `kexec-boot` (a shellscript invoking `kexec`)
2929+3030+These three files are meant to be copied over to the other already running
3131+Linux Distribution.
3232+3333+Note it's symlinks pointing elsewhere, so `cd` in, and use
3434+`scp * root@$destination` to copy it over, rather than rsync.
3535+3636+Once you finished copying, execute `kexec-boot` *on the destination*, and after
3737+some seconds, the machine should be booting into an (ephemeral) NixOS
3838+installation medium.
3939+4040+In case you want to describe your own system closure to kexec into, instead of
4141+the default installer image, you can build your own `configuration.nix`:
4242+4343+```nix
4444+{ modulesPath, ... }: {
4545+ imports = [
4646+ (modulesPath + "/installer/netboot/netboot-minimal.nix")
4747+ ];
4848+4949+ services.openssh.enable = true;
5050+ users.users.root.openssh.authorizedKeys.keys = [
5151+ "my-ssh-pubkey"
5252+ ];
5353+}
5454+```
5555+5656+5757+```ShellSession
5858+nix-build '<nixpkgs/nixos>' \
5959+ --arg configuration ./configuration.nix
6060+ --attr config.system.build.kexecTree
6161+```
6262+6363+Make sure your `configuration.nix` does still import `netboot-minimal.nix` (or
6464+`netboot-base.nix`).