Files
Nomarchy/lib/default.nix
Bernardo Magri 27d1506b54 chore(lib): drop dead helpers, document schema boundary
Two unused helpers and a missing comment in the lib/ surface, found
during the Pillar 8 sweep.

- `readState` in `lib/default.nix` was exported but has no external
  callers — only `readHomeState` and `readSystemState` use it
  internally. Removed from the export list; the function stays in the
  let-block (still wraps the two public readers).

- `getWithDefault` in `lib/state-schema.nix` was a complete dead
  function: declared as a path-walking fallback helper but never called
  anywhere in the tree. core/{system,home}/state.nix use inline
  `togglesState.<key> or schema.<scope>.<key>` instead. Removed.

- Added a header comment to `lib/state-schema.nix` explaining the
  schema's boundary — it lists every state.json field consumed by a
  Nix option, but state.json may also hold runtime-only fields
  (`welcome_done` from `nomarchy-welcome`) that are intentionally
  off-schema because no Nix option reads them. Future readers will
  otherwise think welcome_done is an orphan.

Logged a Later-column roadmap row for consolidating `flake.nix`'s
palette/themeNames re-imports with `nomarchyLib` so the theme list has
one source of truth instead of two.
2026-05-19 18:28:54 +01:00

111 lines
3.5 KiB
Nix

# Nomarchy Shared Library
# Centralized utilities to reduce code duplication across modules
{ lib }:
let
# Import theme palettes once - used by multiple modules
palettes = import ./../themes/palettes;
# Unified state reading function
# Handles both JSON and plain text files with graceful fallbacks
readState = { file, default }:
if builtins.pathExists file then
let
content = builtins.readFile file;
cleanContent = lib.removeSuffix "\n" content;
in
if lib.hasSuffix ".json" (toString file) then
builtins.fromJSON cleanContent
else
cleanContent
else
default;
# Read home state from unified location
readHomeState = homeDirectory:
readState {
file = "${homeDirectory}/.config/nomarchy/state.json";
default = {};
};
# Read system state
readSystemState =
readState {
file = "/etc/nixos/state.json";
default = {};
};
# Resolve wallpaper path with fallback
# Returns either the user-specified wallpaper or a default from the theme
resolveWallpaper = { wallpaperPath, themeName, assetsPath }:
if wallpaperPath != "" then
wallpaperPath
else
let
themeBackgrounds = assetsPath + "/${themeName}/backgrounds";
defaultBackground = assetsPath + "/catppuccin/backgrounds/1-totoro.png";
in
if builtins.pathExists themeBackgrounds then
let
backgrounds = builtins.attrNames (builtins.readDir themeBackgrounds);
in
if backgrounds != [] then
"${themeBackgrounds}/${builtins.head (builtins.sort (a: b: a < b) backgrounds)}"
else
defaultBackground
else
defaultBackground;
# Get current palette from theme name
getPalette = themeName:
(palettes.${themeName} or palettes.nord).palette;
# Get full color scheme from theme name
getColorScheme = themeName:
palettes.${themeName} or palettes.nord;
# Check if a theme is light mode
isThemeLightMode = { themeName, assetsPath }:
builtins.pathExists (assetsPath + "/${themeName}/light.mode");
# Get icons theme for a given theme
getIconsTheme = { themeName, assetsPath, default ? "Yaru-blue" }:
let
iconsFile = assetsPath + "/${themeName}/icons.theme";
in
if builtins.pathExists iconsFile then
lib.removeSuffix "\n" (builtins.readFile iconsFile)
else
default;
# Map an icon-theme name (as declared in a palette's icons.theme file) to
# the nixpkgs attribute that ships it. Anything unknown falls back to
# yaru-theme so GTK still has a working default.
#
# When adding a palette that uses a new icon family, extend this mapping
# so `nomarchy-theme-set <name>` auto-installs the matching package.
iconThemePackage = { iconsTheme, pkgs }:
if lib.hasPrefix "Yaru" iconsTheme then pkgs.yaru-theme
else if lib.hasPrefix "Everforest" iconsTheme then pkgs.everforest-gtk-theme
else if lib.hasPrefix "Papirus" iconsTheme then pkgs.papirus-icon-theme
else if lib.hasPrefix "Adwaita" iconsTheme then pkgs.adwaita-icon-theme
else if lib.hasPrefix "breeze" iconsTheme || lib.hasPrefix "Breeze" iconsTheme then pkgs.kdePackages.breeze-icons
else pkgs.yaru-theme;
# Get list of available theme names
themeNames = builtins.attrNames palettes;
in {
inherit
palettes
readHomeState
readSystemState
resolveWallpaper
getPalette
getColorScheme
isThemeLightMode
getIconsTheme
iconThemePackage
themeNames;
}