Rewrite VPN container

This commit is contained in:
Zuckerberg 2022-05-28 18:54:41 -04:00
parent c560a63182
commit 7e615f814d
13 changed files with 132 additions and 77 deletions

View File

@ -5,10 +5,8 @@ let
in { in {
imports = [ imports = [
./flakes.nix ./flakes.nix
./pia.nix
./zerotier.nix
./auto-update.nix ./auto-update.nix
./hosts.nix ./network
./boot ./boot
./server ./server
./pc ./pc

View File

@ -0,0 +1,10 @@
{ config, lib, ... }:
{
imports = [
./hosts.nix
./pia-openvpn.nix
./vpn.nix
./zerotier.nix
];
}

View File

@ -1,7 +1,7 @@
{ config, lib, ... }: { config, lib, ... }:
let let
system = (import ./ssh.nix).system; system = (import ../ssh.nix).system;
in { in {
networking.hosts = { networking.hosts = {
# some DNS providers filter local ip results from DNS request # some DNS providers filter local ip results from DNS request

View File

@ -108,6 +108,6 @@ in
}; };
}; };
}; };
age.secrets."pia-login.conf".file = ../secrets/pia-login.conf; age.secrets."pia-login.conf".file = ../../secrets/pia-login.conf;
}; };
} }

97
common/network/vpn.nix Normal file
View File

@ -0,0 +1,97 @@
{ config, pkgs, lib, allModules, ... }:
with lib;
let
cfg = config.vpn-container;
in
{
options.vpn-container = {
enable = mkEnableOption "Enable VPN container";
containerName = mkOption {
type = types.str;
default = "vpn";
description = ''
Name of the VPN container.
'';
};
mounts = mkOption {
type = types.listOf types.str;
default = [ "/var/lib" ];
example = "/home/example";
description = ''
List of mounts on the host to bind to the vpn container.
'';
};
config = mkOption {
type = types.anything;
default = {};
example = ''
{
services.nginx.enable = true;
}
'';
description = ''
NixOS config for the vpn container.
'';
};
};
config = mkIf cfg.enable {
containers.${cfg.containerName} = {
ephemeral = true;
autoStart = true;
bindMounts = mkMerge ([{
"/run/agenix" = {
hostPath = "/run/agenix";
isReadOnly = true;
};
}] ++ (lists.forEach cfg.mounts (mount:
{
"${mount}" = {
hostPath = mount;
isReadOnly = false;
};
}
)));
enableTun = true;
privateNetwork = true;
hostAddress = "172.16.100.1";
localAddress = "172.16.100.2";
config = {
imports = allModules ++ [cfg.config];
nixpkgs.pkgs = pkgs;
networking.firewall.enable = mkForce false;
pia.enable = true;
pia.server = "swiss.privacy.network"; # swiss vpn
# run it's own DNS resolver
networking.useHostResolvConf = false;
services.resolved.enable = true;
};
};
# load secrets the container needs
age.secrets = config.containers.${cfg.containerName}.config.age.secrets;
# forwarding for vpn container
networking.nat.enable = true;
networking.nat.internalInterfaces = [
"ve-${cfg.containerName}"
];
boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
# assumes only one potential interface
networking.usePredictableInterfaceNames = false;
networking.nat.externalInterface = "eth0";
};
}

10
flake.lock generated
View File

@ -121,15 +121,15 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1647808006, "lastModified": 1652819416,
"narHash": "sha256-aBlJcylH7/MDiu0RVEiUwV1XufGfVk4OvsFutImCszY=", "narHash": "sha256-OzYSb66kQUVP1FM0E7Z0ij13mm14DkJi79FAMprAavo=",
"owner": "bennofs", "owner": "googlebot42",
"repo": "nix-index", "repo": "nix-index",
"rev": "e7c66ba52fcfba6bfe51adb5400c29a9622664a2", "rev": "a28bb3175d370c6cb9569e6d4b5570e9ca016a3e",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "bennofs", "owner": "googlebot42",
"repo": "nix-index", "repo": "nix-index",
"type": "github" "type": "github"
} }

View File

@ -56,53 +56,15 @@
}) })
]; ];
mkVpnContainer = system: pkgs: mount: config: {
ephemeral = true;
autoStart = true;
bindMounts = {
"/var/lib" = {
hostPath = "/var/lib/";
isReadOnly = false;
};
"/run/agenix" = {
hostPath = "/run/agenix";
isReadOnly = true;
};
"/dev/fuse" = {
hostPath = "/dev/fuse";
isReadOnly = false;
};
"${mount}" = {
hostPath = mount;
isReadOnly = false;
};
};
enableTun = true;
privateNetwork = true;
hostAddress = "172.16.100.1";
localAddress = "172.16.100.2";
config = { lib, ... }: {
imports = (modules system) ++ [config];
nixpkgs.pkgs = pkgs;
networking.firewall.enable = lib.mkForce false;
pia.enable = true;
# run it's own DNS resolver
networking.useHostResolvConf = false;
services.resolved.enable = true;
};
};
mkSystem = system: nixpkgs: path: mkSystem = system: nixpkgs: path:
nixpkgs.lib.nixosSystem { let
allModules = modules system;
in nixpkgs.lib.nixosSystem {
inherit system; inherit system;
modules = (modules system) ++ [path]; modules = allModules ++ [path];
specialArgs = { specialArgs = {
mkVpnContainer = (mkVpnContainer system); inherit allModules;
}; };
}; };
in in

View File

@ -1,4 +1,4 @@
{ config, pkgs, lib, mkVpnContainer, ... }: { config, pkgs, lib, ... }:
{ {
imports =[ imports =[

View File

@ -1,4 +1,4 @@
{ config, pkgs, lib, mkVpnContainer, ... }: { config, pkgs, lib, ... }:
{ {
imports =[ imports =[
@ -55,14 +55,13 @@
}; };
# wrap radio in a VPN # wrap radio in a VPN
containers.vpn = mkVpnContainer pkgs "/dev/null" { vpn-container.enable = true;
vpn-container.config = {
services.radio = { services.radio = {
enable = true; enable = true;
host = "radio.runyan.org"; host = "radio.runyan.org";
}; };
}; };
# containers cannot unlock their own secrets right now. unlock it here
age.secrets."pia-login.conf".file = ../../secrets/pia-login.conf;
# icecast endpoint + website # icecast endpoint + website
services.nginx.virtualHosts."radio.runyan.org" = { services.nginx.virtualHosts."radio.runyan.org" = {
@ -131,13 +130,9 @@
age.secrets.iodine.file = ../../secrets/iodine.age; age.secrets.iodine.file = ../../secrets/iodine.age;
networking.firewall.allowedUDPPorts = [ 53 ]; networking.firewall.allowedUDPPorts = [ 53 ];
boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
networking.nat.enable = true;
networking.nat.internalInterfaces = [ networking.nat.internalInterfaces = [
"dns0" # iodine "dns0" # iodine
"ve-vpn" # vpn container
]; ];
networking.nat.externalInterface = "ens3";
services.nginx.enable = true; services.nginx.enable = true;
services.nginx.virtualHosts."jellyfin.neet.cloud" = { services.nginx.virtualHosts."jellyfin.neet.cloud" = {

View File

@ -31,7 +31,7 @@
# Per-interface useDHCP will be mandatory in the future, so this generated config # Per-interface useDHCP will be mandatory in the future, so this generated config
# replicates the default behaviour. # replicates the default behaviour.
networking.useDHCP = lib.mkDefault false; networking.useDHCP = lib.mkDefault false;
networking.interfaces.ens3.useDHCP = lib.mkDefault true; networking.interfaces.eth0.useDHCP = lib.mkDefault true;
hardware.cpu.intel.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware; hardware.cpu.intel.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware;
} }

View File

@ -1,4 +1,4 @@
{ config, pkgs, lib, mkVpnContainer, ... }: { config, pkgs, lib, ... }:
{ {
imports =[ imports =[
@ -42,10 +42,12 @@
users.users.googlebot.extraGroups = [ "transmission" ]; users.users.googlebot.extraGroups = [ "transmission" ];
users.groups.transmission.gid = config.ids.gids.transmission; users.groups.transmission.gid = config.ids.gids.transmission;
containers.vpn = mkVpnContainer pkgs "/data/samba/Public/Plex" { vpn-container.enable = true;
# swiss vpn vpn-container.mounts = [
pia.server = "swiss.privacy.network"; "/var/lib"
"/data/samba/Public/Plex"
];
vpn-container.config = {
# servarr services # servarr services
services.prowlarr.enable = true; services.prowlarr.enable = true;
services.sonarr.enable = true; services.sonarr.enable = true;
@ -119,15 +121,6 @@
uid = 994; uid = 994;
}; };
}; };
# containers cannot unlock their own secrets right now. unlock it here
age.secrets."pia-login.conf".file = ../../../secrets/pia-login.conf;
boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
# forwarding for vpn container
networking.nat.enable = true;
networking.nat.internalInterfaces = [
"ve-vpn" # vpn container
];
networking.nat.externalInterface = "eth0";
# unpackerr # unpackerr
# flaresolverr # flaresolverr