From e38d3dd1d355a003cc63e8fe6ff66ef2257509ed Mon Sep 17 00:00:00 2001 From: Igor Rzegocki Date: Thu, 3 Oct 2024 15:57:31 +0200 Subject: [PATCH] kubecolor: add module (cherry picked from commit 863273505016a1e88e4ffaec48d1b767104c5652) --- modules/misc/news.nix | 10 +++ modules/modules.nix | 1 + modules/programs/kubecolor.nix | 87 +++++++++++++++++++ tests/default.nix | 1 + tests/modules/programs/kubecolor/default.nix | 5 ++ .../programs/kubecolor/empty-config.nix | 20 +++++ .../example-config-default-paths.nix | 37 ++++++++ .../kubecolor/example-config-xdg-paths.nix | 36 ++++++++ 8 files changed, 197 insertions(+) create mode 100644 modules/programs/kubecolor.nix create mode 100644 tests/modules/programs/kubecolor/default.nix create mode 100644 tests/modules/programs/kubecolor/empty-config.nix create mode 100644 tests/modules/programs/kubecolor/example-config-default-paths.nix create mode 100644 tests/modules/programs/kubecolor/example-config-xdg-paths.nix diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 32394f335..946d44daa 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1827,6 +1827,16 @@ in { as systemd services. ''; } + + { + time = "2024-12-06T10:03:58+00:00"; + message = '' + A new module is available: 'programs.kubecolor'. + + Kubecolor is a kubectl wrapper used to add colors to your kubectl + output. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index 75f8ac461..50dba4619 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -143,6 +143,7 @@ let ./programs/khard.nix ./programs/kitty.nix ./programs/kodi.nix + ./programs/kubecolor.nix ./programs/lazygit.nix ./programs/ledger.nix ./programs/less.nix diff --git a/modules/programs/kubecolor.nix b/modules/programs/kubecolor.nix new file mode 100644 index 000000000..d187c34d0 --- /dev/null +++ b/modules/programs/kubecolor.nix @@ -0,0 +1,87 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.programs.kubecolor; + yamlFormat = pkgs.formats.yaml { }; + inherit (pkgs.stdenv.hostPlatform) isDarwin; + +in { + meta.maintainers = with maintainers; [ ajgon ]; + + options.programs.kubecolor = { + enable = mkEnableOption "kubecolor - Colorize your kubectl output"; + + package = mkPackageOption pkgs "kubecolor" { }; + + enableAlias = mkOption { + type = types.bool; + default = false; + description = '' + When set to true, it will create an alias for kubectl pointing to + kubecolor, thus making kubecolor the default kubectl client. + ''; + }; + + settings = mkOption { + type = yamlFormat.type; + default = { }; + example = literalExpression '' + kubectl = lib.getExe pkgs.kubectl + preset = "dark"; + paging = "auto"; + ''; + description = '' + Configuration written to {file}`~/.kube/color.yaml` (Linux) + or {file}`Library/Application Support/kube/color.yaml` (Darwin). + See for supported + values. + ''; + }; + }; + + config = let + preferXdgDirectories = config.home.preferXdgDirectories + && (!isDarwin || config.xdg.enable); + + # https://github.com/kubecolor/kubecolor/pull/145 + configPathSuffix = if cfg.package.pname == "kubecolor" + && lib.strings.toInt (lib.versions.major cfg.package.version) == 0 + && lib.strings.toInt (lib.versions.minor cfg.package.version) < 4 then + "kube/" + else + "kube/color.yaml"; + + in mkIf cfg.enable { + home.packages = [ cfg.package ]; + + home.sessionVariables = if preferXdgDirectories then { + KUBECOLOR_CONFIG = "${config.xdg.configHome}/${configPathSuffix}"; + } else if isDarwin then { + KUBECOLOR_CONFIG = + "${config.home.homeDirectory}/Library/Application Support/${configPathSuffix}"; + } else + { }; + + xdg.configFile = mkIf preferXdgDirectories { + "kube/color.yaml" = mkIf (cfg.settings != { }) { + source = yamlFormat.generate "kubecolor-settings" cfg.settings; + }; + }; + + home.file = mkIf (!preferXdgDirectories) { + "Library/Application Support/kube/color.yaml" = + mkIf (isDarwin && cfg.settings != { }) { + source = yamlFormat.generate "kubecolor-settings" cfg.settings; + }; + ".kube/color.yaml" = mkIf (!isDarwin && cfg.settings != { }) { + source = yamlFormat.generate "kubecolor-settings" cfg.settings; + }; + }; + + home.shellAliases = + lib.mkIf cfg.enableAlias { kubectl = lib.getExe cfg.package; }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 2eaaaa436..292e3284d 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -98,6 +98,7 @@ in import nmtSrc { ./modules/programs/khal ./modules/programs/khard ./modules/programs/kitty + ./modules/programs/kubecolor ./modules/programs/ledger ./modules/programs/less ./modules/programs/lf diff --git a/tests/modules/programs/kubecolor/default.nix b/tests/modules/programs/kubecolor/default.nix new file mode 100644 index 000000000..4445de604 --- /dev/null +++ b/tests/modules/programs/kubecolor/default.nix @@ -0,0 +1,5 @@ +{ + kubecolor-empty-config = ./empty-config.nix; + kubecolor-example-config-default-paths = ./example-config-default-paths.nix; + kubecolor-example-config-xdg-paths = ./example-config-xdg-paths.nix; +} diff --git a/tests/modules/programs/kubecolor/empty-config.nix b/tests/modules/programs/kubecolor/empty-config.nix new file mode 100644 index 000000000..732321c50 --- /dev/null +++ b/tests/modules/programs/kubecolor/empty-config.nix @@ -0,0 +1,20 @@ +{ pkgs, config, ... }: + +let + configDir = + if pkgs.stdenv.isDarwin then "Library/Application Support" else ".config"; +in { + programs.kubecolor = { + enable = true; + package = config.lib.test.mkStubPackage { + name = "kubecolor"; + version = "0.4.0"; + }; + }; + + test.stubs.kubecolor = { }; + + nmt.script = '' + assertPathNotExists 'home-files/${configDir}/kube/color.yaml' + ''; +} diff --git a/tests/modules/programs/kubecolor/example-config-default-paths.nix b/tests/modules/programs/kubecolor/example-config-default-paths.nix new file mode 100644 index 000000000..da3dcf5e1 --- /dev/null +++ b/tests/modules/programs/kubecolor/example-config-default-paths.nix @@ -0,0 +1,37 @@ +{ pkgs, config, ... }: + +let + configDir = if pkgs.stdenv.isDarwin then + "Library/Application Support/kube" + else + ".kube"; +in { + programs.kubecolor = { + enable = true; + package = config.lib.test.mkStubPackage { + name = "kubecolor"; + version = "0.4.0"; + }; + settings = { + kubectl = "kubectl"; + preset = "dark"; + objFreshThreshold = 0; + paging = "auto"; + pager = "less"; + }; + }; + + nmt.script = '' + assertFileExists 'home-files/${configDir}/color.yaml' + assertFileContent 'home-files/${configDir}/color.yaml' \ + ${ + builtins.toFile "expected.yaml" '' + kubectl: kubectl + objFreshThreshold: 0 + pager: less + paging: auto + preset: dark + '' + } + ''; +} diff --git a/tests/modules/programs/kubecolor/example-config-xdg-paths.nix b/tests/modules/programs/kubecolor/example-config-xdg-paths.nix new file mode 100644 index 000000000..9ac9482c2 --- /dev/null +++ b/tests/modules/programs/kubecolor/example-config-xdg-paths.nix @@ -0,0 +1,36 @@ +{ config, ... }: + +{ + xdg.enable = true; + home.preferXdgDirectories = true; + + programs.kubecolor = { + enable = true; + package = config.lib.test.mkStubPackage { + name = "kubecolor"; + version = "0.4.0"; + }; + settings = { + kubectl = "kubectl"; + preset = "dark"; + objFreshThreshold = 0; + paging = "auto"; + pager = "less"; + }; + }; + + nmt.script = '' + assertFileExists 'home-files/.config/kube/color.yaml' + assertFileContent 'home-files/.config/kube/color.yaml' \ + ${ + builtins.toFile "expected.yaml" '' + kubectl: kubectl + objFreshThreshold: 0 + pager: less + paging: auto + preset: dark + '' + } + ''; +} +