Files
Nomarchy/features/scripts/utils/nomarchy-sync-nix-state
Bernardo Magri dc3346bc55
Some checks failed
Check / eval-and-lint (push) Has been cancelled
feat: implement hybrid declarative state with automatic Nix sync
2026-05-31 20:09:12 +01:00

68 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
# Nomarchy Nix State Sync
# Bridges the gap between runtime UI changes (state.json) and declarative
# Nix configuration (nomarchy-state.nix).
STATE_JSON="$HOME/.config/nomarchy/state.json"
NIX_STATE_FILE="/etc/nixos/nomarchy-state.nix"
# If state.json is missing, nothing to sync
if [ ! -f "$STATE_JSON" ]; then
exit 0
fi
# Determine destination.
# If we are in the repo, we sync to ./nomarchy-state.nix (dev mode).
# Otherwise, we sync to /etc/nixos/nomarchy-state.nix (real install).
if [ -f "flake.nix" ] || [ -d ".git" ]; then
DEST="./nomarchy-state.nix"
elif [ -w "/etc/nixos" ]; then
DEST="$NIX_STATE_FILE"
else
# Nowhere to sync to
exit 0
fi
# Generate the Nix attribute set from JSON.
# We use jq to extract the keys and format them as Nix assignments.
# Note: font in state.json maps to fonts.monospace in Nix.
{
echo "# DO NOT EDIT MANUALLY - Managed by Nomarchy scripts."
echo "# This file mirrors your UI choices (theme, font, etc.) into the declarative"
echo "# Nix configuration. It is imported by your home.nix."
echo "{"
echo " nomarchy = {"
# Core UI strings
echo " theme = \"$(jq -r '.theme // empty' "$STATE_JSON")\";"
echo " fonts.monospace = \"$(jq -r '.font // empty' "$STATE_JSON")\";"
echo " panelPosition = \"$(jq -r '.panelPosition // empty' "$STATE_JSON")\";"
echo " wallpaper = \"$(jq -r '.wallpaper // empty' "$STATE_JSON")\";"
# Hyprland layout (numbers)
echo " hyprland = {"
echo " scale = \"$(jq -r '.hyprland.scale // "auto"' "$STATE_JSON")\";"
echo " gaps_in = $(jq -r '.hyprland.gaps_in // 5' "$STATE_JSON");"
echo " gaps_out = $(jq -r '.hyprland.gaps_out // 10' "$STATE_JSON");"
echo " border_size = $(jq -r '.hyprland.border_size // 2' "$STATE_JSON");"
echo " };"
# Toggles (booleans)
echo " toggles = {"
echo " suspend = $(jq -r '.suspend // true' "$STATE_JSON");"
echo " screensaver = $(jq -r '.screensaver // true' "$STATE_JSON");"
echo " idle = $(jq -r '.idle // true' "$STATE_JSON");"
echo " nightlight = $(jq -r '.nightlight // false' "$STATE_JSON");"
echo " waybar = $(jq -r '.waybar // true' "$STATE_JSON");"
echo " };"
echo " };"
echo "}"
} > "$DEST.tmp"
# Final atomic move
mv "$DEST.tmp" "$DEST"