From bddea23f2eed1dce28e11066aaa4573205dd6df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Forsman?= Date: Tue, 10 Sep 2024 21:26:42 +0200 Subject: [PATCH 1/2] nixos/ups: deduplicate environment variables No functional change, but more DRY. --- nixos/modules/services/monitoring/ups.nix | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/nixos/modules/services/monitoring/ups.nix b/nixos/modules/services/monitoring/ups.nix index 0d157afd51a0b56..8de7812ee203e7d 100644 --- a/nixos/modules/services/monitoring/ups.nix +++ b/nixos/modules/services/monitoring/ups.nix @@ -5,6 +5,11 @@ let cfg = config.power.ups; defaultPort = 3493; + envVars = { + NUT_CONFPATH = "/etc/nut"; + NUT_STATEPATH = "/var/lib/nut"; + }; + nutFormat = { type = with lib.types; let @@ -517,8 +522,7 @@ in ExecReload = "${pkgs.nut}/sbin/upsmon -c reload"; LoadCredential = lib.mapAttrsToList (name: monitor: "upsmon_password_${name}:${monitor.passwordFile}") cfg.upsmon.monitor; }; - environment.NUT_CONFPATH = "/etc/nut"; - environment.NUT_STATEPATH = "/var/lib/nut"; + environment = envVars; }; systemd.services.upsd = let @@ -537,8 +541,7 @@ in ExecReload = "${pkgs.nut}/sbin/upsd -c reload"; LoadCredential = lib.mapAttrsToList (name: user: "upsdusers_password_${name}:${user.passwordFile}") cfg.users; }; - environment.NUT_CONFPATH = "/etc/nut"; - environment.NUT_STATEPATH = "/var/lib/nut"; + environment = envVars; restartTriggers = [ config.environment.etc."nut/upsd.conf".source ]; @@ -555,8 +558,7 @@ in # TODO: replace 'root' by another username. ExecStart = "${pkgs.nut}/bin/upsdrvctl -u root start"; }; - environment.NUT_CONFPATH = "/etc/nut"; - environment.NUT_STATEPATH = "/var/lib/nut"; + environment = envVars; restartTriggers = [ config.environment.etc."nut/ups.conf".source ]; From 3682979b4401261b4d5434d16bc9618740887291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Forsman?= Date: Tue, 10 Sep 2024 21:29:41 +0200 Subject: [PATCH 2/2] nixos/ups: set env vars in the global environment This allows using upsdrvctl interactively, which otherwise tries to use a missing ups.conf in the Nix store, instead of the correct /etc/nut/ups.conf. --- nixos/modules/services/monitoring/ups.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nixos/modules/services/monitoring/ups.nix b/nixos/modules/services/monitoring/ups.nix index 8de7812ee203e7d..67b15b520f59b10 100644 --- a/nixos/modules/services/monitoring/ups.nix +++ b/nixos/modules/services/monitoring/ups.nix @@ -498,7 +498,9 @@ in }) ]; + # For interactive use. environment.systemPackages = [ pkgs.nut ]; + environment.variables = envVars; networking.firewall = lib.mkIf cfg.openFirewall { allowedTCPPorts =