9 cfg = config.services.microsocks;
12 if cfg.execWrapper != null then
13 "${cfg.execWrapper} ${cfg.package}/bin/microsocks"
15 "${cfg.package}/bin/microsocks";
23 ++ lib.optionals (cfg.authOnce) [ "-1" ]
24 ++ lib.optionals (cfg.disableLogging) [ "-q" ]
25 ++ lib.optionals (cfg.outgoingBindIp != null) [
29 ++ lib.optionals (cfg.authUsername != null) [
35 options.services.microsocks = {
36 enable = lib.mkEnableOption "Tiny, portable SOCKS5 server with very moderate resource usage";
38 default = "microsocks";
39 description = "User microsocks runs as.";
42 group = lib.mkOption {
43 default = "microsocks";
44 description = "Group microsocks runs as.";
47 package = lib.mkPackageOption pkgs "microsocks" { };
50 default = "127.0.0.1";
52 IP on which microsocks should listen. Defaults to 127.0.0.1 for
57 type = lib.types.port;
59 description = "Port on which microsocks should listen.";
61 disableLogging = lib.mkOption {
62 type = lib.types.bool;
64 description = "If true, microsocks will not log any messages to stdout/stderr.";
66 authOnce = lib.mkOption {
67 type = lib.types.bool;
70 If true, once a specific ip address authed successfully with user/pass,
71 it is added to a whitelist and may use the proxy without auth.
74 outgoingBindIp = lib.mkOption {
75 type = lib.types.nullOr lib.types.str;
77 description = "Specifies which ip outgoing connections are bound to";
79 authUsername = lib.mkOption {
80 type = lib.types.nullOr lib.types.str;
83 description = "Optional username to use for authentication.";
85 authPasswordFile = lib.mkOption {
86 type = lib.types.nullOr lib.types.path;
88 example = "/run/secrets/microsocks-password";
89 description = "Path to a file containing the password for authentication.";
91 execWrapper = lib.mkOption {
92 type = lib.types.nullOr lib.types.str;
95 ''${pkgs.mullvad-vpn}/bin/mullvad-exclude
98 An optional command to prepend to the microsocks command (such as proxychains, or a VPN exclude command).
102 config = lib.mkIf cfg.enable {
105 assertion = (cfg.authUsername != null) == (cfg.authPasswordFile != null);
106 message = "Need to set both authUsername and authPasswordFile for microsocks";
110 users = lib.mkIf (cfg.user == "microsocks") {
116 groups = lib.mkIf (cfg.group == "microsocks") {
120 systemd.services.microsocks = {
122 description = "a tiny socks server";
123 after = [ "network.target" ];
124 wantedBy = [ "multi-user.target" ];
128 Restart = "on-failure";
130 LoadCredential = lib.optionalString (
131 cfg.authPasswordFile != null
132 ) "MICROSOCKS_PASSWORD_FILE:${cfg.authPasswordFile}";
133 MemoryDenyWriteExecute = true;
134 SystemCallArchitectures = "native";
136 NoNewPrivileges = true;
137 ProtectSystem = "strict";
139 ProtectKernelModules = true;
140 ProtectKernelTunables = true;
141 PrivateDevices = true;
142 RestrictSUIDSGID = true;
143 RestrictNamespaces = [
152 if cfg.authPasswordFile != null then
154 PASSWORD=$(cat "$CREDENTIALS_DIRECTORY/MICROSOCKS_PASSWORD_FILE")
155 ${cmd} ${lib.escapeShellArgs args} -P "$PASSWORD"
159 ${cmd} ${lib.escapeShellArgs args}