{ description = "LakeWatchScraper, scrape data from various sources for LakeWatchAPI"; inputs = { flake-utils.url = "github:numtide/flake-utils"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; poetry2nix = { url = "github:nix-community/poetry2nix"; inputs.nixpkgs.follows = "nixpkgs"; }; }; outputs = { self, nixpkgs, flake-utils, poetry2nix, }: flake-utils.lib.eachDefaultSystem ( system: let pkgs = nixpkgs.legacyPackages.${system}; inherit (poetry2nix.lib.mkPoetry2Nix { inherit pkgs; }) mkPoetryApplication; in rec { packages = { lakewatchscraper = mkPoetryApplication { projectDir = self; }; default = self.packages.${system}.lakewatchscraper; }; apps.default = { type = "app"; program = "${packages.default}/bin/scraper"; }; # Shell for app dependencies. # # nix develop # # Use this shell for developing your app. devShells.default = pkgs.mkShell { inputsFrom = [ self.packages.${system}.lakewatchscraper ]; }; # Shell for poetry. # # nix develop .#poetry # # Use this shell for changes to pyproject.toml and poetry.lock. devShells.poetry = pkgs.mkShell { packages = [ pkgs.poetry ]; }; } ) // { nixosModules.default = { config, lib, pkgs, ... }: let cfg = config.services.lakewatch-scraper; in { options.services.lakewatch-scraper = { enable = lib.mkEnableOption "Enable the lakewatch-api service"; package = lib.mkOption { type = lib.types.package; default = self.packages.${pkgs.system}.default; description = "Package to use for the scraper, defaults to the package provided in the flake"; }; db = lib.mkOption { description = '' Database settings for the application ''; type = lib.types.submodule { options = { name = lib.mkOption { type = lib.types.str; default = "lakewatch"; description = '' The database name to use ''; }; host = lib.mkOption { type = lib.types.str; default = "localhost"; description = '' The database host to use ''; }; port = lib.mkOption { type = lib.types.port; default = 5432; description = '' The port of the database ''; }; passwordFile = lib.mkOption { type = lib.types.path; description = '' The file to read the database password from for the API ''; }; }; }; }; }; config = let username = cfg.db.name; in lib.mkIf cfg.enable { systemd = { timers.lakewatch-scraper = { description = "Trigger the lakewatch scraper every few hours"; wantedBy = [ "timers.target" ]; timerConfig = { OnCalendar = "00/4:00"; Unit = "lakewatch-scraper.service"; }; }; services.lakewatch-scraper = { description = "Scrape data from various data sources for the Lakewatch API"; wantedBy = [ "lakewatch-scraper.timer" ]; environment = { APP_DATABASE_HOST = "${cfg.db.host}"; APP_DATABASE_PORT = "${builtins.toString cfg.db.port}"; APP_DATABASE_USERNAME = "${username}"; APP_DATABASE_NAME = "${cfg.db.name}"; }; serviceConfig = { DynamicUser = true; LoadCredential = [ "APP_DATABASE_PASSWORD_FILE:${cfg.db.passwordFile}" ]; ExecStart = pkgs.writeScript "scraper" '' #!${pkgs.bash}/bin/bash export APP_DATABASE_PASSWORD="$(${pkgs.systemd}/bin/systemd-creds cat APP_DATABASE_PASSWORD_FILE)" ${cfg.package}/bin/scraper ''; Restart = "on-failure"; RestartSec = "5s"; }; }; }; }; }; }; }