From 9356ff1a9b40b72903f399c5012edb3f6d260f2a Mon Sep 17 00:00:00 2001 From: Karol Broda Date: Wed, 24 Dec 2025 13:35:37 +0100 Subject: [PATCH] feat(nix): add snitch home manager module --- README.md | 39 ++++++++++ flake.nix | 7 ++ nix/hm-module.nix | 177 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 223 insertions(+) create mode 100644 nix/hm-module.nix diff --git a/README.md b/README.md index 44d4752..3a940d7 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,45 @@ nix profile install github:karol-broda/snitch # then use: inputs.snitch.packages.${system}.default ``` +### home-manager (flake) + +add snitch to your flake inputs and import the home-manager module: + +```nix +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + home-manager.url = "github:nix-community/home-manager"; + snitch.url = "github:karol-broda/snitch"; + }; + + outputs = { nixpkgs, home-manager, snitch, ... }: { + homeConfigurations."user" = home-manager.lib.homeManagerConfiguration { + pkgs = nixpkgs.legacyPackages.x86_64-linux; + modules = [ + snitch.homeManagerModules.default + { + programs.snitch = { + enable = true; + # optional: use the flake's package instead of nixpkgs + # package = snitch.packages.x86_64-linux.default; + settings = { + defaults = { + theme = "catppuccin-mocha"; + interval = "2s"; + resolve = true; + }; + }; + }; + } + ]; + }; + }; +} +``` + +available themes: `ansi`, `catppuccin-mocha`, `catppuccin-macchiato`, `catppuccin-frappe`, `catppuccin-latte`, `gruvbox-dark`, `gruvbox-light`, `dracula`, `nord`, `tokyo-night`, `tokyo-night-storm`, `tokyo-night-light`, `solarized-dark`, `solarized-light`, `one-dark`, `mono` + ### arch linux (aur) ```bash diff --git a/flake.nix b/flake.nix index ee9b063..3fa8842 100644 --- a/flake.nix +++ b/flake.nix @@ -106,5 +106,12 @@ overlays.default = final: _prev: { snitch = mkSnitch final; }; + + homeManagerModules.default = import ./nix/hm-module.nix; + homeManagerModules.snitch = self.homeManagerModules.default; + + # alias for flake-parts compatibility + homeModules.default = self.homeManagerModules.default; + homeModules.snitch = self.homeManagerModules.default; }; } diff --git a/nix/hm-module.nix b/nix/hm-module.nix new file mode 100644 index 0000000..da3e12e --- /dev/null +++ b/nix/hm-module.nix @@ -0,0 +1,177 @@ +{ + config, + lib, + pkgs, + ... +}: +let + cfg = config.programs.snitch; + + themes = [ + "ansi" + "catppuccin-mocha" + "catppuccin-macchiato" + "catppuccin-frappe" + "catppuccin-latte" + "gruvbox-dark" + "gruvbox-light" + "dracula" + "nord" + "tokyo-night" + "tokyo-night-storm" + "tokyo-night-light" + "solarized-dark" + "solarized-light" + "one-dark" + "mono" + "auto" + ]; + + defaultFields = [ + "pid" + "process" + "user" + "proto" + "state" + "laddr" + "lport" + "raddr" + "rport" + ]; + + tomlFormat = pkgs.formats.toml { }; + + settingsType = lib.types.submodule { + freeformType = tomlFormat.type; + + options = { + defaults = lib.mkOption { + type = lib.types.submodule { + freeformType = tomlFormat.type; + + options = { + interval = lib.mkOption { + type = lib.types.str; + default = "1s"; + example = "2s"; + description = "Default refresh interval for watch/stats/trace commands."; + }; + + numeric = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Disable name/service resolution by default."; + }; + + fields = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = defaultFields; + example = [ "pid" "process" "proto" "state" "laddr" "lport" ]; + description = "Default fields to display."; + }; + + theme = lib.mkOption { + type = lib.types.enum themes; + default = "ansi"; + description = '' + Color theme for the TUI. "ansi" inherits terminal colors. + ''; + }; + + units = lib.mkOption { + type = lib.types.enum [ "auto" "si" "iec" ]; + default = "auto"; + description = "Default units for byte display."; + }; + + color = lib.mkOption { + type = lib.types.enum [ "auto" "always" "never" ]; + default = "auto"; + description = "Default color mode."; + }; + + resolve = lib.mkOption { + type = lib.types.bool; + default = true; + description = "Enable name resolution by default."; + }; + + dns_cache = lib.mkOption { + type = lib.types.bool; + default = true; + description = "Enable DNS caching."; + }; + + ipv4 = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Filter to IPv4 only by default."; + }; + + ipv6 = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Filter to IPv6 only by default."; + }; + + no_headers = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Omit headers in output by default."; + }; + + output_format = lib.mkOption { + type = lib.types.enum [ "table" "json" "csv" ]; + default = "table"; + description = "Default output format."; + }; + + sort_by = lib.mkOption { + type = lib.types.str; + default = ""; + example = "pid"; + description = "Default sort field."; + }; + }; + }; + default = { }; + description = "Default settings for snitch commands."; + }; + }; + }; +in +{ + options.programs.snitch = { + enable = lib.mkEnableOption "snitch, a friendlier ss/netstat for humans"; + + package = lib.mkPackageOption pkgs "snitch" { }; + + settings = lib.mkOption { + type = settingsType; + default = { }; + example = lib.literalExpression '' + { + defaults = { + theme = "catppuccin-mocha"; + interval = "2s"; + resolve = true; + }; + } + ''; + description = '' + Configuration written to {file}`$XDG_CONFIG_HOME/snitch/snitch.toml`. + + See for available options. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + home.packages = [ cfg.package ]; + + xdg.configFile."snitch/snitch.toml" = lib.mkIf (cfg.settings != { }) { + source = tomlFormat.generate "snitch.toml" cfg.settings; + }; + }; +} +