From 9ce2d4800f6f2c3c22fcb333e3a97f1b3c18ab8c Mon Sep 17 00:00:00 2001 From: willemml Date: Sat, 11 Nov 2023 12:34:57 -0800 Subject: [PATCH] Adds support for /etc/hosts on darwin --- common/hosts/ubc | 7 +++ common/hosts/zerotier | 2 + common/system.nix | 5 +++ nixos/hosts/zeus.nix | 4 ++ nixos/modules/darwin/hosts.nix | 82 ++++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 common/hosts/ubc create mode 100644 common/hosts/zerotier create mode 100644 nixos/modules/darwin/hosts.nix diff --git a/common/hosts/ubc b/common/hosts/ubc new file mode 100644 index 0000000..57c743e --- /dev/null +++ b/common/hosts/ubc @@ -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 diff --git a/common/hosts/zerotier b/common/hosts/zerotier new file mode 100644 index 0000000..14f9e13 --- /dev/null +++ b/common/hosts/zerotier @@ -0,0 +1,2 @@ +10.1.2.16 zeus +10.1.2.175 nixbox diff --git a/common/system.nix b/common/system.nix index 4ca885d..53eb865 100644 --- a/common/system.nix +++ b/common/system.nix @@ -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"]; diff --git a/nixos/hosts/zeus.nix b/nixos/hosts/zeus.nix index d592ead..7bbc0cb 100644 --- a/nixos/hosts/zeus.nix +++ b/nixos/hosts/zeus.nix @@ -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) diff --git a/nixos/modules/darwin/hosts.nix b/nixos/modules/darwin/hosts.nix new file mode 100644 index 0000000..f97db48 --- /dev/null +++ b/nixos/modules/darwin/hosts.nix @@ -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; + }; + }; +}