27c45b82cc
This greatly improves readability and makes it easier to discover options. This commit was genereated by running the following script inside the repo root dir: #!/usr/bin/env ruby def transform(src) return false if src.include?('inherit options;') success = false options = nil src.sub!(/^ options.*?^ }.*?;/m) do |match| options = match " inherit options;" end return false if !options src.sub!(/^with lib;\s*let\n+/m) do |match| success = true <<~EOF with lib; let #{options} EOF end success end Dir['modules/**/*.nix'].each do |f| src = File.read(f) if transform(src) puts "Changed file #{f}" File.write(f, src) end end
108 lines
3.6 KiB
Nix
108 lines
3.6 KiB
Nix
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
let
|
|
options.services.recurring-donations = {
|
|
enable = mkEnableOption "recurring-donations";
|
|
tallycoin = mkOption {
|
|
type = types.attrs;
|
|
default = {};
|
|
description = ''
|
|
This option is used to specify tallycoin donation receivers using an
|
|
attribute set. For example the following setting instructs the module
|
|
to repeatedly send 1000 satoshis to djbooth007.
|
|
{
|
|
"djbooth007" = 1000;
|
|
}
|
|
'';
|
|
};
|
|
interval = mkOption {
|
|
type = types.str;
|
|
default = "Mon *-*-* 00:00:00";
|
|
description = ''
|
|
Schedules the donations. Default is weekly on Mon 00:00:00. See `man
|
|
systemd.time` for further options.
|
|
'';
|
|
};
|
|
randomizedDelaySec = mkOption {
|
|
type = types.int;
|
|
default = 86400;
|
|
description = ''
|
|
Random delay to add to scheduled time for donation. Default is one day.
|
|
'';
|
|
};
|
|
enforceTor = nbLib.enforceTor;
|
|
};
|
|
|
|
cfg = config.services.recurring-donations;
|
|
nbLib = config.nix-bitcoin.lib;
|
|
recurring-donations-script = pkgs.writeScript "recurring-donations.sh" ''
|
|
LNCLI="${config.nix-bitcoin.pkgs.clightning}/bin/lightning-cli --lightning-dir=${config.services.clightning.dataDir}"
|
|
pay_tallycoin() {
|
|
NAME=$1
|
|
AMOUNT=$2
|
|
echo "Attempting to pay $AMOUNT sat to $NAME"
|
|
INVOICE=$(curl --socks5-hostname ${config.nix-bitcoin.torClientAddressWithPort} -d "satoshi_amount=$AMOUNT&payment_method=ln&id=$NAME&type=profile" -X POST https://api.tallyco.in/v1/payment/request/ | jq -r '.lightning_pay_request') 2> /dev/null
|
|
if [ -z "$INVOICE" ] || [ "$INVOICE" = "null" ]; then
|
|
echo "ERROR: did not get invoice from tallycoin"
|
|
return
|
|
fi
|
|
# Decode invoice and compare amount with requested amount
|
|
DECODED_AMOUNT=$($LNCLI decodepay "$INVOICE" | jq -r '.amount_msat' | head -c -8)
|
|
if [ -z "$DECODED_AMOUNT" ] || [ "$DECODED_AMOUNT" = "null" ]; then
|
|
echo "ERROR: did not get response from clightning"
|
|
return
|
|
fi
|
|
if [ $DECODED_AMOUNT -eq $AMOUNT ]; then
|
|
echo "Paying with invoice $INVOICE"
|
|
$LNCLI pay "$INVOICE"
|
|
else
|
|
echo "ERROR: requested amount and invoice amount do not match. $AMOUNT vs $DECODED_AMOUNT"
|
|
return
|
|
fi
|
|
}
|
|
${ builtins.foldl'
|
|
(x: receiver: x +
|
|
''
|
|
pay_tallycoin ${receiver} ${toString (builtins.getAttr receiver cfg.tallycoin)}
|
|
'')
|
|
""
|
|
(builtins.attrNames cfg.tallycoin)
|
|
}
|
|
'';
|
|
in {
|
|
inherit options;
|
|
|
|
config = mkIf cfg.enable {
|
|
services.clightning.enable = true;
|
|
|
|
systemd.services.recurring-donations = {
|
|
requires = [ "clightning.service" ];
|
|
after = [ "clightning.service" ];
|
|
path = with pkgs; [ nix-bitcoin.clightning curl jq ];
|
|
serviceConfig = nbLib.defaultHardening // {
|
|
ExecStart = "${pkgs.bash}/bin/bash ${recurring-donations-script}";
|
|
User = "recurring-donations";
|
|
Type = "oneshot";
|
|
} // nbLib.allowedIPAddresses cfg.enforceTor;
|
|
};
|
|
systemd.timers.recurring-donations = {
|
|
requires = [ "clightning.service" ];
|
|
after = [ "clightning.service" ];
|
|
timerConfig = {
|
|
Unit = "recurring-donations.service";
|
|
OnCalendar = cfg.interval;
|
|
RandomizedDelaySec = toString cfg.randomizedDelaySec;
|
|
};
|
|
wantedBy = [ "multi-user.target" ];
|
|
};
|
|
|
|
users.users.recurring-donations = {
|
|
isSystemUser = true;
|
|
group = "recurring-donations";
|
|
extraGroups = [ config.services.clightning.group ];
|
|
};
|
|
users.groups.recurring-donations = {};
|
|
};
|
|
}
|