# The binary cache side (`services.slop-broker-cache`): a forced command on the cache store owner's # key that allows only the store protocol and GC roots, and a timer expiring those roots. { lib, pkgs, config, ... }: let inherit (lib) mkIf hasPrefix; mkOpt' = type: default: description: lib.mkOption { inherit type default description; }; mkBoolOpt' = default: description: mkOpt' lib.types.bool default description; cfg = config.services.slop-broker-cache; roots = "${cfg.store}/nix/var/nix/gcroots/slop-broker"; # Allows the store protocol (what `nix copy --to ssh-ng://` runs) and adding GC roots, nothing else forcedCommand = pkgs.writeShellScript "slop-broker-cache" '' set -euo pipefail export HOME=${cfg.home} store=${cfg.store} roots=${roots} read -ra words <<< "''${SSH_ORIGINAL_COMMAND-}" case "''${words[0]-}" in nix-daemon) if [ "$SSH_ORIGINAL_COMMAND" != "nix-daemon --stdio" ]; then echo "only nix-daemon --stdio is allowed" >&2 exit 1 fi exec ${config.nix.package}/bin/nix-daemon --stdio --store "$store" ;; root) mkdir -p "$roots" for path in "''${words[@]:1}"; do if ! [[ $path =~ ^/nix/store/[0-9a-z]{32}-[A-Za-z0-9+._?=-]+$ ]]; then echo "invalid store path: $path" >&2 exit 1 fi # Replacing a root renews it ln -sfn "$path" "$roots/''${path#/nix/store/}" done ;; *) echo "allowed commands: nix-daemon --stdio, root ..." >&2 exit 1 ;; esac ''; in { options.services.slop-broker-cache = with lib.types; { enable = mkBoolOpt' false "Whether to let `slop-broker` push builds into a binary cache's store."; user = mkOpt' str null "Owner of the store; brokers log in as this user."; store = mkOpt' str null "Root of the store the cache serves."; home = mkOpt' str null "`HOME` for the store's commands; a directory `user` can write to."; keepDays = mkOpt' ints.positive 14 "Days a pushed path stays rooted after its last push."; brokerKeys = mkOpt' (listOf str) [ ] "SSH public keys of brokers that may push."; forcedCommand = lib.mkOption { type = path; default = forcedCommand; readOnly = true; description = "The forced command for brokers' keys, for keys `brokerKeys` can't list, such as ones made at boot."; }; }; config = mkIf cfg.enable { assertions = [ { assertion = hasPrefix "/" cfg.store && hasPrefix "/" cfg.home; message = "slop-broker cache needs absolute store and home paths."; } ]; users.users.${cfg.user}.openssh.authorizedKeys.keys = map (k: ''restrict,command="${cfg.forcedCommand}" ${k}'') cfg.brokerKeys; # Expired roots go daily; the store's own garbage collection then takes their paths systemd = { services.slop-broker-cache-expire = { description = "Expire slop-broker's GC roots in the binary cache"; serviceConfig = { Type = "oneshot"; User = cfg.user; }; script = '' if [ -d ${roots} ]; then find ${roots} -type l -mtime +${toString cfg.keepDays} -delete fi ''; }; timers.slop-broker-cache-expire = { wantedBy = [ "timers.target" ]; timerConfig = { OnCalendar = "daily"; Persistent = true; }; }; }; }; }