Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declarative containers ignore networking.nameservers, even with privateNetwork=true #162686

Open
delroth opened this issue Mar 4, 2022 · 11 comments
Labels
0.kind: bug 6.topic: nixos-container Imperative and declarative systemd-nspawn containers

Comments

@delroth
Copy link
Contributor

delroth commented Mar 4, 2022

Describe the bug

NixOS declarative containers have a "feature" that overwrites the container's /etc/resolv.conf at startup with the one from the host. I have doubts about that implementation in the general case, but in the case of a container configured with privateNetwork = true; it is beyond broken. Not only does it copy a resolv.conf that will in all likelihood not work (because we're in a private network namespace, with different interfaces / addressing), but it also somehow confuses resolvconf and makes networking.nameservers not do anything by default.

There is a workaround: environment.etc."resolv.conf".text = "..."; but that's very counter-intuitive to find, and we shouldn't require that for basic networking configuration.

I'm not entirely sure how to fix this, it seems like there are two issues compounded with each other here. First, I think copying resolv.conf with privateNetwork = true just shouldn't happen, if it ever does something useful I suspect that's accidental. Second, resolvconf should be overwriting that host resolv.conf file with what has been configured in networking.nameservers, but it doesn't do it, and I don't yet understand why. It just decides to not touch the file and copy it verbatim.

Steps To Reproduce

  containers.test = {
    privateNetwork = true;
    config = {
      networking.nameservers = [ "8.8.8.8" ];
    };

    # irrelevant to this bug
    autoStart = true;
    ephemeral = true;
  };
host# nixos-container root-login test
test# cat /etc/resolv.conf
# Generated by resolvconf
search delroth.net
nameserver 127.0.0.53
options edns0 trust-ad
options edns0

Expected behavior

A declarative container can define its own DNS configuration without getting trampled over by a bash script.

Screenshots

n/a

Additional context

The original implementation of the resolv.conf copying dates back to 2014: e620be9

Notify maintainers

Tagging @abbradar because I suspect from git history they might know about resolvconf, but that's a shot in the dark :-)

Metadata

  • system: "x86_64-linux"
  • host os: Linux 5.15.16-hardened1, NixOS, 22.05 (Quokka)
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.7.0pre20220221_caf5172
  • channels(delroth): ""
  • nixpkgs: /etc/nixpkgs
@aanderse
Copy link
Member

aanderse commented Mar 4, 2022

@Ma27 does your rewrite of the containers have this same behaviour?

@veprbl veprbl added the 6.topic: nixos-container Imperative and declarative systemd-nspawn containers label Mar 4, 2022
@Ma27
Copy link
Member

Ma27 commented Mar 5, 2022

You can use the ResolvConf= setting from systemd.nspawn(5). With that you can either forward the host's resolv.conf or not. The container-side uses networkd so you can configure your own networkd/resolved setup within the container as well.

The whole point of the change is to support as much of the usptream features as possible.

Btw, I know that the PR requires a bit of work, I'll be on vacation now, after that I'll hope that I'll get to work on that :)

@onny
Copy link
Contributor

onny commented Aug 26, 2022

@Ma27 Can you please give a more concrete example. I'm not yet able to configure nameservers of the NixOS container :(

@steveej
Copy link
Contributor

steveej commented Oct 17, 2022

i think the culprit is the following:

config = mkIf config.boot.isContainer {

networking.useHostResolvConf = mkDefault true;

@voidzero
Copy link

Perhaps a better default is to use 1.0.0.1 and 1.1.1.1.

@Ultra980
Copy link

I might be wrong here, but doesn't

networking.useHostResolvConf = mkDefault true;

mean that setting networking.useHostResolvConf = mkForce false; in the container's config fix this?

@onny
Copy link
Contributor

onny commented Dec 9, 2023

Bug is still present in NixOS 23.11. I described a workaround here on the wiki page https://nixos.wiki/wiki/NixOS_Containers

@delroth delroth closed this as completed Apr 28, 2024
@voidzero
Copy link

Completed how, any comment?

@shlevy
Copy link
Member

shlevy commented Apr 30, 2024

I think I’m hitting this issue on 23.11, is it fixed on master?

@cmoog
Copy link
Contributor

cmoog commented May 1, 2024

#307173 Sigh

@onny onny reopened this May 1, 2024
@onny
Copy link
Contributor

onny commented May 1, 2024

Bug is still present on master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: bug 6.topic: nixos-container Imperative and declarative systemd-nspawn containers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants