Install NixOS on Hetzner Cloud: Difference between revisions
update example to 24.11 |
Fix nixos-anywhere install |
||
(8 intermediate revisions by 5 users not shown) | |||
Line 85: | Line 85: | ||
nix.settings = { | nix.settings = { | ||
experimental-features = "nix-command flakes"; | experimental-features = "nix-command flakes"; | ||
}; | }; | ||
Line 91: | Line 90: | ||
pkgs.vim | pkgs.vim | ||
pkgs.git | pkgs.git | ||
]; | ]; | ||
Line 110: | Line 106: | ||
]; | ]; | ||
time.timeZone = "Europe/London"; | time.timeZone = "Europe/London"; | ||
i18n.defaultLocale = " | i18n.defaultLocale = "en_US.UTF-8"; | ||
console.keyMap = "us"; | console.keyMap = "us"; | ||
boot.loader.grub.enable = true; | boot.loader.grub.enable = true; | ||
Line 141: | Line 135: | ||
}; | }; | ||
}; | }; | ||
networking.firewall.allowedTCPPorts = [ 22 ]; | networking.firewall.allowedTCPPorts = [ 22 ]; | ||
system.stateVersion = "24.11"; | |||
system.stateVersion = "24.11"; | |||
} | } | ||
To access the | To access the VM, you will need to ensure that port 22 on the VM is opened via the Hetzner firewall if that is configured. | ||
=== nixos-anywhere === | === 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. | ||
#First upload your SSH key via the Hetzner Web UI | |||
#Then click yourself a VM. For the OS choose Ubuntu but anything should work. This guide was tested with x86_64-linux but aarch64 should work with the note from below. | |||
# | |||
# | |||
#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 | # /tmp/my-hetzner-vm/hardware-configuration.nix | ||
{ config, lib, pkgs, modulesPath, ... }: | { config, lib, pkgs, modulesPath, ... }: | ||
Line 192: | Line 158: | ||
]; | ]; | ||
networking.useDHCP = lib.mkDefault true; | networking.useDHCP = lib.mkDefault true; | ||
nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux"; | nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux"; | ||
} | } | ||
</syntaxhighlight><syntaxhighlight lang="nix"> | </syntaxhighlight><syntaxhighlight lang="nix"> | ||
# /tmp | # /tmp/my-hetzner-vm/disko-config.nix | ||
{ | { | ||
Line 241: | Line 202: | ||
} | } | ||
</syntaxhighlight><syntaxhighlight lang="nix"> | </syntaxhighlight><syntaxhighlight lang="nix"> | ||
# /tmp | # /tmp/my-hetzner-vm/configuration.nix | ||
{ config, lib, pkgs, ... }: | { config, lib, pkgs, ... }: | ||
Line 265: | Line 226: | ||
enable = true; | enable = true; | ||
defaultEditor = true; | defaultEditor = true; | ||
}; | }; | ||
system.stateVersion = "24. | system.stateVersion = "24.11"; | ||
} | } | ||
</syntaxhighlight>'''Note''': the value of <code>initialHashedPassword</code> above was obtained using <code>mkpasswd</code> command in Linux, and corresponds to <code>Password.123</code> string used as password.<syntaxhighlight lang="nix"> | </syntaxhighlight>'''Note''': the value of <code>initialHashedPassword</code> above was obtained using <code>mkpasswd</code> command in Linux, and corresponds to <code>Password.123</code> string used as password.<syntaxhighlight lang="nix"> | ||
# /tmp/my- | # /tmp/my-hetzner-vm/flake.nix | ||
{ | { | ||
inputs = { | inputs = { | ||
nixpkgs = { | nixpkgs = { | ||
url = "github:NixOS/nixpkgs/nixos-24. | url = "github:NixOS/nixpkgs/nixos-24.11"; | ||
}; | }; | ||
Line 305: | Line 255: | ||
modules = [ | modules = [ | ||
. | ./configuration.nix | ||
inputs.disko.nixosModules.disko | inputs.disko.nixosModules.disko | ||
]; | ]; | ||
Line 312: | Line 262: | ||
}; | }; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# | #To build NixOS from the flake run:<syntaxhighlight lang="shell"> | ||
nix run --extra-experimental-features 'nix-command flakes' github:nix-community/nixos-anywhere | nix run --extra-experimental-features 'nix-command flakes' github:nix-community/nixos-anywhere -- --flake /tmp/my-hetzner-vm#my-hetzner-vm --target-host root@0.0.0.0 --build-on-remote | ||
</syntaxhighlight>'''Note''': replace <code>0.0.0.0</code> with an IP address obtained during an earlier step. | </syntaxhighlight>'''Note''': replace <code>0.0.0.0</code> with an IP address obtained during an earlier step. | ||
The NixOS on Hetzner is installed! | The NixOS on Hetzner is installed! | ||
=== disko === | === disko === | ||
Line 349: | Line 274: | ||
references: | references: | ||
* [[Disko]] | |||
* [https://github.com/feelssexy/hetzner-auto-nixos/blob/main/hardware-configuration.nix sample regular hardware config] | * [https://github.com/feelssexy/hetzner-auto-nixos/blob/main/hardware-configuration.nix sample regular hardware config] | ||
* [https://github.com/LGUG2Z/nixos-hetzner-cloud-starter/blob/master/disk-config.nix sample config using disko] | * [https://github.com/LGUG2Z/nixos-hetzner-cloud-starter/blob/master/disk-config.nix sample config using disko] | ||
Line 367: | Line 293: | ||
* Run following script. Replace <code>NIX_CHANNEL</code> variable with the version string you wish to install. | * Run following script. Replace <code>NIX_CHANNEL</code> variable with the version string you wish to install. | ||
<syntaxHighlight lang=bash> | <syntaxHighlight lang=bash> | ||
curl https://raw.githubusercontent.com/elitak/nixos-infect/master/nixos-infect | NIX_CHANNEL=nixos- | curl https://raw.githubusercontent.com/elitak/nixos-infect/master/nixos-infect | NIX_CHANNEL=nixos-24.11 bash -x | ||
</syntaxHighlight> | </syntaxHighlight> | ||
* Reboot into NixOS | * Reboot into NixOS | ||
Line 382: | Line 308: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
systemd.network.enable = true; | |||
systemd.network.networks."30-wan" = { | |||
matchConfig.Name = "ens3"; # either ens3 or enp1s0, check 'ip addr' | |||
networkConfig.DHCP = "ipv4"; | |||
address = [ | |||
# replace this subnet with the one assigned to your instance | |||
"2a01:4f8:aaaa:bbbb::1/64" | |||
]; | |||
routes = [ | |||
{ Gateway = "fe80::1"; } | |||
]; | |||
}; | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 401: | Line 326: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
systemd.network.networks."30-wan" = { | |||
matchConfig.Name = "ens3"; # either ens3 (amd64) or enp1s0 (arm64) | |||
networkConfig.DHCP = "no"; | |||
address = [ | |||
# replace this address with the one assigned to your instance | |||
"A.B.C.D/32" | |||
# replace this subnet with the one assigned to your instance | |||
"2a01:4f8:AAAA:BBBB::1/64" | |||
]; | |||
routes = [ | |||
{ Gateway = "172.31.1.1"; GatewayOnLink = true; } | |||
{ Gateway = "fe80::1"; } | |||
]; | |||
}; | |||
</syntaxhighlight> | </syntaxhighlight> | ||