Files
nixpkgs/nixos/tests/fail2ban.nix
Zhong Jianxin 752a5ad36d treewide: libressl.nc -> netcat
Quote from NixOS 25.11 release notes:

> `meta.mainProgram` is now used to determine the `NIX_MAIN_PROGRAM` environment variable. This means that changing it can now lead to a package rebuild.

And `netcat` is:

```
 netcat = libressl.nc.overrideAttrs (old: { 
   meta = old.meta // { 
     description = "Utility which reads and writes data across network connections — LibreSSL implementation"; 
     mainProgram = "nc"; 
   }; 
 });
```

`netcat` and `libressl.nc` are 2 different derivations now, use just one
 of them in nixpkgs for consistency.

This also fixes lots of warnings if `virtualisation.libvirtd.enable = true`:

```
pkgs.buildEnv warning: colliding subpath (ignored): `/nix/store/1mcayh9rmxmjcbmm6swkkyr59rjl66vc-libressl-4.2.1-nc/bin/nc' and `/nix/store/x1vmfpisbd494yykmvjkhvh3dplsnjhg-libressl-4.2.1-nc/bin/nc'
...
```
2025-12-13 00:34:51 +08:00

47 lines
1.2 KiB
Nix

{ pkgs, ... }:
{
name = "fail2ban";
nodes.machine = _: {
services.fail2ban = {
enable = true;
bantime-increment.enable = true;
};
services.openssh.enable = true;
networking.nftables.enable = true;
};
nodes.client = _: {
environment.systemPackages = [
pkgs.sshpass
pkgs.netcat
];
};
testScript = ''
start_all()
# Wait for everything to be ready.
machine.wait_for_unit("multi-user.target")
machine.wait_for_unit("fail2ban")
machine.wait_for_unit("sshd")
client.wait_for_unit("multi-user.target")
client_addr = "2001:db8:1::1"
machine_addr = "2001:db8:1::2"
# Verify there is not ban and the port is reachable from the client.
machine.succeed(f"test 0 -eq $(fail2ban-client get sshd banned {client_addr})")
client.succeed(f"nc -w3 -z {machine_addr} 22")
# Cause authentication failure log entries.
for _ in range(2):
client.fail(f"sshpass -p 'wrongpassword' ssh -o StrictHostKeyChecking=no {machine_addr}")
# Verify there is a ban and the port is unreachable from the client.
machine.wait_until_succeeds(f"test 1 -eq $(fail2ban-client get sshd banned {client_addr})")
client.fail(f"nc -w3 -z {machine_addr} 22")
'';
}