diff --git a/configs/workhorse/finance.nix b/configs/workhorse/finance.nix index 3bccb26..2e70c49 100644 --- a/configs/workhorse/finance.nix +++ b/configs/workhorse/finance.nix @@ -22,41 +22,34 @@ let 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} - ''; - }; + systemd.services.pull_stocks = { + enable = true; + description = "pull stocks for hledger"; + serviceConfig = { + User = "syncthing"; + Type = "oneshot"; }; - 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"; - }; - }; + script = let + command = { symbol, name, currency, ... }: '' + APIKEY=${lib.fileContents } + SYMBOL="${symbol}" + ${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} + sleep 1 + ''; + in lib.concatStringsSep "\n" (map command stocks); + }; + + systemd.timers.pull_stocks = { + enable = true; + wantedBy = [ "multi-user.target" ]; + timerConfig = { + OnCalendar = "weekly"; + Persistent = "true"; }; - in builtins.listToAttrs (map pullTimer stocks); - + }; }