Files
nix/modules/hardening/default.nix
T
2026-11-07 21:19:54 +01:00

106 lines
2.6 KiB
Nix

{
inputs,
config,
lib,
...
}:
let
wakeOnLan = lib.any (iface: iface.wakeOnLan.enable) (lib.attrValues config.networking.interfaces);
randomMac = config.host.role == "laptop" && !wakeOnLan;
profiles = {
workstation = {
preset = [
"compatibility"
]
++ lib.optional config.gaming.enable "performance"; # prioritized (order matters) <https://github.com/cynicsketch/nix-mineral#configuring-options>
settings = {
debug.debugfs = true;
etc.harden-bluetooth = false;
};
};
server = {
preset = [
"compatibility"
"maximum" # prioritized (order matters) <https://github.com/cynicsketch/nix-mineral#configuring-options>
];
settings = {
debug.quiet-boot = false;
network = {
ipv6-tempaddr = false;
max-addresses = false;
neighbor-solicitations = false;
router-solicitations = false;
};
system = {
lower-address-mmap = false;
multilib = false;
yama = "admin-only";
};
};
extras = {
kernel.warn-panic = false;
system.secure-chrony = null;
network = {
tcp-window-scaling = true;
};
};
};
};
in
{
imports = [ inputs.nix-mineral.nixosModules.nix-mineral ];
options.hardening = {
enable = lib.mkOption {
default = false;
type = lib.types.bool;
};
profile = lib.mkOption {
default = "workstation";
type = lib.types.enum [
"workstation"
"server"
];
};
};
config = lib.mkIf config.hardening.enable {
boot.blacklistedKernelModules = [
"dccp"
"sctp"
"rds"
"tipc"
];
networking.wireless.iwd.settings.General.AddressRandomization = lib.mkIf randomMac "network";
nix-mineral = lib.mkMerge [
{
enable = true;
filesystems.normal = {
"/tmp".enable = !config.boot.tmp.useTmpfs;
# only needed for FAT
"/boot".options.umask = lib.mkIf (
!lib.elem (config.fileSystems."/boot".fsType or null) [
"vfat"
"msdos"
"exfat"
]
) (lib.mkForce false);
};
settings = {
etc.harden-gitconfig = false;
kernel.cpu-mitigations = "smt-on";
network = {
ip-forwarding = config.virtualisation.docker.enable || config.services.k3s.enable;
random-mac = randomMac;
router-advertisements = "on";
tcp-sack = true;
};
};
}
profiles.${config.hardening.profile}
];
};
}