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 24580afe56
commit c0d7b0cf73
6 changed files with 298 additions and 311 deletions

View file

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

View file

@ -1,20 +1,18 @@
self:
{
self: {
pkgs,
lib ? pkgs.lib,
module,
optionPrefix,
}:
let
}: let
# Absolute store path of the flake root, used to compute relative subpaths
repoPath = toString self;
eval = lib.evalModules {
modules = [
(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")
@ -28,15 +26,14 @@ let
optionsDoc = pkgs.nixosOptionsDoc {
options = eval.options;
transformOptions =
opt:
transformOptions = opt:
opt
// {
visible = opt.visible && !opt.internal;
# Strip the option prefix so docs show "enable" instead of "programs.mango.enable"
name = lib.removePrefix optionPrefix opt.name;
declarations = [ moduleDeclaration ];
declarations = [moduleDeclaration];
};
};
in
optionsDoc.optionsJSON
optionsDoc.optionsJSON

View file

@ -1,11 +1,9 @@
self:
{
self: {
lib,
config,
pkgs,
...
}:
let
}: let
cfg = config.wayland.windowManager.mango;
selflib = import ./lib.nix lib;
variables = lib.concatStringsSep " " cfg.systemd.variables;
@ -15,8 +13,7 @@ let
${lib.optionalString cfg.systemd.enable systemdActivation}
${cfg.autostart_sh}
'';
in
{
in {
options = {
wayland.windowManager.mango = with lib; {
enable = mkOption {
@ -59,7 +56,7 @@ in
"XCURSOR_THEME"
"XCURSOR_SIZE"
];
example = [ "--all" ];
example = ["--all"];
description = ''
Environment variables imported into the systemd and D-Bus user environment.
'';
@ -80,9 +77,7 @@ in
'';
};
settings = mkOption {
type =
with lib.types;
let
type = with lib.types; let
valueType =
nullOr (oneOf [
bool
@ -98,7 +93,7 @@ in
};
in
valueType;
default = { };
default = {};
description = ''
Mango configuration written in Nix. Entries with the same key
should be written as lists. Variables and colors names should be
@ -178,21 +173,21 @@ in
};
topPrefixes = mkOption {
type = with lib.types; listOf str;
default = [ ];
default = [];
description = ''
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.
'';
example = [ "source" ];
example = ["source"];
};
bottomPrefixes = mkOption {
type = with lib.types; listOf str;
default = [ ];
default = [];
description = ''
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.
'';
example = [ "source" ];
example = ["source"];
};
autostart_sh = mkOption {
description = ''
@ -217,25 +212,25 @@ in
finalConfigText =
# Support old string-based config during transition period
(
if builtins.isString cfg.settings then
cfg.settings
if builtins.isString cfg.settings
then cfg.settings
else
lib.optionalString (cfg.settings != { }) (
lib.optionalString (cfg.settings != {}) (
selflib.toMango {
topCommandsPrefixes = cfg.topPrefixes;
bottomCommandsPrefixes = cfg.bottomPrefixes;
} cfg.settings
}
cfg.settings
)
)
+ lib.optionalString (cfg.extraConfig != "") cfg.extraConfig
+ 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"
${cfg.package}/bin/mango -c "$out" -p || exit 1
'';
in
{
in {
# Backwards compatibility warning for old string-based config
warnings = lib.optional (builtins.isString cfg.settings) ''
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.
'';
home.packages = [ cfg.package ];
home.packages = [cfg.package];
xdg.configFile = {
"mango/config.conf" =
lib.mkIf (cfg.settings != { } || cfg.extraConfig != "" || cfg.autostart_sh != "")
lib.mkIf (cfg.settings != {} || cfg.extraConfig != "" || cfg.autostart_sh != "")
{
source = validatedConfig;
};
@ -259,13 +254,14 @@ in
systemd.user.targets.mango-session = lib.mkIf cfg.systemd.enable {
Unit = {
Description = "mango compositor session";
Documentation = [ "man:systemd.special(7)" ];
BindsTo = [ "graphical-session.target" ];
Wants = [
Documentation = ["man:systemd.special(7)"];
BindsTo = ["graphical-session.target"];
Wants =
[
"graphical-session-pre.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";
};
};

View file

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

View file

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