summary refs log tree commit diff
path: root/nixos/modules/programs/atop.nix
blob: a5f4d990bdbe1fe61b9444e0db8fbe0c635dd259 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Global configuration for atop.

{ config, lib, pkgs, ... }:

with lib;

let cfg = config.programs.atop;

in
{
  ###### interface

  options = {

    programs.atop = rec {

      enable = mkEnableOption (lib.mdDoc "Atop");

      package = mkOption {
        type = types.package;
        default = pkgs.atop;
        defaultText = literalExpression "pkgs.atop";
        description = lib.mdDoc ''
          Which package to use for Atop.
        '';
      };

      netatop = {
        enable = mkOption {
          type = types.bool;
          default = false;
          description = lib.mdDoc ''
            Whether to install and enable the netatop kernel module.
            Note: this sets the kernel taint flag "O" for loading out-of-tree modules.
          '';
        };
        package = mkOption {
          type = types.package;
          default = config.boot.kernelPackages.netatop;
          defaultText = literalExpression "config.boot.kernelPackages.netatop";
          description = lib.mdDoc ''
            Which package to use for netatop.
          '';
        };
      };

      atopgpu.enable = mkOption {
        type = types.bool;
        default = false;
        description = lib.mdDoc ''
          Whether to install and enable the atopgpud daemon to get information about
          NVIDIA gpus.
        '';
      };

      setuidWrapper.enable = mkOption {
        type = types.bool;
        default = false;
        description = lib.mdDoc ''
          Whether to install a setuid wrapper for Atop. This is required to use some of
          the features as non-root user (e.g.: ipc information, netatop, atopgpu).
          Atop tries to drop the root privileges shortly after starting.
        '';
      };

      atopService.enable = mkOption {
        type = types.bool;
        default = true;
        description = lib.mdDoc ''
          Whether to enable the atop service responsible for storing statistics for
          long-term analysis.
        '';
      };
      atopRotateTimer.enable = mkOption {
        type = types.bool;
        default = true;
        description = lib.mdDoc ''
          Whether to enable the atop-rotate timer, which restarts the atop service
          daily to make sure the data files are rotate.
        '';
      };
      atopacctService.enable = mkOption {
        type = types.bool;
        default = true;
        description = lib.mdDoc ''
          Whether to enable the atopacct service which manages process accounting.
          This allows Atop to gather data about processes that disappeared in between
          two refresh intervals.
        '';
      };
      settings = mkOption {
        type = types.attrs;
        default = { };
        example = {
          flags = "a1f";
          interval = 5;
        };
        description = lib.mdDoc ''
          Parameters to be written to {file}`/etc/atoprc`.
        '';
      };
    };
  };

  config = mkIf cfg.enable (
    let
      atop =
        if cfg.atopgpu.enable then
          (cfg.package.override { withAtopgpu = true; })
        else
          cfg.package;
    in
    {
      environment.etc = mkIf (cfg.settings != { }) {
        atoprc.text = concatStrings
          (mapAttrsToList
            (n: v: ''
              ${n} ${toString v}
            '')
            cfg.settings);
      };
      environment.systemPackages = [ atop (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
      boot.extraModulePackages = [ (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
      systemd =
        let
          mkSystemd = type: name: restartTriggers: {
            ${name} = {
              inherit restartTriggers;
              wantedBy = [ (if type == "services" then "multi-user.target" else if type == "timers" then "timers.target" else null) ];
            };
          };
          mkService = mkSystemd "services";
          mkTimer = mkSystemd "timers";
        in
        {
          packages = [ atop (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
          services = lib.mkMerge [
            (lib.mkIf cfg.atopService.enable (lib.recursiveUpdate
              (mkService "atop" [ atop ])
              {
                # always convert logs to newer version first
                # XXX might trigger TimeoutStart but restarting atop.service will
                # convert remainings logs and start eventually
                atop.preStart = ''
                  set -e -u
                  shopt -s nullglob
                  for logfile in "$LOGPATH"/atop_*
                  do
                    ${atop}/bin/atopconvert "$logfile" "$logfile".new
                    # only replace old file if version was upgraded to avoid
                    # false positives for atop-rotate.service
                    if ! ${pkgs.diffutils}/bin/cmp -s "$logfile" "$logfile".new
                    then
                      ${pkgs.coreutils}/bin/mv -v -f "$logfile".new "$logfile"
                    else
                      ${pkgs.coreutils}/bin/rm -f "$logfile".new
                    fi
                  done
                '';
              }))
            (lib.mkIf cfg.atopacctService.enable (mkService "atopacct" [ atop ]))
            (lib.mkIf cfg.netatop.enable (mkService "netatop" [ cfg.netatop.package ]))
            (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 {
        atop = {
          setuid = true;
          owner = "root";
          group = "root";
          source = "${atop}/bin/atop";
        };
      };
    }
  );
}