Install NixOS on Hetzner Cloud: Difference between revisions
Oldhomemovie (talk | contribs) mNo edit summary |
Oldhomemovie (talk | contribs) No edit summary |
||
(7 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]]. | ||
=== | === 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 | 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 | 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. | </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]. | ||
# | #SSH onto the server using IP and root password obtained after the previous step:<syntaxhighlight lang="shell"> | ||
ssh root@0.0.0.0 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
# | #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- | mkdir -p /tmp/my-first-flake/my-vms/my-hetzner-vm/ | ||
</syntaxhighlight> | </syntaxhighlight> | ||
#Using a code editor on your host computer, create 4 files. File contents, as well as the location of where to put corresponding file are indicated below:<syntaxhighlight lang="nix"> | #Using a code editor on your host computer, create 4 files. File contents, as well as the location of where to put corresponding file are indicated below:<syntaxhighlight lang="nix"> | ||
# /tmp/my-first-flake/my- | # /tmp/my-first-flake/my-vms/my-hetzner-vm/hardware-configuration.nix | ||
{ config, lib, pkgs, modulesPath, ... }: | { config, lib, pkgs, modulesPath, ... }: | ||
Line 56: | Line 56: | ||
} | } | ||
</syntaxhighlight><syntaxhighlight lang="nix"> | </syntaxhighlight><syntaxhighlight lang="nix"> | ||
# /tmp/my-first-flake/my- | # /tmp/my-first-flake/my-vms/my-hetzner-vm/disko-config.nix | ||
{ | { | ||
Line 96: | Line 96: | ||
} | } | ||
</syntaxhighlight><syntaxhighlight lang="nix"> | </syntaxhighlight><syntaxhighlight lang="nix"> | ||
# /tmp/my-first-flake/my- | # /tmp/my-first-flake/my-vms/my-hetzner-vm/configuration.nix | ||
{ config, lib, pkgs, ... }: | { config, lib, pkgs, ... }: | ||
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 | colorscheme habamax | ||
''; | ''; | ||
packages.packages = { | packages.packages = { | ||
start = [ | start = [ | ||
pkgs.vimPlugins. | pkgs.vimPlugins.nerdtree | ||
]; | ]; | ||
}; | }; | ||
Line 160: | Line 161: | ||
modules = [ | modules = [ | ||
./my- | ./my-vms/my-hetzner-vm/configuration.nix | ||
inputs.disko.nixosModules.disko | inputs.disko.nixosModules.disko | ||
]; | ]; | ||
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. | ||
#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 | </syntaxhighlight>'''Note''': replace <code>0.0.0.0</code> with an IP address obtained during an earlier step. | ||
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:~/ | ||
</syntaxhighlight> | </syntaxhighlight>'''Note''': again, replace <code>0.0.0.0</code> with an IP address obtained via <code>hcloud server ip my-hetzner-vm</code>. | ||
#Using <code>neovim</code> editor on the VM, modify <code>configuration.nix</code> to include a package containing Elixir programming language runtime for <code>eugene</code> user. Run:<syntaxhighlight lang="shell"> | #Using <code>neovim</code> editor on the VM, modify <code>configuration.nix</code> to include a package containing Elixir programming language runtime for <code>eugene</code> user. Run:<syntaxhighlight lang="shell"> | ||
nvim my-first-flake/my-vms/my-hetzner-vm/configuration.nix | nvim my-first-flake/my-vms/my-hetzner-vm/configuration.nix |