My Nix Configuration
1{ data, lib, ... }:
2let
3
4 pns = data.services;
5 marvinIP = data.hosts.marvin.ts.ip4;
6 marvin = "http://${marvinIP}";
7 inherit (data) tsNet;
8
9in
10rec {
11
12 /**
13 Caddy multiple host creation function
14
15 Allows creation of a set of caddy hosts, based on a function and a list of service names
16
17 # Example
18 ```nix
19 mkHosts mkDirect [ "service-a" "service-b" ]
20 ```
21
22 # Type
23 ```
24 mkHosts :: Function -> [String] -> AttrSet
25 ```
26
27 # Arguments
28
29 - [function] The function to be applied to the services list
30 - [services] The services to pass to the function
31 */
32 mkHosts = function: services: lib.listToAttrs (map function services);
33
34 /**
35 Anubis-proxied host creation function
36
37 Allows creation of an Anubis-proxied host
38
39 # Example
40 ```nix
41 mkAnubis "service-a"
42 => { service-a = {
43 extraConfig = ''
44 reverse_proxy 100.123.15.72:1234 {
45 header_up X-Real-Ip {remote_host}
46 header_up X-Http-Version {http.request.proto}
47 }
48 '';
49 }; }
50 ```
51
52 # Type
53 ```
54 mkAnubis :: String -> AttrSet
55 ```
56
57 # Arguments
58
59 - [service] The service to create a caddy host for
60 */
61 mkAnubis =
62 service:
63 lib.nameValuePair "${pns.${service}.extUrl}" {
64 extraConfig = ''
65 reverse_proxy ${marvin}:${toString pns.${service}.anubis} {
66 header_up X-Real-Ip {remote_host}
67 header_up X-Http-Version {http.request.proto}
68 }
69 '';
70 };
71
72 /**
73 Anubis-proxied multiple host creation function
74
75 Allows creation of several Anubis-proxied hosts
76
77 # Example
78 ```nix
79 mkAnubisSites ["service-a"]
80 => { service-a = {
81 extraConfig = ''
82 reverse_proxy 100.123.15.72:1234 {
83 header_up X-Real-Ip {remote_host}
84 header_up X-Http-Version {http.request.proto}
85 }
86 '';
87 }; }
88 ```
89
90 # Type
91 ```
92 mkAnubisSites :: [String] -> AttrSet
93 ```
94
95 # Arguments
96
97 - [services] The list of services to create Caddy hosts for
98 */
99 mkAnubisSites = services: mkHosts mkAnubis services;
100
101 /**
102 Tailscale-only host creation function
103
104 Allows creation of a service that will only be offered inside the tailnet.
105
106 # Example
107 ```nix
108 mkTs "service-a"
109 => { "service-a.example.ts.net" = {
110 extraConfig = ''
111 bind tailscale/service-a
112 tailscale_auth
113 reverse_proxy 100.123.15.72:1234
114 '';
115 }; }
116 ```
117
118 # Type
119 ```
120 mkTs :: String -> AttrSet
121 ```
122
123 # Arguments
124
125 - [service] The service to create a Tailscale-only host for
126 */
127 mkTs =
128 service:
129 lib.nameValuePair "${pns.${service}.tsHost}.${tsNet}" {
130 extraConfig = ''
131 bind tailscale/${pns.${service}.tsHost}
132 tailscale_auth
133 reverse_proxy ${marvin}:${toString pns.${service}.port}
134 '';
135 };
136
137 /**
138 Tailscale-only multiple host creation function
139
140 Allows creation of several Tailscale-only hosts
141
142 # Example
143 ```nix
144 mkTs ["service-a"]
145 => { "service-a.example.ts.net" = {
146 extraConfig = ''
147 bind tailscale/service-a
148 tailscale_auth
149 reverse_proxy 100.123.15.72:1234
150 '';
151 }; }
152 ```
153
154 # Type
155 ```
156 mkTsSites :: [String] -> AttrSet
157 ```
158
159 # Arguments
160
161 - [services] The list of services to create Tailscale-only hosts for
162 */
163 mkTsSites = services: mkHosts mkTs services;
164
165 /**
166 Direct connection host creation function
167
168 Allows creation of a service that will be proxied via Caddy, but not protected by Anubis.
169
170 # Example
171 ```nix
172 mkDirect "service-a"
173 => { "service-a.example.com" = {
174 extraConfig = ''
175 reverse_proxy 100.123.15.72:1234
176 '';
177 }; }
178 ```
179
180 # Type
181 ```
182 mkDirect :: String -> AttrSet
183 ```
184
185 # Arguments
186
187 - [service] The service to create a direct host for
188 */
189 mkDirect =
190 service:
191 lib.nameValuePair "${pns.${service}.extUrl}" {
192 extraConfig = ''
193 reverse_proxy ${marvin}:${toString pns.${service}.port}
194 '';
195 };
196
197 /**
198 Direct connection multiple host creation function
199
200 Allows creation of several directly-connected hosts
201
202 # Example
203 ```nix
204 mkDirectSites ["service-a"]
205 => { "service-a.example.com" = {
206 extraConfig = ''
207 reverse_proxy 100.123.15.72:1234
208 '';
209 }; }
210 ```
211
212 # Type
213 ```
214 mkDirectSites :: [String] -> AttrSet
215 ```
216
217 # Arguments
218
219 - [services] The list of services to create Tailscale-only hosts for
220 */
221 mkDirectSites = services: mkHosts mkDirect services;
222}