Advertisement
Guest User

Untitled

a guest
Feb 12th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. { config, pkgs, lib, ... }:
  2. let
  3. inherit (lib) mkEnableOption mkOption types mkIf;
  4. cfg = config.networking.ip-gateway;
  5. isIPv6 = str: builtins.match ".*:.*" str == null;
  6. ip4nameservers = builtins.filter
  7. isIPv6
  8. config.networking.nameservers;
  9. in
  10. {
  11.  
  12. options = {
  13. networking.ip-gateway = {
  14. enable = mkEnableOption "IP-Gateway";
  15. external = mkOption {
  16. type = types.str;
  17. description = "The external interface, e.g. a wireless interface";
  18. };
  19. internal = mkOption {
  20. type = types.str;
  21. description = "The internal interface to supply DHCP and RAs to, e.g. a bridge.";
  22. };
  23. };
  24.  
  25. };
  26.  
  27. config = lib.mkIf cfg.enable {
  28.  
  29. boot.kernel.sysctl = {
  30. "net.ipv4.ip_forward" = "1";
  31. "net.ipv6.conf.all.forwarding" = "1";
  32. };
  33.  
  34. services.dhcpd4 =
  35. assert ip4nameservers != [ ];
  36. {
  37. enable = true;
  38. interfaces = [ cfg.internal ];
  39. extraConfig = ''
  40. subnet 192.168.0.0 netmask 255.255.255.0 {
  41. range 192.168.0.100 192.168.0.200;
  42. option subnet-mask 255.255.255.0;
  43. # option broadcast-address 192.168.0.255;
  44. option routers 192.168.0.1;
  45. option domain-name-servers ${builtins.concatStringsSep ", " ip4nameservers};
  46. }
  47. '';
  48. };
  49.  
  50. services.radvd = {
  51. enable = true;
  52. config = ''
  53. interface ${cfg.internal} {
  54. AdvSendAdvert on;
  55. # This special prefix means: Distribute the prefix of the
  56. # non-link-local address on that interface.
  57. prefix ::/64 { };
  58. };
  59. '';
  60. };
  61.  
  62. networking = {
  63. interfaces.${cfg.internal} = {
  64. useDHCP = false;
  65. ipv4.addresses = [
  66. {
  67. address = "192.168.0.1";
  68. prefixLength = 24;
  69. }
  70. ];
  71. };
  72. nat = {
  73. enable = true;
  74. externalInterface = cfg.external;
  75. internalInterfaces = [ cfg.internal ];
  76. };
  77. firewall.interfaces.${cfg.internal}.allowedUDPPorts = [
  78. 67 # DHCP
  79. 68 # DHCP
  80. ];
  81. # request at max a /60 prefix and share cfg.internal a 64 from it
  82. dhcpcd.extraConfig = ''
  83. interface ${cfg.external}
  84. ia_pd 1/::/60 ${cfg.internal}/1/64
  85. # ia_na
  86. '';
  87. };
  88.  
  89. };
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement