# settings/theming.nix — runtime theme switching. # # stylix themes the system from ONE image at build time. This module builds a # complete theme for EVERY image in walls/ instead, and swaps between them at # runtime: the wallpaper picker in the quickshell bar writes a state file, and # `theme-apply` drops that wallpaper's rendered config into place and nudges # every app that can reload without restarting. # # What follows a switch immediately: quickshell, hyprland, ghostty (D-Bus # reload-config), GTK3 apps such as nemo (each wallpaper installs # its own named GTK theme, switched over gsettings) and the folder icon tint. # On next launch only: GTK4/libadwaita apps, btop, zen. # # stylix stays the source of truth for build-time defaults and for every # target not listed here — walls/wallpaper.png remains its image. { config, pkgs, lib, inputs, ... }: let isDesktop = lib.elem config.networking.hostName [ "FredOS-Gaming" "FredOS-Macbook" ]; homeDir = "/home/fred"; stateLink = "${homeDir}/.local/state/quickshell-theme"; zenChrome = ".zen/fraudek5.Default Profile/chrome"; # ── Wallpapers ───────────────────────────────────────────────────────── # One palette derivation per image, not one for the set: generation is a # genetic search that takes seconds, so per-image derivations mean adding a # wallpaper only regenerates the new one. builtins.path hashes the image # content alone, so unrelated repo edits don't invalidate any of them. paletteGenerator = inputs.stylix.packages.${pkgs.stdenv.hostPlatform.system}.palette-generator; wallFiles = builtins.attrNames (lib.filterAttrs (n: t: t == "regular" && lib.any (e: lib.hasSuffix e (lib.toLower n)) [ ".png" ".jpg" ".jpeg" ]) (builtins.readDir (inputs.self + "/walls"))); # Store names allow a narrow charset, filenames don't. Keep wallpaper names # plain — a space survives, anything more exotic may not. wallStem = f: lib.head (lib.splitString "." f); wallSlug = f: lib.replaceStrings [ " " ] [ "-" ] (wallStem f); wallImage = f: builtins.path { name = "wall-" + lib.replaceStrings [ " " ] [ "-" ] f; path = inputs.self + "/walls/${f}"; }; wallPalette = f: pkgs.runCommand "palette-${wallSlug f}" { } '' mkdir -p $out ${paletteGenerator}/bin/palette-generator dark ${wallImage f} $out/palette.json ''; # Import from derivation, the same way stylix reads its own generated # palette. Each wallpaper's palette is built during evaluation so its # colours can be rendered into config files here rather than at runtime. # # The `.override {}` is not decoration: bare mkSchemeAttrs output is not # callable as a template renderer, and calling it anyway returns a plain # attrset that stringifies to the template's path. stylix does the same # thing to build lib.stylix.colors. schemeOf = f: (config.stylix.base16.mkSchemeAttrs (builtins.fromJSON (builtins.readFile "${wallPalette f}/palette.json"))).override { }; # ── Shared colour helpers ────────────────────────────────────────────── # Scaling all three channels by one factor is exactly what Qt.lighter() # does — hue and HSV saturation stay put, only value moves — so GTK lands # on the same surfaces as quickshell's Theme.base01/base02. GTK's own # shade() can't stand in: it multiplies saturation too. tint = s: f: let ch = k: toString (builtins.floor (builtins.fromJSON s."base00-rgb-${k}" * f)); in "rgb(${ch "r"}, ${ch "g"}, ${ch "b"})"; hexDigit = ch: { "0"=0;"1"=1;"2"=2;"3"=3;"4"=4;"5"=5;"6"=6;"7"=7; "8"=8;"9"=9;"a"=10;"b"=11;"c"=12;"d"=13;"e"=14;"f"=15; }.${ch}; hexByte = s: hexDigit (builtins.substring 0 1 s) * 16 + hexDigit (builtins.substring 1 1 s); hexToRgb = hex: let h = lib.toLower hex; in { r = hexByte (builtins.substring 0 2 h); g = hexByte (builtins.substring 2 2 h); b = hexByte (builtins.substring 4 2 h); }; # papirus-folders palette → the front-face fill of each folder-.svg, # read out of the icons themselves. # # NOT the Material Design values the names imply. Papirus paints the front # panel a good deal lighter than its own colour name: nordic's face is # 81a1c1, while 5e81ac is only the small back flap behind it. Matching a # teal accent against the nominal values is what made these folders come # out blue — the real darkcyan is 17x closer than nordic, the nominal one # wasn't. # # Every key here is a variant that actually ships (there is no "palegreen"; # a name with no folder--*.svg files silently recolours nothing). pfPalette = { adwaita="93c0ea"; black="4f4f4f"; blue="5294e2"; bluegrey="607d8b"; breeze="57b8ec"; brown="ae8e6c"; carmine="a30002"; cyan="00bcd4"; darkcyan="45abb7"; deeporange="eb6637"; green="87b158"; grey="8e8e8e"; indigo="5c6bc0"; magenta="ca71df"; nordic="81a1c1"; orange="ee923a"; palebrown="d1bfae"; paleorange="eeca8f"; pink="f06292"; red="e25252"; teal="16a085"; violet="7e57c2"; white="e4e4e4"; yaru="676767"; yellow="f9bd30"; }; pfColorOf = s: let sq = x: x * x; dist = a: b: sq (a.r - b.r) + sq (a.g - b.g) + sq (a.b - b.b); accent = hexToRgb s.base0D; ranked = map (n: { name = n; d = dist accent (hexToRgb pfPalette.${n}); }) (builtins.attrNames pfPalette); in (builtins.foldl' (best: x: if x.d < best.d then x else best) (builtins.head ranked) (builtins.tail ranked)).name; # Map Material You placeholders to the closest base16 slot. The mapping is # approximate (Material You has more semantic colours than base16), but # covers the placeholders used in our Zen userChrome and Vesktop quickCss # templates. stylixize = s: builtins.replaceStrings [ "{{colors.primary.default.hex}}" "{{colors.primary_container.default.hex}}" "{{colors.secondary_container.default.hex}}" "{{colors.tertiary_container.default.hex}}" "{{colors.surface.default.hex}}" "{{colors.surface_container.default.hex}}" "{{colors.surface_container_low.default.hex}}" "{{colors.surface_container_high.default.hex}}" "{{colors.on_surface.default.hex}}" "{{colors.on_surface_variant.default.hex}}" "{{colors.outline.default.hex}}" "{{colors.outline_variant.default.hex}}" ] [ "#${s.base0D}" # primary accent "#${s.base02}" # primary container "#${s.base0C}" # secondary container (cyan) "#${s.base0E}" # tertiary container (purple) "#${s.base00}" # surface (main bg) (tint s 1.7) # surface container (urlbar, panels, dialogs) "#${s.base00}" # surface container low (tint s 2.4) # surface container high (selected tab, focused urlbar) "#${s.base05}" # on surface (fg) "#${s.base04}" # on surface variant (muted fg) "#${s.base03}" # outline "#${s.base02}" # outline variant ]; # ── Per-app renderers ────────────────────────────────────────────────── # GTK reuses stylix's own mustache template, so every @define-color stylix # would have written is still written — our overrides are appended after # it and therefore win. # # `template` is the mustache SOURCE, not a path to it: base16.nix only # treats the argument as a file when it is a path or a derivation, and # anything else — including the store-path string `builtins.path` hands # back — is written out verbatim as the template. That failure is silent # (you get a "stylesheet" containing one store path), which is what the # check in themeFor below exists to catch. gtkCssTemplate = builtins.readFile "${inputs.stylix}/modules/gtk/gtk.css.mustache"; gtkCss = s: pkgs.concatText "gtk3.css" [ (s { template = gtkCssTemplate; extension = ".css"; }) (pkgs.writeText "gtk-extra.css" '' /* Surfaces: one hue, three steps — the same ladder quickshell's Theme uses. Stylix paints every raised surface with base01/base02, which come straight off the wallpaper and routinely land off-hue: base01 is #3f3fab purple over a slate #1c1f26 desktop, which is why nemo's delete dialog was blue. */ @define-color headerbar_bg_color ${tint s 1.7}; @define-color sidebar_bg_color ${tint s 1.7}; @define-color card_bg_color ${tint s 1.7}; @define-color dialog_bg_color ${tint s 1.7}; @define-color popover_bg_color ${tint s 1.7}; @define-color scrollbar_outline_color ${tint s 2.4}; headerbar { min-height: 0; padding: 0; margin: 0; } headerbar .title { font-size: 0; } window:backdrop { background-color: @window_bg_color; color: @window_fg_color; } window:backdrop headerbar { background-color: @headerbar_bg_color; color: @headerbar_fg_color; } '') ]; # GTK4 reads gtk-theme-name and then ignores it, so the stylesheet has to be # pulled in by hand — this is what home-manager's own gtk4.theme option # writes, and taking over the file means taking this with it. It has to lead # the file: everything after an @import overrides it, which is exactly the # order the colours want. gtk4Css = s: pkgs.concatText "gtk4.css" [ (pkgs.writeText "adw-gtk3-import.css" '' @import url("file://${pkgs.adw-gtk3}/share/themes/adw-gtk3/gtk-4.0/gtk.css"); '') (gtkCss s) ]; # Ghostty theme files are plain config syntax, so this is included from the # main config with `config-file` rather than installed as a named theme. ghosttyConf = s: pkgs.writeText "ghostty-theme.conf" (with s.withHashtag; '' background = ${base00} foreground = ${base05} cursor-color = ${base05} selection-background = ${base02} selection-foreground = ${base05} palette = 0=${base00} palette = 1=${base08} palette = 2=${base0B} palette = 3=${base0A} palette = 4=${base0D} palette = 5=${base0E} palette = 6=${base0C} palette = 7=${base05} palette = 8=${base03} palette = 9=${base08} palette = 10=${base0B} palette = 11=${base0A} palette = 12=${base0D} palette = 13=${base0E} palette = 14=${base0C} palette = 15=${base07} ''); btopTheme = s: pkgs.writeText "btop.theme" (with s.withHashtag; '' #Generated by settings/theming.nix theme[main_bg]="${base00}" theme[main_fg]="${base05}" theme[title]="${base05}" theme[hi_fg]="${base0D}" theme[selected_bg]="${base03}" theme[selected_fg]="${base0D}" theme[inactive_fg]="${base04}" theme[graph_text]="${base06}" theme[meter_bg]="${base03}" theme[proc_misc]="${base06}" theme[cpu_box]="${base0E}" theme[mem_box]="${base0B}" theme[net_box]="${base0C}" theme[proc_box]="${base0D}" theme[div_line]="${base01}" theme[temp_start]="${base0B}" theme[temp_mid]="${base0A}" theme[temp_end]="${base08}" theme[cpu_start]="${base0B}" theme[cpu_mid]="${base0A}" theme[cpu_end]="${base08}" theme[free_start]="${base0A}" theme[free_mid]="${base0B}" theme[free_end]="${base0B}" theme[cached_start]="${base0C}" theme[cached_mid]="${base0C}" theme[cached_end]="${base0A}" theme[available_start]="${base08}" theme[available_mid]="${base0A}" theme[available_end]="${base0B}" theme[used_start]="${base0A}" theme[used_mid]="${base09}" theme[used_end]="${base08}" theme[download_start]="${base0B}" theme[download_mid]="${base0A}" theme[download_end]="${base08}" theme[upload_start]="${base0B}" theme[upload_mid]="${base0A}" theme[upload_end]="${base08}" theme[process_start]="${base0B}" theme[process_mid]="${base0A}" theme[process_end]="${base08}" ''); # Lua, fed to `hyprctl eval`. NOT `hyprctl keyword`, which answers # "keyword can't work with non-legacy parsers. Use eval." # under the Lua config this repo uses (settings/hyprland.nix sets # configType = "lua"). eval runs in the live VM with isDynamicParse() true, # so hl.config schedules the refresh that actually repaints the borders. # # Keys are dot-joined here, matching how the Lua config manager flattens # nested tables — the same shape settings/hyprland.nix already declares, # which stays the build-time fallback until theme-apply first runs. # # One line, not pretty-printed: hyprctl joins its argv into the request # string, so keeping the payload free of newlines keeps it intact. hyprLua = s: pkgs.writeText "hypr.lua" ("hl.config({" + lib.concatStringsSep "," [ ''general={["col.active_border"]="rgb(${s.base04})",["col.inactive_border"]="rgb(${s.base03})"}'' ''decoration={shadow={color="rgba(${s.base00}99)"}}'' (''group={["col.border_active"]="rgb(${s.base04})",'' + ''["col.border_inactive"]="rgb(${s.base03})",'' + ''["col.border_locked_active"]="rgb(${s.base05})",'' + ''groupbar={text_color="rgb(${s.base05})",'' + ''["col.active"]="rgb(${s.base04})",["col.inactive"]="rgb(${s.base03})"}}'') ''misc={background_color="rgb(${s.base00})"}'' ] + "})"); # ── App icons ────────────────────────────────────────────────────────── # YAMIS draws the app icons in the launcher. Picked over the far bigger # monochrome sets because coverage beats library size: it is the only one # still shipping commits, so it has the apps actually installed here # (zen, ghostty, vesktop, bazaar, faugus, protonplus) where the 10k-icon # archives — Suru++ Asprómauros, dead since 2019 — do not. # # Not in nixpkgs. Upstream is a bare icon tree with the theme at its root, # so the "build" is a copy into the directory name the spec resolves by. # # FollowsColorScheme=true is a KDE key: only Plasma rewrites the embedded #