1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2025-03-06 08:47:00 +00:00

Revert "Add networking.hosts and .hostFiles from nixos "

This commit is contained in:
Ihar Hrachyshka 2025-02-26 09:24:15 -05:00 committed by GitHub
parent 42be12b510
commit 1d9f622484
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 2 additions and 109 deletions

View file

@ -47,14 +47,12 @@ jobs:
sudo cp modules/examples/simple.nix /etc/nix-darwin/configuration.nix sudo cp modules/examples/simple.nix /etc/nix-darwin/configuration.nix
nixConfHash=$(shasum -a 256 /etc/nix/nix.conf | cut -d ' ' -f 1) nixConfHash=$(shasum -a 256 /etc/nix/nix.conf | cut -d ' ' -f 1)
etcHostsHash=$(shasum -a 256 /etc/hosts | cut -d ' ' -f 1)
sudo /usr/bin/sed -i.bak \ sudo /usr/bin/sed -i.bak \
"s/# programs.fish.enable = true;/ \ "s/# programs.fish.enable = true;/ \
imports = [ \ imports = [ \
({ options, ... }: { \ ({ options, ... }: { \
nix.settings.access-tokens = [ \"github.com=\${{ secrets.GITHUB_TOKEN }}\" ]; \ nix.settings.access-tokens = [ \"github.com=\${{ secrets.GITHUB_TOKEN }}\" ]; \
environment.etc.\"nix\/nix.conf\".knownSha256Hashes = [ \"$nixConfHash\" ]; \ environment.etc.\"nix\/nix.conf\".knownSha256Hashes = [ \"$nixConfHash\" ]; \
environment.etc.hosts.knownSha256Hashes = [ \"$etcHostsHash\" ]; \
nix.nixPath = \ nix.nixPath = \
[ { darwin = \"${PWD////\/}\"; } ] \ [ { darwin = \"${PWD////\/}\"; } ] \
++ options.nix.nixPath.default; \ ++ options.nix.nixPath.default; \
@ -104,9 +102,8 @@ jobs:
pushd /etc/nix-darwin pushd /etc/nix-darwin
sudo nix flake init -t $darwin sudo nix flake init -t $darwin
nixConfHash=$(shasum -a 256 /etc/nix/nix.conf | cut -d ' ' -f 1) nixConfHash=$(shasum -a 256 /etc/nix/nix.conf | cut -d ' ' -f 1)
etcHostsHash=$(shasum -a 256 /etc/hosts | cut -d ' ' -f 1)
sudo /usr/bin/sed -i.bak \ sudo /usr/bin/sed -i.bak \
"s/# programs.fish.enable = true;/nix.settings.access-tokens = [ \"github.com=\${{ secrets.GITHUB_TOKEN }}\" ]; environment.etc.\"nix\/nix.conf\".knownSha256Hashes = [ \"$nixConfHash\" ]; environment.etc.hosts.knownSha256Hashes = [ \"$etcHostsHash\" ];/" \ "s/# programs.fish.enable = true;/nix.settings.access-tokens = [ \"github.com=\${{ secrets.GITHUB_TOKEN }}\" ]; environment.etc.\"nix\/nix.conf\".knownSha256Hashes = [ \"$nixConfHash\" ];/" \
flake.nix flake.nix
sudo /usr/bin/sed -i.bak \ sudo /usr/bin/sed -i.bak \
's/darwinConfigurations."simple"/darwinConfigurations."'$(scutil --get LocalHostName)'"/g' \ 's/darwinConfigurations."simple"/darwinConfigurations."'$(scutil --get LocalHostName)'"/g' \

View file

@ -1,9 +0,0 @@
##
# 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

View file

@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, ... }:
with lib; with lib;
@ -22,8 +22,6 @@ let
esac esac
'') cfg.knownNetworkServices} '') cfg.knownNetworkServices}
''; '';
localhostMultiple = any (elem "localhost") (attrValues (removeAttrs cfg.hosts [ "127.0.0.1" "::1" ]));
in in
{ {
@ -107,50 +105,9 @@ in
Battery powered devices may require being connected to power. Battery powered devices may require being connected to power.
''; '';
}; };
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 = { 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 = [ warnings = [
(mkIf (cfg.knownNetworkServices == [] && cfg.dns != []) "networking.knownNetworkServices is empty, dns servers will not be configured.") (mkIf (cfg.knownNetworkServices == [] && cfg.dns != []) "networking.knownNetworkServices is empty, dns servers will not be configured.")
@ -177,36 +134,5 @@ in
''} ''}
''; '';
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 = {
knownSha256Hashes = [
# Comes from MacOS: Darwin 24.1.0 Darwin Kernel Version 24.1.0.
"c7dd0e2ed261ce76d76f852596c5b54026b9a894fa481381ffd399b556c0e2da"
];
source = pkgs.concatText "hosts" cfg.hostFiles;
};
}; };
} }

View file

@ -86,7 +86,6 @@ in {
tests.launchd-daemons = makeTest ./tests/launchd-daemons.nix; tests.launchd-daemons = makeTest ./tests/launchd-daemons.nix;
tests.launchd-setenv = makeTest ./tests/launchd-setenv.nix; tests.launchd-setenv = makeTest ./tests/launchd-setenv.nix;
tests.networking-hostname = makeTest ./tests/networking-hostname.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.networking-networkservices = makeTest ./tests/networking-networkservices.nix;
tests.nix-enable = makeTest ./tests/nix-enable.nix; tests.nix-enable = makeTest ./tests/nix-enable.nix;
tests.nixpkgs-overlays = makeTest ./tests/nixpkgs-overlays.nix; tests.nixpkgs-overlays = makeTest ./tests/nixpkgs-overlays.nix;

View file

@ -1,20 +0,0 @@
{ config, pkgs, ... }:
{
networking.hosts = {
"127.0.0.1" = [ "my.super.host" ];
"10.0.0.1" = [ "my.super.host" "my.other.host" ];
};
test = ''
set -v
echo checking /etc/hosts file >&2
file=${config.out}/etc/hosts
grep '127.0.0.1' $file | head -n1 | grep localhost$
grep '127.0.0.1' $file | tail -n1 | grep my.super.host$
grep '::1' $file | grep localhost$
grep '10.0.0.1' $file | grep my.super.host\ my.other.host$
'';
}