12{lib, config, ...}:3let4 inherit (lib.modules) mkIf;5 hasPersistentHostname = config.networking.hostName != "";6in7{8 9 systemd.services.generate-nix-cache-key = mkIf hasPersistentHostname {10 wantedBy = ["multi-user.target"];11 serviceConfig.Type = "oneshot";12 path = [config.nix.package];13 script = ''14 [[ -f /etc/nix/private-key ]] && exit15 nix-store --generate-binary-cache-key ${config.networking.hostName}-1 /etc/nix/private-key /etc/nix/public-key16 '';17 };18 nix.settings.secret-key-files = mkIf hasPersistentHostname "/etc/nix/private-key";19}