83 lines
2.8 KiB
Nix
83 lines
2.8 KiB
Nix
# This file is part of birdtown-visit-counter, and is © 2024 Alexander Khodyrev.
|
|
#
|
|
# Full source code is available at https://git.akho.name/akho/birdtown-visit-counter
|
|
#
|
|
# birdtown-visit-counter is free software: you can redistribute it and/or
|
|
# modify it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the License,
|
|
# or (at your option) any later version.
|
|
#
|
|
# birdtown-visit-counter is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
|
# for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
# with birdtown-visit-counter. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
inputs:
|
|
{ config, lib, pkgs, ... }:
|
|
let
|
|
inherit (pkgs.stdenv.hostPlatform) system;
|
|
cfg = config.services.birdtown-visit-counter;
|
|
inherit (lib) types mkOption mkIf;
|
|
package = inputs.self.packages."${system}".default;
|
|
in {
|
|
options.services.birdtown-visit-counter = {
|
|
enable = mkOption {
|
|
type = types.bool;
|
|
default = false;
|
|
};
|
|
listenPort = mkOption {
|
|
type = types.int;
|
|
default = 11568;
|
|
};
|
|
};
|
|
config = mkIf cfg.enable {
|
|
users.users.birdtown-visit-counter = {
|
|
group = "birdtown-visit-counter";
|
|
home = "/var/lib/birdtown-visit-counter";
|
|
isSystemUser = true;
|
|
createHome = true;
|
|
};
|
|
users.groups.birdtown-visit-counter = { };
|
|
|
|
systemd.services.birdtown-visit-counter = {
|
|
after = [ "network.target" ];
|
|
wantedBy = [ "multi-user.target" ];
|
|
serviceConfig = {
|
|
ExecStart =
|
|
"${package}/bin/birdtown-visit-counter -addr :${toString cfg.listenPort} -file visits.json";
|
|
User = "birdtown-visit-counter";
|
|
Group = "birdtown-visit-counter";
|
|
WorkingDirectory = "/var/lib/birdtown-visit-counter";
|
|
};
|
|
};
|
|
|
|
systemd.timers.birdtown-visits-saver = {
|
|
wantedBy = [ "timers.target" ];
|
|
timerConfig = {
|
|
OnBootSec = "10m";
|
|
OnUnitActiveSec = "10m";
|
|
Unit = "birdtown-visits-saver.service";
|
|
};
|
|
};
|
|
|
|
systemd.services.birdtown-visits-saver = {
|
|
script = ''
|
|
set -eu
|
|
if ${pkgs.curl}/bin/curl http://localhost:${toString cfg.listenPort}/visits > visits-new.json; then
|
|
cp visits-new.json "$(date +"%Y-%m-%d-%H-%M").json"
|
|
mv visits-new.json visits.json
|
|
${pkgs.findutils}/bin/find . -ctime +10 -delete
|
|
fi
|
|
'';
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
User = "birdtown-visit-counter";
|
|
Group = "birdtown-visit-counter";
|
|
WorkingDirectory = "/var/lib/birdtown-visit-counter";
|
|
};
|
|
};
|
|
};
|
|
}
|