dotfiles/os/wireless/default.nix
Geoffrey Frogeye 8c45c1989c
Fix time syncing
Can't believe I was ~5 seconds off on all my computers for this long x)
2024-12-25 15:22:27 +01:00

98 lines
2.7 KiB
Nix

{
pkgs,
lib,
config,
...
}:
let
importScript = pkgs.writers.writePython3 "install-wifi-import" {
libraries = [ pkgs.python3Packages.pyaml ];
} (builtins.readFile ./import.py);
applyScript = pkgs.writers.writePython3 "install-wifi-apply" { } (builtins.readFile ./apply.py);
in
{
environment.systemPackages = [
(pkgs.writeShellApplication {
name = "install-wifi";
runtimeInputs = with pkgs; [
wpa_supplicant
diffutils
];
text = ''
temp="$(mktemp --directory --suffix="-install-wifi")"
cd "$temp"
# Save config for diffing later
wpa_cli save_config > /dev/null
cat <(sudo cat /run/wpa_supplicant/wpa_supplicant.conf) > old.conf
# Export Wi-Fi config from pass
${importScript}
# Save on persistent storage for boot
sudo chown root:root wireless_networks.json
sudo chmod "u=r" wireless_networks.json
sudo mkdir -p /etc/keys
sudo mv -f wireless_networks.json /etc/keys
# Apply configuration
sudo ${applyScript}
# Diff the config
wpa_cli save_config > /dev/null
cat <(sudo cat /run/wpa_supplicant/wpa_supplicant.conf) > new.conf
diff --color=auto -U 5 old.conf new.conf
rm old.conf new.conf
cd /
rmdir "$temp"
'';
# This relies on pass password store with wifi/${name} entries,
# containing wpa_supplicant networks loosely converted to YAML
# (see import.py script)
})
];
# wireless support via wpa_supplicant
networking = {
# Tell the time synchronisation service when we got/lost the connection
dhcpcd.runHook = ''
if $if_up; then
${config.services.chrony.package}/bin/chronyc online
elif $if_down; then
${config.services.chrony.package}/bin/chronyc offline
fi
'';
wireless = {
enable = true;
extraConfig = ''
country=NL
'';
# Public wireless networks
networks = lib.genAttrs [
"EurostarTrainsWiFi"
"_SNCF gare-gratuit"
"_SNCF_WIFI_INOUI"
"Wifi in de trein"
"WiFi in de trein"
"_WIFI_LYRIA"
"WIFIonICE"
] (ssid: { });
userControlled.enable = true; # Allow some control with wpa_cli
};
};
# Wait until there's a connection to fetch time
services.chrony.serverOption = "offline";
systemd.services.wifi_apply = {
after = [ "wpa_supplicant.service" ];
wantedBy = [ "wpa_supplicant.service" ];
path = with pkgs; [ wpa_supplicant ];
script = ''
for i in {1..50}; do wpa_cli status &> /dev/null && break; sleep 0.1; done
${applyScript}
'';
};
}