nixos-config/configs/workhorse/finance.nix

63 lines
1.8 KiB
Nix
Raw Normal View History

2021-04-24 14:23:37 +02:00
{ lib, config, pkgs, ... }:
let
# find symbols with
2021-04-27 07:03:55 +02:00
# https://www.alphavantage.co/query?function=SYMBOL_SEARCH&apikey=<api_key>&keywords=<keywords>
2021-04-24 14:23:37 +02:00
# as described here : https://www.alphavantage.co/documentation/#symbolsearch
#
# example:
# --------
# stocks = [
# {
# friendly_name = "google";
# symbol = "GOOGL.DEX";
# name = "google";
# currency = "$";
# }
# ];
# results in
# P 2020-01-30 GOOGL $123
stocks = import <secrets/finance/stocks>;
stocksFile = toString /home/syncthing/finance/hledger/stocks.journal;
in {
systemd.services = let
pullService = { name, symbol, currency, friendly_name, ... }: {
name = "pull_stock_${friendly_name}";
value = {
enable = true;
description = "pull stock_${friendly_name} for hledger";
serviceConfig = {
User = "syncthing";
Type = "oneshot";
};
script = ''
SYMBOL="${symbol}"
APIKEY=${lib.fileContents <secrets/finance/alphavantage/apikey>}
${pkgs.curl}/bin/curl --location --silent \
"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=$SYMBOL&apikey=$APIKEY" \
| ${pkgs.jq}/bin/jq --raw-output '.["Global Quote"]
| "P \(.["07. latest trading day"]) ${name} ${currency}\(.["05. price"] | tonumber)"' \
>> ${stocksFile}
'';
};
};
in builtins.listToAttrs (map pullService stocks);
systemd.timers = let
pullTimer = { friendly_name, ... }: {
name = "pull_stock_${friendly_name}";
value = {
enable = true;
wantedBy = [ "multi-user.target" ];
timerConfig = {
OnCalendar = "weekly";
Persistent = "true";
};
};
};
in builtins.listToAttrs (map pullTimer stocks);
}