refactor: format nix code with alejandra & rename nix files for clarity

This commit is contained in:
pengo 2026-06-08 17:38:35 -06:00
parent bb86ea12ca
commit df492dab6b
6 changed files with 298 additions and 311 deletions

View file

@ -40,25 +40,25 @@
system, system,
}: let }: let
inherit (pkgs) callPackage; inherit (pkgs) callPackage;
mango = callPackage ./nix { mango = callPackage ./nix/package.nix {
inherit (scenefx.packages.${system}) scenefx; inherit (scenefx.packages.${system}) scenefx;
}; };
in { in {
inherit mango; inherit mango;
default = mango; default = mango;
hm-options-json = callPackage (import ./nix/generate-options.nix self) { hm-options-json = callPackage (import ./nix/generate-options.nix self) {
module = ./nix/hm-modules.nix; module = ./nix/hm-module.nix;
optionPrefix = "wayland.windowManager.mango."; optionPrefix = "wayland.windowManager.mango.";
}; };
nixos-options-json = callPackage (import ./nix/generate-options.nix self) { nixos-options-json = callPackage (import ./nix/generate-options.nix self) {
module = ./nix/nixos-modules.nix; module = ./nix/nixos-module.nix;
optionPrefix = "programs.mango."; optionPrefix = "programs.mango.";
}; };
} }
); );
nixosModules.mango = import ./nix/nixos-modules.nix self; nixosModules.mango = import ./nix/nixos-module.nix self;
hmModules.mango = import ./nix/hm-modules.nix self; hmModules.mango = import ./nix/hm-module.nix self;
devShells = forEachSystem ( devShells = forEachSystem (
{system, ...}: { {system, ...}: {

View file

@ -1,20 +1,18 @@
self: self: {
{
pkgs, pkgs,
lib ? pkgs.lib, lib ? pkgs.lib,
module, module,
optionPrefix, optionPrefix,
}: }: let
let
# Absolute store path of the flake root, used to compute relative subpaths # Absolute store path of the flake root, used to compute relative subpaths
repoPath = toString self; repoPath = toString self;
eval = lib.evalModules { eval = lib.evalModules {
modules = [ modules = [
(import module self) (import module self)
{ _module.check = false; } {_module.check = false;}
]; ];
specialArgs = { inherit pkgs; }; specialArgs = {inherit pkgs;};
}; };
# Relative path of the module file within the repo (e.g. "nix/hm-modules.nix") # Relative path of the module file within the repo (e.g. "nix/hm-modules.nix")
@ -28,15 +26,14 @@ let
optionsDoc = pkgs.nixosOptionsDoc { optionsDoc = pkgs.nixosOptionsDoc {
options = eval.options; options = eval.options;
transformOptions = transformOptions = opt:
opt:
opt opt
// { // {
visible = opt.visible && !opt.internal; visible = opt.visible && !opt.internal;
# Strip the option prefix so docs show "enable" instead of "programs.mango.enable" # Strip the option prefix so docs show "enable" instead of "programs.mango.enable"
name = lib.removePrefix optionPrefix opt.name; name = lib.removePrefix optionPrefix opt.name;
declarations = [ moduleDeclaration ]; declarations = [moduleDeclaration];
}; };
}; };
in in
optionsDoc.optionsJSON optionsDoc.optionsJSON

View file

@ -1,11 +1,9 @@
self: self: {
{
lib, lib,
config, config,
pkgs, pkgs,
... ...
}: }: let
let
cfg = config.wayland.windowManager.mango; cfg = config.wayland.windowManager.mango;
selflib = import ./lib.nix lib; selflib = import ./lib.nix lib;
variables = lib.concatStringsSep " " cfg.systemd.variables; variables = lib.concatStringsSep " " cfg.systemd.variables;
@ -15,8 +13,7 @@ let
${lib.optionalString cfg.systemd.enable systemdActivation} ${lib.optionalString cfg.systemd.enable systemdActivation}
${cfg.autostart_sh} ${cfg.autostart_sh}
''; '';
in in {
{
options = { options = {
wayland.windowManager.mango = with lib; { wayland.windowManager.mango = with lib; {
enable = mkOption { enable = mkOption {
@ -59,7 +56,7 @@ in
"XCURSOR_THEME" "XCURSOR_THEME"
"XCURSOR_SIZE" "XCURSOR_SIZE"
]; ];
example = [ "--all" ]; example = ["--all"];
description = '' description = ''
Environment variables imported into the systemd and D-Bus user environment. Environment variables imported into the systemd and D-Bus user environment.
''; '';
@ -80,9 +77,7 @@ in
''; '';
}; };
settings = mkOption { settings = mkOption {
type = type = with lib.types; let
with lib.types;
let
valueType = valueType =
nullOr (oneOf [ nullOr (oneOf [
bool bool
@ -98,7 +93,7 @@ in
}; };
in in
valueType; valueType;
default = { }; default = {};
description = '' description = ''
Mango configuration written in Nix. Entries with the same key Mango configuration written in Nix. Entries with the same key
should be written as lists. Variables and colors names should be should be written as lists. Variables and colors names should be
@ -178,21 +173,21 @@ in
}; };
topPrefixes = mkOption { topPrefixes = mkOption {
type = with lib.types; listOf str; type = with lib.types; listOf str;
default = [ ]; default = [];
description = '' description = ''
List of prefixes for attributes that should appear at the top of the config file. List of prefixes for attributes that should appear at the top of the config file.
Attributes starting with these prefixes will be sorted to the beginning. Attributes starting with these prefixes will be sorted to the beginning.
''; '';
example = [ "source" ]; example = ["source"];
}; };
bottomPrefixes = mkOption { bottomPrefixes = mkOption {
type = with lib.types; listOf str; type = with lib.types; listOf str;
default = [ ]; default = [];
description = '' description = ''
List of prefixes for attributes that should appear at the bottom of the config file. List of prefixes for attributes that should appear at the bottom of the config file.
Attributes starting with these prefixes will be sorted to the end. Attributes starting with these prefixes will be sorted to the end.
''; '';
example = [ "source" ]; example = ["source"];
}; };
autostart_sh = mkOption { autostart_sh = mkOption {
description = '' description = ''
@ -217,25 +212,25 @@ in
finalConfigText = finalConfigText =
# Support old string-based config during transition period # Support old string-based config during transition period
( (
if builtins.isString cfg.settings then if builtins.isString cfg.settings
cfg.settings then cfg.settings
else else
lib.optionalString (cfg.settings != { }) ( lib.optionalString (cfg.settings != {}) (
selflib.toMango { selflib.toMango {
topCommandsPrefixes = cfg.topPrefixes; topCommandsPrefixes = cfg.topPrefixes;
bottomCommandsPrefixes = cfg.bottomPrefixes; bottomCommandsPrefixes = cfg.bottomPrefixes;
} cfg.settings }
cfg.settings
) )
) )
+ lib.optionalString (cfg.extraConfig != "") cfg.extraConfig + lib.optionalString (cfg.extraConfig != "") cfg.extraConfig
+ lib.optionalString (cfg.autostart_sh != "") "\nexec-once=~/.config/mango/autostart.sh\n"; + lib.optionalString (cfg.autostart_sh != "") "\nexec-once=~/.config/mango/autostart.sh\n";
validatedConfig = pkgs.runCommand "mango-config.conf" { } '' validatedConfig = pkgs.runCommand "mango-config.conf" {} ''
cp ${pkgs.writeText "mango-config.conf" finalConfigText} "$out" cp ${pkgs.writeText "mango-config.conf" finalConfigText} "$out"
${cfg.package}/bin/mango -c "$out" -p || exit 1 ${cfg.package}/bin/mango -c "$out" -p || exit 1
''; '';
in in {
{
# Backwards compatibility warning for old string-based config # Backwards compatibility warning for old string-based config
warnings = lib.optional (builtins.isString cfg.settings) '' warnings = lib.optional (builtins.isString cfg.settings) ''
wayland.windowManager.mango.settings: Using a string for settings is deprecated. wayland.windowManager.mango.settings: Using a string for settings is deprecated.
@ -244,10 +239,10 @@ in
The old string format will be removed in a future release. The old string format will be removed in a future release.
''; '';
home.packages = [ cfg.package ]; home.packages = [cfg.package];
xdg.configFile = { xdg.configFile = {
"mango/config.conf" = "mango/config.conf" =
lib.mkIf (cfg.settings != { } || cfg.extraConfig != "" || cfg.autostart_sh != "") lib.mkIf (cfg.settings != {} || cfg.extraConfig != "" || cfg.autostart_sh != "")
{ {
source = validatedConfig; source = validatedConfig;
}; };
@ -259,13 +254,14 @@ in
systemd.user.targets.mango-session = lib.mkIf cfg.systemd.enable { systemd.user.targets.mango-session = lib.mkIf cfg.systemd.enable {
Unit = { Unit = {
Description = "mango compositor session"; Description = "mango compositor session";
Documentation = [ "man:systemd.special(7)" ]; Documentation = ["man:systemd.special(7)"];
BindsTo = [ "graphical-session.target" ]; BindsTo = ["graphical-session.target"];
Wants = [ Wants =
[
"graphical-session-pre.target" "graphical-session-pre.target"
] ]
++ lib.optional cfg.systemd.xdgAutostart "xdg-desktop-autostart.target"; ++ lib.optional cfg.systemd.xdgAutostart "xdg-desktop-autostart.target";
After = [ "graphical-session-pre.target" ]; After = ["graphical-session-pre.target"];
Before = lib.optional cfg.systemd.xdgAutostart "xdg-desktop-autostart.target"; Before = lib.optional cfg.systemd.xdgAutostart "xdg-desktop-autostart.target";
}; };
}; };

View file

@ -1,6 +1,6 @@
lib: lib: let
let inherit
inherit (lib) (lib)
attrNames attrNames
filterAttrs filterAttrs
foldl foldl
@ -9,7 +9,8 @@ let
removeAttrs removeAttrs
; ;
inherit (lib.strings) inherit
(lib.strings)
concatMapStrings concatMapStrings
hasPrefix hasPrefix
; ;
@ -172,42 +173,33 @@ let
::: :::
*/ */
toMango = toMango = {
{ topCommandsPrefixes ? [],
topCommandsPrefixes ? [ ], bottomCommandsPrefixes ? [],
bottomCommandsPrefixes ? [ ], }: attrs: let
}: toMango' = attrs: let
attrs:
let
toMango' =
attrs:
let
# Specially configured `toKeyValue` generator with support for duplicate keys # Specially configured `toKeyValue` generator with support for duplicate keys
# and a legible key-value separator. # and a legible key-value separator.
mkCommands = generators.toKeyValue { mkCommands = generators.toKeyValue {
mkKeyValue = generators.mkKeyValueDefault { } " = "; mkKeyValue = generators.mkKeyValueDefault {} " = ";
listsAsDuplicateKeys = true; listsAsDuplicateKeys = true;
indent = ""; # No indent, since we don't have nesting indent = ""; # No indent, since we don't have nesting
}; };
# Extract keymode definitions if they exist # Extract keymode definitions if they exist
keymodes = attrs.keymode or { }; keymodes = attrs.keymode or {};
attrsWithoutKeymodes = removeAttrs attrs [ "keymode" ]; attrsWithoutKeymodes = removeAttrs attrs ["keymode"];
# Generate keymode blocks # Generate keymode blocks
# Format: keymode=name\nbind=...\nbind=...\n # Format: keymode=name\nbind=...\nbind=...\n
mkKeymodeBlock = mkKeymodeBlock = name: modeAttrs: let
name: modeAttrs:
let
modeCommands = flattenAttrs (p: k: "${p}_${k}") modeAttrs; modeCommands = flattenAttrs (p: k: "${p}_${k}") modeAttrs;
in in "keymode = ${name}\n${mkCommands modeCommands}";
"keymode = ${name}\n${mkCommands modeCommands}";
keymodeBlocks = keymodeBlocks =
if keymodes == { } then if keymodes == {}
"" then ""
else else "\n" + concatMapStrings (name: mkKeymodeBlock name keymodes.${name} + "\n") (attrNames keymodes);
"\n" + concatMapStrings (name: mkKeymodeBlock name keymodes.${name} + "\n") (attrNames keymodes);
# Flatten the attrset, combining keys in a "path" like `"a_b_c" = "x"`. # Flatten the attrset, combining keys in a "path" like `"a_b_c" = "x"`.
# Uses `flattenAttrs` with an underscore separator. # Uses `flattenAttrs` with an underscore separator.
@ -291,22 +283,25 @@ let
::: :::
*/ */
flattenAttrs = flattenAttrs = pred: attrs: let
pred: attrs: flattenAttrs' = prefix: attrs:
let
flattenAttrs' =
prefix: attrs:
builtins.foldl' ( builtins.foldl' (
acc: key: acc: key: let
let
value = attrs.${key}; value = attrs.${key};
newKey = if prefix == "" then key else pred prefix key; newKey =
if prefix == ""
then key
else pred prefix key;
in in
acc // (if builtins.isAttrs value then flattenAttrs' newKey value else { "${newKey}" = value; }) acc
) { } (builtins.attrNames attrs); // (
if builtins.isAttrs value
then flattenAttrs' newKey value
else {"${newKey}" = value;}
)
) {} (builtins.attrNames attrs);
in in
flattenAttrs' "" attrs; flattenAttrs' "" attrs;
in in {
{
inherit flattenAttrs toMango; inherit flattenAttrs toMango;
} }

View file

@ -23,8 +23,7 @@ in {
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
environment.systemPackages = environment.systemPackages = [
[
cfg.package cfg.package
]; ];
@ -60,7 +59,7 @@ in {
programs.xwayland.enable = lib.mkDefault true; programs.xwayland.enable = lib.mkDefault true;
services = { services = {
displayManager.sessionPackages = lib.mkIf cfg.addLoginEntry [ cfg.package ]; displayManager.sessionPackages = lib.mkIf cfg.addLoginEntry [cfg.package];
graphical-desktop.enable = lib.mkDefault true; graphical-desktop.enable = lib.mkDefault true;
}; };