]> Git — Sourcephile - sourcephile-nix.git/blob - hosts/losurdo/nginx/sourcephile.fr/nix-serve.nix
syncoid: fix localTargetAllow
[sourcephile-nix.git] / hosts / losurdo / nginx / sourcephile.fr / nix-serve.nix
1 { domain, ... }:
2 { pkgs, lib, config, hostName, ... }:
3 let
4 inherit (config) networking;
5 inherit (config.security) gnupg;
6 inherit (config.services) nginx nix-serve;
7 inherit (config.users) users groups;
8 srv = "nix-serve";
9 in
10 {
11 nix.trustedUsers = [ users."nix-serve".name ];
12 users.users."nix-serve" = {
13 isSystemUser = true;
14 group = groups."nix-serve".name;
15 extraGroups = [ groups."keys".name ];
16 };
17 users.groups."nix-serve" = {};
18 security.gnupg.secrets."nix/binary-cache-key/1" = {
19 user = users."nix-serve".name;
20 systemdConfig = {
21 before = [ "nix-serve.service" ];
22 wantedBy = [ "nix-serve.service" ];
23 };
24 };
25 services.nix-serve = {
26 enable = true;
27 secretKeyFile = gnupg.secrets."nix/binary-cache-key/1".path;
28 bindAddress = "127.0.0.1";
29 };
30 services.nginx = let vhostConfig = priority:
31 {
32 extraConfig = ''
33 #access_log /var/log/nginx/${domain}/${srv}/access.json json buffer=32k;
34 #error_log /var/log/nginx/${domain}/${srv}/error.log warn;
35 access_log off;
36 error_log /dev/null crit;
37 '';
38 locations."/nix-cache-info" = {
39 # cache.nixos.org has priority 40
40 return = ''200 "StoreDir: ${builtins.storeDir}\nWantMassQuery: 1\nPriority: ${toString priority}\n"'';
41 extraConfig = ''
42 ${nginx.configs.https_add_headers}
43 add_header Content-Type text/plain;
44 '';
45 };
46 locations."/".extraConfig = ''
47 proxy_pass http://localhost:${toString nix-serve.port};
48 proxy_set_header Host $host;
49 proxy_set_header X-Real-IP $remote_addr;
50 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
51 '';
52 };
53 in {
54 virtualHosts."nix-extracache.${domain}" = vhostConfig 60 // {
55 serverAliases = [ "${srv}.${domain}" ];
56 forceSSL = true;
57 useACMEHost = domain;
58 };
59 virtualHosts."nix-localcache.${domain}" = vhostConfig 30 // {
60 forceSSL = true;
61 useACMEHost = domain;
62 };
63 # cache.nixos.org has priority over extracache
64 virtualHosts."nix-extracache.${hostName}.wg" = vhostConfig 60 // {
65 listenAddresses = [ "nix-extracache.${hostName}.wg" ];
66 forceSSL = false;
67 };
68 # localcache has priority over cache.nixos.org
69 virtualHosts."nix-localcache.${hostName}.wg" = vhostConfig 30 // {
70 listenAddresses = [ "nix-localcache.${hostName}.wg" ];
71 forceSSL = false;
72 };
73 };
74 systemd.services.nginx = {
75 serviceConfig = {
76 LogsDirectory = lib.mkForce ["nginx/${domain}/${srv}"];
77 };
78 };
79 }