From 3893fb607860a8bbe698dcaee05a1596caec1e82 Mon Sep 17 00:00:00 2001 From: hektor Date: Sun, 11 Oct 2026 12:17:41 +0200 Subject: [PATCH] fix(wol): generalize 'wol-*' scripts --- hosts/andromache/default.nix | 6 +--- hosts/andromache/wol-interfaces.nix | 3 -- hosts/astyanax/default.nix | 11 +------ hosts/eetion-02/default.nix | 1 + modules/wol/default.nix | 50 +++++++++++++++++++++-------- 5 files changed, 40 insertions(+), 31 deletions(-) delete mode 100644 hosts/andromache/wol-interfaces.nix diff --git a/hosts/andromache/default.nix b/hosts/andromache/default.nix index 2a628c1..ad6ac57 100644 --- a/hosts/andromache/default.nix +++ b/hosts/andromache/default.nix @@ -8,7 +8,6 @@ let meta = import ./meta.nix; - wolInterfaces = import ./wol-interfaces.nix; in { imports = [ @@ -88,10 +87,7 @@ in }; tailscale.enable = true; taskwarrior.enable = true; - wol = { - enable = true; - interfaces.eno1 = { inherit (wolInterfaces.eno1) macAddress; }; - }; + wol.interfaces.eno1.macAddress = "02:68:b3:29:da:98"; zfs.enable = true; disko.devices = { diff --git a/hosts/andromache/wol-interfaces.nix b/hosts/andromache/wol-interfaces.nix deleted file mode 100644 index 51ad843..0000000 --- a/hosts/andromache/wol-interfaces.nix +++ /dev/null @@ -1,3 +0,0 @@ -{ - eno1.macAddress = "02:68:b3:29:da:98"; -} diff --git a/hosts/astyanax/default.nix b/hosts/astyanax/default.nix index c558822..3525da2 100644 --- a/hosts/astyanax/default.nix +++ b/hosts/astyanax/default.nix @@ -71,6 +71,7 @@ in }; }; taskwarrior.enable = true; + wol.wakes = [ "andromache" ]; zfs.enable = true; secrets.nixSigningKey.enable = true; @@ -82,16 +83,6 @@ in hardware.cpu.intel.updateMicrocode = true; - environment.systemPackages = [ - (pkgs.writeShellApplication { - name = "wol-andromache"; - runtimeInputs = [ pkgs.wakeonlan ]; - text = '' - wakeonlan ${(import ../andromache/wol-interfaces.nix).eno1.macAddress} - ''; - }) - ]; - networking = { hostId = "5f0bde3c"; useDHCP = false; diff --git a/hosts/eetion-02/default.nix b/hosts/eetion-02/default.nix index b8c065f..653fe79 100644 --- a/hosts/eetion-02/default.nix +++ b/hosts/eetion-02/default.nix @@ -16,6 +16,7 @@ in ssh.enable = true; tailscale.enable = true; + wol.wakes = [ "andromache" ]; boot = { kernelParams = [ diff --git a/modules/wol/default.nix b/modules/wol/default.nix index 837740e..d156d2e 100644 --- a/modules/wol/default.nix +++ b/modules/wol/default.nix @@ -1,33 +1,57 @@ -{ lib, config, ... }: +{ + lib, + config, + outputs, + paths, + myUtils, + pkgs, + ... +}: let - inherit (lib) mkEnableOption mkOption types; + hosts = myUtils.dirNames paths.hosts; + interfaces = host: outputs.nixosConfigurations.${host}.config.wol.interfaces; in { options.wol = { - enable = mkEnableOption "Wake-on-LAN configuration"; + wakes = lib.mkOption { + type = lib.types.listOf (lib.types.enum hosts); + }; - interfaces = mkOption { - type = types.attrsOf ( - types.submodule { + interfaces = lib.mkOption { + type = lib.types.attrsOf ( + lib.types.submodule { options = { - macAddress = mkOption { - type = types.str; - example = "02:68:b3:29:da:98"; - }; + macAddress = lib.mkOption { type = lib.types.str; }; }; } ); - default = { }; }; }; - config = lib.mkIf config.wol.enable { + config = { + assertions = map (host: { + assertion = interfaces host != { }; + message = "cannot set up wake-on-lan for '${host}' as it declares no 'wol.interfaces'."; + }) config.wol.wakes; + networking.interfaces = lib.mapAttrs (_: iface: { wakeOnLan.enable = true; inherit (iface) macAddress; }) config.wol.interfaces; - # networking.firewall.allowedUDPPorts = [ 9 ]; + environment.systemPackages = map ( + host: + let + macAddresses = lib.mapAttrsToList (_: interface: interface.macAddress) (interfaces host); + in + pkgs.writeShellApplication { + name = "wol-${host}"; + runtimeInputs = [ pkgs.wakeonlan ]; + text = '' + wakeonlan ${lib.escapeShellArgs macAddresses} + ''; + } + ) config.wol.wakes; }; }