module.nix
1 { 2 config, 3 lib, 4 pkgs, 5 ... 6 }: let 7 inherit (lib.lists) optional; 8 inherit (lib.meta) getExe; 9 inherit (lib.modules) mkIf; 10 inherit (lib.options) mkEnableOption mkOption mkPackageOption; 11 inherit (lib.strings) escapeShellArgs; 12 inherit (lib.types) int nullOr str nonEmptyListOf; 13 14 args = 15 (map (subdomain: "--subdomain=${subdomain}") cfg.subdomains) 16 ++ (optional (cfg.fqdn != null) "--fqdn=${cfg.fqdn}") 17 ++ (optional (cfg.ttl != null) "--ttl=${toString cfg.ttl}"); 18 19 cfg = config.services.avahi-subdomains; 20 in { 21 options.services.avahi-subdomains = { 22 enable = mkEnableOption "avahi-subdomains"; 23 package = mkPackageOption pkgs "avahi-subdomains" {}; 24 ttl = mkOption { 25 type = nullOr int; 26 default = null; 27 }; 28 fqdn = mkOption { 29 type = nullOr str; 30 default = null; 31 }; 32 subdomains = mkOption { 33 type = nonEmptyListOf str; 34 }; 35 }; 36 37 config = mkIf cfg.enable { 38 services.avahi = { 39 enable = true; 40 publish = { 41 enable = true; 42 addresses = true; 43 userServices = true; 44 }; 45 }; 46 47 systemd.services = { 48 avahi-daemon.wants = ["avahi-subdomains.service"]; 49 50 avahi-subdomains = { 51 description = "avahi-subdomains"; 52 requires = ["avahi-daemon.service"]; 53 after = ["avahi-daemon.service"]; 54 partOf = ["avahi-daemon.service"]; 55 56 serviceConfig = { 57 ExecStart = "${getExe cfg.package} ${escapeShellArgs args}"; 58 Restart = "on-failure"; 59 RestartSec = 5; 60 }; 61 }; 62 }; 63 }; 64 65 _file = ./module.nix; 66 _class = "nixos"; 67 }