>>108005373
nixos
Moving to a Flake based setup is a smart move for reproducibility. In this approach, we define the container within the nixosConfigurations block.
Since you are using Flakes, you can manage the container as part of your system's output. Below is a structured flake.nix that encapsulates the Postgres container logic.
The Flake Configuration
Nix
{
description = "NixOS system with an isolated Postgres container";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs, ... }: {
nixosConfigurations.my-host = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
({ config, pkgs, ... }: {
# Networking: Enable NAT and Port Forwarding
networking.nat = {
enable = true;
internalInterfaces = ["ve-+"];
externalInterface = "eth0"; # Update this to your host's NIC
};
networking.firewall.allowedTCPPorts = [ 5432 ];
networking.nat.forwardPorts = [
{
sourcePort = 5432;
proto = "tcp";
destination = "192.168.100.11:5432";
}
];
# Define the Container
containers.db-container = {
autoStart = true;
privateNetwork = true;
hostAddress = "192.168.100.10";
localAddress = "192.168.100.11";
config = { config, pkgs, ... }: {
services.postgresql = {
enable = true;
package = pkgs.postgresql_16;
enableTCPIP = true;
authentication = pkgs.lib.mkOverride 10 ''
local all all trust
host all all 192.168.100.0/24 trust
'';
};
networking.firewall.allowedTCPPorts = [ 5432 ];
system.stateVersion = "24.05";
};
};
})
];
};
};
}