# This serves a local binary cache. If the request comes from my home network, # it will set its priority higher than cache.nixos.org (which has a priority of # 40), so local devices get a faster binary cache. If the request coes from # outside my home network, it will set its priority lower, only store paths # exclusive to this cache will be substituted. # This only works well when a host does not change its “location”, since nix # caches binary caches locally (per-user, also for root!) in # ${XDG_CACHE_HOME:-$HOME/.cache}/.cache/nix/binary-cache-v6.sqlite and does # not re-check or invalidate them. Devices that often are not at home should # ensure that the cached priority is 50 to avoid slow substitutions. { config, lib, ... }: let binaryCachePath = "/data/cache/nix-binary-cache"; in { sops.secrets.nix-binary-cache-htpasswd = { owner = "nginx"; sopsFile = ../secrets.yaml; }; services.nginx = { appendHttpConfig = '' geo $nix_binary_cache_priority { default 50; 192.168.100.0/24 30; 2001:470:1f0b:abc::/64 30; } ''; virtualHosts."nix-cache.sbruder.de" = rec { enableACME = true; forceSSL = true; root = binaryCachePath; locations = { "/nix-cache-info" = { return = "200 \"StoreDir: /nix/store\\nPriority: $nix_binary_cache_priority\\n\""; }; "/".extraConfig = '' log_not_found off; client_max_body_size 5G; # WebDAV (for uploading) dav_methods PUT DELETE; create_full_put_path on; # nar/ does not exist by default dav_access user:rw group:r all:r; # same filesystem for temporary files client_body_temp_path ${root}/.upload-tmp; limit_except GET { auth_basic "restricted upload"; auth_basic_user_file ${config.sops.secrets.nix-binary-cache-htpasswd.path}; } ''; }; }; }; systemd.services.nginx.serviceConfig.ReadWritePaths = lib.singleton binaryCachePath; }