mirror of
https://github.com/ryan4yin/nix-config.git
synced 2026-11-30 01:53:57 +01:00
* feat(mihomo): keep ranges that can never be proxied out of TUN The list lives in policy.yaml as tun_exclude_address; sources.yaml may override it. 198.18.0.0/15 stays in TUN: that is the fake-ip range itself. * feat(mihomo): match Valve's content servers by ASN The 20 hand-copied prefixes had drifted from what AS32590 announces. The rule type needs GeoLite2-ASN.mmdb, so geox-url.asn points at the same mirror. * feat(mihomo): render the gateway config from the same sources The hand-kept copy had drifted: a manual sync carried the desktop's secret onto the gateway and dropped external-ui-url. --redact yields a shareable template. * fix(mihomo): open the link DNS takeover only once the core serves it The core opens :1053 after parsing rules, and rule parsing is where missing geodata gets fetched. Flipping at fork time made that fetch SERVFAIL, the config fatal, and the unit unstartable. * fix(justfile): keep local endpoints out of the agent proxy With only HTTP_PROXY set, a request to this host's own llama-swap endpoint went to mihomo. Node matches exact hosts only, so the literals carry it; the CIDRs serve curl and Go. * feat(justfile): seed mihomo geodata into the service state dir So a cold start never needs the download that the DNS takeover made unstartable.
179 lines
6.6 KiB
Nix
179 lines
6.6 KiB
Nix
{
|
||
disko,
|
||
myvars,
|
||
lib,
|
||
pkgs,
|
||
...
|
||
}:
|
||
#############################################################
|
||
#
|
||
# Ai - my main computer, with NixOS + Core Ultra 7 270K Plus + RTX 4090 GPU,
|
||
# 128G DDR5-4800 (mixed 4-DIMM, see MIXED-MEMORY.md), for gaming & daily use.
|
||
#
|
||
#############################################################
|
||
let
|
||
hostName = "ai"; # Define your hostname.
|
||
|
||
inherit (myvars.networking) mainGateway mainGateway6 nameservers;
|
||
inherit (myvars.networking.hostsAddr.${hostName}) iface ipv4 ipv6;
|
||
ipv4WithMask = "${ipv4}/24";
|
||
ipv6WithMask = "${ipv6}/64";
|
||
in
|
||
{
|
||
imports = [
|
||
disko.nixosModules.default
|
||
# disks
|
||
./disko-fs.nix
|
||
./disko-fs-data.nix
|
||
|
||
# Include the results of the hardware scan.
|
||
./hardware-configuration.nix
|
||
./hardware-intel.nix
|
||
./hardware-nvidia.nix
|
||
|
||
./preservation.nix
|
||
./secureboot.nix
|
||
|
||
./restic.nix
|
||
|
||
# others
|
||
./ai
|
||
];
|
||
|
||
# Zram consumes physical memory for compression, which can cause a deadlock and system hang if the model size approaches the physical memory limit.
|
||
# Disable the whole module (zram device + its swappiness=180 sysctl tunings); this host uses a disk swapfile instead.
|
||
modules.zram.enable = false;
|
||
|
||
# zswap: compressed writeback cache in front of the disk swapfile.
|
||
# Keeps swapped cold anon pages compressed in RAM instead of the SSD, which frees more
|
||
# page cache for the ~78GB mmap'd LLM weights (mmap file pages never go through zswap).
|
||
# Safe here: disk swapfile remains the real backing store, no zram deadlock structure.
|
||
# - zstd: CPU is not the bottleneck here (llama.cpp peaks ~30% util); its ~2.5x ratio
|
||
# keeps ~5G more anon bytes in the capped pool than lz4, and ~5us decompress is noise
|
||
# vs the ~100us SSD fault it avoids.
|
||
# - 10% pool (~13G on 128G): the module default 25% (~32G) would compete with the weight
|
||
# cache.
|
||
boot.zswap = {
|
||
enable = true;
|
||
compressor = "zstd";
|
||
maxPoolPercent = 10;
|
||
};
|
||
|
||
services.sunshine.enable = true;
|
||
services.tuned.ppdSettings.main.default = lib.mkForce "performance";
|
||
|
||
modules.btrbk.enable = true;
|
||
|
||
# Permit the desktop user to access Intel accelerator devices for local ASR.
|
||
users.users.${myvars.username}.extraGroups = [ "render" ];
|
||
|
||
# systemd-journal - cap disk usage
|
||
# https://www.freedesktop.org/software/systemd/man/latest/journald.conf.html
|
||
services.journald.settings.Journal = {
|
||
SystemMaxUse = "10G";
|
||
RuntimeMaxUse = "256M";
|
||
};
|
||
|
||
# Monitor SSD/NVMe SMART attributes so a drive going bad is visible before it
|
||
# fails, and so there is data behind the relaxed NVMe power-management
|
||
# settings in hardware-configuration.nix. smartd autodetects devices and
|
||
# reports to the journal (mail notifications stay off without an MTA).
|
||
services.smartd.enable = true;
|
||
|
||
# QEMU/KVM(HostCpuOnly) for local VMs.
|
||
environment.systemPackages = [ pkgs.qemu_kvm ];
|
||
|
||
# Re-evaluate after the Insta360 Link firmware or the kernel's uvcvideo changes.
|
||
powerManagement.resumeCommands = ''
|
||
# Insta360 Link may stay enumerated with a stalled UVC endpoint after S3 resume.
|
||
${pkgs.coreutils}/bin/sleep 1
|
||
${pkgs.usbutils}/bin/usbreset 2e1a:4c01 || true
|
||
'';
|
||
|
||
networking = {
|
||
inherit hostName;
|
||
|
||
# we use networkd instead
|
||
networkmanager.enable = false; # provides nmcli/nmtui for wifi adjustment
|
||
useDHCP = false;
|
||
};
|
||
|
||
networking.useNetworkd = true;
|
||
systemd.network.enable = true;
|
||
|
||
# networkd removes routing policy rules not defined in .network files
|
||
# (ManageForeignRoutingPolicyRules defaults to yes) each time it reconfigures
|
||
# a link. On S3 resume it reconfigures enp130s0, which wiped mihomo's
|
||
# (9000-9010) and Tailscale's rules and briefly broke routing. Keep foreign
|
||
# rules so the VPN/TUN rules survive.
|
||
systemd.network.config.networkConfig.ManageForeignRoutingPolicyRules = false;
|
||
|
||
systemd.network.networks."10-${iface}" = {
|
||
matchConfig.Name = [ iface ];
|
||
networkConfig = {
|
||
Address = [
|
||
ipv4WithMask
|
||
ipv6WithMask
|
||
];
|
||
DNS = nameservers;
|
||
DHCP = "ipv6"; # enable DHCPv6 only, so we can get a GUA.
|
||
IPv6AcceptRA = true; # for Stateless IPv6 Autoconfiguraton (SLAAC)
|
||
LinkLocalAddressing = "ipv6";
|
||
# r8169 drops carrier on S3 resume; without this, networkd tears down the
|
||
# static address/route and mihomo's TUN auto-detect briefly loses its
|
||
# outbound interface, leaving routing broken until mihomo is restarted.
|
||
IgnoreCarrierLoss = "10s";
|
||
};
|
||
routes = [
|
||
{
|
||
Destination = "0.0.0.0/0";
|
||
Gateway = mainGateway;
|
||
}
|
||
{
|
||
Destination = "::/0";
|
||
Gateway = mainGateway6;
|
||
GatewayOnLink = true; # it's a gateway on local link.
|
||
}
|
||
];
|
||
linkConfig.RequiredForOnline = "routable";
|
||
};
|
||
|
||
# Point this link's DNS at mihomo while it runs, and revert it when it stops.
|
||
#
|
||
# Why a lifecycle hook: mihomo's sing-tun stack would call resolvectl itself,
|
||
# but nixpkgs' DynamicUser+hardened unit closes the D-Bus socket and SetLinkDNS
|
||
# needs privileged authorization; without the takeover resolved's upstream
|
||
# queries bypass dns-hijack and leave via the NIC. The '+' prefix escapes that
|
||
# sandbox -- a failed ExecStartPost would kill a healthy service -- and
|
||
# ExecStopPost also runs on crashes, so a dead mihomo leaves the network usable.
|
||
#
|
||
# Why the probe: the core opens :1053 only after parsing the rules, and rule
|
||
# parsing is where missing geodata gets fetched. Flipping at fork time pointed
|
||
# that fetch at a port nobody was listening on -- SERVFAIL, a fatal config
|
||
# error, a unit that could never start.
|
||
systemd.services.mihomo.serviceConfig = {
|
||
ExecStartPost = lib.mkDefault "+${pkgs.writeShellScript "mihomo-dns-takeover" ''
|
||
probe() { (exec 3<>/dev/tcp/127.0.0.1/1053) 2>/dev/null; }
|
||
up=0
|
||
for ((i = 0; i < 100; i++)); do
|
||
if probe; then up=1; break; fi
|
||
sleep 0.1
|
||
done
|
||
if [ "$up" = 0 ]; then
|
||
echo "mihomo never opened 127.0.0.1:1053; leaving ${iface} on its own DNS" >&2
|
||
exit 0
|
||
fi
|
||
exec ${pkgs.systemd}/bin/resolvectl dns ${iface} 127.0.0.1:1053
|
||
''}";
|
||
ExecStopPost = lib.mkDefault "+${pkgs.systemd}/bin/resolvectl revert ${iface}";
|
||
};
|
||
|
||
# This value determines the NixOS release from which the default
|
||
# settings for stateful data, like file locations and database versions
|
||
# on your system were taken. It‘s perfectly fine and recommended to leave
|
||
# this value at the release version of the first install of this system.
|
||
# Before changing this value read the documentation for this option
|
||
# (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
|
||
system.stateVersion = "25.11"; # Did you read the comment?
|
||
}
|