gateway.nix (5174B)
1 { 2 config, 3 pkgs, 4 lib, 5 ... 6 }: 7 let 8 svc = config.system.service; 9 cfg = config.profile.gateway; 10 inherit (lib) 11 mkOption 12 mkEnableOption 13 mkIf 14 types 15 ; 16 inherit (pkgs) liminix; 17 inherit (liminix.services) bundle oneshot; 18 hostaps = 19 let 20 defaults = { 21 auth_algs = 1; # 1=wpa2, 2=wep, 3=both 22 wpa = 2; # 1=wpa, 2=wpa2, 3=both 23 wpa_key_mgmt = "WPA-PSK"; 24 wpa_pairwise = "TKIP CCMP"; # auth for wpa (may not need this?) 25 rsn_pairwise = "CCMP"; # auth for wpa2 26 }; 27 in 28 lib.mapAttrs' ( 29 name: value: 30 let 31 attrs = defaults // { ssid = name; } // value; 32 in 33 lib.nameValuePair "hostap-${name}" ( 34 svc.hostapd.build { 35 interface = attrs.interface; 36 params = lib.filterAttrs (k: v: k != "interface") attrs; 37 } 38 ) 39 ) cfg.wireless.networks; 40 in 41 { 42 43 options.profile.gateway = { 44 lan = { 45 interfaces = mkOption { 46 type = types.listOf liminix.lib.types.interface; 47 default = [ ]; 48 }; 49 address = mkOption { 50 type = types.attrs; 51 }; 52 prefix = mkOption { type = types.str; }; 53 dhcp = { 54 start = mkOption { type = types.int; }; 55 end = mkOption { type = types.int; }; 56 hosts = mkOption { type = types.attrs; }; 57 localDomain = mkOption { type = types.str; }; 58 useHostsFile = mkOption { type = types.bool; }; 59 }; 60 }; 61 62 firewall = { 63 enable = mkEnableOption "firewall"; 64 rules = mkOption { type = types.attrsOf types.attrs; }; 65 zones = mkOption { 66 type = types.attrsOf (types.listOf liminix.lib.types.service); 67 default = { 68 lan = [ config.services.int ]; 69 wan = [ config.services.wan ]; 70 }; 71 }; 72 }; 73 74 wan = { 75 interface = mkOption { type = liminix.lib.types.interface; }; 76 dhcp6.enable = mkOption { type = types.bool; }; 77 }; 78 79 wireless = mkOption { 80 type = types.attrsOf types.anything; 81 }; 82 }; 83 84 imports = [ 85 ../wlan.nix 86 ../network 87 ../ppp 88 ../dnsmasq 89 ../dhcp6c 90 ../firewall 91 ../hostapd 92 ../bridge 93 ../ntp 94 ../ssh 95 { config.services = hostaps; } 96 ]; 97 98 config = { 99 services.int = svc.network.address.build ( 100 { 101 interface = svc.bridge.primary.build { ifname = "int"; }; 102 } 103 // cfg.lan.address 104 ); 105 106 services.bridge = svc.bridge.members.build { 107 primary = config.services.int; 108 members = cfg.lan.interfaces; 109 }; 110 111 services.wan = cfg.wan.interface; 112 113 services.packet_forwarding = svc.network.forward.build { }; 114 115 services.dhcp6c = 116 let 117 client = svc.dhcp6c.client.build { 118 interface = config.services.wan; 119 }; 120 bundl = bundle { 121 name = "dhcp6c"; 122 contents = [ 123 (svc.dhcp6c.prefix.build { 124 inherit client; 125 interface = config.services.int; 126 }) 127 (svc.dhcp6c.address.build { 128 inherit client; 129 interface = config.services.wan; 130 }) 131 ]; 132 }; 133 in 134 mkIf cfg.wan.dhcp6.enable bundl; 135 136 services.dns = 137 let 138 interface = config.services.int; 139 dcfg = cfg.lan.dhcp; 140 in 141 svc.dnsmasq.build { 142 resolvconf = config.services.resolvconf; 143 inherit interface; 144 ranges = [ 145 "${cfg.lan.prefix}.${toString dcfg.start},${cfg.lan.prefix}.${toString dcfg.end}" 146 # ra-stateless: sends router advertisements with the O and A 147 # bits set, and provides a stateless DHCP service. The client 148 # will use a SLAAC address, and use DHCP for other 149 # configuration information. 150 "::,constructor:$(output ${interface} ifname),ra-stateless" 151 ]; 152 153 inherit (dcfg) hosts useHostsFile; 154 upstreams = [ "/${dcfg.localDomain}/" ]; 155 domain = dcfg.localDomain; 156 }; 157 158 services.defaultroute4 = svc.network.route.build { 159 via = "$(output ${config.services.wan} address)"; 160 target = "default"; 161 dependencies = [ config.services.wan ]; 162 }; 163 164 services.defaultroute6 = svc.network.route.build { 165 via = "$(output ${config.services.wan} ipv6-peer-address)"; 166 target = "default"; 167 interface = config.services.wan; 168 }; 169 170 services.firewall = mkIf cfg.firewall.enable ( 171 svc.firewall.build { 172 extraRules = cfg.firewall.rules; 173 inherit (cfg.firewall) zones; 174 } 175 ); 176 177 services.resolvconf = oneshot rec { 178 dependencies = [ config.services.wan ]; 179 name = "resolvconf"; 180 up = '' 181 ( in_outputs ${name} 182 chmod 0755 . # dnsmasq user needs this directory to be readable 183 echo "nameserver $(output ${config.services.wan} ns1)" > resolv.conf 184 echo "nameserver $(output ${config.services.wan} ns2)" >> resolv.conf 185 chmod 0444 resolv.conf 186 ) 187 ''; 188 }; 189 190 filesystem = 191 let 192 inherit (pkgs.pseudofile) dir symlink; 193 in 194 dir { 195 etc = dir { 196 "resolv.conf" = symlink "${config.services.resolvconf}/.outputs/resolv.conf"; 197 }; 198 }; 199 }; 200 }