1
1
mirror of https://github.com/LnL7/nix-darwin.git synced 2024-09-11 12:49:18 +03:00

add networking.hosts and .hostFiles from nixos

This code was taking nearly verbatim from
https://github.com/NixOS/nixpkgs/blob/nixos-unstable/nixos/modules/config/networking.nix

The few changes were related to making default /etc/hosts match the Apple's stock one. This implied
no 127.0.0.2 and forcing the IPV6 ::1 entry.
This commit is contained in:
Pierre Penninckx 2024-04-22 14:58:49 -07:00
parent 9e7c20ffd0
commit 851b145460
No known key found for this signature in database
GPG Key ID: D2FA6AED4D6B5E63
3 changed files with 87 additions and 0 deletions

View File

@ -21,6 +21,8 @@ let
esac
'') cfg.knownNetworkServices}
'';
localhostMultiple = any (elem "localhost") (attrValues (removeAttrs cfg.hosts [ "127.0.0.1" "::1" ]));
in
{
@ -94,9 +96,50 @@ in
default = [];
description = "The list of search paths used when resolving domain names.";
};
networking.hosts = lib.mkOption {
type = types.attrsOf (types.listOf types.str);
example = literalExpression ''
{
"127.0.0.1" = [ "foo.bar.baz" ];
"192.168.0.2" = [ "fileserver.local" "nameserver.local" ];
};
'';
description = ''
Locally defined maps of hostnames to IP addresses.
'';
default = {};
};
networking.hostFiles = lib.mkOption {
type = types.listOf types.path;
defaultText = literalMD "Hosts from {option}`networking.hosts` and {option}`networking.extraHosts`";
example = literalExpression ''[ "''${pkgs.my-blocklist-package}/share/my-blocklist/hosts" ]'';
description = ''
Files that should be concatenated together to form {file}`/etc/hosts`.
'';
};
networking.extraHosts = lib.mkOption {
type = types.lines;
default = "";
example = "192.168.0.1 lanlocalhost";
description = ''
Additional verbatim entries to be appended to {file}`/etc/hosts`.
For adding hosts from derivation results, use {option}`networking.hostFiles` instead.
'';
};
};
config = {
assertions = [{
assertion = !localhostMultiple;
message = ''
`networking.hosts` maps "localhost" to something other than "127.0.0.1"
or "::1". This will break some applications. Please use
`networking.extraHosts` if you really want to add such a mapping.
'';
}];
warnings = [
(mkIf (cfg.knownNetworkServices == [] && cfg.dns != []) "networking.knownNetworkServices is empty, dns servers will not be configured.")
@ -119,5 +162,32 @@ in
${setNetworkServices}
'';
networking.hostFiles = let
# Note: localhostHosts has to appear first in /etc/hosts so that 127.0.0.1
# resolves back to "localhost" (as some applications assume) instead of
# the FQDN!
localhostHosts = pkgs.writeText "localhost-hosts" ''
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
'';
stringHosts =
let
oneToString = set: ip: ip + " " + concatStringsSep " " set.${ip} + "\n";
allToString = set: concatMapStrings (oneToString set) (attrNames set);
in pkgs.writeText "string-hosts" (allToString (filterAttrs (_: v: v != []) cfg.hosts));
extraHosts = pkgs.writeText "extra-hosts" cfg.extraHosts;
in mkBefore [ localhostHosts stringHosts extraHosts ];
environment.etc.hosts = {
copy = true;
source = pkgs.concatText "hosts" cfg.hostFiles;
};
};
}

View File

@ -109,6 +109,7 @@ let
tests.launchd-daemons = makeTest ./tests/launchd-daemons.nix;
tests.launchd-setenv = makeTest ./tests/launchd-setenv.nix;
tests.networking-hostname = makeTest ./tests/networking-hostname.nix;
tests.networking-hosts = makeTest ./tests/networking-hosts.nix;
tests.networking-networkservices = makeTest ./tests/networking-networkservices.nix;
tests.nixpkgs-overlays = makeTest ./tests/nixpkgs-overlays.nix;
tests.programs-ssh = makeTest ./tests/programs-ssh.nix;

View File

@ -0,0 +1,16 @@
{ config, pkgs, ... }:
{
networking.hosts = {
"127.0.0.1" = [ "my.super.host" ];
"10.0.0.1" = [ "my.super.host" "my.other.host" ];
};
test = ''
echo checking /etc/hosts file >&2
file=${config.out}/etc/hosts
grep '127.0.0.1' $file
grep '10.0.0.1 my.super.host my.other.host' $file
'';
}