2 { pkgs, lib, config, hostName, ... }:
4 inherit (config) networking;
5 inherit (config.security) gnupg;
6 inherit (config.services) nginx nix-serve;
7 inherit (config.users) users groups;
11 nix.trustedUsers = [ users."nix-serve".name ];
12 users.users."nix-serve".extraGroups = [ groups."keys".name ];
13 security.gnupg.secrets."nix/binary-cache-key/1" = {
14 user = users."nix-serve".name;
16 before = [ "nix-serve.service" ];
17 wantedBy = [ "nix-serve.service" ];
20 services.nix-serve = {
22 secretKeyFile = gnupg.secrets."nix/binary-cache-key/1".path;
23 bindAddress = "127.0.0.1";
25 services.nginx = let vhostConfig = priority:
32 #access_log /var/log/nginx/${domain}/${srv}/access.log json buffer=32k;
33 #error_log /var/log/nginx/${domain}/${srv}/error.log warn;
35 error_log /dev/null crit;
37 locations."/nix-cache-info" = {
38 # cache.nixos.org has priority 40
39 return = ''200 "StoreDir: ${builtins.storeDir}\nWantMassQuery: 1\nPriority: ${toString priority}\n"'';
41 ${nginx.configs.https_add_headers}
42 add_header Content-Type text/plain;
45 locations."/".extraConfig = ''
46 proxy_pass http://localhost:${toString nix-serve.port};
47 proxy_set_header Host $host;
48 proxy_set_header X-Real-IP $remote_addr;
49 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
53 virtualHosts."nix-localcache.${domain}" = vhostConfig 30;
54 virtualHosts."nix-extracache.${domain}" = vhostConfig 60 // {
55 serverAliases = [ "${srv}.${domain}" ];
58 systemd.services.nginx = {
60 LogsDirectory = lib.mkForce ["nginx/${domain}/${srv}"];