| 1 | { config, lib, pkgs, ... }: |
| 2 | let |
| 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 | udpPorts = xmppUDPPorts ++ [ irohNetwork.quic spacebarVoice.media ]; |
| 11 | encryptionRoots = { |
| 12 | clover = "clover"; |
| 13 | media = "clover/Media"; |
| 14 | logs = "logs"; |
| 15 | prod = "prod"; |
| 16 | staging = "staging"; |
| 17 | }; |
| 18 | in |
| 19 | { |
| 20 | imports = [ ./snowglobe-network.nix ./mac-studio-sandbox.nix ./knife-linux.nix ./mac-vm.nix ./vm-disks.nix ./zfs-read-cache.nix ./gpu-unlock.nix ]; |
| 21 | |
| 22 | networking.hostName = "snowglobe"; |
| 23 | environment.etc."machine-info".text = '' |
| 24 | PRETTY_HOSTNAME="snow globe" |
| 25 | ''; |
| 26 | networking.hostId = "4fa19ccb"; |
| 27 | services.nomad.settings.name = config.networking.hostName; |
| 28 | networking.firewall.allowedTCPPorts = [ 80 443 ] ++ xmppTCPPorts; |
| 29 | networking.firewall.allowedUDPPorts = udpPorts; |
| 30 | networking.firewall.allowedUDPPortRanges = [{ from = xmppNetwork.relay_first; to = xmppNetwork.relay_last; }]; |
| 31 | networking.firewall.extraCommands = lib.concatMapStrings ({ address, prefixLength, ... }: '' |
| 32 | iptables -A nixos-fw -i enp4s0 -s ${address}/${toString prefixLength} -p tcp --dport 445 -j nixos-fw-accept |
| 33 | '') config.networking.interfaces.enp4s0.ipv4.addresses + '' |
| 34 | 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 |
| 35 | ip6tables -A nixos-fw -i enp4s0 -s fe80::/10 -p tcp --dport 445 -j nixos-fw-accept |
| 36 | ip6tables -A nixos-fw -i enp4s0 -s fc00::/7 -p tcp --dport 445 -j nixos-fw-accept |
| 37 | ''; |
| 38 | |
| 39 | services.caddy.virtualHosts."${spacebarVoice.subdomain}.${config.environment.variables.SNOWGLOBE_DOMAIN}".extraConfig = '' |
| 40 | reverse_proxy 127.0.0.1:${toString spacebarVoice.signaling} |
| 41 | ''; |
| 42 | |
| 43 | systemd.services.public-ports = { |
| 44 | wantedBy = [ "multi-user.target" ]; |
| 45 | wants = [ "network-online.target" ]; |
| 46 | after = [ "network-online.target" ]; |
| 47 | startAt = "*:0/5"; |
| 48 | serviceConfig = { Type = "oneshot"; DynamicUser = true; }; |
| 49 | script = '' |
| 50 | description="$(${pkgs.python3}/bin/python3 ${./upnp-discover.py} ${lib.escapeShellArg config.networking.defaultGateway.address})" |
| 51 | exec ${pkgs.miniupnpc}/bin/upnpc -u "$description" -e "Snow Globe services" -r \ |
| 52 | ${lib.concatMapStringsSep " " (port: "${toString port} TCP") xmppTCPPorts} \ |
| 53 | ${lib.concatMapStringsSep " " (port: "${toString port} UDP") (udpPorts ++ lib.range xmppNetwork.relay_first xmppNetwork.relay_last)} |
| 54 | ''; |
| 55 | }; |
| 56 | |
| 57 | boot.loader.systemd-boot.enable = true; |
| 58 | boot.loader.efi.canTouchEfiVariables = true; |
| 59 | boot.zfs.forceImportRoot = false; |
| 60 | boot.zfs.extraPools = [ pool "work" ]; |
| 61 | boot.zfs.requestEncryptionCredentials = false; |
| 62 | security.tpm2.enable = true; |
| 63 | |
| 64 | systemd.services.snowglobe-zfs-unlock = { |
| 65 | requires = [ "zfs-import-${pool}.service" "dev-tpmrm0.device" ]; |
| 66 | after = [ "zfs-import-${pool}.service" "dev-tpmrm0.device" ]; |
| 67 | before = [ "zfs-mount.service" ]; |
| 68 | requiredBy = [ "zfs-mount.service" ]; |
| 69 | unitConfig.DefaultDependencies = false; |
| 70 | serviceConfig = { |
| 71 | Type = "oneshot"; |
| 72 | RemainAfterExit = true; |
| 73 | LoadCredentialEncrypted = lib.mapAttrsToList |
| 74 | (name: _: "zfs-${name}:/etc/credstore.encrypted/zfs-${name}") encryptionRoots; |
| 75 | }; |
| 76 | script = lib.concatStrings (lib.mapAttrsToList (name: dataset: '' |
| 77 | if test "$(${pkgs.zfs}/bin/zfs get -H -o value keystatus ${pool}/${dataset})" = unavailable; then |
| 78 | ${pkgs.zfs}/bin/zfs load-key -L "file://$CREDENTIALS_DIRECTORY/zfs-${name}" ${pool}/${dataset} |
| 79 | fi |
| 80 | '') encryptionRoots); |
| 81 | }; |
| 82 | |
| 83 | services.xserver.videoDrivers = [ "nvidia" ]; |
| 84 | hardware.graphics.enable = true; |
| 85 | hardware.nvidia.open = false; |
| 86 | hardware.nvidia.nvidiaPersistenced = true; |
| 87 | hardware.nvidia.nvidiaSettings = false; |
| 88 | |
| 89 | systemd.services.nomad = { |
| 90 | requires = [ "zfs-import-${pool}.service" ]; |
| 91 | wants = [ "mac-studio-sandbox.service" ]; |
| 92 | after = [ "zfs-import-${pool}.service" "zfs-mount.service" "mac-studio-sandbox.service" ]; |
| 93 | preStart = '' |
| 94 | test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv)" = ${pool} |
| 95 | test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv/clover)" = ${pool}/clover |
| 96 | test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv/clover/Media)" = ${pool}/clover/Media |
| 97 | test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv/prod)" = ${pool}/prod |
| 98 | test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv/staging)" = ${pool}/staging |
| 99 | ''; |
| 100 | }; |
| 101 | |
| 102 | users.groups.chloe.gid = 3000; |
| 103 | users.users.root.openssh.authorizedKeys.keys = [ adminKey ]; |
| 104 | users.users.clo = { |
| 105 | isNormalUser = true; |
| 106 | uid = 3000; |
| 107 | group = "chloe"; |
| 108 | extraGroups = [ "wheel" ]; |
| 109 | openssh.authorizedKeys.keys = [ adminKey ]; |
| 110 | }; |
| 111 | environment.variables.SNOWGLOBE_POOL = pool; |
| 112 | environment.variables.SNOWGLOBE_DOMAIN = "paperclover.net"; |
| 113 | environment.variables.SNOWGLOBE_LOCAL_AI = "true"; |
| 114 | |
| 115 | system.stateVersion = "26.05"; |
| 116 | } |