iac/nixos/modules/base.nix
Sergei Poljanski cbf14b4b99
nixos: monitoring as native services
Every host runs node_exporter, cAdvisor (now against Podman), Alloy and
the security-metrics script, on its private address. Each service
declares its own expected ports, users and privileged containers next
to its definition, so the security baseline follows the config instead
of a separate inventory list.

On ops: Prometheus, Loki, Grafana, the blackbox prober and the Postgres
exporter, with the same scrape jobs, probes, retention, dashboards and
33 alert rules. Two rules matched container-era names and now match the
native ones: Forgejo's log is unit forgejo.service, Grafana's route is
grafana@file.

Grafana keeps its default secret_key on production, because its
database was written with it; rotating it is a separate step.

Prometheus, Grafana and Alloy come from nixos-unstable: their data is
from newer versions than 26.05 ships.

LLMNR and mDNS are off: the baseline flagged them as public listeners.

Tried on the rehearsal pair: all 28 scrape targets up, logs from both
hosts in Loki, Grafana WireGuard-only, no unexpected port, user or
container on either host.

Assisted-by: Claude:opus-5-5
2026-11-17 04:48:56 +04:00

155 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
jq
htop
ncdu
tree
sqlite
rclone
restic
];
};
}