Adds support for /etc/hosts on darwin

This commit is contained in:
willemml 2023-11-11 12:34:57 -08:00
parent f36bf9914a
commit 9ce2d4800f
Signed by: willemml
GPG key ID: C3DE5DF6198DACBD
5 changed files with 100 additions and 0 deletions

7
common/hosts/ubc Normal file
View file

@ -0,0 +1,7 @@
gambier.students.cs.ubc.ca gambierubc
pender.students.cs.ubc.ca penderubc
thetis.students.cs.ubc.ca thetisubc
anvil.students.cs.ubc.ca anvilubc
valdes.students.cs.ubc.ca valdesubc
bowen.students.cs.ubc.ca bowenubc
remote.students.cs.ubc.ca ubc

2
common/hosts/zerotier Normal file
View file

@ -0,0 +1,2 @@
10.1.2.16 zeus
10.1.2.175 nixbox

View file

@ -18,6 +18,11 @@
SHELL = "${pkgs.zsh}/bin/zsh";
};
networking.hostFiles = [
./hosts/ubc
./hosts/zerotier
];
nix = {
package = pkgs.nix;
settings.experimental-features = ["nix-command" "flakes" "repl-flake"];

View file

@ -3,6 +3,7 @@
../../common/system.nix
../modules/nix/link-inputs.nix
../modules/nix/use-flake-pkgs.nix
../modules/darwin/hosts.nix
];
nix = {
@ -12,6 +13,9 @@
linux-builder.enable = true;
};
networking.hostName = "zeus";
networking.computerName = "Zeus";
environment.etc."nix/user-sandbox.sb".text = ''
(version 1)
(allow default)

View file

@ -0,0 +1,82 @@
# copied from https://github.com/NixOS/nixpkgs/blob/85f1ba3e51676fa8cc604a3d863d729026a6b8eb/nixos/modules/config/networking.nix
{
config,
lib,
pkgs,
...
}: let
inherit (lib) types mkBefore;
inherit (lib.lists) optional;
inherit (lib.options) literalExpression literalMD;
inherit (lib.attrsets) attrNames filterAttrs;
inherit (lib.strings) concatStringsSep concatMapStrings;
cfg = config.networking;
in {
options = {
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 = lib.mdDoc ''
Locally defined maps of hostnames to IP addresses.
'';
};
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 = lib.mdDoc ''
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 = lib.mdDoc ''
Additional verbatim entries to be appended to {file}`/etc/hosts`.
For adding hosts from derivation results, use {option}`networking.hostFiles` instead.
'';
};
};
config = {
networking.hosts = let
hostnames =
optional (cfg.hostName != "") cfg.hostName; # Then the hostname (without the domain)
in {
"127.0.0.1" = hostnames;
"::1" = hostnames;
};
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! By default "networking.hosts" also contains entries for the
# FQDN so that e.g. "hostname -f" works correctly.
localhostHosts = pkgs.writeText "localhost-hosts" ''
127.0.0.1 localhost
::1 localhost
255.255.255.255 broadcasthost
'';
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;
};
};
}