# Home Manager environment module { config, lib, ... }: let inherit (lib) mkEnableOption mkOption mkIf types ; cfg = config.custom.environment; # Convert attrset to shell export statements toShellExports = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList (name: value: ''export ${name}="${value}"'') vars); # Convert attrset to fish set statements toFishSets = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList (name: value: ''set -gx ${name} "${value}"'') vars); in { options.custom.environment = { enable = mkEnableOption "custom environment configuration"; variables = mkOption { type = types.attrsOf types.str; default = { }; description = "Environment variables to set across all shells."; example = { EDITOR = "nvim"; SOPS_AGE_KEY_FILE = "$HOME/.config/sops/age/keys.txt"; }; }; }; config = mkIf cfg.enable { home.sessionVariables = cfg.variables; # Also add to shell configs for immediate availability programs.bash.bashrcExtra = lib.mkAfter (toShellExports cfg.variables); programs.zsh.initContent = lib.mkAfter (toShellExports cfg.variables); programs.fish.shellInit = lib.mkAfter (toFishSets cfg.variables); }; }