{ flake, ... }: let inherit (flake.config.people) user0; inherit (flake.config.services.instances) syncthing; serviceCfg = syncthing; in { syncthingVM = { user, ip, mac, userMac, ssh, mnt, folders, devices, tmpIn, mounts, }: { microvm.vms = { "${serviceCfg.name}-${user}" = { autostart = true; restartIfChanged = true; config = { system.stateVersion = "24.05"; time.timeZone = "America/Winnipeg"; users.users.root.openssh.authorizedKeys.keys = flake.config.people.users.${user0}.sshKeys; services = { syncthing = { enable = true; overrideDevices = false; overrideFolders = false; openDefaultPorts = true; systemService = true; guiAddress = "0.0.0.0:${toString serviceCfg.ports.port0}"; guiPasswordFile = "/run/secrets/${user}-pass"; settings = { folders = folders; devices = devices; options = { upAccepted = -1; user = user; authMode = "static"; }; }; }; openssh = { enable = true; settings = { PasswordAuthentication = false; PermitRootLogin = "prohibit-password"; }; }; }; networking.firewall.allowedTCPPorts = [ 22 serviceCfg.ports.port0 serviceCfg.ports.port1 serviceCfg.ports.port2 ]; systemd = { services = { systemd-networkd.wantedBy = [ "multi-user.target" ]; }; network = { enable = true; networks."20-lan" = { matchConfig.Name = "enp0s*"; addresses = [ { Address = "${ip}/24"; } ]; routes = [ { Destination = "0.0.0.0/0"; Gateway = "192.168.50.1"; } ]; dns = [ "1.1.1.1" "8.8.8.8" ]; }; }; tmpfiles.rules = [ "d /var/lib/${serviceCfg.name} 0755 ${serviceCfg.name} ${serviceCfg.name} -" ] ++ tmpIn; }; microvm = { vcpu = 1; mem = 512; hypervisor = "qemu"; interfaces = [ { type = "tap"; id = "vm-st-${user}"; mac = mac; } { type = "user"; id = "vm-sync"; mac = userMac; } ]; forwardPorts = [ { from = "host"; host.port = ssh; guest.port = 22; } ]; shares = [ { mountPoint = "/nix/.ro-store"; proto = "virtiofs"; source = "/nix/store"; tag = "read_only_nix_store"; } { mountPoint = "/var/lib/${serviceCfg.name}"; proto = "virtiofs"; source = "${mnt}/${serviceCfg.name}"; tag = "${serviceCfg.name}_${user}_data"; } { mountPoint = "/run/secrets"; proto = "virtiofs"; source = "/run/secrets/${serviceCfg.name}"; tag = "host_secrets"; } ] ++ mounts; }; }; }; }; systemd.tmpfiles.rules = [ "d ${mnt} 0751 microvm wheel - -" "d ${mnt}/${serviceCfg.name} 0751 microvm wheel - -" ]; sops.secrets = { "${serviceCfg.name}/${user}-pass" = { owner = "root"; mode = "0600"; }; }; }; }