git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #123053 from pschyska/master

atop, netatop, nixos/atop: improve packaging and options

commit02ba3238d2be056673a3a6bbaedc09db6e6448f5 authorMichael Raskin <7c6f434c@mail.ru> dateTue 18 May 2021, 10:54 (6 years ago) parente3088f6033cdff
10 files changed+475−29
nixos/modules/programs/atop.nix+118−8 nixos/tests/all-tests.nix+1 nixos/tests/atop.nix+213 pkgs/os-specific/linux/atop/atop.service.patch+10 pkgs/os-specific/linux/atop/atopacct.service.patch+7 pkgs/os-specific/linux/atop/default.nix+47−16 pkgs/os-specific/linux/atop/fix-paths.patch+48 pkgs/os-specific/linux/netatop/default.nix+13−5 pkgs/os-specific/linux/netatop/fix-paths.patch+11 pkgs/os-specific/linux/netatop/netatop.service.patch+7
nixos/modules/programs/atop.nix+118−8
@@ -1,6 +1,6 @@
11# Global configuration for atop.
22
3−{ config, lib, ... }:
3+{ config, lib, pkgs, ... }:
44
55with lib;
66
@@ -12,11 +12,83 @@ in
1212
1313 options = {
1414
15− programs.atop = {
15+ programs.atop = rec {
1616
17+ enable = mkEnableOption "Atop";
18+
19+ package = mkOption {
20+ type = types.package;
21+ default = pkgs.atop;
22+ description = ''
23+ Which package to use for Atop.
24+ '';
25+ };
26+
27+ netatop = {
28+ enable = mkOption {
29+ type = types.bool;
30+ default = false;
31+ description = ''
32+ Whether to install and enable the netatop kernel module.
33+ Note: this sets the kernel taint flag "O" for loading out-of-tree modules.
34+ '';
35+ };
36+ package = mkOption {
37+ type = types.package;
38+ default = config.boot.kernelPackages.netatop;
39+ description = ''
40+ Which package to use for netatop.
41+ '';
42+ };
43+ };
44+
45+ atopgpu.enable = mkOption {
46+ type = types.bool;
47+ default = false;
48+ description = ''
49+ Whether to install and enable the atopgpud daemon to get information about
50+ NVIDIA gpus.
51+ '';
52+ };
53+
54+ setuidWrapper.enable = mkOption {
55+ type = types.bool;
56+ default = false;
57+ description = ''
58+ Whether to install a setuid wrapper for Atop. This is required to use some of
59+ the features as non-root user (e.g.: ipc information, netatop, atopgpu).
60+ Atop tries to drop the root privileges shortly after starting.
61+ '';
62+ };
63+
64+ atopService.enable = mkOption {
65+ type = types.bool;
66+ default = true;
67+ description = ''
68+ Whether to enable the atop service responsible for storing statistics for
69+ long-term analysis.
70+ '';
71+ };
72+ atopRotateTimer.enable = mkOption {
73+ type = types.bool;
74+ default = true;
75+ description = ''
76+ Whether to enable the atop-rotate timer, which restarts the atop service
77+ daily to make sure the data files are rotate.
78+ '';
79+ };
80+ atopacctService.enable = mkOption {
81+ type = types.bool;
82+ default = true;
83+ description = ''
84+ Whether to enable the atopacct service which manages process accounting.
85+ This allows Atop to gather data about processes that disappeared in between
86+ two refresh intervals.
87+ '';
88+ };
1789 settings = mkOption {
1890 type = types.attrs;
19− default = {};
91+ default = { };
2092 example = {
2193 flags = "a1f";
2294 interval = 5;
@@ -25,12 +97,50 @@ in
2597 Parameters to be written to <filename>/etc/atoprc</filename>.
2698 '';
2799 };
28−
29100 };
30101 };
31102
32− config = mkIf (cfg.settings != {}) {
33− environment.etc.atoprc.text =
34− concatStrings (mapAttrsToList (n: v: "${n} ${toString v}\n") cfg.settings);
35− };
103+ config = mkIf cfg.enable (
104+ let
105+ atop =
106+ if cfg.atopgpu.enable then
107+ (cfg.package.override { withAtopgpu = true; })
108+ else
109+ cfg.package;
110+ in
111+ {
112+ environment.etc = mkIf (cfg.settings != { }) {
113+ atoprc.text = concatStrings
114+ (mapAttrsToList
115+ (n: v: ''
116+ ${n} ${toString v}
117+ '')
118+ cfg.settings);
119+ };
120+ environment.systemPackages = [ atop (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
121+ boot.extraModulePackages = [ (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
122+ systemd =
123+ let
124+ mkSystemd = type: cond: name: restartTriggers: {
125+ ${name} = lib.mkIf cond {
126+ inherit restartTriggers;
127+ wantedBy = [ (if type == "services" then "multi-user.target" else if type == "timers" then "timers.target" else null) ];
128+ };
129+ };
130+ mkService = mkSystemd "services";
131+ mkTimer = mkSystemd "timers";
132+ in
133+ {
134+ packages = [ atop (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
135+ services =
136+ mkService cfg.atopService.enable "atop" [ atop ]
137+ // mkService cfg.atopacctService.enable "atopacct" [ atop ]
138+ // mkService cfg.netatop.enable "netatop" [ cfg.netatop.package ]
139+ // mkService cfg.atopgpu.enable "atopgpu" [ atop ];
140+ timers = mkTimer cfg.atopRotateTimer.enable "atop-rotate" [ atop ];
141+ };
142+ security.wrappers =
143+ lib.mkIf cfg.setuidWrapper.enable { atop = { source = "${atop}/bin/atop"; }; };
144+ }
145+ );
36146}
nixos/tests/all-tests.nix+1
@@ -29,6 +29,7 @@ in
2929 ammonite = handleTest ./ammonite.nix {};
3030 apparmor = handleTest ./apparmor.nix {};
3131 atd = handleTest ./atd.nix {};
32+ atop = handleTest ./atop.nix {};
3233 avahi = handleTest ./avahi.nix {};
3334 avahi-with-resolved = handleTest ./avahi.nix { networkd = true; };
3435 awscli = handleTest ./awscli.nix { };
nixos/tests/atop.nixnew file+213
@@ -0,0 +1,213 @@
1+{ system ? builtins.currentSystem
2+, config ? { }
3+, pkgs ? import ../.. { inherit system config; }
4+}:
5+
6+with import ../lib/testing-python.nix { inherit system pkgs; };
7+with pkgs.lib;
8+
9+let assertions = rec {
10+ path = program: path: ''
11+ with subtest("The path of ${program} should be ${path}"):
12+ p = machine.succeed("type -p \"${program}\" | head -c -1")
13+ assert p == "${path}", f"${program} is {p}, expected ${path}"
14+ '';
15+ unit = name: state: ''
16+ with subtest("Unit ${name} should be ${state}"):
17+ machine.require_unit_state("${name}", "${state}")
18+ '';
19+ version = ''
20+ import re
21+
22+ with subtest("binary should report the correct version"):
23+ pkgver = "${pkgs.atop.version}"
24+ ver = re.sub(r'(?s)^Version: (\d\.\d\.\d).*', r'\1', machine.succeed("atop -V"))
25+ assert ver == pkgver, f"Version is `{ver}`, expected `{pkgver}`"
26+ '';
27+ atoprc = contents:
28+ if builtins.stringLength contents > 0 then ''
29+ with subtest("/etc/atoprc should have the correct contents"):
30+ f = machine.succeed("cat /etc/atoprc")
31+ assert f == "${contents}", f"/etc/atoprc contents: '{f}', expected '${contents}'"
32+ '' else ''
33+ with subtest("/etc/atoprc should not be present"):
34+ machine.succeed("test ! -e /etc/atoprc")
35+ '';
36+ wrapper = present:
37+ if present then path "atop" "/run/wrappers/bin/atop" + ''
38+ with subtest("Wrapper should be setuid root"):
39+ stat = machine.succeed("stat --printf '%a %u' /run/wrappers/bin/atop")
40+ assert stat == "4511 0", f"Wrapper stat is {stat}, expected '4511 0'"
41+ ''
42+ else path "atop" "/run/current-system/sw/bin/atop";
43+ atopService = present:
44+ if present then
45+ unit "atop.service" "active"
46+ + ''
47+ with subtest("atop.service should have written some data to /var/log/atop"):
48+ files = int(machine.succeed("ls -1 /var/log/atop | wc -l"))
49+ assert files > 0, "Expected at least 1 data file"
50+ '' else unit "atop.service" "inactive";
51+ atopRotateTimer = present:
52+ unit "atop-rotate.timer" (if present then "active" else "inactive");
53+ atopacctService = present:
54+ if present then
55+ unit "atopacct.service" "active"
56+ + ''
57+ with subtest("atopacct.service should enable process accounting"):
58+ machine.succeed("test -f /run/pacct_source")
59+
60+ with subtest("atopacct.service should write data to /run/pacct_shadow.d"):
61+ files = int(machine.succeed("ls -1 /run/pacct_shadow.d | wc -l"))
62+ assert files >= 1, "Expected at least 1 pacct_shadow.d file"
63+ '' else unit "atopacct.service" "inactive";
64+ netatop = present:
65+ if present then
66+ unit "netatop.service" "active"
67+ + ''
68+ with subtest("The netatop kernel module should be loaded"):
69+ out = machine.succeed("modprobe -n -v netatop")
70+ assert out == "", f"Module should be loaded already, but modprobe would have done {out}."
71+ '' else ''
72+ with subtest("The netatop kernel module should be absent"):
73+ machine.fail("modprobe -n -v netatop")
74+ '';
75+ atopgpu = present:
76+ if present then
77+ (unit "atopgpu.service" "active") + (path "atopgpud" "/run/current-system/sw/bin/atopgpud")
78+ else (unit "atopgpu.service" "inactive") + ''
79+ with subtest("atopgpud should not be present"):
80+ machine.fail("type -p atopgpud")
81+ '';
82+};
83+in
84+{
85+ name = "atop";
86+
87+ justThePackage = makeTest {
88+ name = "atop-justThePackage";
89+ machine = {
90+ environment.systemPackages = [ pkgs.atop ];
91+ };
92+ testScript = with assertions; builtins.concatStringsSep "\n" [
93+ version
94+ (atoprc "")
95+ (wrapper false)
96+ (atopService false)
97+ (atopRotateTimer false)
98+ (atopacctService false)
99+ (netatop false)
100+ (atopgpu false)
101+ ];
102+ };
103+ defaults = makeTest {
104+ name = "atop-defaults";
105+ machine = {
106+ programs.atop = {
107+ enable = true;
108+ };
109+ };
110+ testScript = with assertions; builtins.concatStringsSep "\n" [
111+ version
112+ (atoprc "")
113+ (wrapper false)
114+ (atopService true)
115+ (atopRotateTimer true)
116+ (atopacctService true)
117+ (netatop false)
118+ (atopgpu false)
119+ ];
120+ };
121+ minimal = makeTest {
122+ name = "atop-minimal";
123+ machine = {
124+ programs.atop = {
125+ enable = true;
126+ atopService.enable = false;
127+ atopRotateTimer.enable = false;
128+ atopacctService.enable = false;
129+ };
130+ };
131+ testScript = with assertions; builtins.concatStringsSep "\n" [
132+ version
133+ (atoprc "")
134+ (wrapper false)
135+ (atopService false)
136+ (atopRotateTimer false)
137+ (atopacctService false)
138+ (netatop false)
139+ (atopgpu false)
140+ ];
141+ };
142+ netatop = makeTest {
143+ name = "atop-netatop";
144+ machine = {
145+ programs.atop = {
146+ enable = true;
147+ netatop.enable = true;
148+ };
149+ };
150+ testScript = with assertions; builtins.concatStringsSep "\n" [
151+ version
152+ (atoprc "")
153+ (wrapper false)
154+ (atopService true)
155+ (atopRotateTimer true)
156+ (atopacctService true)
157+ (netatop true)
158+ (atopgpu false)
159+ ];
160+ };
161+ atopgpu = makeTest {
162+ name = "atop-atopgpu";
163+ machine = {
164+ nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (getName pkg) [
165+ "cudatoolkit"
166+ ];
167+
168+ programs.atop = {
169+ enable = true;
170+ atopgpu.enable = true;
171+ };
172+ };
173+ testScript = with assertions; builtins.concatStringsSep "\n" [
174+ version
175+ (atoprc "")
176+ (wrapper false)
177+ (atopService true)
178+ (atopRotateTimer true)
179+ (atopacctService true)
180+ (netatop false)
181+ (atopgpu true)
182+ ];
183+ };
184+ everything = makeTest {
185+ name = "atop-everthing";
186+ machine = {
187+ nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (getName pkg) [
188+ "cudatoolkit"
189+ ];
190+
191+ programs.atop = {
192+ enable = true;
193+ settings = {
194+ flags = "faf1";
195+ interval = 2;
196+ };
197+ setuidWrapper.enable = true;
198+ netatop.enable = true;
199+ atopgpu.enable = true;
200+ };
201+ };
202+ testScript = with assertions; builtins.concatStringsSep "\n" [
203+ version
204+ (atoprc "flags faf1\\ninterval 2\\n")
205+ (wrapper true)
206+ (atopService true)
207+ (atopRotateTimer true)
208+ (atopacctService true)
209+ (netatop true)
210+ (atopgpu true)
211+ ];
212+ };
213+}
pkgs/os-specific/linux/atop/atop.service.patchnew file+10
@@ -0,0 +1,10 @@
1+--- a/atop.service
2++++ b/atop.service
3+@@ -9,5 +9,6 @@
4+ Environment=LOGPATH=/var/log/atop
5+-EnvironmentFile=/etc/default/atop
6++EnvironmentFile=-/etc/default/atop
7+ ExecStartPre=/bin/sh -c 'test -n "$LOGINTERVAL" -a "$LOGINTERVAL" -eq "$LOGINTERVAL"'
8+ ExecStartPre=/bin/sh -c 'test -n "$LOGGENERATIONS" -a "$LOGGENERATIONS" -eq "$LOGGENERATIONS"'
9++ExecStartPre=/bin/sh -c 'mkdir -p "${LOGPATH}"'
10+ ExecStart=/bin/sh -c 'exec @out@/bin/atop ${LOGOPTS} -w "${LOGPATH}/atop_$(date +%%Y%%m%%d)" ${LOGINTERVAL}'
pkgs/os-specific/linux/atop/atopacct.service.patchnew file+7
@@ -0,0 +1,7 @@
1+--- a/atopacct.service
2++++ b/atopacct.service
3+@@ -9,3 +9,3 @@
4+ Type=forking
5+-PIDFile=/var/run/atopacctd.pid
6++PIDFile=/run/atopacctd.pid
7+ ExecStart=@out@/bin/atopacctd
pkgs/os-specific/linux/atop/default.nix+47−16
@@ -1,4 +1,14 @@
1−{lib, stdenv, fetchurl, zlib, ncurses}:
1+{ lib
2+, stdenv
3+, fetchurl
4+, zlib
5+, ncurses
6+, findutils
7+, systemd
8+, python3
9+# makes the package unfree via pynvml
10+, withAtopgpu ? false
11+}:
212
313stdenv.mkDerivation rec {
414 pname = "atop";
@@ -9,31 +19,52 @@ stdenv.mkDerivation rec {
919 sha256 = "nsLKOlcWkvfvqglfmaUQZDK8txzCLNbElZfvBIEFj3I=";
1020 };
1121
12− buildInputs = [zlib ncurses];
22+ nativeBuildInputs = lib.optionals withAtopgpu [ python3.pkgs.wrapPython ];
23+ buildInputs = [ zlib ncurses ] ++ lib.optionals withAtopgpu [ python3 ];
24+ pythonPath = lib.optionals withAtopgpu [ python3.pkgs.pynvml ];
1325
1426 makeFlags = [
15− "SCRPATH=$out/etc/atop"
16− "LOGPATH=/var/log/atop"
17− "INIPATH=$out/etc/rc.d/init.d"
18− "SYSDPATH=$out/lib/systemd/system"
19− "CRNPATH=$out/etc/cron.d"
20− "DEFPATH=$out/etc/default"
21− "ROTPATH=$out/etc/logrotate.d"
27+ "DESTDIR=$(out)"
28+ "BINPATH=/bin"
29+ "SBINPATH=/bin"
30+ "MAN1PATH=/share/man/man1"
31+ "MAN5PATH=/share/man/man5"
32+ "MAN8PATH=/share/man/man8"
33+ "SYSDPATH=/lib/systemd/system"
34+ "PMPATHD=/lib/systemd/system-sleep"
35+ ];
36+
37+ patches = [
38+ # Fix paths in atop.service, atop-rotate.service, atopgpu.service, atopacct.service,
39+ # and atop-pm.sh
40+ ./fix-paths.patch
41+ # Don't fail on missing /etc/default/atop, make sure /var/log/atop exists pre-start
42+ ./atop.service.patch
43+ # Specify PIDFile in /run, not /var/run to silence systemd warning
44+ ./atopacct.service.patch
2245 ];
2346
2447 preConfigure = ''
25− sed -e "s@/usr/@$out/@g" -i $(find . -type f )
26− sed -e "/mkdir.*LOGPATH/s@mkdir@echo missing dir @" -i Makefile
27− sed -e "/touch.*LOGPATH/s@touch@echo should have created @" -i Makefile
28− sed -e 's/chown/true/g' -i Makefile
29− sed -e '/chkconfig/d' -i Makefile
30− sed -e 's/chmod 04711/chmod 0711/g' -i Makefile
48+ for f in *.{sh,service}; do
49+ findutils=${findutils} systemd=${systemd} substituteAllInPlace "$f"
50+ done
51+
52+ substituteInPlace Makefile --replace 'chown' 'true'
53+ substituteInPlace Makefile --replace 'chmod 04711' 'chmod 0711'
3154 '';
3255
3356 installTargets = [ "systemdinstall" ];
3457 preInstall = ''
35− mkdir -p "$out"/{bin,sbin}
58+ mkdir -p $out/bin
3659 '';
60+ postInstall = ''
61+ # remove extra files we don't need
62+ rm -r $out/{var,etc} $out/bin/atop{sar,}-${version}
63+ '' + (if withAtopgpu then ''
64+ wrapPythonPrograms
65+ '' else ''
66+ rm $out/lib/systemd/system/atopgpu.service $out/bin/atopgpud $out/share/man/man8/atopgpud.8
67+ '');
3768
3869 meta = with lib; {
3970 platforms = platforms.linux;
pkgs/os-specific/linux/atop/fix-paths.patchnew file+48
@@ -0,0 +1,48 @@
1+--- a/atop.service
2++++ b/atop.service
3+@@ -12,4 +12,4 @@
4+ ExecStartPre=/bin/sh -c 'test -n "$LOGGENERATIONS" -a "$LOGGENERATIONS" -eq "$LOGGENERATIONS"'
5+-ExecStart=/bin/sh -c 'exec /usr/bin/atop ${LOGOPTS} -w "${LOGPATH}/atop_$(date +%%Y%%m%%d)" ${LOGINTERVAL}'
6+-ExecStartPost=/usr/bin/find "${LOGPATH}" -name "atop_*" -mtime +${LOGGENERATIONS} -exec rm -v {} \;
7++ExecStart=/bin/sh -c 'exec @out@/bin/atop ${LOGOPTS} -w "${LOGPATH}/atop_$(date +%%Y%%m%%d)" ${LOGINTERVAL}'
8++ExecStartPost=@findutils@/bin/find "${LOGPATH}" -name "atop_*" -mtime +${LOGGENERATIONS} -exec rm -v {} \;
9+ KillSignal=SIGUSR2
10+
11+--- a/atop-rotate.service
12++++ b/atop-rotate.service
13+@@ -4,3 +4,3 @@
14+ [Service]
15+ Type=oneshot
16+-ExecStart=/usr/bin/systemctl try-restart atop.service
17++ExecStart=@systemd@/bin/systemctl try-restart atop.service
18+
19+--- a/atopgpu.service
20++++ b/atopgpu.service
21+@@ -6,5 +6,5 @@
22+
23+ [Service]
24+-ExecStart=/usr/sbin/atopgpud
25++ExecStart=@out@/bin/atopgpud
26+ Type=oneshot
27+ RemainAfterExit=yes
28+
29+--- a/atopacct.service
30++++ b/atopacct.service
31+@@ -10,3 +10,3 @@
32+ PIDFile=/var/run/atopacctd.pid
33+-ExecStart=/usr/sbin/atopacctd
34++ExecStart=@out@/bin/atopacctd
35+
36+--- a/atop-pm.sh
37++++ b/atop-pm.sh
38+@@ -2,8 +2,8 @@
39+
40+ case "$1" in
41+- pre) /usr/bin/systemctl stop atop
42++ pre) @systemd@/bin/systemctl stop atop
43+ exit 0
44+ ;;
45+- post) /usr/bin/systemctl start atop
46++ post) @systemd@/bin/systemctl start atop
47+ exit 0
48+ ;;
pkgs/os-specific/linux/netatop/default.nix+13−5
@@ -1,4 +1,4 @@
1−{ lib, stdenv, fetchurl, kernel, zlib }:
1+{ lib, stdenv, fetchurl, kernel, kmod, zlib }:
22
33let
44 version = "3.1";
@@ -12,10 +12,16 @@ stdenv.mkDerivation {
1212 sha256 = "0qjw8glfdmngfvbn1w63q128vxdz2jlabw13y140ga9i5ibl6vvk";
1313 };
1414
15− buildInputs = [ zlib ];
15+ buildInputs = [ kmod zlib ];
1616
1717 hardeningDisable = [ "pic" ];
1818
19+ patches = [
20+ # fix paths in netatop.service
21+ ./fix-paths.patch
22+ # Specify PIDFile in /run, not /var/run to silence systemd warning
23+ ./netatop.service.patch
24+ ];
1925 preConfigure = ''
2026 patchShebangs mkversion
2127 sed -i -e 's,^KERNDIR.*,KERNDIR=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build,' \
@@ -24,12 +30,14 @@ stdenv.mkDerivation {
2430 -e s,/usr,$out, \
2531 -e /init.d/d \
2632 -e /depmod/d \
27− -e /netatop.service/d \
33+ -e s,/lib/systemd,$out/lib/systemd, \
2834 Makefile
35+
36+ kmod=${kmod} substituteAllInPlace netatop.service
2937 '';
3038
3139 preInstall = ''
32− mkdir -p $out/bin $out/sbin $out/share/man/man{4,8}
40+ mkdir -p $out/lib/systemd/system $out/bin $out/sbin $out/share/man/man{4,8}
3341 mkdir -p $out/lib/modules/${kernel.modDirVersion}/extra
3442 '';
3543
@@ -38,6 +46,6 @@ stdenv.mkDerivation {
3846 homepage = "https://www.atoptool.nl/downloadnetatop.php";
3947 license = lib.licenses.gpl2;
4048 platforms = lib.platforms.linux;
41− maintainers = with lib.maintainers; [viric];
49+ maintainers = with lib.maintainers; [ viric ];
4250 };
4351}
pkgs/os-specific/linux/netatop/fix-paths.patchnew file+11
@@ -0,0 +1,11 @@
1+--- a/netatop.service
2++++ b/netatop.service
3+@@ -8,5 +8,5 @@
4+ Type=oneshot
5+-ExecStartPre=/sbin/modprobe netatop
6+-ExecStart=/usr/sbin/netatopd
7+-ExecStopPost=/sbin/rmmod netatop
8++ExecStartPre=@kmod@/bin/modprobe netatop
9++ExecStart=@out@/bin/netatopd
10++ExecStopPost=@kmod@/bin/rmmod netatop
11+ PIDFile=/var/run/netatop.pid
pkgs/os-specific/linux/netatop/netatop.service.patchnew file+7
@@ -0,0 +1,7 @@
1+--- a/netatop.service
2++++ b/netatop.service
3+@@ -11,3 +11,3 @@
4+ ExecStopPost=@kmod@/bin/rmmod netatop
5+-PIDFile=/var/run/netatop.pid
6++PIDFile=/run/netatop.pid
7+ RemainAfterExit=yes