aboutsummaryrefslogtreecommitdiff
path: root/modules/services/nixos/btopweb.nix
blob: 812e97ef6e3ee26ce266ac31760d218f9333245b (plain)
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
{
  config,
  lib,
  pkgs,
  ...
}: let
  cfg = config.collinux.services.btopweb;

  modularService = {
    lib,
    config,
    options,
    ...
  }: let
    cfg = config.btopweb;
  in {
    options.btopweb = {
      ttydPackage = lib.mkOption {
        description = "the `ttyd` package";
        type = lib.types.package;
      };
      btopPackage = lib.mkOption {
        description = "the `btop` package";
        type = lib.types.package;
      };
      btopSettings = lib.mkOption {
        description = "configuration file for btop";
        type = lib.types.lines;
      };
    };

    config =
      {
        configData."btop.conf".text = cfg.btopSettings;
        process.argv = [
          (lib.getExe cfg.ttydPackage)
          "-W"
          "-i"
          "/run/btopweb/ttyd.sock"
          "-t"
          "renderType=canvas"
          "-t"
          "fontSize=16"
          (lib.getExe cfg.btopPackage)
          "-c"
          config.configData."btop.conf".path
        ];
      }
      // lib.optionalAttrs (options ? systemd) {
        systemd.service = {
          description = "Host btop on a website";
          wants = ["network-online.target"];
          after = ["network-online.target"];
          wantedBy = ["multi-user.target"];

          serviceConfig = {
            DynamicUser = true;
            Group = "caddy";

            RuntimeDirectory = "btopweb";
            RuntimeDirectoryMode = "0750";
            UMask = "0002";

            # allow btop to monitor system stats
            ProtectProc = "default";
            ProcSubset = "all";

            ProtectSystem = "strict";
            ProtectHome = true;
            PrivateTmp = true;
            NoNewPrivileges = true;

            Restart = "on-failure";
          };
        };
      };
  };
in {
  config = lib.mkIf cfg.enable {
    system.services."btopweb" = {
      imports = [modularService];
      btopweb = {
        ttydPackage = pkgs.ttyd;
        btopPackage = pkgs.btop;
        btopSettings = ''
          color_theme = "tomorrow-night"
          enable_mouse = true
          background_update = false

          proc_tree = true
          proc_colors = true
        '';
      };
    };

    services.caddy.virtualHosts."btop.ganymede".extraConfig = ''
      import internal
      reverse_proxy unix//run/btopweb/ttyd.sock
    '';

    collinux.services.glance.homelabServices."btop" = {
      url = "https://btop.ganymede";
      icon = "si:htop";
    };
  };
}