bordervm-configuration.nix (4904B)
1 nixpkgs: 2 { 3 config, 4 pkgs, 5 lib, 6 ... 7 }: 8 let 9 cfg = config.bordervm; 10 inherit (lib) 11 mkOption 12 mkEnableOption 13 mdDoc 14 types 15 optional 16 optionals 17 ; 18 in 19 { 20 options.bordervm = { 21 keys = mkOption { 22 type = types.listOf types.str; 23 default = [ ]; 24 }; 25 l2tp = { 26 host = mkOption { 27 description = mdDoc '' 28 Hostname or IP address of an L2TP LNS that this VM 29 will connect to when it receives a PPPoE connection request 30 ''; 31 type = types.str; 32 example = "l2tp.example.org"; 33 }; 34 port = mkOption { 35 description = mdDoc '' 36 Port number, if non-standard, of the LNS. 37 ''; 38 type = types.int; 39 default = 1701; 40 }; 41 }; 42 ethernet = { 43 pci = { 44 enable = mkEnableOption "passthru PCI ethernet"; 45 id = mkOption { 46 description = '' 47 Host PCI ID (as shown by `lspci`) of the ethernet adaptor 48 to be used by the VM. This uses VFIO and requires setup 49 on the emulation host before it will work! 50 ''; 51 type = types.str; 52 example = "04:00.0"; 53 }; 54 }; 55 usb = { 56 enable = mkEnableOption "passthru USB ethernet"; 57 vendor = mkOption { 58 type = types.str; 59 example = "0x0bda"; 60 }; 61 product = mkOption { 62 type = types.str; 63 example = "0x8153"; 64 }; 65 }; 66 }; 67 }; 68 imports = [ 69 "${nixpkgs}/nixos/modules/virtualisation/qemu-vm.nix" 70 ] ++ (let f = ./local/bordervm-configuration-module.nix; 71 in lib.optional (builtins.pathExists f) f); 72 config = { 73 boot.kernelParams = [ "loglevel=9" ]; 74 systemd.services.pppoe = 75 let 76 conf = pkgs.writeText "kpppoed.toml" '' 77 interface_name = "eth1" 78 services = [ "myservice" ] 79 lns_ipaddr = "${cfg.l2tp.host}:${builtins.toString cfg.l2tp.port}" 80 ac_name = "kpppoed-1.0" 81 ''; 82 in 83 { 84 wantedBy = [ "multi-user.target" ]; 85 after = [ "network-online.target" ]; 86 serviceConfig = { 87 ExecStart = "${pkgs.go-l2tp}/bin/kpppoed -config ${conf}"; 88 }; 89 }; 90 systemd.services.tufted = { 91 wantedBy = [ "multi-user.target" ]; 92 serviceConfig = { 93 ExecStart = "${pkgs.tufted}/bin/tufted /home/liminix/liminix"; 94 }; 95 }; 96 services.openssh.enable = true; 97 services.dnsmasq = { 98 enable = true; 99 resolveLocalQueries = false; 100 settings = { 101 # domain-needed = true; 102 dhcp-range = [ "10.0.0.12,10.0.0.240" ]; 103 interface = "eth1"; 104 }; 105 }; 106 107 services.nginx = { 108 enable = true; 109 user = "liminix"; 110 virtualHosts.${config.networking.hostName} = { 111 root = "/home/liminix"; 112 default = true; 113 }; 114 }; 115 systemd.services.nginx.serviceConfig.ProtectHome = "read-only"; 116 117 systemd.services.sshd.wantedBy = pkgs.lib.mkForce [ "multi-user.target" ]; 118 119 virtualisation = { 120 forwardPorts = [ 121 { 122 host.port = 2222; 123 guest.address = "10.0.2.15"; 124 guest.port = 22; 125 } 126 ]; 127 qemu = { 128 networkingOptions = [ ]; 129 options = 130 [ ] 131 ++ optional cfg.ethernet.pci.enable "-device vfio-pci,host=${cfg.ethernet.pci.id}" 132 ++ optionals cfg.ethernet.usb.enable [ 133 "-device qemu-xhci" 134 # "-device usb-ehci,id=ehci" 135 # "-device usb-host,bus=ehci.0,vendorid=${cfg.ethernet.usb.vendor},productid=${cfg.ethernet.usb.product}" 136 "-device usb-host,vendorid=${cfg.ethernet.usb.vendor},productid=${cfg.ethernet.usb.product}" 137 ] 138 ++ [ 139 "-nographic" 140 "-serial mon:stdio" 141 ]; 142 }; 143 sharedDirectories = { 144 liminix = { 145 source = builtins.toString ./.; 146 target = "/home/liminix/liminix"; 147 }; 148 }; 149 }; 150 151 environment.systemPackages = 152 let 153 wireshark-nogui = pkgs.wireshark.override { withQt = false; }; 154 in 155 with pkgs; 156 [ 157 tcpdump 158 wireshark-nogui 159 socat 160 tufted 161 iptables 162 usbutils 163 busybox 164 ]; 165 security.sudo.wheelNeedsPassword = false; 166 networking = { 167 hostName = "border"; 168 firewall = { 169 enable = false; 170 }; 171 interfaces.eth1 = { 172 useDHCP = false; 173 ipv4.addresses = [ 174 { 175 address = "10.0.0.1"; 176 prefixLength = 24; 177 } 178 ]; 179 }; 180 nat = { 181 enable = true; 182 internalInterfaces = [ "eth1" ]; 183 externalInterface = "eth0"; 184 }; 185 }; 186 users.users.liminix = { 187 isNormalUser = true; 188 uid = 1000; 189 extraGroups = [ "wheel" ]; 190 openssh.authorizedKeys.keys = cfg.keys; 191 }; 192 services.getty.autologinUser = "liminix"; 193 }; 194 }