From 1ecf4d461de751a4833d9bae3af294a7b7d90362 Mon Sep 17 00:00:00 2001 From: technofab Date: Mon, 15 Sep 2025 15:37:32 +0200 Subject: [PATCH] feat: automatically populate meta from env variables if they have a desc --- lib/modules/env.nix | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/lib/modules/env.nix b/lib/modules/env.nix index ee5da94..4f382e5 100644 --- a/lib/modules/env.nix +++ b/lib/modules/env.nix @@ -4,7 +4,18 @@ config, ... }: let - inherit (lib) filter assertMsg head length escapeShellArg types mkOption concatStringsSep; + inherit + (lib) + filter + filterAttrs + assertMsg + head + length + escapeShellArg + types + mkOption + concatStringsSep + ; envToBash = { name, @@ -37,7 +48,11 @@ then ''unset ${name}'' else throw "[devshell] BUG in the env module. This should never be reached."; - envType = types.submodule ({name, ...}: { + envType = types.submodule ({ + config, + name, + ... + }: { options = { name = mkOption { type = types.str; @@ -46,7 +61,7 @@ Name of the environment variable, usually already set to ``. ''; }; - desc = mkOption { + description = mkOption { type = types.nullOr types.str; default = null; description = '' @@ -55,7 +70,7 @@ }; visible = mkOption { type = types.bool; - default = true; + default = config.description != null; description = '' Whether to include this env var and it's description in the menu. ''; @@ -120,16 +135,12 @@ in { env = { "XDG_DATA_DIRS".eval = "$DEVSHELL_DIR/share:\${XDG_DATA_DIRS-}"; }; - enterShellCommands."env".text = concatStringsSep "\n" (map envToBash (builtins.attrValues config.env)); - # TODO: collect all env vars, then add them to the menu if they have a description and "visibile == true" - - meta = { - sections."Environment Variables".color = "red"; - entries."XDG_DATA_DIRS" = { - description = "Hello world"; - section = "Environment Variables"; - subEntries."Usage" = "hello world"; - }; - }; + enterShellCommands."env".text = + concatStringsSep "\n" (map envToBash (builtins.attrValues config.env)); + # meta entries for the menu + meta.entries = builtins.mapAttrs (_name: env: { + inherit (env) description; + section = "Environment Variables"; + }) (filterAttrs (_name: env: env.visible) config.env); }; }