test: vaultwarden microVM

This commit is contained in:
Nick 2025-11-06 22:20:14 -06:00
parent 9c19cdf131
commit 9f6f5cda5e
5 changed files with 219 additions and 234 deletions

View file

@ -244,7 +244,7 @@ in
ceresStorageDriveName = "NAS1"; ceresStorageDriveName = "NAS1";
erisStorageDriveName = "NAS2"; erisStorageDriveName = "NAS2";
ceresIP = "192.168.50.140"; ceresIP = "192.168.50.250";
erisIP = "192.168.50.245"; erisIP = "192.168.50.245";
deimosIP = "192.168.50.176"; deimosIP = "192.168.50.176";
marsIP = "192.168.50.218"; marsIP = "192.168.50.218";

View file

@ -51,7 +51,7 @@ in
# comfyui # comfyui
# filesorter # filesorter
# firefly-iii # firefly-iii
forgejo # forgejo
# glance # glance
# jellyfin # jellyfin
# logrotate # logrotate
@ -64,7 +64,7 @@ in
# prompter # prompter
# sambaCeres # sambaCeres
# searx # searx
# vaultwarden vaultwarden
# website # website
# zookeeper # zookeeper
# wireguard - moved to systems/ceres/config/wireguard.nix # wireguard - moved to systems/ceres/config/wireguard.nix

View file

@ -1,5 +1,8 @@
{ {
flake, flake,
config,
lib,
pkgs,
... ...
}: }:
let let
@ -7,169 +10,88 @@ let
inherit (flake.config.services.instances) vaultwarden smtp web; inherit (flake.config.services.instances) vaultwarden smtp web;
service = vaultwarden; service = vaultwarden;
host = vaultwarden.domains.url0; host = vaultwarden.domains.url0;
secrets = service.secretPaths.path0; vmInterface = service.interface.id;
localhost = web.localhost.address0; vmMac = service.interface.mac;
sshPort = 22; vmIP = service.interface.ip;
vmGateway = service.interface.gate;
acmeCertPath = config.security.acme.certs.${host}.directory;
in in
{ {
microvm = { # Import the microvm host module
vms = { imports = [
vaultwarden = { # Add microvm.nixosModules.host to your flake inputs if not already present
autostart = true; ];
config =
{
config,
pkgs,
lib,
...
}:
{
system.stateVersion = "25.05";
time.timeZone = "America/Winnipeg";
users.users.root = { # Configure the bridge for microVM networking
openssh.authorizedKeys.keys = flake.config.people.users.${user0}.sshKeys; systemd.network = {
}; enable = true;
services = { # Create bridge interface
vaultwarden = { netdevs."10-virbr0" = {
enable = true; netdevConfig = {
environmentFile = config.sops.secrets."${service.name}-env".path; Kind = "bridge";
config = { Name = "virbr0";
# Domain Configuration };
DOMAIN = "https://${host}"; };
# Email Configuration
SMTP_AUTH_MECHANISM = "Plain";
SMTP_EMBED_IMAGES = true;
SMTP_FROM = smtp.email.address0;
SMTP_FROM_NAME = service.label;
SMTP_HOST = smtp.hostname;
SMTP_PORT = smtp.ports.port1;
SMTP_SECURITY = smtp.records.record1;
SMTP_USERNAME = smtp.email.address0;
# 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 = localhost;
ROCKET_PORT = service.ports.port0;
};
};
openssh = { # Configure bridge network
enable = true; networks."10-virbr0" = {
settings = { matchConfig.Name = "virbr0";
PasswordAuthentication = false; addresses = [
PermitRootLogin = "prohibit-password"; { Address = "${vmGateway}/24"; }
}; ];
}; networkConfig = {
}; IPv6AcceptRA = false;
};
};
systemd = { # Attach tap interfaces to bridge
tmpfiles.rules = [ networks."11-microvm" = {
"d ${secrets} 0755 ${service.name} ${service.name} -" matchConfig.Name = "vm-*";
]; networkConfig = {
Bridge = "virbr0";
network = {
enable = true;
networks."10-enp" = {
matchConfig.Name = "enp0s4";
addresses = [ { Address = "${service.interface.ip}/24"; } ];
routes = [
{
Destination = "${localhost}/0";
Gateway = service.interface.gate;
}
];
dns = [ service.interface.gate ];
};
};
};
networking.firewall.allowedTCPPorts = [
sshPort
service.ports.port0
];
microvm = {
vcpu = 2;
mem = 2048;
hypervisor = "qemu";
interfaces = [
{
type = "tap";
id = service.interface.id;
mac = service.interface.mac;
}
{
type = "user";
id = service.interface.idUser;
mac = service.interface.macUser;
}
];
shares = [
{
mountPoint = "/nix/.ro-store";
proto = "virtiofs";
source = "/nix/store";
tag = "read_only_nix_store";
}
{
mountPoint = service.varPaths.path0;
proto = "virtiofs";
source = service.mntPaths.path0;
tag = "${service.name}_data";
}
{
mountPoint = service.secretPaths.path0;
proto = "virtiofs";
source = service.secretPaths.path0;
tag = "${service.name}_secrets";
}
{
mountPoint = service.ssl.path;
proto = "virtiofs";
source = service.ssl.path;
tag = "acme_certs";
}
];
forwardPorts = [
{
from = "host";
host.port = service.interface.ssh;
guest.port = sshPort;
}
];
};
};
}; };
}; };
}; };
services.caddy.virtualHosts."${host}" = { # Enable NAT for microVMs to access internet
extraConfig = '' networking = {
reverse_proxy ${service.interface.ip}:${toString service.ports.port0} { nat = {
header_up X-Real-IP {remote_host} enable = true;
} internalInterfaces = [ "virbr0" ];
tls ${service.ssl.cert} ${service.ssl.key} externalInterface = lib.mkDefault "enp10s0"; # Use your WireGuard interface by default
encode zstd gzip };
''; firewall = {
trustedInterfaces = [ "virbr0" ];
};
nftables = {
enable = true;
ruleset = ''
table inet filter {
chain forward {
iifname "virbr0" oifname "virbr0" accept
}
}
'';
};
}; };
# Persist microVM data with impermanence
environment.persistence."/persist" = {
directories = [
# Keep existing directories...
"/var/lib/microvms"
service.varPaths.path0
];
};
# SOPS secrets configuration (only env file needed)
sops = sops =
let let
sopsPath = secret: { sopsPath = secret: {
path = "${secrets}/${service.name}-${secret}"; path = "${service.secretPaths.path0}/${service.name}-${secret}";
owner = "root"; owner = "root";
mode = "600"; mode = "0600";
}; };
in in
{ {
@ -184,4 +106,151 @@ in
] ]
); );
}; };
# MicroVM configuration
microvm.vms.vaultwarden = {
autostart = true;
config =
{
config,
pkgs,
lib,
...
}:
{
system.stateVersion = "25.05";
time.timeZone = "America/Winnipeg";
# Network configuration
systemd.network = {
enable = true;
networks."20-enp0s5" = {
matchConfig.Name = "enp0s5";
addresses = [ { Address = "${vmIP}/24"; } ];
routes = [
{
Destination = "0.0.0.0/0";
Gateway = vmGateway;
}
];
dns = [
"8.8.8.8"
"8.8.4.4"
];
};
};
networking = {
hostName = "vaultwarden";
firewall = {
enable = true;
allowedTCPPorts = [
22
service.ports.port0
];
};
};
users.users.root = {
openssh.authorizedKeys.keys = flake.config.people.users.${user0}.sshKeys;
};
services = {
openssh = {
enable = true;
settings = {
PasswordAuthentication = false;
PermitRootLogin = "prohibit-password";
};
};
vaultwarden = {
enable = true;
environmentFile = "/run/secrets/${service.name}-env";
config = {
# Domain Configuration
DOMAIN = "https://${host}";
# Email Configuration
SMTP_AUTH_MECHANISM = "Plain";
SMTP_EMBED_IMAGES = true;
SMTP_FROM = smtp.email.address0;
SMTP_FROM_NAME = service.label;
SMTP_HOST = smtp.hostname;
SMTP_PORT = smtp.ports.port1;
SMTP_SECURITY = smtp.records.record1;
SMTP_USERNAME = smtp.email.address0;
# 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 = service.ports.port0;
};
};
};
# MicroVM-specific configuration
microvm = {
vcpu = 2;
mem = 2048;
hypervisor = "qemu";
interfaces = [
{
type = "tap";
id = vmInterface;
mac = vmMac;
}
];
shares = [
{
mountPoint = "/nix/.ro-store";
proto = "virtiofs";
source = "/nix/store";
tag = "ro-store";
}
{
mountPoint = "/var/lib/vaultwarden";
proto = "virtiofs";
source = service.varPaths.path0;
tag = "vaultwarden-data";
}
{
mountPoint = "/run/secrets";
proto = "virtiofs";
source = "/run/secrets";
tag = "secrets";
}
];
};
};
};
# Caddy reverse proxy configuration on host
services.caddy = {
enable = true;
virtualHosts."${host}" = {
extraConfig = ''
reverse_proxy ${vmIP}:${toString service.ports.port0} {
header_up X-Real-IP {remote_host}
}
tls ${acmeCertPath}/fullchain.pem ${acmeCertPath}/key.pem
encode zstd gzip
'';
};
};
# Ensure data directory exists
systemd.tmpfiles.rules = [
"d ${service.varPaths.path0} 0755 root root -"
];
# Ensure caddy can read ACME certs
users.users.caddy.extraGroups = [ "acme" ];
} }

View file

@ -1,66 +0,0 @@
{
lib,
...
}:
{
# Enable systemd-networkd for bridge management
systemd.network = {
enable = true;
netdevs = {
"20-br-vms" = {
netdevConfig = {
Name = "br-vms";
Kind = "bridge";
};
bridgeConfig = {
STP = false;
};
};
};
networks = {
# Connect physical interface to bridge
"30-enp10s0" = {
matchConfig.Name = "enp10s0";
networkConfig = {
Bridge = "br-vms";
ConfigureWithoutCarrier = true;
KeepConfiguration = "yes";
};
linkConfig = {
RequiredForOnline = false;
};
};
# Configure bridge to get IP from LAN DHCP
"40-br-vms" = {
matchConfig.Name = "br-vms";
networkConfig = {
DHCP = "ipv4";
KeepConfiguration = "yes";
};
linkConfig = {
RequiredForOnline = "routable";
};
};
# VM tap interface to bridge
"50-vm-forgejo" = {
matchConfig.Name = "vm-forgejo";
networkConfig = {
Bridge = "br-vms";
ConfigureWithoutCarrier = true;
};
linkConfig = {
RequiredForOnline = false;
};
};
};
};
# IP forwarding (needed for both bridge networking and WireGuard NAT)
boot.kernel.sysctl = {
"net.ipv4.ip_forward" = 1;
};
}

View file

@ -34,24 +34,6 @@ in
}; };
}; };
# Remote rebuild safeguards:
# These settings prevent network services from restarting during nixos-rebuild,
# which would otherwise drop SSH connections when done remotely.
# The bridge configuration changes enp10s0, so we need to prevent systemd-networkd
# and NetworkManager from restarting to maintain connectivity.
# Prevent SSH connections from being killed during network reconfiguration
systemd.services.sshd = {
stopIfChanged = false;
reloadIfChanged = true;
};
# Prevent systemd-networkd from restarting during switches to avoid dropping SSH
systemd.services.systemd-networkd = {
stopIfChanged = false;
restartTriggers = lib.mkForce [ ];
};
services = { services = {
avahi = { avahi = {
enable = true; enable = true;