Skip to content

Commit

Permalink
k9s: configuration files in Darwin without XDG
Browse files Browse the repository at this point in the history
Support alternate configuration files for k9s in darwin where XDG is
not mandated and k9s expects configuration files in
`~/Library/Application Support/k9s/`.
  • Loading branch information
jplana authored and rycee committed Apr 14, 2024
1 parent 76a1650 commit 9f32c66
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 31 deletions.
63 changes: 52 additions & 11 deletions modules/programs/k9s.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let

cfg = config.programs.k9s;
yamlFormat = pkgs.formats.yaml { };
inherit (pkgs.stdenv.hostPlatform) isDarwin;

in {
meta.maintainers = with maintainers; [
Expand Down Expand Up @@ -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 ''
Expand All @@ -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 ''
Expand All @@ -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 ''
Expand All @@ -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 ''
Expand All @@ -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 ''
Expand Down Expand Up @@ -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 ''
Expand Down Expand Up @@ -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);
Expand All @@ -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);
};
}
14 changes: 10 additions & 4 deletions tests/modules/programs/k9s/empty-settings.nix
Original file line number Diff line number Diff line change
@@ -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}
'';
}
39 changes: 23 additions & 16 deletions tests/modules/programs/k9s/example-settings.nix
Original file line number Diff line number Diff line change
@@ -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 { };
Expand Down Expand Up @@ -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}
'';
}

0 comments on commit 9f32c66

Please sign in to comment.