nixos-config/modules/nixos/profiles/server.nix

150 lines
4.6 KiB
Nix
Raw Normal View History

2022-12-06 07:55:11 +00:00
# All of the settings related to server systems. Take note they cannot be used
2022-12-10 10:47:34 +00:00
# alongside the desktop profile since there are conflicting configurations
# between them.
2022-11-29 13:05:42 +00:00
{ config, options, lib, pkgs, ... }:
let
cfg = config.profiles.server;
in
{
options.profiles.server = {
enable = lib.mkEnableOption "server-related settings";
headless.enable = lib.mkEnableOption "configuration for headless servers";
hardened-config.enable = lib.mkEnableOption "additional hardened configuration for NixOS systems";
cleanup.enable = lib.mkEnableOption "cleanup service for the system";
};
config = lib.mkIf cfg.enable (lib.mkMerge [
({
assertions = [{
assertion =
!config.profiles.desktop.enable || !config.profiles.server.enable;
message = ''
Desktop profile is also enabled. The profiles `desktop` and `server`
are mutually exclusive.
'';
}];
# Most servers will have to be accessed for debugging so it is here. But
# be sure to set the appropriate public keys for the users from that
# server.
services.openssh = lib.mkDefault {
enable = true;
# Both are good for hardening as it only requires the keyfiles.
passwordAuthentication = false;
permitRootLogin = "no";
2023-01-11 05:23:20 +00:00
2023-01-11 05:23:39 +00:00
# Making it verbose for services such as fail2ban.
logLevel = "VERBOSE";
};
2023-01-11 05:23:20 +00:00
# Manage your servers like a Linux-using basement dweller with their
# precious window manager configuration.
programs.tmux = {
enable = true;
baseIndex = 1;
clock24 = true;
customPaneNavigationAndResize = true;
keyMode = "vi";
newSession = true;
secureSocket = true;
2022-11-29 13:05:42 +00:00
};
2022-12-28 06:13:23 +00:00
# It is expected that server configurations should be complete
# service-wise so we're not allowing user database to be mutable.
users.mutableUsers = false;
2022-11-29 13:05:42 +00:00
# Most of the servers will be deployed with outside access in mind so
# generate them certificates. Anything with a private network, ehh... so
# just set it off.
2022-12-06 07:55:11 +00:00
#
# Don't forget to set your certificates or set DNS-related options for
# this.
2022-11-29 13:05:42 +00:00
security.acme = {
acceptTerms = true;
defaults.email = "admin@foodogsquared.one";
};
# We're only going to deal with servers in English.
i18n.defaultLocale = "en_US.UTF-8";
2023-01-11 05:23:39 +00:00
i18n.supportedLocales = lib.mkForce [ "en_US.UTF-8/UTF-8" ];
2022-11-29 13:05:42 +00:00
})
# We're only covering the most basic settings here.
(lib.mkIf cfg.headless.enable {
# So does sounds...
sound.enable = false;
# ...and Bluetooth because it's so insecure.
hardware.bluetooth.enable = false;
# And other devices...
hardware.opentabletdriver.enable = false;
services.printing.enable = false;
})
# Most of the things here are based from the Securing Debian document.
(lib.mkIf cfg.hardened-config.enable {
# Don't replace it mid-way! DON'T TURN LEFT!!!!
security.protectKernelImage = true;
# Hardened config equals hardened kernel.
boot.kernelPackages = pkgs.linuxKernel.packages.linux_6_0_hardened;
# Be STRICT! MUAHAHAHAHA!!!!
services.fail2ban = {
enable = true;
bantime-increment = {
enable = true;
factor = "4";
maxtime = "24h";
2023-01-11 05:23:39 +00:00
overalljails = true;
2022-11-29 13:05:42 +00:00
};
2023-01-11 05:23:20 +00:00
extraPackages = with pkgs; [ ipset ];
2022-11-29 13:05:42 +00:00
};
boot.kernel.sysctl = {
# Disable system console entirely. We don't need it so get rid of it.
"kernel.sysrq" = 0;
};
})
(lib.mkIf cfg.cleanup.enable {
# Weekly garbage collection of Nix store. Unlike in the desktop config,
# this has looser requirements for the store items age for up to 21 days
# older.
nix.gc = {
automatic = true;
persistent = true;
dates = "weekly";
options = "--delete-older-than 21d";
};
# Run the optimizer.
nix.optimise = {
automatic = true;
dates = [ "weekly" ];
};
# Journals cleanup every week.
systemd.services.cleanup-logs = {
description = "Weekly log cleanup";
documentation = [ "man:journalctl(1)" ];
script = "${pkgs.systemd}/bin/journalctl --vacuum-time=30d";
};
systemd.timers.clean-log = {
description = "Weekly log cleanup";
documentation = [ "man:journalctl(1)" ];
wantedBy = [ "multi-user.target" ];
timerConfig = {
OnCalendar = "weekly";
Persistent = true;
};
};
})
]);
}