From b768ad0ca4208e8b3588b881067c43d85246e967 Mon Sep 17 00:00:00 2001 From: Collin Williams <96917990+bluedragon1221@users.noreply.github.com> Date: Fri, 2 Oct 2026 11:23:45 -0500 Subject: experiment with modular services; more unix domain sockets, clean up old code --- modules/services/nixos/btopweb.nix | 119 +++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 38 deletions(-) (limited to 'modules/services/nixos/btopweb.nix') diff --git a/modules/services/nixos/btopweb.nix b/modules/services/nixos/btopweb.nix index 9c78295..74a2447 100644 --- a/modules/services/nixos/btopweb.nix +++ b/modules/services/nixos/btopweb.nix @@ -6,46 +6,89 @@ }: let cfg = config.collinux.services.btopweb; - btopSettings = pkgs.writeText "btop.conf" '' - color_theme = "tomorrow-night" - enable_mouse = true - background_update = false - - proc_tree = true - proc_colors = true - ''; + 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 { - users.groups."btopweb" = {}; - users.users."btopweb" = { - isSystemUser = true; - group = "btopweb"; - }; - users.users.caddy.extraGroups = ["btopweb"]; - - systemd.services."btopweb" = { - description = "Host btop on a website"; - restartIfChanged = true; - wants = ["network-online.target"]; - after = ["network-online.target"]; - wantedBy = ["multi-user.target"]; - - serviceConfig = { - User = "btopweb"; - Group = "btopweb"; - Type = "simple"; - - RuntimeDirectory = "btopweb"; - RuntimeDirectoryMode = "0770"; - UMask = "0007"; - - ExecStart = '' - ${lib.getExe pkgs.ttyd} \ - -W \ - -i /run/btopweb/ttyd.sock \ - -t renderType=canvas \ - -t fontSize=16 \ - ${pkgs.btop}/bin/btop -c ${btopSettings} + 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 ''; }; }; -- cgit v1.3.1