{ overlay }: { lib, pkgs, config, ... }: with lib; { options.services.madd-client = { enable = mkEnableOption "MADD client"; endpoint = mkOption { type = types.str; description = "Endpoint for MADD client to connect to."; }; interface = mkOption { type = types.str; default = "eth0"; description = "Network interface to use for MADD client."; }; priv-key-file = mkOption { type = types.str; default = "/etc/ssh/ssh_host_ed25519_key"; description = "Path to the private SSH key file identifying this machine."; }; pub-key-file = mkOption { type = types.str; default = "${config.services.madd-client.priv_key_file}.pub"; description = "Path to the public SSH key file identifying this machine."; }; hostname = mkOption { type = types.str; default = config.networking.hostName; description = "Hostname to use for MADD client."; }; }; options.services.madd-server = { enable = mkEnableOption "MADD server"; }; config = { nixpkgs.overlays = [ overlay ]; } // ( let cfg = config.services.madd-client; in optionalAttrs config.madd-client.enable { systemd.services.madd-client = { description = "MADD Client Service"; wantedBy = [ "multi-user.target" ]; before = [ "network-pre.target" ]; requires = [ "network-pre.target" ]; script = '' ${pkgs.iproute2}/bin/ip -4 monitor address label dev "${cfg.interface}" | while read -r event; do if [[ $event == \[ADDR\]* ]]; then ipv4=$(${pkgs.iproute2}/bin/ip -4 addr show dev "${cfg.interface}" | grep -Po 'inet \K[\d.]+') if [ -n "$ipv4" ]; then export MADD_ENDPOINT="${cfg.endpoint}" export MADD_PRIV_KEY="${cfg.priv-key-file}" export MADD_PUB_KEY="${cfg.pub-key-file}" export MADD_HOSTNAME="${cfg.hostname}" export MADD_IP="$ipv4" ${pkgs.madd-client}/bin/madd-client fi done ''; }; } ) // (optionalAttrs config.madd-server.enable { }); }