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/`.
This commit is contained in:
Jose Plana 2024-04-10 13:34:46 +02:00 committed by Robert Helgesson
parent 76a1650c45
commit 9f32c66a51
No known key found for this signature in database
GPG key ID: 96E745BD17AA17ED
3 changed files with 85 additions and 31 deletions

View file

@ -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
<https://k9scli.io/topics/config/> 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
<https://k9scli.io/topics/skins/> 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
<https://k9scli.io/topics/aliases/> 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
<https://k9scli.io/topics/hotkeys/> 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
<https://k9scli.io/topics/plugins/> 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 <https://k9scli.io/topics/columns/> 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);
};
}

View file

@ -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}
'';
}

View file

@ -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}
'';
}