Add custom impermanence module

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2024-08-08 22:03:15 +03:00
parent 5e57d63a54
commit 22e0150a65
69 changed files with 777 additions and 494 deletions

View File

@@ -45,40 +45,6 @@ in
with lib;
with types;
let
checkCssPath = path: lib.filesystem.pathIsRegularFile path && lib.strings.hasSuffix ".css" path;
cssSnippetsOptions =
{ config, ... }:
{
options = {
enable = mkOption {
type = bool;
default = true;
description = "Whether to enable the snippet.";
};
name = mkOption {
type = str;
defaultText = literalExpression "lib.strings.removeSuffix \".css\" (builtins.baseNameOf source)";
description = "Name of the snippet.";
};
source = mkOption {
type = nullOr (addCheck path checkCssPath);
description = "Path of the source file.";
default = null;
};
text = mkOption {
type = nullOr str;
description = "Text of the file.";
default = null;
};
};
config.name = mkDefault (toCssName config.source);
};
corePluginsOptions =
{ config, ... }:
{
@@ -125,6 +91,40 @@ in
};
};
checkCssPath = path: lib.filesystem.pathIsRegularFile path && lib.strings.hasSuffix ".css" path;
cssSnippetsOptions =
{ config, ... }:
{
options = {
enable = mkOption {
type = bool;
default = true;
description = "Whether to enable the snippet.";
};
name = mkOption {
type = str;
defaultText = literalExpression "lib.strings.removeSuffix \".css\" (builtins.baseNameOf source)";
description = "Name of the snippet.";
};
source = mkOption {
type = nullOr (addCheck path checkCssPath);
description = "Path of the source file.";
default = null;
};
text = mkOption {
type = nullOr str;
description = "Text of the file.";
default = null;
};
};
config.name = mkDefault (toCssName config.source);
};
themesOptions =
{ config, ... }:
{
@@ -292,25 +292,27 @@ in
corePlugins = mkOption {
description = "Core plugins to activate.";
type = listOf (either (enum corePlugins) (submodule corePluginsOptions));
type = listOf (coercedTo (enum corePlugins) (p: { name = p; }) (submodule corePluginsOptions));
default = cfg.sharedSettings.corePlugins;
};
communityPlugins = mkOption {
description = "Community plugins to install and activate.";
type = listOf (either package (submodule communityPluginsOptions));
type = listOf (coercedTo package (p: { pkg = p; }) (submodule communityPluginsOptions));
default = cfg.sharedSettings.communityPlugins;
};
cssSnippets = mkOption {
description = "CSS snippets to install.";
type = listOf (either (addCheck path checkCssPath) (submodule cssSnippetsOptions));
type = listOf (
coercedTo (addCheck path checkCssPath) (p: { source = p; }) (submodule cssSnippetsOptions)
);
default = cfg.sharedSettings.cssSnippets;
};
themes = mkOption {
description = "Themes to install.";
type = listOf (either package (submodule themesOptions));
type = listOf (coercedTo package (p: { pkg = p; }) (submodule themesOptions));
default = cfg.sharedSettings.themes;
};
@@ -339,15 +341,10 @@ in
config =
let
vaults = builtins.filter (vault: vault.enable == true) (builtins.attrValues cfg.vaults);
toName = item: if item ? name then item.name else item;
toPkg = item: if item ? pkg then item.pkg else item;
isEnabled = item: if item ? enable then item.enable else true;
hasOptions = item: item ? options;
getCssName = item: if builtins.isAttrs item then item.name else toCssName item;
getManifest =
item:
let
manifest = builtins.fromJSON (builtins.readFile "${toPkg item}/manifest.json");
manifest = builtins.fromJSON (builtins.readFile "${item.pkg}/manifest.json");
in
manifest.id or manifest.name;
in
@@ -366,16 +363,16 @@ in
name = "${vault.target}/.obsidian/appearance.json";
value =
let
enabledCssSnippets = builtins.filter isEnabled vault.settings.cssSnippets;
activeTheme =
lib.lists.findSingle isEnabled null (throw "Only one theme can be enabled at a time.")
vault.settings.themes;
enabledCssSnippets = builtins.filter (snippet: snippet.enable) vault.settings.cssSnippets;
activeTheme = lib.lists.findSingle (
theme: theme.enable
) null (throw "Only one theme can be enabled at a time.") vault.settings.themes;
in
{
source = (pkgs.formats.json { }).generate "appearance.json" (
vault.settings.appearance
// {
enabledCssSnippets = builtins.map getCssName enabledCssSnippets;
enabledCssSnippets = builtins.map (snippet: snippet.name) enabledCssSnippets;
}
// lib.attrsets.optionalAttrs (activeTheme != null) { cssTheme = getManifest activeTheme; }
);
@@ -388,7 +385,7 @@ in
{
name = "${vault.target}/.obsidian/core-plugins.json";
value.source = (pkgs.formats.json { }).generate "core-plugins.json" (
builtins.map toName vault.settings.corePlugins
builtins.map (plugin: plugin.name) vault.settings.corePlugins
);
}
{
@@ -397,9 +394,7 @@ in
builtins.listToAttrs (
builtins.map (name: {
inherit name;
value = builtins.any (
plugin: name == (toName plugin) && isEnabled plugin
) vault.settings.corePlugins;
value = builtins.any (plugin: name == plugin.name && plugin.enable) vault.settings.corePlugins;
}) corePlugins
)
);
@@ -408,7 +403,7 @@ in
++ builtins.map (plugin: {
name = "${vault.target}/.obsidian/${plugin.name}.json";
value.source = (pkgs.formats.json { }).generate "${plugin.name}.json" plugin.options;
}) (builtins.filter hasOptions vault.settings.corePlugins);
}) (builtins.filter (plugin: plugin.options != { }) vault.settings.corePlugins);
mkCommunityPlugins =
vault:
@@ -416,38 +411,35 @@ in
{
name = "${vault.target}/.obsidian/community-plugins.json";
value.source = (pkgs.formats.json { }).generate "community-plugins.json" (
builtins.map getManifest (builtins.filter isEnabled vault.settings.communityPlugins)
builtins.map getManifest (builtins.filter (plugin: plugin.enable) vault.settings.communityPlugins)
);
}
]
++ builtins.map (plugin: {
name = "${vault.target}/.obsidian/plugins/${getManifest plugin}";
value = {
source = toPkg plugin;
source = plugin.pkg;
recursive = true;
};
}) vault.settings.communityPlugins
++ builtins.map (plugin: {
name = "${vault.target}/.obsidian/plugins/${getManifest plugin}/data.json";
value.source = (pkgs.formats.json { }).generate "data.json" plugin.options;
}) (builtins.filter hasOptions vault.settings.communityPlugins);
}) (builtins.filter (plugin: plugin.options != { }) vault.settings.communityPlugins);
mkCssSnippets =
vault:
builtins.map (snippet: {
name = "${vault.target}/.obsidian/snippets/${getCssName snippet}.css";
name = "${vault.target}/.obsidian/snippets/${snippet.name}.css";
value =
if snippet ? source || snippet ? text then
if snippet.source != null then { inherit (snippet) source; } else { inherit (snippet) text; }
else
{ source = snippet; };
if snippet.source != null then { inherit (snippet) source; } else { inherit (snippet) text; };
}) vault.settings.cssSnippets;
mkThemes =
vault:
builtins.map (theme: {
name = "${vault.target}/.obsidian/themes/${getManifest theme}";
value.source = toPkg theme;
value.source = theme.pkg;
}) vault.settings.themes;
mkHotkeys = vault: {
@@ -494,9 +486,7 @@ in
{
assertion = builtins.all (
vault:
builtins.all (
snippet: (!snippet ? source && !snippet ? text) || (snippet.source == null || snippet.text == null)
) vault.settings.cssSnippets
builtins.all (snippet: snippet.source == null || snippet.text == null) vault.settings.cssSnippets
) (builtins.attrValues cfg.vaults);
message = "Only one of `source` and `text` must be set";
}