Install NixOS on Hetzner Cloud: Difference between revisions

mNo edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 9: Line 9:
There are several ways to install NixOS, such as the "traditional" ISO installation, [[nixos-infect]] or [[nixos-anywhere]].
There are several ways to install NixOS, such as the "traditional" ISO installation, [[nixos-infect]] or [[nixos-anywhere]].


=== From NixOS minimal ISO ===
=== Tradition ISO installation ===
TODO
 
=== nixos-anywhere ===
The tutorial assumes you already have an account on Hetzner Cloud, and no prior access to a system with NixOS or nix CLI utility installed:
The tutorial assumes you already have an account on Hetzner Cloud, and no prior access to a system with NixOS or nix CLI utility installed:
# Create a temp folder for future use. Run:<syntaxhighlight lang="shell">
# Create a temp folder for future use. Run:<syntaxhighlight lang="shell">
Line 18: Line 21:
</syntaxhighlight>'''Note''': this is done in a container in order to reduce the "setup footprint and residue", allowing to throw away this setup environment quickly.
</syntaxhighlight>'''Note''': this is done in a container in order to reduce the "setup footprint and residue", allowing to throw away this setup environment quickly.
#Install <code>nix</code> and <code>hcloud</code> CLI utilities. Run:<syntaxhighlight lang="shell">
#Install <code>nix</code> and <code>hcloud</code> CLI utilities. Run:<syntaxhighlight lang="shell">
apk add nix hcloud openssh-client
apk add nix hcloud
</syntaxhighlight>
</syntaxhighlight>
#Authenticate <code>hcloud</code> CLI utility. Run:<syntaxhighlight lang="shell">
#Authenticate <code>hcloud</code> CLI utility. Run:<syntaxhighlight lang="shell">
Line 25: Line 28:
#When asked, enter value of the token in the prompt.  '''Note''': the token with "Read/Write" permissions can be obtained on a project page inside Hetzner Cloud: <nowiki>https://console.hetzner.cloud/projects/0000000/security/tokens</nowiki>
#When asked, enter value of the token in the prompt.  '''Note''': the token with "Read/Write" permissions can be obtained on a project page inside Hetzner Cloud: <nowiki>https://console.hetzner.cloud/projects/0000000/security/tokens</nowiki>
#Create a VM on Hetzner. Run:<syntaxhighlight lang="shell">
#Create a VM on Hetzner. Run:<syntaxhighlight lang="shell">
hcloud server create --name my-hetzner-vm --type cpx21 --image ubuntu-24.04 --location fsn1 --start-after-create=false
hcloud server create --name my-hetzner-vm --type cpx21 --image ubuntu-24.04 --location fsn1
</syntaxhighlight>'''Note 1''': this tutorial uses <code>cpx21</code> VM instance type which corresponds to an x86 architecture marchine with 3 CPU cores and 4GB of RAM, and <code>fsn1</code> location which corresponds to a data center in the city of Falkenstein in Germany. A list of all instance types can be obtained by running command <code>hcloud server-type list</code>, while a list of all locations can be obtained by running <code>hcloud location list</code> command. '''Note 2''': Hopefully, Hetzner Cloud team will support NixOS disk images soon, see [https://www.reddit.com/r/NixOS/comments/1desdbv/could_we_convince_hetzner_to_add_nixos_as_a/ Could we convince Hetzner to add Nixos as a standard image choice].
</syntaxhighlight>Write down IP and generated user password. '''Note 1''': this tutorial uses <code>cpx21</code> VM instance type which corresponds to an x86 architecture marchine with 3 CPU cores and 4GB of RAM, and <code>fsn1</code> location which corresponds to a data center in the city of Falkenstein in Germany. A list of all instance types can be obtained by running command <code>hcloud server-type list</code>, while a list of all locations can be obtained by running <code>hcloud location list</code> command. '''Note 2''': Hopefully, Hetzner Cloud team will support NixOS disk images soon, see [https://www.reddit.com/r/NixOS/comments/1desdbv/could_we_convince_hetzner_to_add_nixos_as_a/ Could we convince Hetzner to add Nixos as a standard image choice].
#Attach an ISO with NixOS installer. Run:<syntaxhighlight lang="shell">
#SSH onto the server using IP and root password obtained after the previous step:<syntaxhighlight lang="shell">
hcloud server attach-iso my-hetzner-vm nixos-minimal-24.05.1503.752c634c09ce-x86_64-linux.iso
ssh root@0.0.0.0
</syntaxhighlight>'''Note''': Hetzner attempts to keep the image as up-to-date as possible, hence the hash of the nixos-minimal image at the time of following this tutorial is highly likely to have changed. Run <code>hcloud iso list</code> and look up an up-to-date name of the nixos-minimal ISO image.
#Start a VM. Run:<syntaxhighlight lang="shell">
hcloud server poweron my-hetzner-vm
</syntaxhighlight>
</syntaxhighlight>
#Open Hetzner Cloud console web page, find the <code>my-hetzner-vm</code> server, open a remote web terminal (aka "VNC over "wss://") and change password of <code>nixos</code> user to <code>my-temp-password-123</code>:[[File:Prompt_with_a_token.png|right|frameless|197x197px]]
#Change the password to something temporal, for example: <code>my-temp-password-123</code>.
# On your host computer, create a folder. Run:<syntaxhighlight lang="shell">
# On your host computer, create a folder. Run:<syntaxhighlight lang="shell">
mkdir -p /tmp/my-first-flake/my-vms/my-hetzner-vm/
mkdir -p /tmp/my-first-flake/my-vms/my-hetzner-vm/
Line 108: Line 108:


   boot.loader.grub.enable = true;
   boot.loader.grub.enable = true;
  boot.loader.grub.device = "/dev/sda1";


   services.openssh.enable = true;
   services.openssh.enable = true;
Line 122: Line 123:
     configure = {
     configure = {
       customRC = ''
       customRC = ''
         colorscheme base16-ashes
         colorscheme habamax
       '';
       '';


       packages.packages = {
       packages.packages = {
         start = [
         start = [
           pkgs.vimPlugins.base16-nvim
           pkgs.vimPlugins.nerdtree
         ];
         ];
       };
       };
Line 168: Line 169:
}
}
</syntaxhighlight>'''Note''': all these files constitute what's known as a ''nix [[flake]]''. The flake in question is small, though not exactly a minimal one.
</syntaxhighlight>'''Note''': all these files constitute what's known as a ''nix [[flake]]''. The flake in question is small, though not exactly a minimal one.
#Get the service IP address. Run:<syntaxhighlight lang="shell">
hcloud server ip my-hetzner-vm
</syntaxhighlight>
#Build NixOS from flake. Run:<syntaxhighlight lang="shell">
#Build NixOS from flake. Run:<syntaxhighlight lang="shell">
nix run --extra-experimental-features 'nix-command flakes' github:nix-community/nixos-anywhere -- --flake /tmp/my-first-flake#my-hetzner-vm nixos@0.0.0.0 --build-on-remote
nix run --extra-experimental-features 'nix-command flakes' github:nix-community/nixos-anywhere -- --flake /tmp/my-first-flake#my-hetzner-vm nixos@0.0.0.0 --build-on-remote
</syntaxhighlight>'''Note''': replace <code>0.0.0.0</code> with an IP address obtained during the previous step.
</syntaxhighlight>'''Note''': replace <code>0.0.0.0</code> with an IP address obtained during an earlier step.
#Detach ISO from VM. Run:<syntaxhighlight lang="shell">
The NixOS on Hetzner is installed!
hcloud server detach-iso my-hetzner-vm


</syntaxhighlight>
Let's do a few more steps to customize the installation.
#Reboot VM. Run:<syntaxhighlight lang="shell">
hcloud server reboot my-hetzner-vm
</syntaxhighlight>
The NixOS on Hetzner is installed! Let's do a few more steps to customize the installation.
#Copy flake files onto the server. Run:<syntaxhighlight lang="shell">
#Copy flake files onto the server. Run:<syntaxhighlight lang="shell">
scp -r /tmp/my-first-flake eugene@0.0.0.0:~/
scp -r /tmp/my-first-flake eugene@0.0.0.0:~/