nixops icon indicating copy to clipboard operation
nixops copied to clipboard

/etc/hosts does not include private IPv4 addresses of other hosts in the network

Open justinas opened this issue 3 years ago • 3 comments

Using NixOps master faed0635d24e93f38046af58fb7715327de28e39.

An older NixOps manual mentions:

<...> NixOps generates a /etc/hosts file that contains entries for all the logical machines in the network, mapping names to each machine’s IP address

It is mentioned under VirtualBox section, back from when that backend existed in-tree. Am I wrong in assuming this should work for the "none" backend, i.e. deploying to an existing machine using deployment.targetHost?

Here's my problem: I define a network:

{
  foo = { ... }: rec {
      deployment.targetHost = "1.2.3.4";
      networking.privateIPv4 = "10.0.0.2";
      networking.publicIPv4 = deployment.targetHost;
  };
    
  bar = { ... }: rec {
      deployment.targetHost = "1.2.3.5";
      networking.privateIPv4 = "10.0.0.3";
      networking.publicIPv4 = deployment.targetHost;
  };
}

I then proceed with nixops deploy and /etc/hosts for each host contains only its own hostname. nixops show-physical shows this:

{
  foo = { config, lib, pkgs, ... }: {
    config = {
      boot.kernelModules = [];
      networking = {
        extraHosts = "\n";
        firewall.trustedInterfaces = [];
        publicIPv4 = "1.2.3.4";
      };
      system.stateVersion = ( lib.mkDefault "20.09" );
    };
  };
  # <snip>
}

So, it picks up networking.publicIPv4 from the definition, but not privateIPv4. The option is still present in the manual, but I am not sure why it is not picked up.

I am not too familiar with the codebase, but this seems like it could be relevant: https://github.com/NixOS/nixops/blob/8de09879d7b1733bc4085257d5bf3cc734f1ed38/nixops/backends/init.py#L451-L453

justinas avatar Dec 19 '20 22:12 justinas

For those looking for a possible workaround:

{ config, lib, nodes, ... }:
{
  networking.extraHosts = with lib; concatStringsSep "\n"
    (mapAttrsToList (n: v: "${v} ${n}")
      (filterAttrs (n: v: n != config.networking.hostName)
        (mapAttrs (n: v: v.config.networking.privateIPv4) nodes)));
}

does what I want it to.

justinas avatar Dec 19 '20 22:12 justinas

This is also not working for me using the AWS backend.

glittershark avatar Jan 29 '21 03:01 glittershark

It is mentioned under VirtualBox section, back from when that backend existed in-tree. Am I wrong in assuming this should work for the "none" backend, i.e. deploying to an existing machine using deployment.targetHost?

FTR this is currently failing to work with virtualbox backend too. 😕

Was the feature removed?

yajo avatar Jul 08 '22 12:07 yajo