{ lib, config, pkgs, ... }: let # find symbols with # https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords=&apikey= # 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 ; 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 } ${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); }