8 cfg = config.services.microsocks;
11 if cfg.execWrapper != null
12 then "${cfg.execWrapper} ${cfg.package}/bin/microsocks"
13 else "${cfg.package}/bin/microsocks";
15 [ "-i" cfg.ip "-p" (toString cfg.port) ]
16 ++ lib.optionals (cfg.authOnce) [ "-1" ]
17 ++ lib.optionals (cfg.disableLogging) [ "-q" ]
18 ++ lib.optionals (cfg.outgoingBindIp != null) [ "-b" cfg.outgoingBindIp ]
19 ++ lib.optionals (cfg.authUsername != null) [ "-u" cfg.authUsername ];
21 options.services.microsocks = {
22 enable = lib.mkEnableOption "Tiny, portable SOCKS5 server with very moderate resource usage";
24 default = "microsocks";
25 description = "User microsocks runs as.";
28 group = lib.mkOption {
29 default = "microsocks";
30 description = "Group microsocks runs as.";
33 package = lib.mkPackageOption pkgs "microsocks" {};
36 default = "127.0.0.1";
38 IP on which microsocks should listen. Defaults to 127.0.0.1 for
43 type = lib.types.port;
45 description = "Port on which microsocks should listen.";
47 disableLogging = lib.mkOption {
48 type = lib.types.bool;
50 description = "If true, microsocks will not log any messages to stdout/stderr.";
52 authOnce = lib.mkOption {
53 type = lib.types.bool;
56 If true, once a specific ip address authed successfully with user/pass,
57 it is added to a whitelist and may use the proxy without auth.
60 outgoingBindIp = lib.mkOption {
61 type = lib.types.nullOr lib.types.str;
63 description = "Specifies which ip outgoing connections are bound to";
65 authUsername = lib.mkOption {
66 type = lib.types.nullOr lib.types.str;
69 description = "Optional username to use for authentication.";
71 authPasswordFile = lib.mkOption {
72 type = lib.types.nullOr lib.types.path;
74 example = "/run/secrets/microsocks-password";
75 description = "Path to a file containing the password for authentication.";
77 execWrapper = lib.mkOption {
78 type = lib.types.nullOr lib.types.str;
81 ''${pkgs.mullvad-vpn}/bin/mullvad-exclude
84 An optional command to prepend to the microsocks command (such as proxychains, or a VPN exclude command).
88 config = lib.mkIf cfg.enable {
91 assertion = (cfg.authUsername != null) == (cfg.authPasswordFile != null);
92 message = "Need to set both authUsername and authPasswordFile for microsocks";
96 users = lib.mkIf (cfg.user == "microsocks") {
102 groups = lib.mkIf (cfg.group == "microsocks") {
106 systemd.services.microsocks = {
108 description = "a tiny socks server";
109 after = [ "network.target" ];
110 wantedBy = [ "multi-user.target" ];
114 Restart = "on-failure";
116 LoadCredential = lib.optionalString (cfg.authPasswordFile != null) "MICROSOCKS_PASSWORD_FILE:${cfg.authPasswordFile}";
117 MemoryDenyWriteExecute = true;
118 SystemCallArchitectures = "native";
120 NoNewPrivileges = true;
121 ProtectSystem = "strict";
123 ProtectKernelModules = true;
124 ProtectKernelTunables = true;
125 PrivateDevices = true;
126 RestrictSUIDSGID = true;
127 RestrictNamespaces = [
136 if cfg.authPasswordFile != null
138 PASSWORD=$(cat "$CREDENTIALS_DIRECTORY/MICROSOCKS_PASSWORD_FILE")
139 ${cmd} ${lib.escapeShellArgs args} -P "$PASSWORD"
142 ${cmd} ${lib.escapeShellArgs args}