2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-10-05 17:11:32 +02:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2009-10-05 17:11:32 +02:00
|
|
|
|
2009-10-12 19:27:57 +02:00
|
|
|
let
|
2009-10-05 17:11:32 +02:00
|
|
|
|
2014-07-23 00:30:04 +02:00
|
|
|
cfg = config.services.privoxy;
|
2009-10-05 17:11:32 +02:00
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
serialise = name: val:
|
|
|
|
if isList val then concatMapStrings (serialise name) val
|
|
|
|
else if isBool val then serialise name (if val then "1" else "0")
|
|
|
|
else "${name} ${toString val}\n";
|
|
|
|
|
|
|
|
configType = with types;
|
|
|
|
let atom = oneOf [ int bool string path ];
|
|
|
|
in attrsOf (either atom (listOf atom))
|
|
|
|
// { description = ''
|
|
|
|
privoxy configuration type. The format consists of an attribute
|
|
|
|
set of settings. Each setting can be either a value (integer, string,
|
|
|
|
boolean or path) or a list of such values.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
ageType = types.str // {
|
|
|
|
check = x:
|
|
|
|
isString x &&
|
|
|
|
(builtins.match "([0-9]+([smhdw]|min|ms|us)*)+" x != null);
|
|
|
|
description = "tmpfiles.d(5) age format";
|
|
|
|
};
|
|
|
|
|
|
|
|
configFile = pkgs.writeText "privoxy.conf"
|
|
|
|
(concatStrings (
|
|
|
|
# Relative paths in some options are relative to confdir. Privoxy seems
|
|
|
|
# to parse the options in order of appearance, so this must come first.
|
|
|
|
# Nix however doesn't preserve the order in attrsets, so we have to
|
|
|
|
# hardcode confdir here.
|
|
|
|
[ "confdir ${pkgs.privoxy}/etc\n" ]
|
|
|
|
++ mapAttrsToList serialise cfg.settings
|
|
|
|
));
|
|
|
|
|
|
|
|
inspectAction = pkgs.writeText "inspect-all-https.action"
|
|
|
|
''
|
|
|
|
# Enable HTTPS inspection for all requests
|
|
|
|
{+https-inspection}
|
|
|
|
/
|
|
|
|
'';
|
2009-10-05 17:11:32 +02:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
options.services.privoxy = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
enable = mkEnableOption "Privoxy, non-caching filtering proxy";
|
2009-10-05 17:11:32 +02:00
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
enableTor = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-03-03 17:46:00 +01:00
|
|
|
Whether to configure Privoxy to use Tor's faster SOCKS port,
|
|
|
|
suitable for HTTP.
|
|
|
|
'';
|
|
|
|
};
|
2009-10-05 17:11:32 +02:00
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
inspectHttps = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to configure Privoxy to inspect HTTPS requests, meaning all
|
|
|
|
encrypted traffic will be filtered as well. This works by decrypting
|
|
|
|
and re-encrypting the requests using a per-domain generated certificate.
|
2009-10-05 17:11:32 +02:00
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
To issue per-domain certificates, Privoxy must be provided with a CA
|
|
|
|
certificate, using the <literal>ca-cert-file</literal>,
|
|
|
|
<literal>ca-key-file</literal> settings.
|
2014-07-23 00:30:04 +02:00
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
<warning><para>
|
|
|
|
The CA certificate must also be added to the system trust roots,
|
|
|
|
otherwise browsers will reject all Privoxy certificates as invalid.
|
|
|
|
You can do so by using the option
|
|
|
|
<option>security.pki.certificateFiles</option>.
|
|
|
|
</para></warning>
|
|
|
|
'';
|
|
|
|
};
|
2014-07-23 00:30:04 +02:00
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
certsLifetime = mkOption {
|
|
|
|
type = ageType;
|
|
|
|
default = "10d";
|
|
|
|
example = "12h";
|
|
|
|
description = ''
|
|
|
|
If <literal>inspectHttps</literal> is enabled, the time generated HTTPS
|
|
|
|
certificates will be stored in a temporary directory for reuse. Once
|
|
|
|
the lifetime has expired the directory will cleared and the certificate
|
|
|
|
will have to be generated again, on-demand.
|
2009-10-05 17:11:32 +02:00
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
Depending on the traffic, you may want to reduce the lifetime to limit
|
|
|
|
the disk usage, since Privoxy itself never deletes the certificates.
|
2020-12-15 22:30:24 +01:00
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
<note><para>The format is that of the <literal>tmpfiles.d(5)</literal>
|
|
|
|
Age parameter.</para></note>
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
userActions = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Actions to be included in a `user.action` file. This
|
2021-03-03 17:46:00 +01:00
|
|
|
will have a higher priority and can be used to override all other
|
|
|
|
actions.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
userFilters = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Filters to be included in a `user.filter` file. This
|
2021-03-03 17:46:00 +01:00
|
|
|
will have a higher priority and can be used to override all other
|
|
|
|
filters definitions.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = configType;
|
|
|
|
|
|
|
|
options.listen-address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1:8118";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Pair of address:port the proxy server is listening to.";
|
2021-03-03 17:46:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
options.enable-edit-actions = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Whether the web-based actions file editor may be used.";
|
2021-03-03 17:46:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
options.actionsfile = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
# This must come after all other entries, in order to override the
|
|
|
|
# other actions/filters installed by Privoxy or the user.
|
|
|
|
apply = x: x ++ optional (cfg.userActions != "")
|
|
|
|
(toString (pkgs.writeText "user.actions" cfg.userActions));
|
|
|
|
default = [ "match-all.action" "default.action" ];
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-03-03 17:46:00 +01:00
|
|
|
List of paths to Privoxy action files. These paths may either be
|
|
|
|
absolute or relative to the privoxy configuration directory.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
options.filterfile = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ "default.filter" ];
|
|
|
|
apply = x: x ++ optional (cfg.userFilters != "")
|
|
|
|
(toString (pkgs.writeText "user.filter" cfg.userFilters));
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-03-03 17:46:00 +01:00
|
|
|
List of paths to Privoxy filter files. These paths may either be
|
|
|
|
absolute or relative to the privoxy configuration directory.
|
|
|
|
'';
|
|
|
|
};
|
2009-10-05 17:11:32 +02:00
|
|
|
};
|
2021-03-03 17:46:00 +01:00
|
|
|
default = {};
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''
|
2021-03-09 16:59:35 +01:00
|
|
|
{ # Listen on IPv6 only
|
|
|
|
listen-address = "[::]:8118";
|
|
|
|
|
|
|
|
# Forward .onion requests to Tor
|
|
|
|
forward-socks5 = ".onion localhost:9050 .";
|
|
|
|
|
|
|
|
# Log redirects and filters
|
|
|
|
debug = [ 128 64 ];
|
|
|
|
# This is equivalent to writing these lines
|
|
|
|
# in the Privoxy configuration file:
|
|
|
|
# debug 128
|
|
|
|
# debug 64
|
2021-03-03 17:46:00 +01:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
This option is mapped to the main Privoxy configuration file.
|
|
|
|
Check out the Privoxy user manual at
|
2021-03-09 16:59:35 +01:00
|
|
|
<link xlink:href="https://www.privoxy.org/user-manual/config.html"/>
|
2021-03-03 17:46:00 +01:00
|
|
|
for available settings and documentation.
|
2021-03-09 16:59:35 +01:00
|
|
|
|
|
|
|
<note><para>
|
|
|
|
Repeated settings can be represented by using a list.
|
|
|
|
</para></note>
|
2021-03-03 17:46:00 +01:00
|
|
|
'';
|
2009-10-05 17:11:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2014-07-23 00:30:04 +02:00
|
|
|
config = mkIf cfg.enable {
|
2016-12-05 12:27:51 +01:00
|
|
|
|
|
|
|
users.users.privoxy = {
|
2021-03-03 17:46:00 +01:00
|
|
|
description = "Privoxy daemon user";
|
2016-12-05 12:27:51 +01:00
|
|
|
isSystemUser = true;
|
|
|
|
group = "privoxy";
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups.privoxy = {};
|
2009-10-05 17:11:32 +02:00
|
|
|
|
2021-03-11 08:02:09 +01:00
|
|
|
systemd.tmpfiles.rules = optional cfg.inspectHttps
|
|
|
|
"d ${cfg.settings.certificate-directory} 0770 privoxy privoxy ${cfg.certsLifetime}";
|
2021-03-03 17:46:00 +01:00
|
|
|
|
2014-07-23 00:30:04 +02:00
|
|
|
systemd.services.privoxy = {
|
|
|
|
description = "Filtering web proxy";
|
|
|
|
after = [ "network.target" "nss-lookup.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-03-03 17:46:00 +01:00
|
|
|
serviceConfig = {
|
|
|
|
User = "privoxy";
|
|
|
|
Group = "privoxy";
|
|
|
|
ExecStart = "${pkgs.privoxy}/bin/privoxy --no-daemon ${configFile}";
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectSystem = "full";
|
|
|
|
};
|
|
|
|
unitConfig = mkIf cfg.inspectHttps {
|
|
|
|
ConditionPathExists = with cfg.settings;
|
|
|
|
[ ca-cert-file ca-key-file ];
|
|
|
|
};
|
2014-07-23 00:30:04 +02:00
|
|
|
};
|
2009-10-05 17:11:32 +02:00
|
|
|
|
2020-09-11 07:46:59 +02:00
|
|
|
services.tor.settings.SOCKSPort = mkIf cfg.enableTor [
|
|
|
|
# Route HTTP traffic over a faster port (without IsolateDestAddr).
|
|
|
|
{ addr = "127.0.0.1"; port = 9063; IsolateDestAddr = false; }
|
|
|
|
];
|
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
services.privoxy.settings = {
|
|
|
|
user-manual = "${pkgs.privoxy}/share/doc/privoxy/user-manual";
|
2021-03-05 16:16:55 +01:00
|
|
|
# This is needed for external filters
|
|
|
|
temporary-directory = "/tmp";
|
2021-03-03 17:46:00 +01:00
|
|
|
filterfile = [ "default.filter" ];
|
|
|
|
actionsfile =
|
|
|
|
[ "match-all.action"
|
|
|
|
"default.action"
|
|
|
|
] ++ optional cfg.inspectHttps (toString inspectAction);
|
|
|
|
} // (optionalAttrs cfg.enableTor {
|
2021-03-28 21:16:55 +02:00
|
|
|
forward-socks5 = "/ 127.0.0.1:9063 .";
|
2021-03-03 17:46:00 +01:00
|
|
|
toggle = true;
|
|
|
|
enable-remote-toggle = false;
|
|
|
|
enable-edit-actions = false;
|
|
|
|
enable-remote-http-toggle = false;
|
|
|
|
}) // (optionalAttrs cfg.inspectHttps {
|
|
|
|
# This allows setting absolute key/crt paths
|
|
|
|
ca-directory = "/var/empty";
|
|
|
|
certificate-directory = "/run/privoxy/certs";
|
|
|
|
trusted-cas-file = "/etc/ssl/certs/ca-certificates.crt";
|
|
|
|
});
|
|
|
|
|
2009-10-05 17:11:32 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2021-03-03 17:46:00 +01:00
|
|
|
imports =
|
|
|
|
let
|
|
|
|
top = x: [ "services" "privoxy" x ];
|
|
|
|
setting = x: [ "services" "privoxy" "settings" x ];
|
|
|
|
in
|
|
|
|
[ (mkRenamedOptionModule (top "enableEditActions") (setting "enable-edit-actions"))
|
|
|
|
(mkRenamedOptionModule (top "listenAddress") (setting "listen-address"))
|
|
|
|
(mkRenamedOptionModule (top "actionsFiles") (setting "actionsfile"))
|
|
|
|
(mkRenamedOptionModule (top "filterFiles") (setting "filterfile"))
|
|
|
|
(mkRemovedOptionModule (top "extraConfig")
|
|
|
|
''
|
|
|
|
Use services.privoxy.settings instead.
|
|
|
|
This is part of the general move to use structured settings instead of raw
|
|
|
|
text for config as introduced by RFC0042:
|
|
|
|
https://github.com/NixOS/rfcs/blob/master/rfcs/0042-config-option.md
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
|
2019-12-04 17:07:45 +01:00
|
|
|
meta.maintainers = with lib.maintainers; [ rnhmjoj ];
|
|
|
|
|
2009-10-05 17:11:32 +02:00
|
|
|
}
|