infra/nix/os/devices/router0-dmz0/configuration.nix

853 lines
26 KiB
Nix
Raw Normal View History

{
repoFlake,
pkgs,
lib,
config,
nodeFlake,
nodeName,
system,
...
}: let
inherit
(nodeFlake.inputs)
bpir3
nixos-nftables-firewall
;
vlanRangeStart = builtins.head vlanRange;
vlanRangeEnd = builtins.elemAt vlanRange ((builtins.length vlanRange)-1);
2023-12-24 20:01:20 +01:00
vlanRange = builtins.map (vlanid: (lib.strings.toInt vlanid)) (builtins.attrNames vlans);
vlanRangeWith0 = [ 0 ] ++ vlanRange;
mkVlanIpv4HostAddr = { vlanid, host, thirdIpv4SegmentMin ? 20, cidr ? true }: let
# reserve the first subnet for vlanid == 0
# number the other subnets continously from there
offset =
if vlanid == 0
then thirdIpv4SegmentMin
else thirdIpv4SegmentMin + 1 - vlanRangeStart;
in
builtins.concatStringsSep "."
[ "192" "168" (toString (vlanid + offset)) "${toString host}${lib.strings.optionalString cidr "/24"}" ];
2023-12-24 20:01:20 +01:00
defaultVlan = {
name = "internal";
packet_priority = 0;
};
2023-12-24 20:01:20 +01:00
vlans = {
"10".name = "mgmt";
"10".packet_priority = 0;
"11".name = "dmz";
"11".packet_priority = -5;
"12".name = "iot";
"12".packet_priority = -5;
"13".name = "office";
"13".packet_priority = -10;
"14".name = "guests";
"14".packet_priority = 10;
"15".name = "iot2";
"15".packet_priority = -10;
2023-12-24 20:01:20 +01:00
};
getVlanDomain = { vlanid }:
if vlanid == 0
then
defaultVlan.name
else
vlans."${toString vlanid}".name + "." + defaultVlan.name
;
2023-12-24 20:01:20 +01:00
in {
imports = [
repoFlake.inputs.sops-nix.nixosModules.sops
../../profiles/common/user.nix
"${bpir3}/lib/sd-image-mt7986.nix"
nixos-nftables-firewall.nixosModules.default
2023-12-24 20:01:20 +01:00
{
nix.nixPath = [
"nixpkgs=${pkgs.path}"
];
nix.settings.experimental-features = [
"nix-command"
"flakes"
];
nix.settings.max-jobs = lib.mkDefault "auto";
nix.settings.cores = lib.mkDefault 0;
2023-12-24 20:01:20 +01:00
}
# TODO
# ./network.nix
# ./monitoring.nix
{
services.openssh.enable = true;
services.openssh.settings.PermitRootLogin = "yes";
users.commonUsers = {
enable = true;
enableNonRoot = false;
rootPasswordFile = config.sops.secrets.passwords-root.path;
};
2023-12-24 20:01:20 +01:00
sops.defaultSopsFile = ../../../../secrets/${nodeName}/secrets.yaml;
sops.defaultSopsFormat = "yaml";
sops.secrets.passwords-root.neededForUsers = true;
sops.secrets.wlan0_saePasswordsFile = { };
sops.secrets.wlan0_wpaPskFile = { };
}
];
# sops.secrets.ssh_host_ed25519_key = {
# sopsFile = ../../../../secrets/${nodeName}/secrets.yaml;
# format = "yaml";
# path = "/etc/ssh/ssh_host_ed25519_key";
# mode = "0600";
# };
# sops.secrets.ssh_host_ed25519_key_pub = {
# sopsFile = ../../../../secrets/${nodeName}/secrets.yaml;
# format = "yaml";
# path = "/etc/ssh/ssh_host_ed25519_key.pub";
# mode = "0600";
# };
# sops.secrets.ssh_host_rsa_key = {
# sopsFile = ../../../../secrets/${nodeName}/secrets.yaml;
# format = "yaml";
# path = "/etc/ssh/ssh_host_rsa_key";
# mode = "0600";
# };
# sops.secrets.ssh_host_rsa_key_pub = {
# sopsFile = ../../../../secrets/${nodeName}/secrets.yaml;
# format = "yaml";
# path = "/etc/ssh/ssh_host_rsa_key.pub";
# mode = "0644";
# };
boot = {
kernel = {
sysctl = {
"net.ipv4.conf.all.forwarding" = true;
"net.ipv6.conf.all.forwarding" = true;
};
};
};
networking = {
hostName = nodeName;
useNetworkd = true;
useDHCP = false;
# these will be configured via nftables
nat.enable = lib.mkForce false;
firewall.enable = lib.mkForce false;
# Use the nftables firewall instead of the base nixos scripted rules.
# This flake provides a similar utility to the base nixos scripting.
# https://github.com/thelegy/nixos-nftables-firewall/tree/main
2023-12-24 20:01:20 +01:00
# TODO: configure packet_priority for VLANs (see https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Base_chain_priority, https://wiki.nftables.org/wiki-nftables/index.php/Setting_packet_metainformation#packet_priority)
nftables = {
enable = true;
stopRuleset = "";
firewall = {
enable = true;
zones = {
lan.interfaces = [ "br-lan" ];
vlan.interfaces = builtins.map (vlanid: "br-lan.${toString vlanid}") vlanRange;
2023-12-24 20:01:20 +01:00
# lan.ipv4Addresses = ["192.168.0.0/16"];
2023-08-22 10:20:16 +02:00
wan.interfaces = ["wan" "lan0"];
} //
# TODO: generate a zone for each vlan
{}
;
2023-12-24 20:01:20 +01:00
rules = let
ipv6IcmpTypes = [
"destination-unreachable" "echo-reply" "echo-request"
"packet-too-big" "parameter-problem" "time-exceeded"
# Without the nd-* ones ipv6 will not work.
"nd-neighbor-solicit" "nd-router-advert" "nd-neighbor-advert"
];
ipv4IcmpTypes = [
"destination-unreachable" "echo-reply" "echo-request" "source-quench" "time-exceeded"
"router-advertisement"
];
allowIcmpLines = [
"ip protocol icmp icmp type { ${builtins.concatStringsSep ", " ipv4IcmpTypes} } accept"
"ip6 nexthdr icmpv6 icmpv6 type { ${builtins.concatStringsSep ", " ipv6IcmpTypes} } accept"
];
in {
fw = {
from = ["fw"];
verdict = "accept";
};
2023-12-24 20:01:20 +01:00
lan-to-fw = {
from = ["lan"];
to = ["fw" "lan"];
verdict = "accept";
};
2023-12-24 20:01:20 +01:00
lan-to-wan = {
from = ["lan"];
2023-12-24 20:01:20 +01:00
to = ["wan"];
verdict = "accept";
};
2023-12-24 20:01:20 +01:00
vlan-to-wan = {
from = ["vlan"];
to = ["wan"];
verdict = "accept";
};
vlan-to-fw = {
allowedUDPPortRanges = [
{ from = 67; to = 68; }
{ from = 53; to = 53; }
];
allowedTCPPortRanges = [
{ from = 22; to = 22; }
{ from = 53; to = 53; }
{ from = 5201; to = 5201; }
2023-12-24 20:01:20 +01:00
];
from = ["vlan"];
to = ["fw"];
extraLines = allowIcmpLines ++ [
"drop"
];
};
to-wan-nat = {
from = ["lan" "vlan"];
to = ["wan"];
masquerade = true;
2023-12-24 20:01:20 +01:00
verdict = "accept";
};
2023-12-24 20:01:20 +01:00
wan-to-fw = {
from = ["wan"];
to = ["fw"];
2023-08-22 10:20:16 +02:00
allowedTCPPortRanges = [
{
from = 22;
to = 22;
}
];
2023-12-24 20:01:20 +01:00
extraLines = allowIcmpLines ++ [
"drop"
];
};
};
};
};
};
systemd.network = {
wait-online.anyInterface = true;
netdevs = {
2023-12-17 23:28:00 +01:00
# Create the bridge interface
"20-br-lan" = {
netdevConfig = {
Kind = "bridge";
Name = "br-lan";
};
2023-12-17 23:28:00 +01:00
extraConfig = ''
[Bridge]
STP=yes
VLANFiltering=yes
VLANProtocol=802.1q
DefaultPVID=0
2023-12-17 23:28:00 +01:00
'';
};
}
# generate the vlan devices. these will be tagged on the main bridge
// builtins.foldl'
(acc: cur: acc // cur)
{}
(builtins.map
({ vlanid, vlanid' }: {
"20-br-lan.${vlanid'}" = {
netdevConfig = {
Kind = "vlan";
Name = "br-lan.${vlanid'}";
};
vlanConfig.Id = vlanid;
};
})
(builtins.map
(vlanid: { inherit vlanid; vlanid' = builtins.toString vlanid; })
vlanRange
)
)
;
networks = {
2023-12-24 20:01:20 +01:00
# use lan0 as secondary WAN interface
"10-lan0-wan" = {
matchConfig.Name = "lan0";
networkConfig = {
# start a DHCP Client for IPv4 Addressing/Routing
DHCP = "ipv4";
# accept Router Advertisements for Stateless IPv6 Autoconfiguraton (SLAAC)
IPv6AcceptRA = true;
DNSOverTLS = true;
DNSSEC = true;
IPv6PrivacyExtensions = false;
IPForward = true;
};
# Don't wait for it as it also would wait for wlan and DFS which takes around 5 min
linkConfig.RequiredForOnline = "no";
};
"10-wan" = {
matchConfig.Name = "wan";
networkConfig = {
# start a DHCP Client for IPv4 Addressing/Routing
DHCP = "ipv4";
# accept Router Advertisements for Stateless IPv6 Autoconfiguraton (SLAAC)
IPv6AcceptRA = true;
DNSOverTLS = true;
DNSSEC = true;
IPv6PrivacyExtensions = false;
IPForward = true;
};
# make routing on this interface a dependency for network-online.target
linkConfig.RequiredForOnline = "routable";
};
# Connect the bridge ports to the bridge
"30-lan1" = {
matchConfig.Name = "lan1";
networkConfig = {
Bridge = "br-lan";
ConfigureWithoutCarrier = true;
};
linkConfig.RequiredForOnline = "enslaved";
};
"30-lan2" = {
matchConfig.Name = "lan2";
networkConfig = {
Bridge = "br-lan";
ConfigureWithoutCarrier = true;
};
linkConfig.RequiredForOnline = "enslaved";
};
"30-lan3" = {
matchConfig.Name = "lan3";
networkConfig = {
Bridge = "br-lan";
ConfigureWithoutCarrier = true;
};
linkConfig.RequiredForOnline = "enslaved";
bridgeVLANs = [
{
bridgeVLANConfig = {
VLAN = "${toString vlanRangeStart}-${toString vlanRangeEnd}";
};
}
];
};
# Configure the bridge for its desired function
"40-br-lan" = {
matchConfig.Name = "br-lan";
bridgeConfig = {};
address = [
2023-12-24 20:01:20 +01:00
(mkVlanIpv4HostAddr { vlanid = 0; host = 1;})
];
networkConfig = {
ConfigureWithoutCarrier = true;
};
# Don't wait for it as it also would wait for wlan and DFS which takes around 5 min
linkConfig.RequiredForOnline = "no";
linkConfig.ActivationPolicy = "always-up";
bridgeVLANs = [
{
bridgeVLANConfig = {
VLAN = "${toString vlanRangeStart}-${toString vlanRangeEnd}";
};
}
];
2023-12-24 20:01:20 +01:00
vlan = (builtins.map
(vlanid: "br-lan.${builtins.toString vlanid}")
vlanRange
);
};
}
# configuration for the hostapd dynamic interfaces
# TODO: refactor this to configure the following per vlanid
# * netdev type vlan
# * host address for vlan
# * vlan config for wlan interface
//
builtins.foldl'
(acc: cur: acc // cur)
{}
(builtins.map ({ vlanid, vlanid' }: {
# set an address on the tagged br-lan device.
# this address will be picked up by dnsmasq.
"41-br-lan.${vlanid'}" = {
matchConfig.Name = "br-lan.${vlanid'}";
address = [
(mkVlanIpv4HostAddr { inherit vlanid; host = 1; })
];
networkConfig = {
ConfigureWithoutCarrier = true;
};
linkConfig.RequiredForOnline = "no";
linkConfig.ActivationPolicy = "always-up";
bridgeVLANs = [
{
bridgeVLANConfig = {
# TODO debug this: each vlanid is native to each port
VLAN = vlanid;
};
}
];
};
# configure the wlan interface as a bridge member that
# * only gets traffic for vid 15
# * untags traffic after receiving it
# * tags traffic that comes out of it
"41-wlan0.${vlanid'}" = {
matchConfig.Name = "wlan0.${vlanid'}";
networkConfig = {
Bridge = "br-lan";
ConfigureWithoutCarrier = true;
};
linkConfig.RequiredForOnline = "no";
bridgeVLANs = [
{
bridgeVLANConfig = {
# TODO debug this: each vlanid is native to each port
VLAN = vlanid;
PVID = vlanid;
EgressUntagged = vlanid;
};
}
];
};
"50-br-lan.${vlanid'}" = {
matchConfig.Name = "br-lan.${vlanid'}";
address = [
(mkVlanIpv4HostAddr { inherit vlanid; host = 1; })
];
networkConfig = {
ConfigureWithoutCarrier = true;
};
linkConfig.RequiredForOnline = "no";
bridgeVLANs = [
{
bridgeVLANConfig = {
# TODO debug this: each vlanid is native to each port
VLAN = vlanid;
PVID = vlanid;
};
}
];
};
})
(builtins.map
(vlanid: { inherit vlanid; vlanid' = builtins.toString vlanid; })
vlanRange
))
;
};
# wireless access point
services.hostapd = {
enable = true;
package = nodeFlake.packages.${system}.hostapd_patched;
2023-08-22 10:20:16 +02:00
radios = let
2023-12-24 20:01:20 +01:00
# generated with https://miniwebtool.com/mac-address-generator/
mkBssid = i: "34:56:ce:0f:ed:4${toString i}";
2023-08-22 10:20:16 +02:00
in {
wlan0 = {
band = "2g";
countryCode = "CH";
channel = 0; # ACS
# use 'iw phy#1 info' to determine your VHT capabilities
wifi4 = {
enable = true;
capabilities = ["HT40+" "LDPC" "SHORT-GI-20" "SHORT-GI-40" "TX-STBC" "RX-STBC1" "MAX-AMSDU-7935"];
};
networks = {
wlan0 = let
iface = "wlan0";
in {
2023-12-24 20:01:20 +01:00
ssid = "mlsia";
2023-08-22 10:20:16 +02:00
bssid = mkBssid 0;
# authentication.mode = "wpa3-sae";
authentication.mode = "wpa3-sae-transition";
authentication.wpaPskFile = config.sops.secrets."${iface}_wpaPskFile".path;
authentication.saePasswordsFile = config.sops.secrets."${iface}_saePasswordsFile".path;
settings = {
2023-12-24 20:01:20 +01:00
# bridge = "br-lan";
# wpa_psk_file = config.sops.secrets.wlan0_wpaPskFile.path;
# not yet supported on hostapd 2.10
# sae_password_file = config.sops.secrets.wlan0_saePasswordsFile.path;
logger_stdout_level= lib.mkForce 0;
logger_syslog_level= lib.mkForce 0;
2023-12-17 23:28:00 +01:00
# resources on vlan tagging
# https://wireless.wiki.kernel.org/en/users/Documentation/hostapd#dynamic_vlan_tagging
# https://forum.openwrt.org/t/individual-per-passphrase-wifi-vlans-using-wpa-psk-file-no-radius-required/161696/4
dynamic_vlan = 1;
vlan_no_bridge = 1;
/* not used due to the above vlan_no_bridge setting
2023-12-17 23:28:00 +01:00
vlan_tagged_interface = "br-lan";
2023-12-24 20:01:20 +01:00
vlan_naming = 1;
vlan_bridge = "br-${iface}.";
*/
vlan_file = let
generated = builtins.map (vlanid:
"${builtins.toString vlanid} ${iface}.${builtins.toString vlanid}"
) vlanRange
;
wildcard = [
# Optional wildcard entry matching all VLAN IDs. The first # in the interface
# name will be replaced with the VLAN ID. The network interfaces are created
# (and removed) dynamically based on the use.
# see https://w1.fi/cgit/hostap/tree/hostapd/hostapd.vlan
"* ${iface}.#"
];
2023-12-24 20:01:20 +01:00
file = pkgs.writeText "hostapd.vlan"
(builtins.concatStringsSep "\n" (generated ++ wildcard));
filePath = toString file;
in filePath;
wpa_key_mgmt = lib.mkForce (builtins.concatStringsSep " " [
"WPA-PSK"
2023-12-24 20:01:20 +01:00
# TODO: the printer can't connect when this is on
# "WPA-PSK-SHA256"
2023-12-24 20:01:20 +01:00
# unfortunately SAE doesn't support VLAN passwords in the way i'd like to use them
# "SAE"
]);
2023-12-24 20:01:20 +01:00
# IEEE 802.11i (authentication) related configuration
# Encrypt management frames to protect against deauthentication and similar attacks
ieee80211w = 1;
sae_require_mfp = 1;
sae_groups = "19 20 21";
};
};
2023-12-24 20:01:20 +01:00
# wlan0-1 = {
# ssid = "justtestingwifi-wpa3";
# authentication = {
# mode = "wpa3-sae";
# saePasswordsFile = config.sops.secrets.wlan0_1_saePasswordFile.path;
# };
# bssid = mkBssid 1;
# settings = {
# bridge = "br-lan";
# };
# };
# Uncomment when needed otherwise remove
# wlan0-1 = {
# ssid = "koteczkowo3";
# authentication = {
# mode = "none"; # this is overriden by settings
# };
# managementFrameProtection = "optional";
# bssid = "e6:02:43:07:00:00";
# settings = {
# bridge = "br-lan";
# wpa = lib.mkForce 2;
# wpa_key_mgmt = "WPA-PSK";
# wpa_pairwise = "CCMP";
# wpa_psk_file = config.sops.secrets.legacyWifiPassword.path;
# };
# };
};
};
2023-12-17 23:28:00 +01:00
# wlan1 = {
# band = "5g";
# # channels with 160 MHz width in Poland: 36, 52, 100 i 116
# channel = 0; # ACS
# countryCode = "PL";
# # use 'iw phy#1 info' to determine your VHT capabilities
# wifi4 = {
# enable = true;
# capabilities = ["HT40+" "LDPC" "SHORT-GI-20" "SHORT-GI-40" "TX-STBC" "RX-STBC1" "MAX-AMSDU-7935"];
# };
# wifi5 = {
# enable = true;
# operatingChannelWidth = "160";
# capabilities = ["RXLDPC" "SHORT-GI-80" "SHORT-GI-160" "TX-STBC-2BY1" "SU-BEAMFORMER" "SU-BEAMFORMEE" "MU-BEAMFORMER" "MU-BEAMFORMEE" "RX-ANTENNA-PATTERN" "TX-ANTENNA-PATTERN" "RX-STBC-1" "SOUNDING-DIMENSION-4" "BF-ANTENNA-4" "VHT160" "MAX-MPDU-11454" "MAX-A-MPDU-LEN-EXP7"];
# };
# wifi6 = {
# enable = true;
# singleUserBeamformer = true;
# singleUserBeamformee = true;
# multiUserBeamformer = true;
# operatingChannelWidth = "160";
# };
# settings = {
# # these two are mandatory for wifi 5 & 6 to work
# vht_oper_centr_freq_seg0_idx = 50;
# he_oper_centr_freq_seg0_idx = 50;
# # The "tx_queue_data2_burst" parameter in Linux refers to the burst size for
# # transmitting data packets from the second data queue of a network interface.
# # It determines the number of packets that can be sent in a burst.
# # Adjusting this parameter can impact network throughput and latency.
# tx_queue_data2_burst = 2;
# # The "he_bss_color" parameter in Wi-Fi 6 (802.11ax) refers to the BSS Color field in the HE (High Efficiency) MAC header.
# # BSS Color is a mechanism introduced in Wi-Fi 6 to mitigate interference and improve network efficiency in dense deployment scenarios.
# # It allows multiple overlapping Basic Service Sets (BSS) to differentiate and coexist in the same area without causing excessive interference.
# he_bss_color = 63; # was set to 128 by openwrt but range of possible values in 2.10 is 1-63
# # Magic values that were set by openwrt but I didn't bother inspecting every single one
# he_spr_sr_control = 3;
# he_default_pe_duration = 4;
# he_rts_threshold = 1023;
# he_mu_edca_qos_info_param_count = 0;
# he_mu_edca_qos_info_q_ack = 0;
# he_mu_edca_qos_info_queue_request = 0;
# he_mu_edca_qos_info_txop_request = 0;
# # he_mu_edca_ac_be_aci=0; missing in 2.10
# he_mu_edca_ac_be_aifsn = 8;
# he_mu_edca_ac_be_ecwmin = 9;
# he_mu_edca_ac_be_ecwmax = 10;
# he_mu_edca_ac_be_timer = 255;
# he_mu_edca_ac_bk_aifsn = 15;
# he_mu_edca_ac_bk_aci = 1;
# he_mu_edca_ac_bk_ecwmin = 9;
# he_mu_edca_ac_bk_ecwmax = 10;
# he_mu_edca_ac_bk_timer = 255;
# he_mu_edca_ac_vi_ecwmin = 5;
# he_mu_edca_ac_vi_ecwmax = 7;
# he_mu_edca_ac_vi_aifsn = 5;
# he_mu_edca_ac_vi_aci = 2;
# he_mu_edca_ac_vi_timer = 255;
# he_mu_edca_ac_vo_aifsn = 5;
# he_mu_edca_ac_vo_aci = 3;
# he_mu_edca_ac_vo_ecwmin = 5;
# he_mu_edca_ac_vo_ecwmax = 7;
# he_mu_edca_ac_vo_timer = 255;
# };
# networks = {
# wlan1 = {
# ssid = "koteczkowo5";
# authentication = {
# mode = "wpa3-sae";
# saePasswordsFile = config.sops.secrets.wifiPassword.path; # Use saePasswordsFile if possible.
# };
# bssid = "36:b9:02:21:08:a2";
# settings = {
# bridge = "br-lan";
# };
# };
# };
# };
};
};
services.resolved.enable = false;
2023-12-24 20:01:20 +01:00
services.dnsmasq = let
mkIfName = { vlanid }: if vlanid == 0 then "br-lan" else "br-lan.${toString vlanid}";
2023-12-24 20:01:20 +01:00
in {
enable = true;
settings = {
# sensible behaviours
domain-needed = true;
bogus-priv = true;
no-resolv = true;
2023-12-24 20:01:20 +01:00
dhcp-range = let
mkDhcpRange = { tag, vlanid }: builtins.concatStringsSep "," [
tag
(mkVlanIpv4HostAddr { inherit vlanid; host = 100; cidr = false; })
(mkVlanIpv4HostAddr { inherit vlanid; host = 199; cidr = false; })
"30m"
2023-12-24 20:01:20 +01:00
];
in
builtins.map
(vlanid:
mkDhcpRange { tag = mkIfName {inherit vlanid;}; inherit vlanid; }
)
vlanRangeWith0
2023-12-24 20:01:20 +01:00
;
2023-12-17 23:28:00 +01:00
# interface = "br-lan";
# bind-interfaces = true;
# dhcp-host = "192.168.10.1";
# local domains
2023-12-24 20:01:20 +01:00
# local = "/${getVlanDomain {vlanid = 0;}/";
# domain = getVlanDomain {vlanid = 0;};
expand-hosts = true;
2023-08-22 10:20:16 +02:00
# don't use /etc/hosts as this would advertise ${nodeName} as localhost
no-hosts = true;
2023-12-24 20:01:20 +01:00
# address = "/${nodeName}.lan/${fwLanHostAddr}";
server = [
# upstream DNS servers
"9.9.9.9" "8.8.8.8" "1.1.1.1"
] ++ builtins.map
(vlanid: "/${getVlanDomain {inherit vlanid;}}/")
2023-12-24 20:01:20 +01:00
vlanRangeWith0
;
# TODO: compare this to using `interface-name`
dynamic-host = [
] ++ builtins.map
(vlanid:
builtins.concatStringsSep "," [
"${nodeName}.${getVlanDomain{inherit vlanid;}}" "0.0.0.1" (mkIfName {inherit vlanid;})
]
)
vlanRangeWith0
;
dhcp-option-force = builtins.map
(vlanid: "${mkIfName {inherit vlanid;}},option:domain-search,${getVlanDomain{inherit vlanid;}}")
2023-12-24 20:01:20 +01:00
vlanRangeWith0
;
localise-queries = true;
};
};
# The service irqbalance is useful as it assigns certain IRQ calls to specific CPUs instead of letting the first CPU core to handle everything. This is supposed to increase performance by hitting CPU cache more often.
services.irqbalance.enable = true;
system.stateVersion = "23.05";
boot.kernelPackages = pkgs.linuxPackages_bpir3_latest;
# We exclude a number of modules included in the default list. A non-insignificant amount do
# not apply to embedded hardware like this, so simply skip the defaults.
#
# Custom kernel is required as a lot of MTK components misbehave when built as modules.
# They fail to load properly, leaving the system without working ethernet, they'll oops on
# remove. MTK-DSA parts and PCIe were observed to do this.
boot.initrd.includeDefaultModules = false;
boot.initrd.kernelModules = ["rfkill" "cfg80211" "mt7915e"];
boot.initrd.availableKernelModules = ["nvme"];
boot.kernelParams = ["console=ttyS0,115200"];
hardware.enableRedistributableFirmware = true;
# Wireless hardware exists, regulatory database is essential.
hardware.wirelessRegulatoryDatabase = true;
# Extlinux compatible with custom uboot patches in this repo, which also provide unique
# MAC addresses instead of the non-unique one that gets used by a lot of MTK devices...
boot.loader.grub.enable = false;
boot.loader.generic-extlinux-compatible.enable = true;
# Known to work with u-boot; bz2, lzma, and lz4 should be safe too, need to test.
boot.initrd.compressor = "gzip";
hardware.deviceTree.filter = "mt7986a-bananapi-bpi-r3.dtb";
hardware.deviceTree.overlays = [
{
name = "bpir3-sd-enable";
dtsFile = "${bpir3}/bpir3-dts/mt7986a-bananapi-bpi-r3-sd.dts";
}
{
name = "bpir3-nand-enable";
dtsFile = "${bpir3}/bpir3-dts/mt7986a-bananapi-bpi-r3-nand.dts";
}
{
name = "bpi-r3 wifi training data";
dtsFile = "${bpir3}/bpir3-dts/mt7986a-bananapi-bpi-r3-wirless.dts";
}
{
name = "reset button disable";
dtsFile = "${bpir3}/bpir3-dts/mt7986a-bananapi-bpi-r3-pcie-button.dts";
}
{
name = "mt7986a efuses";
dtsFile = "${bpir3}/bpir3-dts/mt7986a-efuse-device-tree-node.dts";
}
];
boot.initrd.preDeviceCommands = ''
if [ ! -d /sys/bus/pci/devices/0000:01:00.0 ]; then
if [ -d /sys/bus/pci/devices/0000:00:00.0 ]; then
# Remove PCI bridge, then rescan. NVMe init crashes if PCI bridge not removed first
echo 1 > /sys/bus/pci/devices/0000:00:00.0/remove
# Rescan brings PCI root back and brings the NVMe device in.
echo 1 > /sys/bus/pci/rescan
else
info "PCIe bridge missing"
fi
fi
'';
environment.systemPackages = [
pkgs.ethtool
pkgs.neovim
(pkgs.writeShellScriptBin "dbg-ip" ''
echo links:
ip -br -c l
echo
echo addresses:
ip -br -c a
echo
echo vlans:
bridge -c vlan
'')
(pkgs.writeShellScriptBin "dbg-dnsmasq" ''
# get the rendered in-use config
pgrep -a dnsmasq | grep -Eo '[^ ]*conf' | xargs cat | grep -Eo '[^=]*conf' | xargs cat
'')
];
}