{ flake, ... }: let inherit (flake.config.people) user0; inherit (flake.config.services) instances; serviceCfg = instances.vaultwarden; smtpCfg = instances.smtp; in { vaultwardenVM = { user, ip, mac, userMac, ssh, host, mnt, }: { 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 = { vaultwarden = { enable = true; dbBackend = "sqlite"; config = { # Domain Configuration DOMAIN = "https://${host}"; # Email Configuration SMTP_AUTH_MECHANISM = "Plain"; SMTP_EMBED_IMAGES = true; SMTP_FROM = smtpCfg.interfaces.interface0.email; SMTP_FROM_NAME = serviceCfg.label; SMTP_HOST = smtpCfg.interfaces.interface0.domain; SMTP_PORT = smtpCfg.ports.port0; SMTP_USERNAME = smtpCfg.interfaces.interface0.email; SMTP_SECURITY = "starttls"; # Security Configuration DISABLE_ADMIN_TOKEN = false; # Event and Backup Management EVENTS_DAYS_RETAIN = 90; # User Features SENDS_ALLOWED = true; SIGNUPS_VERIFY = true; WEB_VAULT_ENABLED = true; # Rocket (Web Server) Settings ROCKET_ADDRESS = "0.0.0.0"; ROCKET_PORT = serviceCfg.ports.port0; }; # Environment file with secrets (mounted from host) environmentFile = "/run/secrets/${user}-env"; }; openssh = { enable = true; settings = { PasswordAuthentication = false; PermitRootLogin = "prohibit-password"; }; }; }; networking.firewall.allowedTCPPorts = [ 22 587 ]; systemd = { services = { systemd-networkd.wantedBy = [ "multi-user.target" ]; }; network = { enable = true; networks."20-lan" = { matchConfig.Name = "enp0s5"; 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} -" ]; }; microvm = { vcpu = 1; mem = 1024; hypervisor = "qemu"; interfaces = [ { type = "tap"; id = "vm-vw-${user}"; mac = mac; } { type = "user"; id = "vmuser-vault"; 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/bitwarden_rs"; proto = "virtiofs"; source = "${mnt}/${serviceCfg.name}"; tag = "${serviceCfg.name}_${user}_data"; } { mountPoint = "/run/secrets"; proto = "virtiofs"; source = "/run/secrets/${serviceCfg.name}"; tag = "host_secrets"; } ]; }; }; }; }; systemd.tmpfiles.rules = [ "d ${mnt}/${serviceCfg.name} 0751 microvm wheel - -" ]; sops.secrets = { "${serviceCfg.name}/${user}-env" = { owner = "root"; mode = "0600"; }; }; }; }