1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.squid;
8
9
10 squidConfig = pkgs.writeText "squid.conf"
11 (if cfg.configText != null then cfg.configText else
12 ''
13 #
14 # Recommended minimum configuration (3.5):
15 #
16
17 # Example rule allowing access from your local networks.
18 # Adapt to list your (internal) IP networks from where browsing
19 # should be allowed
20 acl localnet src 10.0.0.0/8 # RFC 1918 possible internal network
21 acl localnet src 172.16.0.0/12 # RFC 1918 possible internal network
22 acl localnet src 192.168.0.0/16 # RFC 1918 possible internal network
23 acl localnet src 169.254.0.0/16 # RFC 3927 link-local (directly plugged) machines
24 acl localnet src fc00::/7 # RFC 4193 local private network range
25 acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
26
27 acl SSL_ports port 443 # https
28 acl Safe_ports port 80 # http
29 acl Safe_ports port 21 # ftp
30 acl Safe_ports port 443 # https
31 acl Safe_ports port 70 # gopher
32 acl Safe_ports port 210 # wais
33 acl Safe_ports port 1025-65535 # unregistered ports
34 acl Safe_ports port 280 # http-mgmt
35 acl Safe_ports port 488 # gss-http
36 acl Safe_ports port 591 # filemaker
37 acl Safe_ports port 777 # multiling http
38 acl CONNECT method CONNECT
39
40 #
41 # Recommended minimum Access Permission configuration:
42 #
43 # Deny requests to certain unsafe ports
44 http_access deny !Safe_ports
45
46 # Deny CONNECT to other than secure SSL ports
47 http_access deny CONNECT !SSL_ports
48
49 # Only allow cachemgr access from localhost
50 http_access allow localhost manager
51 http_access deny manager
52
53 # We strongly recommend the following be uncommented to protect innocent
54 # web applications running on the proxy server who think the only
55 # one who can access services on "localhost" is a local user
56 http_access deny to_localhost
57
58 # Application logs to syslog, access and store logs have specific files
59 cache_log syslog
60 access_log stdio:/var/log/squid/access.log
61 cache_store_log stdio:/var/log/squid/store.log
62
63 # Required by systemd service
64 pid_filename /run/squid.pid
65
66 # Run as user and group squid
67 cache_effective_user squid squid
68
69 #
70 # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
71 #
72 ${cfg.extraConfig}
73
74 # Example rule allowing access from your local networks.
75 # Adapt localnet in the ACL section to list your (internal) IP networks
76 # from where browsing should be allowed
77 http_access allow localnet
78 http_access allow localhost
79
80 # And finally deny all other access to this proxy
81 http_access deny all
82
83 # Squid normally listens to port 3128
84 http_port ${
85 optionalString (cfg.proxyAddress != null) "${cfg.proxyAddress}:"
86 }${toString cfg.proxyPort}
87
88 # Leave coredumps in the first cache dir
89 coredump_dir /var/cache/squid
90
91 #
92 # Add any of your own refresh_pattern entries above these.
93 #
94 refresh_pattern ^ftp: 1440 20% 10080
95 refresh_pattern ^gopher: 1440 0% 1440
96 refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
97 refresh_pattern . 0 20% 4320
98 '');
99
100in
101
102{
103
104 options = {
105
106 services.squid = {
107
108 enable = mkOption {
109 type = types.bool;
110 default = false;
111 description = lib.mdDoc "Whether to run squid web proxy.";
112 };
113
114 package = mkOption {
115 default = pkgs.squid;
116 defaultText = literalExpression "pkgs.squid";
117 type = types.package;
118 description = lib.mdDoc "Squid package to use.";
119 };
120
121 proxyAddress = mkOption {
122 type = types.nullOr types.str;
123 default = null;
124 description = lib.mdDoc "IP address on which squid will listen.";
125 };
126
127 proxyPort = mkOption {
128 type = types.int;
129 default = 3128;
130 description = lib.mdDoc "TCP port on which squid will listen.";
131 };
132
133 extraConfig = mkOption {
134 type = types.lines;
135 default = "";
136 description = lib.mdDoc ''
137 Squid configuration. Contents will be added
138 verbatim to the configuration file.
139 '';
140 };
141
142 configText = mkOption {
143 type = types.nullOr types.lines;
144 default = null;
145 description = lib.mdDoc ''
146 Verbatim contents of squid.conf. If null (default), use the
147 autogenerated file from NixOS instead.
148 '';
149 };
150
151 };
152
153 };
154
155 config = mkIf cfg.enable {
156
157 users.users.squid = {
158 isSystemUser = true;
159 group = "squid";
160 home = "/var/cache/squid";
161 createHome = true;
162 };
163
164 users.groups.squid = {};
165
166 systemd.services.squid = {
167 description = "Squid caching proxy";
168 documentation = [ "man:squid(8)" ];
169 after = [ "network.target" "nss-lookup.target" ];
170 wantedBy = [ "multi-user.target"];
171 preStart = ''
172 mkdir -p "/var/log/squid"
173 chown squid:squid "/var/log/squid"
174 ${cfg.package}/bin/squid --foreground -z -f ${squidConfig}
175 '';
176 serviceConfig = {
177 PIDFile="/run/squid.pid";
178 ExecStart = "${cfg.package}/bin/squid --foreground -YCs -f ${squidConfig}";
179 ExecReload="kill -HUP $MAINPID";
180 KillMode="mixed";
181 NotifyAccess="all";
182 };
183 };
184
185 };
186
187}