refactor: change to litterate config
Configuration is now held by the `.org` files. All `.nix` files are tangled from the org-mode files.
This commit is contained in:
+21
-39
@@ -1,44 +1,26 @@
|
||||
{
|
||||
flake.modules.nixos.hardened = {
|
||||
lib,
|
||||
config,
|
||||
...
|
||||
}:
|
||||
with lib; let
|
||||
cfg = config.mySystem.boot.kernel;
|
||||
in {
|
||||
options.mySystem.boot.kernel.hardened = mkEnableOption "Enables hardened Linux kernel";
|
||||
|
||||
config.boot = {
|
||||
kernelModules = lists.optional cfg.hardened "tcp_bbr";
|
||||
kernel.sysctl = mkIf cfg.hardened {
|
||||
"kernel.sysrq" = 0; # Disable magic SysRq key
|
||||
# Ignore ICMP broadcasts to avoid participating in Smurf attacks
|
||||
"net.ipv4.icmp_echo_ignore_broadcasts" = 1;
|
||||
# Ignore bad ICMP errors
|
||||
"net.ipv4.icmp_ignore_bogus_error_responses" = 1;
|
||||
# SYN flood protection
|
||||
"net.ipv4.tcp_syncookies" = 1;
|
||||
# Do not accept ICMP redirects (prevent MITM attacks)
|
||||
"net.ipv4.conf.all.accept_redirects" = 0;
|
||||
"net.ipv4.conf.default_accept_redirects" = 0;
|
||||
"net.ipv4.conf.all.secure_redirects" = 0;
|
||||
"net.ipv4.conf.default.secure_redirects" = 0;
|
||||
"net.ipv6.conf.all.accept_redirects" = 0;
|
||||
"net.ipv6.conf.default.accept_redirects" = 0;
|
||||
# Do not send ICMP redirects (we are not a router)
|
||||
"net.ipv4.conf.all.send_redirects" = 0;
|
||||
# Do not accept IP source route packets (we are not a router)
|
||||
"net.ipv4.conf.all.accept_source_route" = 0;
|
||||
"net.ipv6.conf.all.accept_source_route" = 0;
|
||||
# Protect against tcp time-wait assassination hazards
|
||||
"net.ipv4.tcp_rfc1337" = 1;
|
||||
# Latency reduction
|
||||
"net.ipv4.tcp_fastopen" = 3;
|
||||
# Bufferfloat mitigations
|
||||
"net.ipv4.tcp_congestion_control" = "bbr";
|
||||
"net.core.default_qdisc" = "cake";
|
||||
};
|
||||
boot = {
|
||||
kernelModules = ["tcp_bbr"];
|
||||
kernel.sysctl = {
|
||||
"kernel.sysrq" = 0;
|
||||
"net.ipv4.icmp_echo_ignore_broadcasts" = 1;
|
||||
"net.ipv4.icmp_ignore_bogus_error_responses" = 1;
|
||||
"net.ipv4.conf.all.accept_redirects" = 0;
|
||||
"net.ipv4.conf.default_accept_redirects" = 0;
|
||||
"net.ipv4.conf.all.secure_redirects" = 0;
|
||||
"net.ipv4.conf.default.secure_redirects" = 0;
|
||||
"net.ipv6.conf.all.accept_redirects" = 0;
|
||||
"net.ipv6.conf.default.accept_redirects" = 0;
|
||||
"net.ipv4.conf.all.send_redirects" = 0;
|
||||
"net.ipv4.conf.all.accept_source_route" = 0;
|
||||
"net.ipv6.conf.all.accept_source_route" = 0;
|
||||
"net.ipv4.tcp_syncookies" = 1;
|
||||
"net.ipv4.tcp_rfc1337" = 1;
|
||||
"net.ipv4.tcp_congestion_control" = "bbr";
|
||||
"net.core.default_qdisc" = "cake";
|
||||
"net.ipv4.tcp_fastopen" = 3;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
#+title: Kernel Hardening
|
||||
#+setupfile: ../headers
|
||||
|
||||
* Kernel Hardening
|
||||
Some of my machines are exposed to the Internet, and therefore get
|
||||
their kernel hardened. First, let me declare the Nix file’s skeleton,
|
||||
with the =nixos.hardened= module.
|
||||
|
||||
#+begin_src nix :tangle yes
|
||||
{
|
||||
flake.modules.nixos.hardened = {
|
||||
boot = {
|
||||
<<kernel-modules>>
|
||||
<<kernel-options>>
|
||||
};
|
||||
};
|
||||
}
|
||||
#+end_src
|
||||
|
||||
** Kernel Modules
|
||||
The very first thing to do is to load the =tcp_bbr= kernel module. It
|
||||
increases the connection speed of the system with a better congestion
|
||||
control. It is particularly interesting for my servers, as they may
|
||||
have to deal with high traffic if a crawler ever decides to explore
|
||||
all webpages offered by some websites I host. See [[https://www.cyberciti.biz/cloud-computing/increase-your-linux-server-internet-speed-with-tcp-bbr-congestion-control/][this article]] by
|
||||
Nixcraft for more details.
|
||||
#+name: kernel-modules
|
||||
#+begin_src nix
|
||||
kernelModules = ["tcp_bbr"];
|
||||
#+end_src
|
||||
|
||||
** Kernel Options
|
||||
Next are a series of kernel options.
|
||||
#+name: kernel-options
|
||||
#+begin_src nix
|
||||
kernel.sysctl = {
|
||||
<<sysrq-key>>
|
||||
<<icmp>>
|
||||
<<icmp-no-accept-redirects>>
|
||||
<<icmp-no-send-redirects>>
|
||||
<<ip-source-route-packets>>
|
||||
<<syn>>
|
||||
<<tcp-time-wait>>
|
||||
<<bufferfloat>>
|
||||
<<latency>>
|
||||
};
|
||||
#+end_src
|
||||
|
||||
First, we’ll disable the magic SysRq key. Not that I expect anyone to
|
||||
have physical access to my servers, but it is a really powerful tool
|
||||
that I’d rather have off.
|
||||
#+name: sysrq-key
|
||||
#+begin_src nix
|
||||
"kernel.sysrq" = 0;
|
||||
#+end_src
|
||||
|
||||
Next, we’ll ignore ICMP broadcasts to avoid participating in Smurf
|
||||
attacks, and we’ll also ignore ICMP errors.
|
||||
#+name: icmp
|
||||
#+begin_src nix
|
||||
"net.ipv4.icmp_echo_ignore_broadcasts" = 1;
|
||||
"net.ipv4.icmp_ignore_bogus_error_responses" = 1;
|
||||
#+end_src
|
||||
|
||||
Speaking of ICMP, we won’t accept ICMP redirects to prevent some MITM
|
||||
attacks.
|
||||
#+name: icmp-no-accept-redirects
|
||||
#+begin_src nix
|
||||
"net.ipv4.conf.all.accept_redirects" = 0;
|
||||
"net.ipv4.conf.default_accept_redirects" = 0;
|
||||
"net.ipv4.conf.all.secure_redirects" = 0;
|
||||
"net.ipv4.conf.default.secure_redirects" = 0;
|
||||
"net.ipv6.conf.all.accept_redirects" = 0;
|
||||
"net.ipv6.conf.default.accept_redirects" = 0;
|
||||
#+end_src
|
||||
|
||||
And we won’t send ICMP redirects (we’re not a router).
|
||||
#+name: icmp-no-send-redirects
|
||||
#+begin_src nix
|
||||
"net.ipv4.conf.all.send_redirects" = 0;
|
||||
#+end_src
|
||||
|
||||
We’re stil not a router, so we’ll refuse IP source route packets, both
|
||||
on IPV4 and IPV6.
|
||||
#+name: ip-source-route-packets
|
||||
#+begin_src nix
|
||||
"net.ipv4.conf.all.accept_source_route" = 0;
|
||||
"net.ipv6.conf.all.accept_source_route" = 0;
|
||||
#+end_src
|
||||
|
||||
Now, let’s get some SYN flood protection.
|
||||
#+name: syn
|
||||
#+begin_src nix
|
||||
"net.ipv4.tcp_syncookies" = 1;
|
||||
#+end_src
|
||||
|
||||
And protection against TCP time-wait assassination hazards.
|
||||
#+name: tcp-time-wait
|
||||
#+begin_src nix
|
||||
"net.ipv4.tcp_rfc1337" = 1;
|
||||
#+end_src
|
||||
|
||||
We will also mitigate bufferfloat, including with BBR (hey, we enabled that above!)
|
||||
#+name: bufferfloat
|
||||
#+begin_src nix
|
||||
"net.ipv4.tcp_congestion_control" = "bbr";
|
||||
"net.core.default_qdisc" = "cake";
|
||||
#+end_src
|
||||
|
||||
And lastly, we’ll reduce latency on IPV4.
|
||||
#+name: latency
|
||||
#+begin_src nix
|
||||
"net.ipv4.tcp_fastopen" = 3;
|
||||
#+end_src
|
||||
|
||||
And we should be good to go!
|
||||
@@ -0,0 +1,74 @@
|
||||
#+title: Bootloaders and Filesystems
|
||||
#+setupfile: ../headers
|
||||
|
||||
* Bootloaders and Filesystems
|
||||
This page sets up the bootloader of my machines. Whilst I generally
|
||||
prefer to use [[https://systemd.io/BOOT/][systemd-boot]], some of my VPS use GRUB, and some use ZFS.
|
||||
All that gets exposed with my module =nixos.loader=.
|
||||
|
||||
#+begin_src nix :tangle loader.new.nix
|
||||
{
|
||||
flake.modules.nixos.loader = {
|
||||
lib,
|
||||
config,
|
||||
...
|
||||
}:
|
||||
with lib; let
|
||||
cfg = config.mySystem.boot;
|
||||
in {
|
||||
|
||||
};
|
||||
}
|
||||
#+end_src
|
||||
|
||||
** Bootloaders
|
||||
By default, I want to use systemd-boot on my machines, but I need it
|
||||
to be disabled whenever I use something else. For now, this “something
|
||||
else” is only GRUB, but I’m not excluding using something else on yet
|
||||
another machine such as [[https://www.rodsbooks.com/refind/][rEFInd]]. To ensure a single source of truth
|
||||
regarding whether systemd-boot is to be used, I have an option for
|
||||
that.
|
||||
#+name: systemd-boot-options
|
||||
#+begin_src nix
|
||||
options.mySystem.boot.systemd-boot = mkOption {
|
||||
type = types.bool;
|
||||
default = !cfg.grub.enable;
|
||||
description = "Does the system use systemd-boot?";
|
||||
};
|
||||
#+end_src
|
||||
|
||||
I can now set some options depending on that, such as whether to
|
||||
enable systemd-boot itself (duh), and whether the installation process
|
||||
can touch my EFI variables.
|
||||
#+name: systemd-boot-config
|
||||
#+begin_src nix
|
||||
config.boot.loader = {
|
||||
systemd-boot.enable = cfg.systemd-boot;
|
||||
efi.canTouchEfiVariables = cfg.systemd-boot;
|
||||
};
|
||||
#+end_src
|
||||
|
||||
But, I have a VPS that requires me to use GRUB. For this, I also have
|
||||
an option to enable it.
|
||||
#+name: grub-options
|
||||
#+begin_src nix
|
||||
options.mySystem.boot.grub = {
|
||||
enable = mkEnableOption "Does the system use GRUB? (Disables systemd-boot)";
|
||||
device = mkOption {
|
||||
type = types.path;
|
||||
description = "The GRUB device";
|
||||
default = "";
|
||||
};
|
||||
};
|
||||
#+end_src
|
||||
|
||||
I can no pass these options to the boot configuration of my machine.
|
||||
#+name: grub-config
|
||||
#+begin_src nix
|
||||
config.boot.loader.grub = mkIf cfg.grub.enable {
|
||||
inherit (cfg.grub) enable device;
|
||||
};
|
||||
#+end_src
|
||||
|
||||
** ZFS
|
||||
Now, this is where I enable
|
||||
Reference in New Issue
Block a user