dotfiles/os/wireless/default.nix

79 lines
2.3 KiB
Nix
Raw Permalink Normal View History

2024-02-18 13:38:01 +01:00
{ pkgs, lib, config, ... }:
2024-06-08 15:54:33 +02:00
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
2024-02-18 13:38:01 +01:00
{
environment.systemPackages = [
(pkgs.writeShellApplication {
name = "install-wifi";
2024-06-08 15:54:33 +02:00
runtimeInputs = with pkgs; [ wpa_supplicant diffutils ];
2024-02-18 13:38:01 +01:00
text = ''
temp="$(mktemp --directory --suffix="-install-wifi")"
cd "$temp"
2024-06-08 15:54:33 +02:00
# 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
2024-02-18 13:38:01 +01:00
sudo mkdir -p /etc/keys
2024-06-08 15:54:33 +02:00
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 /
2024-02-18 13:38:01 +01:00
rmdir "$temp"
'';
2024-06-08 15:54:33 +02:00
# This relies on pass password store with wifi/${name} entries,
# containing wpa_supplicant networks loosely converted to YAML
# (see import.py script)
2024-02-18 13:38:01 +01:00
})
];
# wireless support via wpa_supplicant
networking.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
};
services.chrony.serverOption = "offline";
2024-06-08 15:54:33 +02:00
systemd.services.wifi_apply = {
after = [ "wpa_supplicant.service" ];
wantedBy = [ "wpa_supplicant.service" ];
2024-06-08 15:54:33 +02:00
path = with pkgs; [ wpa_supplicant ];
2024-06-17 15:23:25 +02:00
script = ''
for i in {1..50}; do wpa_cli status &> /dev/null && break; sleep 0.1; done
2024-06-17 15:23:25 +02:00
${applyScript}
'';
2024-06-08 15:54:33 +02:00
};
2024-02-18 13:38:01 +01:00
}