202 lines
6.5 KiB
Nix
202 lines
6.5 KiB
Nix
# roles/thin-client/network/wifi.nix
|
|
#
|
|
# 802.1X EAP-TLS WiFi via NetworkManager + wpa_supplicant backend.
|
|
# Q8 decisions: EAP-TLS, per-machine client cert, AD CS 3-5y lifetime,
|
|
# single Corp SSID.
|
|
#
|
|
# Per-host secrets (via agenix):
|
|
# secrets/<hostname>-wifi-client.pem.age
|
|
# → decrypted to /etc/wifi/client.pem
|
|
# Contains the full PKCS#12 export OR combined PEM (cert + key).
|
|
# Mode 0600, owner root.
|
|
#
|
|
# CA cert is NOT secret — checked into the repo as a public file under
|
|
# roles/thin-client/network/assets/corp-wifi-ca.pem. Replace placeholder
|
|
# with real AD CS root cert.
|
|
{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}: let
|
|
inherit (lib) mkIf mkMerge mkOption types;
|
|
cfg = config.az.tc;
|
|
hostname = config.networking.hostName;
|
|
in {
|
|
options.az.tc.wifi = {
|
|
mode = mkOption {
|
|
type = types.enum ["psk" "eap-tls"];
|
|
default = "eap-tls";
|
|
description = "WiFi authentication mode: temporary WPA-Personal PSK or machine EAP-TLS.";
|
|
};
|
|
|
|
ssid = mkOption {
|
|
type = types.str;
|
|
default = "AZ-CORP"; # TODO: real SSID
|
|
description = "Corporate SSID for thin clients.";
|
|
};
|
|
|
|
caCert = mkOption {
|
|
type = types.path;
|
|
default = ./assets/corp-wifi-ca.pem;
|
|
description = ''
|
|
Corporate WiFi CA certificate (PEM format, not secret — checked
|
|
into the repo). Replace the placeholder with the real AD CS root
|
|
cert.
|
|
'';
|
|
};
|
|
};
|
|
|
|
config = mkIf (cfg.enable && cfg.features.wifi) (mkMerge [
|
|
{
|
|
# Disable iwd explicitly — wpa_supplicant is the backend we use.
|
|
# Note: NetworkManager with `wifi.backend = "wpa_supplicant"` automatically
|
|
# sets networking.wireless.enable = true (it owns wpa_supplicant).
|
|
networking.wireless.iwd.enable = false;
|
|
|
|
networking.networkmanager = {
|
|
enable = true;
|
|
wifi.backend = "wpa_supplicant";
|
|
};
|
|
|
|
systemd.tmpfiles.rules = [
|
|
"d /etc/wifi 0700 root root -"
|
|
"d /run/NetworkManager/system-connections 0700 root root -"
|
|
];
|
|
|
|
# Directory for the cert — NetworkManager reads it as root.
|
|
# Ensure wpa_supplicant doesn't try to use the cert/PSK before
|
|
# agenix decrypted it.
|
|
systemd.services.NetworkManager = {
|
|
after = ["age-identity.service"];
|
|
wants = ["age-identity.service"];
|
|
};
|
|
}
|
|
|
|
(mkIf (cfg.wifi.mode == "eap-tls") {
|
|
# Declarative NetworkManager profile for corp WiFi (EAP-TLS).
|
|
# NixOS has no built-in `ensureProfiles` option in the
|
|
# `networking.networkmanager` namespace, so we write the
|
|
# `.nmconnection` file directly to the system-connections dir.
|
|
# NetworkManager picks it up on restart.
|
|
environment.etc."NetworkManager/system-connections/${cfg.wifi.ssid}.nmconnection" = {
|
|
mode = "0600";
|
|
source = pkgs.writeText "${cfg.wifi.ssid}.nmconnection" ''
|
|
[connection]
|
|
id=${cfg.wifi.ssid}
|
|
type=wifi
|
|
autoconnect=true
|
|
permissions=
|
|
|
|
[wifi]
|
|
mode=infrastructure
|
|
ssid=${cfg.wifi.ssid}
|
|
|
|
[wifi-security]
|
|
key-mgmt=wpa-eap
|
|
|
|
[802-1x]
|
|
eap=tls
|
|
identity=${hostname}$
|
|
ca-cert=${cfg.wifi.caCert}
|
|
client-cert=/etc/wifi/client.pem
|
|
private-key=/etc/wifi/client.pem
|
|
private-key-password=
|
|
phase2-auth=
|
|
|
|
[ipv4]
|
|
method=auto
|
|
|
|
[ipv6]
|
|
method=auto
|
|
'';
|
|
};
|
|
|
|
# `environment.etc` files are owned by root but world-readable by
|
|
# default — make this profile root-only since it references the cert
|
|
# path (the cert itself is root-only via agenix).
|
|
|
|
# NOTE: We intentionally do NOT add the WiFi CA to
|
|
# security.pki.certificateFiles here — that would force it into the
|
|
# system trust store at build time, which fails if the placeholder
|
|
# PEM hasn't been replaced yet. NetworkManager references the CA
|
|
# directly via the `ca-cert=` key in the .nmconnection profile, which
|
|
# is sufficient for 802.1X EAP-TLS. Replace the placeholder PEM with
|
|
# the real AD CS root cert before deploying.
|
|
|
|
# Per-host client cert (via agenix)
|
|
age.secrets."${hostname}-wifi-client-cert" = {
|
|
file = ../../../secrets/${hostname}-wifi-client-cert.age;
|
|
path = "/etc/wifi/client.pem";
|
|
mode = "0600";
|
|
owner = "root";
|
|
group = "root";
|
|
};
|
|
})
|
|
|
|
(mkIf (cfg.wifi.mode == "psk") {
|
|
# Temporary WPA-Personal PSK for pilot bootstrap (deferred EAP-TLS
|
|
# certificate). The keyfile is generated at runtime under /run,
|
|
# root-only, and never enters the Nix store. The PSK is read from
|
|
# the agenix runtime file and is never passed as a command-line
|
|
# argument.
|
|
age.secrets."${hostname}-wifi-psk" = {
|
|
file = ../../../secrets/${hostname}-wifi-psk.age;
|
|
mode = "0400";
|
|
owner = "root";
|
|
group = "root";
|
|
};
|
|
|
|
systemd.services.wifi-psk-provision = {
|
|
description = "Provision the ${cfg.wifi.ssid} WPA-Personal connection";
|
|
wantedBy = ["multi-user.target"];
|
|
after = ["NetworkManager.service" "age-identity.service"];
|
|
wants = ["NetworkManager.service" "age-identity.service"];
|
|
path = [pkgs.coreutils pkgs.networkmanager];
|
|
environment = {
|
|
WIFI_CONNECTION = "az-tc-${cfg.wifi.ssid}";
|
|
WIFI_SSID = cfg.wifi.ssid;
|
|
WIFI_PSK_FILE = config.age.secrets."${hostname}-wifi-psk".path;
|
|
};
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
RemainAfterExit = true;
|
|
};
|
|
script = ''
|
|
set -eu
|
|
test -s "$WIFI_PSK_FILE"
|
|
install -d -m 0700 /run/NetworkManager/system-connections
|
|
profile="/run/NetworkManager/system-connections/$WIFI_CONNECTION.nmconnection"
|
|
umask 077
|
|
{
|
|
printf '%s\n' \
|
|
'[connection]' \
|
|
"id=$WIFI_CONNECTION" \
|
|
'type=wifi' \
|
|
'autoconnect=true' \
|
|
''' \
|
|
'[wifi]' \
|
|
'mode=infrastructure' \
|
|
"ssid=$WIFI_SSID" \
|
|
''' \
|
|
'[wifi-security]' \
|
|
'key-mgmt=wpa-psk'
|
|
printf 'psk='
|
|
cat "$WIFI_PSK_FILE"
|
|
printf '%s\n' \
|
|
''' \
|
|
'[ipv4]' \
|
|
'method=auto' \
|
|
''' \
|
|
'[ipv6]' \
|
|
'method=auto'
|
|
} > "$profile"
|
|
chmod 0600 "$profile"
|
|
nmcli connection reload
|
|
nmcli connection up id "$WIFI_CONNECTION"
|
|
'';
|
|
};
|
|
})
|
|
]);
|
|
}
|