From d6c1627c3e8d449956ae8a61b91c03979b7263e2 Mon Sep 17 00:00:00 2001 From: gigirassy Date: Mon, 10 Nov 2025 17:07:29 +0100 Subject: [PATCH] Add nixos/modules/smart-ipv6-rotator.nix --- nixos/modules/smart-ipv6-rotator.nix | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 nixos/modules/smart-ipv6-rotator.nix diff --git a/nixos/modules/smart-ipv6-rotator.nix b/nixos/modules/smart-ipv6-rotator.nix new file mode 100644 index 0000000..3bd59f1 --- /dev/null +++ b/nixos/modules/smart-ipv6-rotator.nix @@ -0,0 +1,75 @@ +{ config, lib, pkgs, ... }: + +{ + options = { + services.smart-ipv6-rotator = { + enable = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Enable smart-ipv6-rotator service"; + }; + + ipv6range = lib.mkOption { + type = lib.types.str; + default = ""; + description = "IPv6 range to rotate (e.g. 2407:7000:9827:4100::/64)"; + }; + + rev = lib.mkOption { + type = lib.types.str; + default = "master"; + description = "git rev (branch/commit/tag) for the upstream repo"; + }; + + sha256 = lib.mkOption { + type = lib.types.str; + default = "0000000000000000000000000000000000000000000000000000"; + description = "sha256 for fetchFromGitHub (replace after first build)"; + }; + }; + }; + + config = lib.mkIf config.services.smart-ipv6-rotator.enable (let + rev = config.services.smart-ipv6-rotator.rev; + sha256 = config.services.smart-ipv6-rotator.sha256; + + src = pkgs.fetchFromGitHub { + owner = "iv-org"; + repo = "smart-ipv6-rotator"; + inherit rev sha256; + }; + + pythonEnv = pkgs.python3.withPackages (ps: with ps; [ requests pyroute2 ]); + + rotatorPkg = pkgs.runCommand "smart-ipv6-rotator" { inherit src; } '' + mkdir -p $out/bin + cat > $out/bin/smart-ipv6-rotator <<'EOF' +#!${pkgs.stdenv.shell} +exec ${pythonEnv}/bin/python3 ${src}/smart-ipv6-rotator.py "$@" +EOF + chmod +x $out/bin/smart-ipv6-rotator +''; + + in { + environment.systemPackages = [ rotatorPkg ]; + + systemd.services.smart-ipv6-rotator = { + description = "Smart IPv6 Rotator (oneshot)"; + wantedBy = [ "multi-user.target" ]; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${rotatorPkg}/bin/smart-ipv6-rotator run --cron --ipv6range=${config.services.smart-ipv6-rotator.ipv6range} --services google"; + }; + }; + + systemd.timers.smart-ipv6-rotator = { + description = "Run smart-ipv6-rotator twice a day"; + wantedBy = [ "timers.target" ]; + timerConfig = { + OnBootSec = "30s"; + OnUnitActiveSec = "12h"; + }; + unitConfig = { Unit = "smart-ipv6-rotator.service"; }; + }; + }); +} \ No newline at end of file