Files
slop-broker/nix/modules/nixos/cache.nix
T
jackos1998andClaude Opus 5.5 76eb39fb7e demo: Add a binary cache for slop build
The box VM holds the cache's store in a `cache` account, set up by
`services.slop-broker-cache`, and the demo's broker pushes to it, so
`slop build` can be tried by hand. The broker's key is only known at
boot, so the module exposes its forced command for keys it can't list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 13:27:48 +00:00

97 lines
3.4 KiB
Nix

# 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 <store path>..." >&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;
};
};
};
};
}