nixos/atop: Fix regression in enabling atop units

Fix regression where the systemd units for atop are no longer
automatically started at boot when programs.atop.enable = true.

Regression was introduced in commit: 09350ff7d4
  nixos/atop: Convert log format to fix service start

This commit restructures the atop systemd service config so that the
code to convert the log format gets configured as a preStart script
along with the addition of the wantedBy rule.
This commit is contained in:
Bruce Toll 2023-06-30 10:11:23 -04:00
parent 4bc72cae10
commit 8f4f1ce005

View File

@ -123,8 +123,8 @@ in
boot.extraModulePackages = [ (lib.mkIf cfg.netatop.enable cfg.netatop.package) ]; boot.extraModulePackages = [ (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
systemd = systemd =
let let
mkSystemd = type: cond: name: restartTriggers: { mkSystemd = type: name: restartTriggers: {
${name} = lib.mkIf cond { ${name} = {
inherit restartTriggers; inherit restartTriggers;
wantedBy = [ (if type == "services" then "multi-user.target" else if type == "timers" then "timers.target" else null) ]; wantedBy = [ (if type == "services" then "multi-user.target" else if type == "timers" then "timers.target" else null) ];
}; };
@ -134,42 +134,44 @@ in
in in
{ {
packages = [ atop (lib.mkIf cfg.netatop.enable cfg.netatop.package) ]; packages = [ atop (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
services = services = lib.mkMerge [
mkService cfg.atopService.enable "atop" [ atop ] (lib.mkIf cfg.atopService.enable (lib.recursiveUpdate
// lib.mkIf cfg.atopService.enable { (mkService "atop" [ atop ])
# always convert logs to newer version first {
# XXX might trigger TimeoutStart but restarting atop.service will # always convert logs to newer version first
# convert remainings logs and start eventually # XXX might trigger TimeoutStart but restarting atop.service will
atop.serviceConfig.ExecStartPre = pkgs.writeShellScript "atop-update-log-format" '' # convert remainings logs and start eventually
set -e -u atop.preStart = ''
shopt -s nullglob set -e -u
for logfile in "$LOGPATH"/atop_* shopt -s nullglob
do for logfile in "$LOGPATH"/atop_*
${atop}/bin/atopconvert "$logfile" "$logfile".new do
# only replace old file if version was upgraded to avoid ${atop}/bin/atopconvert "$logfile" "$logfile".new
# false positives for atop-rotate.service # only replace old file if version was upgraded to avoid
if ! ${pkgs.diffutils}/bin/cmp -s "$logfile" "$logfile".new # false positives for atop-rotate.service
then if ! ${pkgs.diffutils}/bin/cmp -s "$logfile" "$logfile".new
${pkgs.coreutils}/bin/mv -v -f "$logfile".new "$logfile" then
else ${pkgs.coreutils}/bin/mv -v -f "$logfile".new "$logfile"
${pkgs.coreutils}/bin/rm -f "$logfile".new else
fi ${pkgs.coreutils}/bin/rm -f "$logfile".new
done fi
''; done
} '';
// mkService cfg.atopacctService.enable "atopacct" [ atop ] }))
// mkService cfg.netatop.enable "netatop" [ cfg.netatop.package ] (lib.mkIf cfg.atopacctService.enable (mkService "atopacct" [ atop ]))
// mkService cfg.atopgpu.enable "atopgpu" [ atop ]; (lib.mkIf cfg.netatop.enable (mkService "netatop" [ cfg.netatop.package ]))
timers = mkTimer cfg.atopRotateTimer.enable "atop-rotate" [ atop ]; (lib.mkIf cfg.atopgpu.enable (mkService "atopgpu" [ atop ]))
];
timers = lib.mkIf cfg.atopRotateTimer.enable (mkTimer "atop-rotate" [ atop ]);
}; };
security.wrappers = lib.mkIf cfg.setuidWrapper.enable { security.wrappers = lib.mkIf cfg.setuidWrapper.enable {
atop = atop = {
{ setuid = true; setuid = true;
owner = "root"; owner = "root";
group = "root"; group = "root";
source = "${atop}/bin/atop"; source = "${atop}/bin/atop";
}; };
}; };
} }
); );