iac/nixos/modules/base.nix
Sergei Poljanski be68c5adef
nixos: describe both servers as NixOS systems
Start of the move from Ansible to NixOS. Services run as native NixOS
services where nixpkgs has a module and as Podman containers otherwise;
there is no Docker.

Shared: users with their Debian UIDs, SSH with the Ansible role's crypto
settings, nftables firewall, WireGuard (ops is the hub), sops-nix
secrets per host, the restic backup script with its schedule and
retention, mail on unit failure.

asxpio-services: Xray, SillyTavern, Minecraft (Paper) and the ACME
forwarder natively; XMPP stays in containers because the server is a
custom build.

asxpio-ops: Postgres, Garage and Forgejo natively. Roles, buckets, keys
and grants move here from the storage repo's init scripts.

Services that own data wait for /var/lib/.data-in-place, so an install
never creates empty state where a restore is about to land.
migration/*-data.sh move the Debian-era data into the new layout.

hosts/rehearsal turns the two hosts into a test pair with its own keys
and dummy secrets. Everything here ran there, Postgres, Garage, Forgejo,
Minecraft and SillyTavern on a restored copy of production data. Three
differences from the containers showed up and are handled: Garage
rejects a 0440 credential file, Geyser needs libcrypto and netlink,
SillyTavern ignores the module's listen flags.

Production is untouched: both servers still run Debian under Ansible.

Assisted-by: Claude:fable-5-2
2026-11-16 22:59:59 +04:00

154 lines
4.3 KiB
Nix

# What every host gets: users, SSH, fail2ban, journal cap, Nix settings.
{
config,
lib,
pkgs,
...
}:
let
keys = {
asxpi = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMZ+/5F7SxdTVW47iiStLpGK77oWfR5NgaK4tTSR/aVB me@asxp.io";
zerrtika = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIELE7hTD4sC5raqjSxy2fXyM+q1CwwVuy9KLFgDR6SFl zerrtika@zerrtika-mac";
deploy = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGTDPg3mV55lO8uBnvpcGRxJ2vMRP0qB3Pxhg+C8rwj9 asxpio-deploy";
};
admin = name: uid: key: {
inherit uid;
isNormalUser = true;
group = name;
extraGroups = [
"wheel"
"podman" # the Docker-compatible socket
];
openssh.authorizedKeys.keys = [ key ];
};
in
{
options.asxpio.sshFrom = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [
"10.20.0.0/24" # WireGuard
"10.10.0.0/20" # private network
];
description = "Networks shell users may log in from.";
};
# Units that own data start only once /var/lib/.data-in-place exists,
# so a fresh install never creates empty state where a restore is about
# to land. Create the file after the data is in place.
options.asxpio.waitForData = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "systemd services that must not start before the host has its data.";
};
config = {
time.timeZone = "UTC";
systemd.services = lib.genAttrs config.asxpio.waitForData (_: {
unitConfig.ConditionPathExists = "/var/lib/.data-in-place";
});
# UIDs as on the Debian hosts, so files in /opt and /home keep their owners
users.mutableUsers = false;
users.users = {
root.openssh.authorizedKeys.keys = [ keys.asxpi ];
asxpi = admin "asxpi" 1000 keys.asxpi;
deploy = admin "deploy" 1001 keys.deploy; # CI
zerrtika = admin "zerrtika" 1002 keys.zerrtika;
};
users.groups = {
asxpi.gid = 1000;
deploy.gid = 1001;
zerrtika.gid = 1002;
};
security.sudo.wheelNeedsPassword = false;
services.openssh = {
enable = true;
settings = {
PasswordAuthentication = false;
KbdInteractiveAuthentication = false;
PermitRootLogin = "prohibit-password";
X11Forwarding = false;
MaxAuthTries = 3;
MaxSessions = 5;
ClientAliveInterval = 300;
ClientAliveCountMax = 2;
Ciphers = [
"chacha20-poly1305@openssh.com"
"aes256-gcm@openssh.com"
"aes128-gcm@openssh.com"
"aes256-ctr"
"aes192-ctr"
"aes128-ctr"
];
Macs = [
"hmac-sha2-512-etm@openssh.com"
"hmac-sha2-256-etm@openssh.com"
"hmac-sha2-512"
"hmac-sha2-256"
];
KexAlgorithms = [
"sntrup761x25519-sha512@openssh.com"
"curve25519-sha256"
"curve25519-sha256@libssh.org"
"diffie-hellman-group-exchange-sha256"
"diffie-hellman-group16-sha512"
"diffie-hellman-group18-sha512"
];
# Shell users only from the trusted networks; the Hetzner
# firewall is the outer layer
AllowUsers = lib.concatMap (
net:
map (u: "${u}@${net}") [
"root"
"asxpi"
"deploy"
"zerrtika"
]
) config.asxpio.sshFrom;
};
};
services.fail2ban.enable = true;
# The Hetzner firewall decides what the internet reaches; this one
# opens each service's ports next to its definition. The admin
# networks are trusted, as they were on Debian.
networking.nftables.enable = true;
networking.firewall.extraInputRules = ''
ip saddr { 10.20.0.0/24, 10.10.0.0/20 } accept
ip6 saddr fd69:69:69::/64 accept
'';
sops.age.sshKeyPaths = [ "/etc/ssh/ssh_host_ed25519_key" ];
services.journald.extraConfig = "SystemMaxUse=500M";
nix.settings = {
experimental-features = [
"nix-command"
"flakes"
];
trusted-users = [
"root"
"@wheel" # nixos-rebuild --target-host as deploy
];
};
nix.gc = {
automatic = true;
dates = "weekly";
options = "--delete-older-than 14d";
};
environment.systemPackages = with pkgs; [
curl
git
htop
ncdu
tree
sqlite
rclone
restic
];
};
}