Files
nixconf/network-base.nix
2026-02-14 08:40:15 +01:00

57 lines
1.8 KiB
Nix

{ config, pkgs, lib, ... }:
let
# Filter interfaces that have a specific speed set
speedInterfaces = lib.filter (i: i.speed != null) config.vars.interfaces;
# Find the interface designated as the "primary" (first one with an IP) for the gateway
# If none have IPs, we just pick the first interface in the list
primaryInterface = lib.findFirst (i: i.ip != null) (lib.head config.vars.interfaces) config.vars.interfaces;
in
{
networking.hostName = config.vars.hostname;
networking.hostId = config.vars.hostid;
# Global DNS settings
networking.nameservers = [ "192.168.178.10" "9.9.9.9" ];
# Default Gateway
networking.defaultGateway = {
address = "192.168.178.1";
interface = primaryInterface.name;
};
# 1. Generate Static IP Configurations
networking.interfaces = lib.listToAttrs (map (iface: {
name = iface.name;
value = {
# If an IP is provided, use it. Otherwise empty list.
ipv4.addresses = if iface.ip != null then [{
address = iface.ip;
prefixLength = iface.prefixLength;
}] else [];
# Logic: If we have a static IP, disable DHCP. If no IP, enable DHCP.
useDHCP = (iface.ip == null);
};
}) config.vars.interfaces);
# 2. Systemd Service for Link Speed
systemd.services.force-interface-speeds = lib.mkIf (speedInterfaces != []) {
description = "Force link speed on configured interfaces";
wantedBy = [ "network-pre.target" ];
before = [ "network-pre.target" ];
serviceConfig = {
Type = "oneshot";
ExecStart = pkgs.writeShellScript "force-interface-speeds" (
lib.concatMapStringsSep "\n" (iface: ''
${pkgs.ethtool}/bin/ethtool -s ${iface.name} speed ${toString iface.speed} duplex full autoneg on
${pkgs.ethtool}/bin/ethtool --set-eee ${iface.name} eee off || true
'') speedInterfaces
);
};
};
}