From 9f32c66a51d05e6d4ec0dea555bbff9135749ec7 Mon Sep 17 00:00:00 2001 From: Jose Plana Date: Wed, 10 Apr 2024 13:34:46 +0200 Subject: [PATCH] k9s: configuration files in Darwin without XDG Support alternate configuration files for k9s in darwin where XDG is not mandated and k9s expects configuration files in `~/Library/Application Support/k9s/`. --- modules/programs/k9s.nix | 63 +++++++++++++++---- tests/modules/programs/k9s/empty-settings.nix | 14 +++-- .../modules/programs/k9s/example-settings.nix | 39 +++++++----- 3 files changed, 85 insertions(+), 31 deletions(-) diff --git a/modules/programs/k9s.nix b/modules/programs/k9s.nix index a9e3fe55..6ef53291 100644 --- a/modules/programs/k9s.nix +++ b/modules/programs/k9s.nix @@ -6,6 +6,7 @@ let cfg = config.programs.k9s; yamlFormat = pkgs.formats.yaml { }; + inherit (pkgs.stdenv.hostPlatform) isDarwin; in { meta.maintainers = with maintainers; [ @@ -33,7 +34,8 @@ in { type = yamlFormat.type; default = { }; description = '' - Configuration written to {file}`$XDG_CONFIG_HOME/k9s/config.yaml`. See + Configuration written to {file}`$XDG_CONFIG_HOME/k9s/config.yaml` (linux) + or {file}`Library/Application Support/k9s/config.yaml` (darwin), See for supported values. ''; example = literalExpression '' @@ -47,7 +49,8 @@ in { type = types.attrsOf yamlFormat.type; default = { }; description = '' - Skin files written to {file}`$XDG_CONFIG_HOME/k9s/skins/`. See + Skin files written to {file}`$XDG_CONFIG_HOME/k9s/skins/` (linux) + or {file}`Library/Application Support/k9s/skins/` (darwin). See for supported values. ''; example = literalExpression '' @@ -65,7 +68,8 @@ in { type = yamlFormat.type; default = { }; description = '' - Aliases written to {file}`$XDG_CONFIG_HOME/k9s/aliases.yaml`. See + Aliases written to {file}`$XDG_CONFIG_HOME/k9s/aliases.yaml` (linux) + or {file}`Library/Application Support/k9s/aliases.yaml` (darwin). See for supported values. ''; example = literalExpression '' @@ -80,7 +84,8 @@ in { type = yamlFormat.type; default = { }; description = '' - Hotkeys written to {file}`$XDG_CONFIG_HOME/k9s/hotkeys.yaml`. See + Hotkeys written to {file}`$XDG_CONFIG_HOME/k9s/hotkeys.yaml` (linux) + or {file}`Library/Application Support/k9s/hotkeys.yaml` (darwin). See for supported values. ''; example = literalExpression '' @@ -101,7 +106,8 @@ in { type = yamlFormat.type; default = { }; description = '' - Plugins written to {file}`$XDG_CONFIG_HOME/k9s/plugins.yaml`. See + Plugins written to {file}`$XDG_CONFIG_HOME/k9s/plugins.yaml (linux)` + or {file}`Library/Application Support/k9s/plugins.yaml` (darwin). See for supported values. ''; example = literalExpression '' @@ -132,7 +138,9 @@ in { type = yamlFormat.type; default = { }; description = '' - Resource column views written to {file}`$XDG_CONFIG_HOME/k9s/views.yaml`. + Resource column views written to + {file}`$XDG_CONFIG_HOME/k9s/views.yaml (linux)` + or {file}`Library/Application Support/k9s/views.yaml` (darwin). See for supported values. ''; example = literalExpression '' @@ -162,13 +170,19 @@ in { { }; skinFiles = mapAttrs' (name: value: - nameValuePair "k9s/skins/${name}.yaml" { - source = yamlFormat.generate "k9s-skin-${name}.yaml" value; - }) cfg.skins; + nameValuePair (if !(isDarwin && !config.xdg.enable) then + "k9s/skins/${name}.yaml" + else + "Library/Application Support/k9s/skins/${name}.yaml") { + source = yamlFormat.generate "k9s-skin-${name}.yaml" value; + }) cfg.skins; + + enableXdgConfig = !isDarwin || config.xdg.enable; + in mkIf cfg.enable { home.packages = [ cfg.package ]; - xdg.configFile = { + xdg.configFile = mkIf enableXdgConfig ({ "k9s/config.yaml" = mkIf (cfg.settings != { }) { source = yamlFormat.generate "k9s-config" (lib.recursiveUpdate skinSetting cfg.settings); @@ -189,6 +203,33 @@ in { "k9s/views.yaml" = mkIf (cfg.views != { }) { source = yamlFormat.generate "k9s-views" cfg.views; }; - } // skinFiles; + } // skinFiles); + + home.file = mkIf (!enableXdgConfig) ({ + "Library/Application Support/k9s/config.yaml" = + mkIf (cfg.settings != { }) { + source = yamlFormat.generate "k9s-config" + (lib.recursiveUpdate skinSetting cfg.settings); + }; + + "Library/Application Support/k9s/aliases.yaml" = + mkIf (cfg.aliases != { }) { + source = yamlFormat.generate "k9s-aliases" cfg.aliases; + }; + + "Library/Application Support/k9s/hotkeys.yaml" = + mkIf (cfg.hotkey != { }) { + source = yamlFormat.generate "k9s-hotkey" cfg.hotkey; + }; + + "Library/Application Support/k9s/plugins.yaml" = + mkIf (cfg.plugin != { }) { + source = yamlFormat.generate "k9s-plugin" cfg.plugin; + }; + + "Library/Application Support/k9s/views.yaml" = mkIf (cfg.views != { }) { + source = yamlFormat.generate "k9s-views" cfg.views; + }; + } // skinFiles); }; } diff --git a/tests/modules/programs/k9s/empty-settings.nix b/tests/modules/programs/k9s/empty-settings.nix index 5084be4f..35534005 100644 --- a/tests/modules/programs/k9s/empty-settings.nix +++ b/tests/modules/programs/k9s/empty-settings.nix @@ -1,11 +1,17 @@ -{ ... }: +{ pkgs, lib, ... }: { -{ programs.k9s.enable = true; + xdg.enable = lib.mkIf pkgs.stdenv.isDarwin (lib.mkForce false); + test.stubs.k9s = { }; - nmt.script = '' - assertPathNotExists home-files/.config/k9s + nmt.script = let + configDir = if !pkgs.stdenv.isDarwin then + ".config/k9s" + else + "Library/Application Support/k9s"; + in '' + assertPathNotExists home-files/${configDir} ''; } diff --git a/tests/modules/programs/k9s/example-settings.nix b/tests/modules/programs/k9s/example-settings.nix index f37f192c..23a42120 100644 --- a/tests/modules/programs/k9s/example-settings.nix +++ b/tests/modules/programs/k9s/example-settings.nix @@ -1,6 +1,8 @@ -{ config, ... }: +{ config, pkgs, lib, ... }: { + xdg.enable = lib.mkIf pkgs.stdenv.isDarwin (lib.mkForce false); + programs.k9s = { enable = true; package = config.lib.test.mkStubPackage { }; @@ -76,34 +78,39 @@ }; }; - nmt.script = '' - assertFileExists home-files/.config/k9s/config.yaml + nmt.script = let + configDir = if !pkgs.stdenv.isDarwin then + ".config/k9s" + else + "Library/Application Support/k9s"; + in '' + assertFileExists "home-files/${configDir}/config.yaml" assertFileContent \ - home-files/.config/k9s/config.yaml \ + "home-files/${configDir}/config.yaml" \ ${./example-config-expected.yaml} - assertFileExists home-files/.config/k9s/skins/default.yaml + assertFileExists "home-files/${configDir}/skins/default.yaml" assertFileContent \ - home-files/.config/k9s/skins/default.yaml \ + "home-files/${configDir}/skins/default.yaml" \ ${./example-skin-expected.yaml} - assertFileExists home-files/.config/k9s/skins/alt-skin.yaml + assertFileExists "home-files/${configDir}/skins/alt-skin.yaml" assertFileContent \ - home-files/.config/k9s/skins/alt-skin.yaml \ + "home-files/${configDir}/skins/alt-skin.yaml" \ ${./example-skin-expected-alt.yaml} - assertFileExists home-files/.config/k9s/hotkeys.yaml + assertFileExists "home-files/${configDir}/hotkeys.yaml" assertFileContent \ - home-files/.config/k9s/hotkeys.yaml \ + "home-files/${configDir}/hotkeys.yaml" \ ${./example-hotkey-expected.yaml} - assertFileExists home-files/.config/k9s/aliases.yaml + assertFileExists "home-files/${configDir}/aliases.yaml" assertFileContent \ - home-files/.config/k9s/aliases.yaml \ + "home-files/${configDir}/aliases.yaml" \ ${./example-aliases-expected.yaml} - assertFileExists home-files/.config/k9s/plugins.yaml + assertFileExists "home-files/${configDir}/plugins.yaml" assertFileContent \ - home-files/.config/k9s/plugins.yaml \ + "home-files/${configDir}/plugins.yaml" \ ${./example-plugin-expected.yaml} - assertFileExists home-files/.config/k9s/views.yaml + assertFileExists "home-files/${configDir}/views.yaml" assertFileContent \ - home-files/.config/k9s/views.yaml \ + "home-files/${configDir}/views.yaml" \ ${./example-views-expected.yaml} ''; }