1# "Booting" into NixOS via kexec {#sec-booting-via-kexec}
2
3In some cases, your system might already be booted into/preinstalled with
4another Linux distribution, and booting NixOS by attaching an installation
5image is quite a manual process.
6
7This is particularly useful for (cloud) providers where you can't boot a custom
8image, but get some Debian or Ubuntu installation.
9
10In these cases, it might be easier to use `kexec` to "jump into NixOS" from the
11running system, which only assumes `bash` and `kexec` to be installed on the
12machine.
13
14Note that kexec may not work correctly on some hardware, as devices are not
15fully re-initialized in the process. In practice, this however is rarely the
16case.
17
18To build the necessary files from your current version of nixpkgs,
19you can run:
20
21```ShellSession
22nix-build -A kexec.x86_64-linux '<nixpkgs/nixos/release.nix>'
23```
24
25This will create a `result` directory containing the following:
26 - `bzImage` (the Linux kernel)
27 - `initrd` (the initrd file)
28 - `kexec-boot` (a shellscript invoking `kexec`)
29
30These three files are meant to be copied over to the other already running
31Linux Distribution.
32
33Note its symlinks pointing elsewhere, so `cd` in, and use
34`scp * root@$destination` to copy it over, rather than rsync.
35
36Once you finished copying, execute `kexec-boot` *on the destination*, and after
37some seconds, the machine should be booting into an (ephemeral) NixOS
38installation medium.
39
40In case you want to describe your own system closure to kexec into, instead of
41the default installer image, you can build your own `configuration.nix`:
42
43```nix
44{ modulesPath, ... }: {
45 imports = [
46 (modulesPath + "/installer/netboot/netboot-minimal.nix")
47 ];
48
49 services.openssh.enable = true;
50 users.users.root.openssh.authorizedKeys.keys = [
51 "my-ssh-pubkey"
52 ];
53}
54```
55
56
57```ShellSession
58nix-build '<nixpkgs/nixos>' \
59 --arg configuration ./configuration.nix
60 --attr config.system.build.kexecTree
61```
62
63Make sure your `configuration.nix` does still import `netboot-minimal.nix` (or
64`netboot-base.nix`).