diff --git a/configs/workhorse/configuration.nix b/configs/workhorse/configuration.nix index 07c6bca..7bb94fc 100644 --- a/configs/workhorse/configuration.nix +++ b/configs/workhorse/configuration.nix @@ -29,6 +29,7 @@ ./jupyter.nix ./mysql.nix ./property.nix + ./finance.nix ]; nixpkgs.config.permittedInsecurePackages = diff --git a/configs/workhorse/finance.nix b/configs/workhorse/finance.nix new file mode 100644 index 0000000..9c939bb --- /dev/null +++ b/configs/workhorse/finance.nix @@ -0,0 +1,62 @@ +{ 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); + +}