at 23.11-pre 2.9 kB view raw
1{ config, pkgs, lib, ... }: 2 3let 4 5 cfg = config.services.quicktun; 6 7in 8 9with lib; 10 11{ 12 options = { 13 14 services.quicktun = mkOption { 15 default = { }; 16 description = lib.mdDoc "QuickTun tunnels"; 17 type = types.attrsOf (types.submodule { 18 options = { 19 tunMode = mkOption { 20 type = types.int; 21 default = 0; 22 example = 1; 23 description = lib.mdDoc ""; 24 }; 25 26 remoteAddress = mkOption { 27 type = types.str; 28 example = "tunnel.example.com"; 29 description = lib.mdDoc ""; 30 }; 31 32 localAddress = mkOption { 33 type = types.str; 34 example = "0.0.0.0"; 35 description = lib.mdDoc ""; 36 }; 37 38 localPort = mkOption { 39 type = types.int; 40 default = 2998; 41 description = lib.mdDoc ""; 42 }; 43 44 remotePort = mkOption { 45 type = types.int; 46 default = 2998; 47 description = lib.mdDoc ""; 48 }; 49 50 remoteFloat = mkOption { 51 type = types.int; 52 default = 0; 53 description = lib.mdDoc ""; 54 }; 55 56 protocol = mkOption { 57 type = types.str; 58 default = "nacltai"; 59 description = lib.mdDoc ""; 60 }; 61 62 privateKey = mkOption { 63 type = types.str; 64 description = lib.mdDoc ""; 65 }; 66 67 publicKey = mkOption { 68 type = types.str; 69 description = lib.mdDoc ""; 70 }; 71 72 timeWindow = mkOption { 73 type = types.int; 74 default = 5; 75 description = lib.mdDoc ""; 76 }; 77 78 upScript = mkOption { 79 type = types.lines; 80 default = ""; 81 description = lib.mdDoc ""; 82 }; 83 }; 84 }); 85 }; 86 87 }; 88 89 config = mkIf (cfg != []) { 90 systemd.services = foldr (a: b: a // b) {} ( 91 mapAttrsToList (name: qtcfg: { 92 "quicktun-${name}" = { 93 wantedBy = [ "multi-user.target" ]; 94 after = [ "network.target" ]; 95 environment = { 96 INTERFACE = name; 97 TUN_MODE = toString qtcfg.tunMode; 98 REMOTE_ADDRESS = qtcfg.remoteAddress; 99 LOCAL_ADDRESS = qtcfg.localAddress; 100 LOCAL_PORT = toString qtcfg.localPort; 101 REMOTE_PORT = toString qtcfg.remotePort; 102 REMOTE_FLOAT = toString qtcfg.remoteFloat; 103 PRIVATE_KEY = qtcfg.privateKey; 104 PUBLIC_KEY = qtcfg.publicKey; 105 TIME_WINDOW = toString qtcfg.timeWindow; 106 TUN_UP_SCRIPT = pkgs.writeScript "quicktun-${name}-up.sh" qtcfg.upScript; 107 SUID = "nobody"; 108 }; 109 serviceConfig = { 110 Type = "simple"; 111 ExecStart = "${pkgs.quicktun}/bin/quicktun.${qtcfg.protocol}"; 112 }; 113 }; 114 }) cfg 115 ); 116 }; 117 118}