Files
Nomarchy/lib.nix
Bernardo Magri d8e1a13d50
Some checks failed
Check / eval (push) Has been cancelled
refactor(#107): theme-state.json → state.json, theme-sync → state-sync
The machine flake's git-tracked settings file is system state, not
"theme" only — rename it to state.json. CLI becomes nomarchy-state-sync
with a nomarchy-theme-sync symlink for scripts and muscle memory.

Eval (mkFlake, doctor, lifecycle) still accepts theme-state.json; the
next write migrates to state.json and removes the legacy file.
Documented in MIGRATION.md; drop the CLI alias after release notes.
2026-07-15 11:26:59 +01:00

121 lines
5.0 KiB
Nix

# Downstream sugar: one call wires a whole machine flake.
# Users own ONLY system.nix, home.nix and state.json; their flake.nix
# is generated once (by `nix flake init -t` or the future installer) and
# never hand-edited. The raw exports (nixosModules/homeModules/overlays)
# in flake.nix remain the escape hatch for power users.
{ nixpkgs, home-manager, nixos-hardware, nomarchy }:
let
inherit (nixpkgs) lib;
# Shared pure reader (modules/state-read.nix) — re-exported so
# power users composing without mkFlake can call it too.
readThemeState = import ./modules/state-read.nix { inherit lib; };
in
{
inherit readThemeState;
mkFlake =
{ src # the downstream flake directory (./.)
, username # login name; also the homeConfigurations attr
, hardwareProfile ? null # nixos-hardware module name(s): string, list, or null
, system ? "x86_64-linux"
}:
let
# One profile or several (the installer's autodetection emits a few
# common-* modules alongside the model-specific one).
profileNames =
if hardwareProfile == null then [ ]
else if builtins.isList hardwareProfile then hardwareProfile
else [ hardwareProfile ];
lookupProfile = name:
if builtins.hasAttr name nixos-hardware.nixosModules then
nixos-hardware.nixosModules.${name}
else
throw ''
nomarchy.lib.mkFlake: unknown hardwareProfile "${name}".
${let
close = builtins.filter
(candidate: lib.strings.levenshteinAtMost 3 candidate name)
(builtins.attrNames nixos-hardware.nixosModules);
in
lib.optionalString (close != [ ])
"Did you mean: ${lib.concatStringsSep ", " close}?\n"
}Valid names are the attribute names of nixos-hardware.nixosModules:
https://github.com/NixOS/nixos-hardware
or list them locally with:
nix eval github:NixOS/nixos-hardware#nixosModules --apply builtins.attrNames
Omit hardwareProfile entirely if your machine has no profile.
'';
hardwareModules = map lookupProfile profileNames;
pkgs = import nixpkgs {
inherit system;
overlays = [ nomarchy.overlays.default ];
# Matches nixpkgs.config.allowUnfree in modules/nixos: the system
# and the standalone HM desktop see the same package set.
config.allowUnfree = true;
};
# Early fail-closed gate: missing/empty/non-object state.json
# throws here with a template + validate pointer, before module
# evaluation buries a raw readFile/fromJSON stack. Field-level
# schema still runs in modules/home/theme.nix after defaults merge.
# Forced via seq on the whole return set — attrNames alone must not
# skip the check (Nix is lazy on unused let bindings and attr values).
# #107: prefer state.json; accept legacy theme-state.json until
# the user's next menu write migrates them.
statePath =
if builtins.pathExists (src + "/state.json") then src + "/state.json"
else if builtins.pathExists (src + "/theme-state.json") then src + "/theme-state.json"
else src + "/state.json";
_themeState = readThemeState statePath;
in
builtins.seq _themeState {
# System layer — rebuilt rarely:
# sudo nixos-rebuild switch --flake .#default
nixosConfigurations.default = nixpkgs.lib.nixosSystem {
inherit system;
specialArgs = { inherit username; };
modules =
[
nomarchy.nixosModules.nomarchy
# The standalone HM CLI ships with the system so theme switching
# (`nomarchy-state-sync apply` → `home-manager switch`) works
# out of the box — same pinned input as the desktop modules.
{ environment.systemPackages = [ home-manager.packages.${system}.home-manager ]; }
# System-side theme consumers (Plymouth splash background)
# read the same JSON the desktop does.
{ nomarchy.system.stateFile = statePath; }
]
++ hardwareModules
++ [
(src + "/hardware-configuration.nix") # from nixos-generate-config
(src + "/system.nix")
];
};
# Desktop layer — every theme change, no sudo:
# home-manager switch --flake .#<username>
# (`nomarchy-state-sync apply` runs this for you.)
homeConfigurations.${username} = home-manager.lib.homeManagerConfiguration {
inherit pkgs;
modules = [
nomarchy.homeModules.nomarchy
(src + "/home.nix")
{
# Written by nomarchy-state-sync; reading it is pure — the
# file is part of the downstream flake's source.
nomarchy.stateFile = statePath;
home = {
inherit username;
homeDirectory = "/home/${username}";
};
}
];
};
};
}