Install NixOS on Hetzner Online: Difference between revisions

imported>IgorM
m Fixed syntax highlighting
Hexa (talk | contribs)
Network configuration: Bring in sync with Hetzner cloud and give only networkd examples for a static config, as well as one using DHCP
Line 12: Line 12:
== Network configuration ==
== Network configuration ==


From Hetzner's [https://accounts.hetzner.com/login web interface], one can obtain both ipv4/ipv6 addresses and gateways.
Hetzner Cloud offers both IPv4 (/32 or shorter subnets) and IPv6 (/64 subnet) connectivity to each machine. The assigned addresses can be looked up on the [https://robot.hetzner.com/server Hetzner Robot] on the IPs tab of a machine. The public IPv4 address of the server can automatically be obtained via DHCP. For IPv6 you have to statically configure both address and gateway.
Hetzner does announce ipv6 addresses servers, so you need to assign those statically.
In this example, we use networkd to configure the interface. The same configuration can be used for both
the kexec installation image and the final server configuration.


<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
{ ... }: {
{
  # This make sure that our interface is named `eth0`.
  # This should be ok as long as you don't have multiple physical network cards
  # For multiple cards one could add a netdev unit to rename the interface based on the mac address
  networking.usePredictableInterfaceNames = false;
   systemd.network = {
   systemd.network = {
     enable = true;
     enable = true;
     networks."eth0".extraConfig = ''
     networks."30-wan" = {
       [Match]
       matchConfig.Name = "enp1s0"; # The predictable name of the network interface
       Name = eth0
       networkConfig.DHCP = "ipv4";
       [Network]
       addresses = [
      # Add your own assigned ipv6 subnet here here!
        # Replace the subnet with the one assigned to your machine
      Address = 2a01:4f9:ffff::1/64
        "2a01:4f8:AAAA:BBBB::1/64"
       Gateway = fe80::1
       ];
       # optionally you can do the same for ipv4 and disable DHCP (networking.dhcpcd.enable = false;)
      gateway = [
       # Address =  144.x.x.x/26
        "fe80::1"
      # Gateway = 144.x.x.1
       ];
     '';
       linkConfig.RequiredForOnline = "routable";
     };
   };
   };
}
}
</syntaxHighlight>
</syntaxhighlight>


Another possibility is to use <code>networking.interfaces</code>:
=== Static IPv4 configuration ===
<syntaxHighlight lang=nix>
Since the IPv4 network configuration is known, it can also be configured statically, preventing reliance on the DHCP service. The gateway and subnet information is visible when hovering the IPv4 address. The subnet size is usually a /26 (255.255.255.224) or a /27 (255.255.255.192).<syntaxhighlight lang="nix">
let
{
   external-mac = "00:11:22:33:44:55";
   systemd.network = {
  ext-if = "et0";
    enable = true;
  external-ip = "144.x.x.x";
    networks."30-wan" = {
  external-gw = "144.x.x.255";
      matchConfig.Name = "enp1s0"; # The predictable name of the network interface
  external-ip6 = "2a01:XXXX:XXXX::1";
      networkConfig.DHCP = "no";
  external-gw6 = "fe80::1";
       addresses = [
  external-netmask = 27;
         # Replace the address and subnet with the one assigned to your machine
  external-netmask6 = 64;
        "A.B.C.D/26"
in {
        # Replace the subnet with the one assigned to your machine
  # rename the external interface based on the MAC of the interface
         "2a01:4f8:AAAA:BBBB::1/64"
  services.udev.extraRules = ''SUBSYSTEM=="net", ATTR{address}=="${external-mac}", NAME="${ext-if}"'';
       ];
  networking = {
       gateway = [
    interfaces."${ext-if}" = {
         # Replace the gateway address with the one in your subnet
       ipv4.addresses = [{
        "A.B.C.E"
         address = external-ip;
         "fe80::1"
         prefixLength = external-netmask;
       ];
       }];
       linkConfig.RequiredForOnline = "routable";
       ipv6.addresses = [{
         address = external-ip6;
         prefixLength = external-netmask6;
       }];
    };
    defaultGateway6 = {
      address = external-gw6;
       interface = ext-if;
     };
     };
    defaultGateway = external-gw;
   };
   };
}
}
</syntaxHighlight>
</syntaxhighlight>


== Bootstrap from the Rescue System ==
== Bootstrap from the Rescue System ==