Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix/module: toHyprconf -> toHyprlang #9221

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 111 additions & 79 deletions nix/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,98 @@ inputs: {
inherit (pkgs.stdenv.hostPlatform) system;
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
toHyprlang = {
topCommandsPrefixes ? ["$"],
bottomCommandsPrefixes ? [],
}: attrs: let
inherit (pkgs) lib;
inherit
(lib)
all
concatMapStringsSep
concatStrings
concatStringsSep
(lib.generators)
mkKeyValueDefault
toKeyValue
;
inherit
(lib.attrsets)
filterAttrs
foldl
generators
hasPrefix
isAttrs
isList
mapAttrsToList
replicate
;
inherit
(lib.lists)
foldl
isList
;
inherit
(lib.strings)
concatMapStringsSep
concatStringsSep
hasPrefix
;
inherit
(lib.trivial)
boolToString
isBool
;
inherit
(builtins)
all
attrNames
partition
;

initialIndent = concatStrings (replicate indentLevel " ");

toHyprconf' = indent: attrs: let
sections =
filterAttrs (n: v: isAttrs v || (isList v && all isAttrs v)) attrs;
toHyprlang' = attrs: let
toStr = x:
if isBool x
then boolToString x
else toString x;

mkSection = n: attrs:
categories = filterAttrs (n: v: isAttrs v || (isList v && all isAttrs v)) attrs;
mkCategory = parent: attrs:
if lib.isList attrs
then (concatMapStringsSep "\n" (a: mkSection n a) attrs)
else ''
${indent}${n} {
${toHyprconf' " ${indent}" attrs}${indent}}
'';

mkFields = generators.toKeyValue {
then concatMapStringsSep "\n" (a: mkCategory parent a) attrs
else
concatStringsSep "\n" (
mapAttrsToList (
k: v:
if isAttrs v
then mkCategory "${parent}:${k}" v
else if isList v
then concatMapStringsSep "\n" (item: "${parent}:${k} = ${toStr item}") v
else "${parent}:${k} = ${toStr v}"
)
attrs
);

mkCommands = toKeyValue {
mkKeyValue = mkKeyValueDefault {} " = ";
listsAsDuplicateKeys = true;
inherit indent;
indent = "";
};

allFields =
filterAttrs (n: v: !(isAttrs v || (isList v && all isAttrs v)))
attrs;
allCommands = 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;
filterCommands = list: n: foldl (acc: prefix: acc || hasPrefix prefix n) false list;

importantFields = filterAttrs isImportantField allFields;
# Get topCommands attr names
result = partition (filterCommands topCommandsPrefixes) (attrNames allCommands);
# Filter top commands from all commands
topCommands = filterAttrs (n: _: (builtins.elem n result.right)) allCommands;
# Remaining commands = allcallCommands - topCommands
remainingCommands = removeAttrs allCommands result.right;

fields =
builtins.removeAttrs allFields
(mapAttrsToList (n: _: n) importantFields);
# Get bottomCommands attr names
result2 = partition (filterCommands bottomCommandsPrefixes) result.wrong;
# Filter bottom commands from remainingCommands
bottomCommands = filterAttrs (n: _: (builtins.elem n result2.right)) remainingCommands;
# Regular commands = allCommands - topCommands - bottomCommands
regularCommands = removeAttrs remainingCommands result2.right;
in
mkFields importantFields
+ concatStringsSep "\n" (mapAttrsToList mkSection sections)
+ mkFields fields;
mkCommands topCommands
+ concatStringsSep "\n" (mapAttrsToList mkCategory categories)
+ mkCommands regularCommands
+ mkCommands bottomCommands;
in
toHyprconf' initialIndent attrs;
toHyprlang' attrs;
in {
options = {
programs.hyprland = {
Expand Down Expand Up @@ -106,6 +134,9 @@ in {
should be written as lists. Variables' and colors' names should be
quoted. See <https://wiki.hyprland.org> 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.
Expand Down Expand Up @@ -151,20 +182,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.
'';
};
};
Expand All @@ -173,38 +205,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 != [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean we can have distro specific configuration now? (which and user config would work together/be merged and then evaluated?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In short, no.

The current hyprutils implementation only looks for configs in the order XDG_CONFIG_HOME/hypr -> HOME/.config/hypr -> XDG_CONFIG_DIRS/hypr -> /etc/xdg/hypr.

I've been meaning to work on extending this to accept configs from multiple sources (hypr.d/xx-config.conf), but I'm not sure combining user and system configs is such a great idea by default. Can be done with a simple source though.

Copy link
Contributor

@JohnRTitor JohnRTitor Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well as we discussed previously on Nixpkgs, enabling distros to apply default configurations would be helpful in cases where we need things to work out of the box, like importing all env vars (including PATH, XDG_DATA_DIRS), enabling/starting certain services.

These default system configs could just be ignored with a flag like disable-distro-config = true

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somewhat out of the scope of this PR, but I'm curious what @vaxerski would say about this.


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:
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;
})
(toHyprlang {
topCommandsPrefixes = cfg.topPrefixes;
bottomCommandsPrefixes = cfg.bottomPrefixes;
}
cfg.settings)
+ lib.optionalString (cfg.extraConfig != "") cfg.extraConfig;
};
})
Expand Down
Loading