2025-12-07 01:38:13 -06:00
|
|
|
{
|
|
|
|
|
flake,
|
|
|
|
|
...
|
|
|
|
|
}:
|
|
|
|
|
let
|
|
|
|
|
inherit (flake.config.people) user0;
|
|
|
|
|
serviceCfg = {
|
2025-12-08 03:32:04 -06:00
|
|
|
name = "minecraft";
|
2025-12-07 01:38:13 -06:00
|
|
|
};
|
|
|
|
|
in
|
|
|
|
|
{
|
2025-12-08 03:32:04 -06:00
|
|
|
minecraftVM =
|
2025-12-07 01:38:13 -06:00
|
|
|
{
|
|
|
|
|
user,
|
|
|
|
|
ip,
|
|
|
|
|
mac,
|
|
|
|
|
userMac,
|
|
|
|
|
ssh,
|
2025-12-08 03:32:04 -06:00
|
|
|
port,
|
|
|
|
|
mnt,
|
|
|
|
|
config,
|
|
|
|
|
whitelist,
|
|
|
|
|
worldNumber,
|
2025-12-07 01:38:13 -06:00
|
|
|
}:
|
|
|
|
|
{
|
|
|
|
|
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 = {
|
2025-12-08 03:32:04 -06:00
|
|
|
minecraft-server = {
|
2025-12-07 01:38:13 -06:00
|
|
|
enable = true;
|
2025-12-08 03:32:04 -06:00
|
|
|
eula = true;
|
|
|
|
|
openFirewall = true;
|
|
|
|
|
declarative = true;
|
|
|
|
|
serverProperties = {
|
2025-12-08 22:25:13 -06:00
|
|
|
"rcon.password" = "/etc/${serviceCfg.name}-secrets/${user}-world${worldNumber}";
|
2025-12-08 03:32:04 -06:00
|
|
|
server-port = port;
|
|
|
|
|
}
|
|
|
|
|
// config;
|
|
|
|
|
whitelist = whitelist;
|
2025-12-07 01:38:13 -06:00
|
|
|
};
|
|
|
|
|
openssh = {
|
|
|
|
|
enable = true;
|
|
|
|
|
settings = {
|
|
|
|
|
PasswordAuthentication = false;
|
|
|
|
|
PermitRootLogin = "prohibit-password";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
networking.firewall.allowedTCPPorts = [
|
|
|
|
|
22
|
2025-12-08 03:32:04 -06:00
|
|
|
port
|
2025-12-07 01:38:13 -06:00
|
|
|
];
|
|
|
|
|
systemd = {
|
|
|
|
|
services = {
|
2025-12-08 03:32:04 -06:00
|
|
|
"${serviceCfg.name}-copy-secrets" = {
|
|
|
|
|
description = "Copy secrets from virtiofs to local filesystem";
|
|
|
|
|
before = [ "minecraft-server.service" ];
|
|
|
|
|
requiredBy = [ "minecraft-server.service" ];
|
|
|
|
|
|
2025-12-07 04:37:17 -06:00
|
|
|
serviceConfig = {
|
2025-12-08 03:32:04 -06:00
|
|
|
Type = "oneshot";
|
|
|
|
|
RemainAfterExit = true;
|
2025-12-07 04:37:17 -06:00
|
|
|
};
|
2025-12-08 03:32:04 -06:00
|
|
|
script = ''
|
|
|
|
|
mkdir -p /etc/${serviceCfg.name}-secrets
|
|
|
|
|
cp /run/secrets/${user}-world${worldNumber} /etc/${serviceCfg.name}-secrets/${user}-world${worldNumber}
|
|
|
|
|
chmod 755 /etc/${serviceCfg.name}-secrets
|
|
|
|
|
chmod 644 /etc/${serviceCfg.name}-secrets/*
|
|
|
|
|
'';
|
2025-12-07 04:37:17 -06:00
|
|
|
};
|
2025-12-08 03:32:04 -06:00
|
|
|
systemd-networkd.wantedBy = [ "multi-user.target" ];
|
2025-12-07 01:38:13 -06:00
|
|
|
};
|
|
|
|
|
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"
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
};
|
2025-12-08 03:32:04 -06:00
|
|
|
tmpfiles.rules = [
|
|
|
|
|
"d /var/lib/${serviceCfg.name} 0755 ${serviceCfg.name} ${serviceCfg.name} -"
|
|
|
|
|
];
|
2025-12-07 01:38:13 -06:00
|
|
|
};
|
|
|
|
|
microvm = {
|
2025-12-08 03:32:04 -06:00
|
|
|
vcpu = 4;
|
|
|
|
|
mem = 1024 * 4;
|
2025-12-07 01:38:13 -06:00
|
|
|
hypervisor = "qemu";
|
|
|
|
|
interfaces = [
|
|
|
|
|
{
|
|
|
|
|
type = "tap";
|
2025-12-08 03:32:04 -06:00
|
|
|
id = "vm-mc${worldNumber}-${user}";
|
2025-12-07 01:38:13 -06:00
|
|
|
mac = mac;
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
type = "user";
|
2025-12-08 03:32:04 -06:00
|
|
|
id = "vmuser-craft";
|
2025-12-07 01:38:13 -06:00
|
|
|
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";
|
2025-12-08 03:32:04 -06:00
|
|
|
source = "${mnt}/${serviceCfg.name}/world${worldNumber}";
|
2025-12-07 01:38:13 -06:00
|
|
|
tag = "${serviceCfg.name}_${user}_data";
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
mountPoint = "/run/secrets";
|
|
|
|
|
proto = "virtiofs";
|
|
|
|
|
source = "/run/secrets/${serviceCfg.name}";
|
|
|
|
|
tag = "host_secrets";
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
2025-12-08 03:32:04 -06:00
|
|
|
networking.firewall.allowedTCPPorts = [ port ];
|
2025-12-07 01:38:13 -06:00
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
2025-12-08 03:32:04 -06:00
|
|
|
"d ${mnt}/${serviceCfg.name} 0751 microvm wheel - -"
|
2025-12-07 01:38:13 -06:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
sops.secrets = {
|
2025-12-08 03:32:04 -06:00
|
|
|
"${serviceCfg.name}/${user}-world${worldNumber}" = {
|
2025-12-07 01:38:13 -06:00
|
|
|
owner = "root";
|
|
|
|
|
mode = "0600";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|