NixOS / nixops

NixOps is a tool for deploying to NixOS machines in a network or cloud.
https://nixos.org/nixops
GNU Lesser General Public License v3.0
1.82k stars 363 forks source link

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

Open justinas opened 3 years ago

justinas commented 3 years ago

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 commented 3 years ago

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.

glittershark commented 3 years ago

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

yajo commented 2 years ago

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?