From 4a219c8aebf8275118981db856355f88aaa84bca Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Sun, 10 Dec 2023 10:57:50 -0800 Subject: [PATCH 1/3] nut: patch nutshutdown to set a default for NUT_CONFPATH The way we build the package results in the binaries being hardcoded to look for $out/etc/ups.conf instead of /etc/nut/ups.conf (where the module places the file). This results in `upsdrvctl` failing to dispatch shutdown commands (since it cannot find the configuration) and prevents a primary monitor from cutting power to the appropriate UPS. Note that we cannot use `--sysconfdir=/etc/nut` as a configure flag since that results in the install phase trying to install directly into /etc/nut which predictably fails in the sandbox. --- pkgs/applications/misc/nut/default.nix | 7 +++++++ .../misc/nut/nutshutdown-conf-default.patch | 10 ++++++++++ 2 files changed, 17 insertions(+) create mode 100644 pkgs/applications/misc/nut/nutshutdown-conf-default.patch diff --git a/pkgs/applications/misc/nut/default.nix b/pkgs/applications/misc/nut/default.nix index 5596fc6010ec..9436b14e5138 100644 --- a/pkgs/applications/misc/nut/default.nix +++ b/pkgs/applications/misc/nut/default.nix @@ -30,6 +30,13 @@ stdenv.mkDerivation rec { }; patches = [ + # This patch injects a default value for NUT_CONFPATH into the nutshutdown script + # since the way we build the package results in the binaries being hardcoded to check + # $out/etc/ups.conf instead of /etc/nut/ups.conf (where the module places the file). + # We also cannot use `--sysconfdir=/etc/nut` since that results in the install phase + # trying to install directly into /etc/nut which predictably fails + ./nutshutdown-conf-default.patch + (substituteAll { src = ./hardcode-paths.patch; avahi = "${avahi}/lib"; diff --git a/pkgs/applications/misc/nut/nutshutdown-conf-default.patch b/pkgs/applications/misc/nut/nutshutdown-conf-default.patch new file mode 100644 index 000000000000..28e5a286126d --- /dev/null +++ b/pkgs/applications/misc/nut/nutshutdown-conf-default.patch @@ -0,0 +1,10 @@ +diff --git a/scripts/systemd/nutshutdown.in b/scripts/systemd/nutshutdown.in +index ace2485b3..9dee869bb 100755 +--- a/scripts/systemd/nutshutdown.in ++++ b/scripts/systemd/nutshutdown.in +@@ -1,4 +1,5 @@ + #!/bin/sh ++export NUT_CONFPATH="${NUT_CONFPATH-/etc/nut}" + + # This script requires both nut-server (drivers) + # and nut-client (upsmon) to be present locally From 7d9956755e5a31d4718c949fabe786caf538fc50 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Sun, 10 Dec 2023 11:05:18 -0800 Subject: [PATCH 2/3] nut: fix systemd unit patching The NixOS module will create a file in /etc/nut/nut.conf (not in /etc/nut.conf) so the systemd units should be patched accordingly. --- pkgs/applications/misc/nut/default.nix | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pkgs/applications/misc/nut/default.nix b/pkgs/applications/misc/nut/default.nix index 9436b14e5138..85fe7afdaaf2 100644 --- a/pkgs/applications/misc/nut/default.nix +++ b/pkgs/applications/misc/nut/default.nix @@ -18,6 +18,7 @@ , substituteAll , systemd , udev +, gnused }: stdenv.mkDerivation rec { @@ -70,14 +71,18 @@ stdenv.mkDerivation rec { postInstall = '' substituteInPlace $out/lib/systemd/system-shutdown/nutshutdown \ + --replace /bin/sed "${gnused}/bin/sed" \ --replace /bin/sleep "${coreutils}/bin/sleep" \ --replace /bin/systemctl "${systemd}/bin/systemctl" for file in system/{nut-monitor.service,nut-driver-enumerator.service,nut-server.service,nut-driver@.service} system-shutdown/nutshutdown; do - substituteInPlace $out/lib/systemd/$file \ - --replace "$out/etc/nut.conf" "/etc/nut.conf" + substituteInPlace $out/lib/systemd/$file \ + --replace "$out/etc/nut.conf" "/etc/nut/nut.conf" done + substituteInPlace $out/lib/systemd/system/nut-driver-enumerator.path \ + --replace "$out/etc/ups.conf" "/etc/nut/ups.conf" + # we don't need init.d scripts rm -r $out/share/solaris-init ''; From fc004b09e5fbe0affdf4cf7c12cc856dcf214849 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Sun, 10 Dec 2023 11:23:37 -0800 Subject: [PATCH 3/3] nixos/ups: install udev rules for nut This is necessary to allow the usbhid driver to successfully send commands to the attached UPS. It is possible to work around this by explicitly using setting the user flag (e.g. `upsdrvctl -u root shutdown`), though it is much simpler to install the udev rules rather than patch things further. --- nixos/modules/services/monitoring/ups.nix | 1 + pkgs/applications/misc/nut/default.nix | 3 +++ 2 files changed, 4 insertions(+) diff --git a/nixos/modules/services/monitoring/ups.nix b/nixos/modules/services/monitoring/ups.nix index c9dda8a8c093..63afb5deb5bd 100644 --- a/nixos/modules/services/monitoring/ups.nix +++ b/nixos/modules/services/monitoring/ups.nix @@ -590,6 +590,7 @@ in "d /var/lib/nut 700" ]; + services.udev.packages = [ pkgs.nut ]; /* users.users.nut = diff --git a/pkgs/applications/misc/nut/default.nix b/pkgs/applications/misc/nut/default.nix index 85fe7afdaaf2..2f3be342a7a8 100644 --- a/pkgs/applications/misc/nut/default.nix +++ b/pkgs/applications/misc/nut/default.nix @@ -85,6 +85,9 @@ stdenv.mkDerivation rec { # we don't need init.d scripts rm -r $out/share/solaris-init + + # Suspicious/overly broad rule, remove it until we know better + rm $out/etc/udev/rules.d/52-nut-ipmipsu.rules ''; meta = with lib; {