From 905ca39bc96c772b218cde3f0d3df02868bd2ae0 Mon Sep 17 00:00:00 2001 From: Mihai Fufezan Date: Sun, 2 Mar 2025 15:41:49 +0200 Subject: nix/module: toHyprconf -> toHyprlang (#9221) * nix/module: toHyprconf -> toHyprlang Updated generator that will end up living in Nixpkgs' `lib/generators`. * nix/module: use xdph package directly The downstream module already applies hyprland's finalPackage to the portalPackage. * new lib * lib: add flattenAttrs, remove category processing Flattening attributes means we no longer need to process categories separately. For all intents and purposes, they do not exist. Simplify the codebase once again, while introducing an easy to grasp recursive function. Add a bit of documentation for toHyprlang, though I doubt it's clear enough even now. Still needs proper NixDoc. * lib: add proper NixDoc * nix/lib: inherit from lib --- nix/module.nix | 132 ++++++++++++++++----------------------------------------- 1 file changed, 36 insertions(+), 96 deletions(-) (limited to 'nix/module.nix') diff --git a/nix/module.nix b/nix/module.nix index 46191dfa..0b8c4f42 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -5,72 +5,8 @@ inputs: { ... }: let inherit (pkgs.stdenv.hostPlatform) system; + selflib = import ./lib.nix lib; cfg = config.programs.hyprland; - - # basically 1:1 taken from https://github.com/nix-community/home-manager/blob/master/modules/services/window-managers/hyprland.nix - toHyprconf = { - attrs, - indentLevel ? 0, - importantPrefixes ? ["$"], - }: let - inherit - (lib) - all - concatMapStringsSep - concatStrings - concatStringsSep - filterAttrs - foldl - generators - hasPrefix - isAttrs - isList - mapAttrsToList - replicate - ; - - initialIndent = concatStrings (replicate indentLevel " "); - - toHyprconf' = indent: attrs: let - sections = - filterAttrs (n: v: isAttrs v || (isList v && all isAttrs v)) attrs; - - mkSection = n: attrs: - if lib.isList attrs - then (concatMapStringsSep "\n" (a: mkSection n a) attrs) - else '' - ${indent}${n} { - ${toHyprconf' " ${indent}" attrs}${indent}} - ''; - - mkFields = generators.toKeyValue { - listsAsDuplicateKeys = true; - inherit indent; - }; - - allFields = - filterAttrs (n: v: !(isAttrs v || (isList v && all isAttrs v))) - attrs; - - isImportantField = n: _: - foldl (acc: prev: - if hasPrefix prev n - then true - else acc) - false - importantPrefixes; - - importantFields = filterAttrs isImportantField allFields; - - fields = - builtins.removeAttrs allFields - (mapAttrsToList (n: _: n) importantFields); - in - mkFields importantFields - + concatStringsSep "\n" (mapAttrsToList mkSection sections) - + mkFields fields; - in - toHyprconf' initialIndent attrs; in { options = { programs.hyprland = { @@ -106,6 +42,9 @@ in { should be written as lists. Variables' and colors' names should be quoted. See for more examples. + Special categories (e.g `devices`) should be written as + `"devices[device-name]"`. + ::: {.note} Use the [](#programs.hyprland.plugins) option to declare plugins. @@ -151,20 +90,21 @@ in { ''; }; - sourceFirst = - lib.mkEnableOption '' - putting source entries at the top of the configuration - '' - // { - default = true; - }; + topPrefixes = lib.mkOption { + type = with lib.types; listOf str; + default = ["$" "bezier"]; + example = ["$" "bezier" "source"]; + description = '' + List of prefix of attributes to put at the top of the config. + ''; + }; - importantPrefixes = lib.mkOption { + bottomPrefixes = lib.mkOption { type = with lib.types; listOf str; - default = ["$" "bezier" "name"] ++ lib.optionals cfg.sourceFirst ["source"]; - example = ["$" "bezier"]; + default = []; + example = ["source"]; description = '' - List of prefix of attributes to source at the top of the config. + List of prefix of attributes to put at the bottom of the config. ''; }; }; @@ -173,38 +113,38 @@ in { { programs.hyprland = { package = lib.mkDefault inputs.self.packages.${system}.hyprland; - portalPackage = lib.mkDefault (inputs.self.packages.${system}.xdg-desktop-portal-hyprland.override { - hyprland = cfg.finalPackage; - }); + portalPackage = lib.mkDefault inputs.self.packages.${system}.xdg-desktop-portal-hyprland; }; } (lib.mkIf cfg.enable { environment.etc."xdg/hypr/hyprland.conf" = let shouldGenerate = cfg.extraConfig != "" || cfg.settings != {} || cfg.plugins != []; - pluginsToHyprconf = plugins: - toHyprconf { - attrs = { - plugin = let - mkEntry = entry: - if lib.types.package.check entry - then "${entry}/lib/lib${entry.pname}.so" - else entry; - in - map mkEntry cfg.plugins; - }; - inherit (cfg) importantPrefixes; + pluginsToHyprlang = plugins: + selflib.toHyprlang { + topCommandsPrefixes = cfg.topPrefixes; + bottomCommandsPrefixes = cfg.bottomPrefixes; + } + { + plugin = let + mkEntry = entry: + if lib.types.package.check entry + then "${entry}/lib/lib${entry.pname}.so" + else entry; + in + map mkEntry cfg.plugins; }; in lib.mkIf shouldGenerate { text = lib.optionalString (cfg.plugins != []) - (pluginsToHyprconf cfg.plugins) + (pluginsToHyprlang cfg.plugins) + lib.optionalString (cfg.settings != {}) - (toHyprconf { - attrs = cfg.settings; - inherit (cfg) importantPrefixes; - }) + (selflib.toHyprlang { + topCommandsPrefixes = cfg.topPrefixes; + bottomCommandsPrefixes = cfg.bottomPrefixes; + } + cfg.settings) + lib.optionalString (cfg.extraConfig != "") cfg.extraConfig; }; }) -- cgit v1.2.3