···11-{pkgs, lib, config, options, ...}: let
11+{lib, config, ...}: let
22 l = lib;
33 t = l.types;
44 taggedPorts = config.networking.firewall.public;
···1010 type = t.ints.unsigned;
1111 description = "The ID of the firewall to update.";
1212 };
1313- app = l.mkOption {
1414- type = t.package;
1313+ mkApp = l.mkOption {
1414+ type = t.functionTo t.package;
1515 readOnly = true;
1616 description = ''
1717- The generated app for this provider, run it to apply the configuration.
1717+ Function that generates a script for this provider, pass it an instance of nixpkgs and run to apply the configuration.
18181919- For this to work, you need to set the `HETZNER_API_TOKEN` environment variable to a valid API token from Hetzner.
1919+ For this app to work, you need to set the `HETZNER_API_TOKEN` environment variable to a valid API token from Hetzner.
2020 '';
2121 };
2222 };
2323 };
24242525- config = let
2626- mkRule = proto: tag: port: {
2727- description = tag;
2828- direction = "in";
2929- protocol = proto;
3030- port =
3131- if l.isAttrs port
3232- then l.concatMapStringsSep "-" toString [port.from port.to]
3333- else toString port;
3434- source_ips = ["0.0.0.0/0" "::/0"];
3535- };
3636- mkTcpRule = mkRule "tcp";
3737- mkUdpRule = mkRule "udp";
3838- firewallRules = pkgs.writers.writeJSON "hetzner-firewall-${toString cfg.id}-rules.json" {
3939- rules = l.flatten (
4040- l.mapAttrsToList
4141- (tag: ports: [
4242- (l.map (mkTcpRule tag) ports.allowedTCPPorts)
4343- (l.map (mkTcpRule tag) ports.allowedTCPPortRanges)
4444- (l.map (mkUdpRule tag) ports.allowedUDPPorts)
4545- (l.map (mkUdpRule tag) ports.allowedUDPPortRanges)
4646- ])
4747- taggedPorts
4848- );
2525+ config = {
2626+ providers.hetzner.firewall.mkApp = pkgs: import ./app.nix {
2727+ inherit pkgs lib taggedPorts;
2828+ inherit (cfg) id;
4929 };
5050- in {
5151- providers.hetzner.firewall.app = pkgs.writers.writeNu "apply-hetzner" ''
5252- let firewallId = ${toString cfg.id}
5353- let rulesFile = "${firewallRules}"
5454- ${l.fileContents ./app.nu}
5555- '';
5630 };
5731}