nixos-config/machines/orbi/service-nix-cache.nix
2024-09-30 20:48:04 +09:00

62 lines
1.7 KiB
Nix

{
config,
lib,
pkgs,
...
}:
{
# nixpkgs.config.packageOverrides = p: {
# nix-serve = p.haskellPackages.nix-serve-ng;
# };
# generate private key with:
# nix-store --generate-binary-cache-key my-secret-key my-public-key
clan.core.facts.services."nix-serve" = {
secret."nix-serve.key" = { };
public."nix-serve.pub" = { };
generator.path = with pkgs; [
coreutils
nix
];
generator.script = ''
nix-store --generate-binary-cache-key "cache.${config.networking.hostName}.wg0" nix-serve.key nix-serve.pub
mv nix-serve.key "$secrets"/nix-serve.key
mv nix-serve.pub "$facts"/nix-serve.pub
'';
};
services.nix-serve = {
enable = true;
secretKeyFile = config.clan.core.facts.services.nix-serve.secret."nix-serve.key".path;
port = 5005;
};
healthchecks.closed.public.ports.nix-serve = [ config.services.nix-serve.port ];
healthchecks.http.nix-serve = {
url = "cache.${config.networking.hostName}.wg0/nix-cache-info";
expectedContent = "Priority: 50";
};
services.nginx = {
enable = true;
virtualHosts."cache.${config.networking.hostName}.wg0" = {
locations."/".extraConfig = ''
proxy_pass http://localhost:${toString config.services.nix-serve.port};
allow ${config.wireguard.wg0.subnet};
deny all;
'';
# curl https://cache.nixos.org/nix-cache-info
# lower priority means ask first.
locations."= /nix-cache-info".extraConfig = ''
alias ${pkgs.writeText "cache-info" ''
StoreDir: /nix/store
WantMassQuery: 1
Priority: 50
''};
allow ${config.wireguard.wg0.subnet};
deny all;
'';
};
};
}