1{ config, lib, pkgs, ... }:
2let
3 adminKey = lib.fileContents ../config/admin.pub;
4 pool = "globe";
5 xmppNetwork = builtins.fromJSON (builtins.readFile ../service/xmpp/network.json);
6 xmppTCPPorts = map (name: xmppNetwork.${name}) [ "c2s" "c2s_tls" "s2s" "turn" "turn_tls" ];
7 xmppUDPPorts = [ xmppNetwork.turn xmppNetwork.turn_tls ];
8 irohNetwork = builtins.fromJSON (builtins.readFile ../service/iroh-relay/network.json);
9 spacebarVoice = builtins.fromJSON (builtins.readFile ../service/spacebar/voice-network.json);
10 extMantikafasi = builtins.fromJSON (builtins.readFile ../config/ext-mantikafasi.json);
11 udpPorts = xmppUDPPorts ++ [ irohNetwork.quic spacebarVoice.media spacebarVoice.nativeMedia ];
12 encryptionRoots = {
13 clover = "clover";
14 media = "clover/Media";
15 logs = "logs";
16 prod = "prod";
17 staging = "staging";
18 };
19in
20{
21 imports = [ ./snowglobe-network.nix ./mac-studio-sandbox.nix ./knife-linux.nix ./adb.nix ./mac-vm.nix ./ext-mantikafasi.nix ./win11-gaming.nix ./vm-disks.nix ./zfs-read-cache.nix ./gpu-unlock.nix ];
22
23 networking.hostName = "snowglobe";
24 environment.etc."machine-info".text = ''
25 PRETTY_HOSTNAME="snow globe"
26 '';
27 networking.hostId = "4fa19ccb";
28 services.nomad.settings.name = config.networking.hostName;
29 networking.firewall.allowedTCPPorts = [ 80 443 ] ++ xmppTCPPorts;
30 networking.firewall.allowedUDPPorts = udpPorts;
31 networking.firewall.allowedUDPPortRanges = [{ from = xmppNetwork.relay_first; to = xmppNetwork.relay_last; }];
32 networking.firewall.extraCommands = lib.concatMapStrings ({ address, prefixLength, ... }: ''
33 iptables -A nixos-fw -i enp4s0 -s ${address}/${toString prefixLength} -p tcp --dport 445 -j nixos-fw-accept
34 # Fusion Render Node
35 iptables -A nixos-fw -i enp4s0 -s ${address}/${toString prefixLength} -p tcp -m multiport --dports 1144,49152:49159 -j nixos-fw-accept
36 iptables -A nixos-fw -i enp4s0 -s ${address}/${toString prefixLength} -p udp -m multiport --dports 1144,49152:49159 -j nixos-fw-accept
37 '') config.networking.interfaces.enp4s0.ipv4.addresses + ''
38 iptables -A nixos-fw -i enp4s0 -s ${(builtins.fromJSON (builtins.readFile ../config/mac-studio-sandbox.json)).guestAddress}/32 -p tcp --dport 2049 -j nixos-fw-accept
39 ip6tables -A nixos-fw -i enp4s0 -s fe80::/10 -p tcp --dport 445 -j nixos-fw-accept
40 ip6tables -A nixos-fw -i enp4s0 -s fc00::/7 -p tcp --dport 445 -j nixos-fw-accept
41 '' + ''
42 iptables -t mangle -N sandbox-egress 2>/dev/null || iptables -t mangle -F sandbox-egress
43 iptables -t mangle -A sandbox-egress -d ${(builtins.head config.networking.interfaces.enp4s0.ipv4.addresses).address} -p tcp --dport 15432 -j RETURN
44 ${lib.concatMapStringsSep "\n" (range: "iptables -t mangle -A sandbox-egress -d ${range} -j DROP") [ "10.0.0.0/8" "172.16.0.0/12" "192.168.0.0/16" "100.64.0.0/10" "169.254.0.0/16" ]}
45 iptables -t mangle -C PREROUTING -i sandbox0 -m conntrack --ctstate NEW -j sandbox-egress 2>/dev/null || iptables -t mangle -I PREROUTING -i sandbox0 -m conntrack --ctstate NEW -j sandbox-egress
46 '';
47
48 # Containers on `sandbox` reach the public internet and PostgreSQL, nothing else on the host or LAN.
49 environment.etc."cni/net.d/sandbox.conflist".text = builtins.toJSON {
50 cniVersion = "1.0.0";
51 name = "sandbox";
52 plugins = [
53 { type = "loopback"; }
54 { type = "bridge"; bridge = "sandbox0"; isGateway = true; ipMasq = true; ipam = { type = "host-local"; ranges = [ [ { subnet = "10.89.0.0/24"; } ] ]; routes = [ { dst = "0.0.0.0/0"; } ]; }; }
55 { type = "portmap"; capabilities.portMappings = true; snat = true; }
56 ];
57 };
58 services.nomad.settings.client.cni_path = "${pkgs.cni-plugins}/bin";
59 services.nomad.settings.client.cni_config_dir = "/etc/cni/net.d";
60 systemd.services.nomad.restartTriggers = [ config.environment.etc."cni/net.d/sandbox.conflist".source ];
61 networking.firewall.interfaces.sandbox0.allowedTCPPorts = [ 15432 ];
62 networking.firewall.extraStopCommands = ''
63 iptables -t mangle -D PREROUTING -i sandbox0 -m conntrack --ctstate NEW -j sandbox-egress 2>/dev/null || true
64 iptables -t mangle -F sandbox-egress 2>/dev/null || true
65 iptables -t mangle -X sandbox-egress 2>/dev/null || true
66 '';
67
68 services.caddy.virtualHosts."${spacebarVoice.subdomain}.${config.environment.variables.SNOWGLOBE_DOMAIN}".extraConfig = ''
69 reverse_proxy 127.0.0.1:${toString spacebarVoice.signaling}
70 '';
71
72 systemd.services.phoenix-voice-udp-relay = {
73 wantedBy = [ "multi-user.target" ];
74 wants = [ "network-online.target" ];
75 after = [ "network-online.target" "nomad.service" ];
76 serviceConfig = {
77 DynamicUser = true;
78 ExecStart = "${pkgs.nodejs_24}/bin/node ${../tools/phoenix-voice-udp-relay.mjs} ${spacebarVoice.phoenixRelayAddress} ${(builtins.head config.networking.interfaces.enp4s0.ipv4.addresses).address} ${toString spacebarVoice.nativeMedia}";
79 Restart = "always";
80 RestartSec = 5;
81 };
82 };
83
84 systemd.services.public-ports = {
85 wantedBy = [ "multi-user.target" ];
86 wants = [ "network-online.target" ];
87 after = [ "network-online.target" ];
88 startAt = "*:0/5";
89 serviceConfig = { Type = "oneshot"; DynamicUser = true; };
90 script = ''
91 description="$(${pkgs.python3}/bin/python3 ${./upnp-discover.py} ${lib.escapeShellArg config.networking.defaultGateway.address})"
92 exec ${pkgs.miniupnpc}/bin/upnpc -u "$description" -e "Snow Globe services" -r \
93 ${lib.concatMapStringsSep " " (port: "${toString port} TCP") (xmppTCPPorts ++ [ extMantikafasi.sshPort extMantikafasi.vncPort ])} \
94 ${lib.concatMapStringsSep " " (port: "${toString port} UDP") (udpPorts ++ lib.range xmppNetwork.relay_first xmppNetwork.relay_last)}
95 '';
96 };
97
98 boot.loader.systemd-boot.enable = true;
99 boot.loader.efi.canTouchEfiVariables = true;
100 boot.zfs.forceImportRoot = false;
101 boot.zfs.extraPools = [ pool "work" ];
102 boot.zfs.requestEncryptionCredentials = false;
103 security.tpm2.enable = true;
104
105 systemd.services.snowglobe-zfs-unlock = {
106 requires = [ "zfs-import-${pool}.service" "dev-tpmrm0.device" ];
107 after = [ "zfs-import-${pool}.service" "dev-tpmrm0.device" ];
108 before = [ "zfs-mount.service" ];
109 requiredBy = [ "zfs-mount.service" ];
110 unitConfig.DefaultDependencies = false;
111 serviceConfig = {
112 Type = "oneshot";
113 RemainAfterExit = true;
114 LoadCredentialEncrypted = lib.mapAttrsToList
115 (name: _: "zfs-${name}:/etc/credstore.encrypted/zfs-${name}") encryptionRoots;
116 };
117 script = lib.concatStrings (lib.mapAttrsToList (name: dataset: ''
118 if test "$(${pkgs.zfs}/bin/zfs get -H -o value keystatus ${pool}/${dataset})" = unavailable; then
119 ${pkgs.zfs}/bin/zfs load-key -L "file://$CREDENTIALS_DIRECTORY/zfs-${name}" ${pool}/${dataset}
120 fi
121 '') encryptionRoots);
122 };
123
124 services.xserver.videoDrivers = [ "nvidia" ];
125 hardware.graphics.enable = true;
126 hardware.nvidia.open = false;
127 hardware.nvidia.nvidiaPersistenced = true;
128 hardware.nvidia.nvidiaSettings = false;
129
130 systemd.services.nomad = {
131 requires = [ "zfs-import-${pool}.service" ];
132 wants = [ "mac-studio-sandbox.service" ];
133 after = [ "zfs-import-${pool}.service" "zfs-mount.service" "mac-studio-sandbox.service" ];
134 preStart = ''
135 test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv)" = ${pool}
136 test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv/clover)" = ${pool}/clover
137 test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv/clover/Media)" = ${pool}/clover/Media
138 test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv/prod)" = ${pool}/prod
139 test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv/staging)" = ${pool}/staging
140 '';
141 };
142
143 users.groups.chloe.gid = 3000;
144 users.users.root.openssh.authorizedKeys.keys = [ adminKey ];
145 users.users.clo = {
146 isNormalUser = true;
147 uid = 3000;
148 group = "chloe";
149 extraGroups = [ "wheel" ];
150 openssh.authorizedKeys.keys = [ adminKey ];
151 };
152 environment.variables.SNOWGLOBE_POOL = pool;
153 environment.variables.SNOWGLOBE_DOMAIN = "paperclover.net";
154 environment.variables.SNOWGLOBE_LOCAL_AI = "true";
155
156 system.stateVersion = "26.05";
157}