2024-10-06 17:25:27 +01:00
|
|
|
{
|
|
|
|
inputs,
|
|
|
|
lib,
|
|
|
|
config,
|
2024-10-07 14:19:27 +01:00
|
|
|
configVars,
|
2024-10-06 17:25:27 +01:00
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}: let
|
|
|
|
postgresPasswordPath = lib.optionalString (lib.hasAttr "sops-nix" inputs) config.sops.secrets."software/postgres/postgres/password".path;
|
|
|
|
pubKeys = lib.filesystem.listFilesRecursive ../../users/keys;
|
2024-10-07 14:19:27 +01:00
|
|
|
containerName = "postgres";
|
|
|
|
containerIp = configVars.networking.addresses.postgres.ip;
|
2024-10-12 00:19:24 +01:00
|
|
|
subnetIp = configVars.networking.addresses.subnet.ip;
|
2024-10-07 14:19:27 +01:00
|
|
|
gatewayIp = configVars.networking.addresses.gateway.ip;
|
2024-10-06 17:25:27 +01:00
|
|
|
in {
|
|
|
|
sops.secrets = {
|
|
|
|
"software/postgres/postgres/password" = {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.persistence."/persist" = {
|
|
|
|
hideMounts = true;
|
|
|
|
directories = [
|
2024-10-07 14:19:27 +01:00
|
|
|
"/var/lib/nixos-containers/${containerName}"
|
2024-10-06 17:25:27 +01:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.nat.enable = true;
|
|
|
|
networking.nat.internalInterfaces = ["ve-+"];
|
|
|
|
networking.nat.externalInterface = "br0";
|
|
|
|
|
2024-10-07 14:19:27 +01:00
|
|
|
containers.${containerName} = {
|
2024-10-06 17:25:27 +01:00
|
|
|
autoStart = true;
|
|
|
|
privateNetwork = true;
|
|
|
|
hostBridge = "br0";
|
|
|
|
nixpkgs = pkgs.path;
|
|
|
|
bindMounts = {
|
|
|
|
"/var/lib/postgresql" = {
|
|
|
|
hostPath = "/media/main-ssd/postgresql";
|
|
|
|
isReadOnly = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}: {
|
|
|
|
networking = {
|
2024-10-07 14:19:27 +01:00
|
|
|
defaultGateway = "${gatewayIp}";
|
2024-10-06 17:25:27 +01:00
|
|
|
interfaces.eth0.ipv4.addresses = [
|
|
|
|
{
|
2024-10-07 14:19:27 +01:00
|
|
|
"address" = "${containerIp}";
|
2024-10-06 17:25:27 +01:00
|
|
|
"prefixLength" = 24;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
firewall = {
|
|
|
|
enable = true;
|
|
|
|
allowedTCPPorts = [
|
|
|
|
5432
|
|
|
|
];
|
|
|
|
};
|
|
|
|
useHostResolvConf = lib.mkForce false;
|
|
|
|
};
|
|
|
|
|
|
|
|
services.resolved.enable = true;
|
|
|
|
|
|
|
|
environment.systemPackages = with pkgs; [
|
|
|
|
lsof
|
|
|
|
];
|
|
|
|
|
|
|
|
services.postgresql = {
|
|
|
|
enable = true;
|
|
|
|
enableJIT = true;
|
|
|
|
package = pkgs.postgresql_16;
|
|
|
|
extraPlugins = with pkgs.postgresql_16.pkgs; [ postgis ];
|
2024-10-12 00:19:24 +01:00
|
|
|
enableTCPIP = true;
|
2024-10-06 17:25:27 +01:00
|
|
|
settings = {
|
|
|
|
max_worker_processes = "12";
|
|
|
|
max_parallel_workers = "8";
|
|
|
|
max_parallel_workers_per_gather = "4";
|
|
|
|
max_connections = "100";
|
|
|
|
autovacuum_work_mem = "2GB";
|
|
|
|
shared_buffers = "32GB";
|
|
|
|
work_mem = "0.32GB";
|
|
|
|
maintenance_work_mem = "64MB";
|
|
|
|
};
|
|
|
|
authentication = pkgs.lib.mkOverride 10 ''
|
2024-10-12 00:19:24 +01:00
|
|
|
#type database DBuser origin-address auth-method
|
|
|
|
local all postgres peer
|
|
|
|
host all all ${subnetIp}/24 scram-sha-256
|
|
|
|
local replication all peer
|
|
|
|
host replication all 127.0.0.1/32 scram-sha-256
|
2024-10-06 17:25:27 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.postgresql.postStart = ''
|
|
|
|
$PSQL -tA <<'EOF'
|
|
|
|
DO $$
|
|
|
|
DECLARE password TEXT;
|
|
|
|
BEGIN
|
|
|
|
password := trim(both from replace(pg_read_file('${postgresPasswordPath}'), E'\n', '''));
|
|
|
|
EXECUTE format('ALTER ROLE postgres WITH PASSWORD '''%s''';', password);
|
|
|
|
END $$;
|
|
|
|
EOF
|
|
|
|
'';
|
|
|
|
|
|
|
|
services.openssh = {
|
|
|
|
enable = true;
|
|
|
|
settings.PasswordAuthentication = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users.root = {
|
|
|
|
openssh.authorizedKeys.keys = lib.lists.forEach pubKeys (key: builtins.readFile key);
|
|
|
|
};
|
|
|
|
|
|
|
|
system.stateVersion = "24.05";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|