cosmic: fix doc

This commit is contained in:
atagen 2024-08-01 14:39:29 +10:00
parent a12755f889
commit 70e3671a4f

View file

@ -1,8 +1,22 @@
{ pkgs, lib, config, ... }: {
let pkgs,
inherit (lib) lib,
filterAttrs concatStrings concatStringsSep mapAttrsToList concatLists config,
foldlAttrs concatMapAttrs mapAttrs' nameValuePair boolToString; ...
}: let
inherit
(lib)
filterAttrs
concatStrings
concatStringsSep
mapAttrsToList
concatLists
foldlAttrs
concatMapAttrs
mapAttrs'
nameValuePair
boolToString
;
inherit (builtins) typeOf toString stringLength; inherit (builtins) typeOf toString stringLength;
# build up serialisation machinery from here for various types # build up serialisation machinery from here for various types
@ -14,23 +28,22 @@ let
assoc = a: '' assoc = a: ''
{ {
${ ${
concatStringsSep '' concatStringsSep ''
, ,
'' (concatLists (map _assoc a)) '' (concatLists (map _assoc a))
} }
}''; }'';
# attrset -> struct # attrset -> struct
_struct_kv = k: v: _struct_kv = k: v:
if v == null then if v == null
"" then ""
else else (concatStringsSep ":" [k (serialise.${typeOf v} v)]);
(concatStringsSep ":" [ k (serialise.${typeOf v} v) ]);
_struct_concat = s: _struct_concat = s:
foldlAttrs (acc: k: v: foldlAttrs (acc: k: v:
if stringLength acc > 0 then if stringLength acc > 0
concatStringsSep ", " [ acc (_struct_kv k v) ] then concatStringsSep ", " [acc (_struct_kv k v)]
else else _struct_kv k v) ""
_struct_kv k v) "" s; s;
_struct_filt = s: _struct_concat (filterAttrs (k: v: v != null) s); _struct_filt = s: _struct_concat (filterAttrs (k: v: v != null) s);
struct = s: "(${_struct_filt s})"; struct = s: "(${_struct_filt s})";
toQuotedString = s: ''"${toString s}"''; toQuotedString = s: ''"${toString s}"'';
@ -51,41 +64,46 @@ let
defineBinding = binding: defineBinding = binding:
struct { struct {
inherit (binding) modifiers; inherit (binding) modifiers;
key = if isNull binding.key then null else toQuotedString binding.key; key =
if isNull binding.key
then null
else toQuotedString binding.key;
}; };
# map keybinding from list of attrset to hashmap of (mod,key): action # map keybinding from list of attrset to hashmap of (mod,key): action
_mapBindings = bindings: _mapBindings = bindings:
map (inner: { map (inner: {
"${defineBinding inner}" = maybeToString (checkAction inner.action); "${defineBinding inner}" = maybeToString (checkAction inner.action);
}) bindings; })
bindings;
mapBindings = bindings: assoc (_mapBindings bindings); mapBindings = bindings: assoc (_mapBindings bindings);
# check a keybinding's action # check a keybinding's action
# escape with quotes if it's a Spawn action # escape with quotes if it's a Spawn action
checkAction = a: checkAction = a:
if typeOf a == "set" && a.type == "Spawn" then { if typeOf a == "set" && a.type == "Spawn"
then {
inherit (a) type; inherit (a) type;
data = toQuotedString a.data; data = toQuotedString a.data;
} else }
a; else a;
maybeToString = s: maybeToString = s:
if typeOf s == "set" then if typeOf s == "set"
concatStrings [ s.type "(" (toString s.data) ")" ] then concatStrings [s.type "(" (toString s.data) ")"]
else else s;
s;
mapCosmicSettings = application: options: mapCosmicSettings = application: options:
mapAttrs' (k: v: mapAttrs' (k: v:
nameValuePair "cosmic/${application}/v${options.version}/${k}" { nameValuePair "cosmic/${application}/v${options.version}/${k}" {
enable = true; enable = true;
text = serialise.${typeOf v} v; text = serialise.${typeOf v} v;
}) options.option; })
options.option;
cfg = config.programs.cosmic; cfg = config.programs.cosmic;
in { in {
meta.maintainers = [ hm.maintainers.atagen ]; meta.maintainers = [hm.maintainers.atagen];
options.programs.cosmic = { options.programs.cosmic = {
enable = with lib; mkEnableOption "COSMIC DE"; enable = with lib; mkEnableOption "COSMIC DE";
@ -98,13 +116,13 @@ in {
keybindings = with lib; keybindings = with lib;
mkOption { mkOption {
default = [ ]; default = [];
type = with types; type = with types;
listOf (submodule { listOf (submodule {
options = { options = {
modifiers = mkOption { modifiers = mkOption {
type = listOf str; type = listOf str;
default = [ ]; default = [];
}; };
key = mkOption { key = mkOption {
type = nullOr str; type = nullOr str;
@ -113,9 +131,9 @@ in {
action = mkOption { action = mkOption {
type = either str (submodule { type = either str (submodule {
options = { options = {
type = mkOption { type = str; }; type = mkOption {type = str;};
data = mkOption { data = mkOption {
type = oneOf [ str int ]; type = oneOf [str int];
default = ""; default = "";
}; };
}; };
@ -157,7 +175,7 @@ in {
settings = with lib; settings = with lib;
mkOption { mkOption {
default = { }; default = {};
type = with types; type = with types;
attrsOf (submodule { attrsOf (submodule {
options = { options = {
@ -165,11 +183,11 @@ in {
type = str; type = str;
default = "1"; default = "1";
}; };
option = mkOption { type = attrsOf anything; }; option = mkOption {type = attrsOf anything;};
}; };
}); });
description = '' description = ''
A list of explicit settings for COSMIC apps, using their full config path. An attrset of explicit settings for COSMIC apps, using their full config path.
''; '';
example = literalExpression '' example = literalExpression ''
{ {
@ -182,14 +200,16 @@ in {
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
xdg.configFile = { xdg.configFile =
"cosmic/com.system76.CosmicSettings.Shortcuts/v1/custom".text = {
(lib.mkIf cfg.keybindings != [ ]) mapBindings cfg.keybindings; "cosmic/com.system76.CosmicSettings.Shortcuts/v1/custom".text =
"cosmic/com.system76.CosmicSettings.Shortcuts/v1/defaults" = { (lib.mkIf cfg.keybindings != []) mapBindings cfg.keybindings;
text = "{}"; "cosmic/com.system76.CosmicSettings.Shortcuts/v1/defaults" = {
enable = !cfg.defaultKeybindings; text = "{}";
}; enable = !cfg.defaultKeybindings;
} // concatMapAttrs };
}
// concatMapAttrs
(application: options: mapCosmicSettings application options) (application: options: mapCosmicSettings application options)
cfg.settings; cfg.settings;
}; };