config/modules/profiles/desktop/plasma.nix

89 lines
2.0 KiB
Nix
Raw Normal View History

2023-10-09 05:47:36 +03:00
# thanks k900
{
config,
lib,
pkgs,
...
2024-07-29 15:39:54 +03:00
}:
let
2023-10-09 05:47:36 +03:00
cfg = config.mj.plasma;
2024-07-29 15:39:54 +03:00
setValue =
v:
let
setValueArgs = ty: vs: "--type ${ty} ${lib.escapeShellArg vs}";
in
if builtins.isBool v then
setValueArgs "bool" (if v then "true" else "false")
else
setValueArgs "str" (builtins.toString v);
2023-10-09 05:47:36 +03:00
2024-07-29 15:39:54 +03:00
pathToArgs =
path:
let
groupArg = item: "--group ${lib.escapeShellArg item}";
groupArgs = builtins.map groupArg path;
in
2023-10-09 05:47:36 +03:00
groupArgs;
2024-07-29 15:39:54 +03:00
entryToArgs =
{ path, value }:
let
file = builtins.head path;
subpath = builtins.tail path;
groups = lib.lists.init subpath;
name = lib.lists.last subpath;
2023-10-09 05:47:36 +03:00
2024-07-29 15:39:54 +03:00
fileArg = "--file ${lib.escapeShellArg file}";
pathArgs = pathToArgs groups;
keyArg = "--key ${lib.escapeShellArg name}";
valueArg = setValue value;
allArgs = pathArgs ++ [
fileArg
keyArg
valueArg
];
in
2023-10-09 05:47:36 +03:00
lib.strings.concatStringsSep " " allArgs;
2024-07-29 15:39:54 +03:00
flattenAttrs =
attrs: pathSoFar:
lib.lists.flatten (
lib.attrsets.mapAttrsToList (
2023-10-09 05:47:36 +03:00
name: value:
2024-07-29 15:39:54 +03:00
if builtins.isAttrs value then
flattenAttrs value (pathSoFar ++ [ name ])
else
{
path = pathSoFar ++ [ name ];
2023-10-09 05:47:36 +03:00
inherit value;
}
2024-07-29 15:39:54 +03:00
) attrs
);
2023-10-09 05:47:36 +03:00
2024-07-29 15:39:54 +03:00
configToArgs = attrs: builtins.map entryToArgs (flattenAttrs attrs [ ]);
2023-10-09 05:47:36 +03:00
2024-07-29 15:39:54 +03:00
configToScript =
attrs:
let
args = configToArgs attrs;
argToCommand = arg: "${pkgs.plasma5Packages.kconfig}/bin/kwriteconfig5 ${arg}";
commands = builtins.map argToCommand args;
in
2023-10-09 05:47:36 +03:00
lib.strings.concatStringsSep "\n" commands;
writeConfig = attrs: pkgs.writeScript "kconfig-setup" (configToScript attrs);
2024-07-29 15:39:54 +03:00
in
{
2023-10-09 05:47:36 +03:00
options.mj.plasma = {
kconfig = lib.mkOption {
type = lib.types.attrs;
2024-07-29 15:39:54 +03:00
default = { };
2023-10-09 05:47:36 +03:00
};
};
2024-07-29 15:39:54 +03:00
config = lib.mkIf (cfg.kconfig != { }) {
2023-10-09 05:47:36 +03:00
home.activation.kconfig-setup = "$DRY_RUN_CMD ${writeConfig cfg.kconfig}";
};
}