{home-environment,bash,zsh}: support ignoring null sessionVariables

Some applications consider environment variables to be “true” simply if
they are set. Previously it was not possible to have a var set and later
override (e.g. with `lib.mkForce`) to *unset* it. This makes that
possible. The filtering must be done in the `exportAll` as attempting to
do it in the option’s `apply` causing infinite recursion (likely why it
is `lazyAttrsOf`).
This commit is contained in:
Andrew Marshall
2025-08-07 15:19:58 -04:00
committed by Austin Horstman
parent 62f445c13c
commit d45a13f96c
9 changed files with 52 additions and 21 deletions

View File

@@ -266,12 +266,14 @@ in
default = { };
type =
with types;
lazyAttrsOf (oneOf [
str
path
int
float
]);
lazyAttrsOf (
nullOr (oneOf [
str
path
int
float
])
);
example = {
EDITOR = "emacs";
GS_OPTIONS = "-sPAPERSIZE=a4";
@@ -307,6 +309,9 @@ in
BAR = "''${config.home.sessionVariables.FOO} World!";
};
```
Setting a value to `null` will skip setting the variable at all, which
may be useful when overriding.
'';
};

View File

@@ -69,7 +69,9 @@ in
# Given an attribute set containing shell variable names and their
# assignment, this function produces a string containing an export
# statement for each set entry.
exportAll = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList export vars);
exportAll =
vars:
lib.concatStringsSep "\n" (lib.mapAttrsToList export (lib.filterAttrs (_k: v: v != null) vars));
# Formats a list of items for shell array content with intelligent width optimization.
# IMPORTANT: This formats the CONTENTS of an array (what goes inside parentheses),

View File

@@ -38,5 +38,7 @@ rec {
let
separator = if indent == "" then "\n" else "\n" + indent;
in
lib.concatStringsSep separator (lib.mapAttrsToList export vars);
lib.concatStringsSep separator (
lib.mapAttrsToList export (lib.filterAttrs (_k: v: v != null) vars)
);
}

View File

@@ -135,17 +135,22 @@ in
default = { };
type =
with types;
lazyAttrsOf (oneOf [
str
path
int
float
]);
lazyAttrsOf (
nullOr (oneOf [
str
path
int
float
])
);
example = {
MAILCHECK = 30;
};
description = ''
Environment variables that will be set for the Bash session.
Setting a value to `null` will skip setting the variable at all, which
may be useful when overriding.
'';
};

View File

@@ -249,16 +249,23 @@ in
default = { };
type =
with types;
lazyAttrsOf (oneOf [
str
path
int
float
]);
lazyAttrsOf (
nullOr (oneOf [
str
path
int
float
])
);
example = {
MAILCHECK = 30;
};
description = "Environment variables that will be set for zsh session.";
description = ''
Environment variables that will be set for zsh session.
Setting a value to `null` will skip setting the variable at all, which
may be useful when overriding.
'';
};
initContent = mkOption {

View File

@@ -9,6 +9,7 @@ let
if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi
export __HM_SESS_VARS_SOURCED=1
export IS_EMPTY=""
export LOCALE_ARCHIVE_2_27="${config.i18n.glibcLocales}/lib/locale/locale-archive"
export V1="v1"
export V2="v2-v1"
@@ -25,6 +26,7 @@ let
if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi
export __HM_SESS_VARS_SOURCED=1
export IS_EMPTY=""
export TERMINFO_DIRS="/home/hm-user/.nix-profile/share/terminfo:$TERMINFO_DIRS''${TERMINFO_DIRS:+:}/usr/share/terminfo"
export V1="v1"
export V2="v2-v1"
@@ -45,6 +47,8 @@ in
home.sessionVariables = {
V1 = "v1";
V2 = "v2-${config.home.sessionVariables.V1}";
IS_EMPTY = "";
IS_NULL = null;
};
nmt.script = ''

View File

@@ -8,6 +8,8 @@
sessionVariables = {
V1 = "v1";
V2 = "v2-${config.programs.bash.sessionVariables.V1}";
IS_EMPTY = "";
IS_NULL = null;
};
};
@@ -18,6 +20,7 @@
${builtins.toFile "session-variables-expected" ''
. "/nix/store/00000000000000000000000000000000-hm-session-vars.sh/etc/profile.d/hm-session-vars.sh"
export IS_EMPTY=""
export V1="v1"
export V2="v2-v1"

View File

@@ -8,6 +8,8 @@
PATH = "$HOME/bin:$PATH";
V1 = "v1";
V2 = "v2-${config.programs.zsh.sessionVariables.V1}";
IS_EMPTY = "";
IS_NULL = null;
};
};

View File

@@ -4,6 +4,7 @@
# Only source this once
if [[ -z "$__HM_ZSH_SESS_VARS_SOURCED" ]]; then
export __HM_ZSH_SESS_VARS_SOURCED=1
export IS_EMPTY=""
export PATH="$HOME/bin:$PATH"
export V1="v1"
export V2="v2-v1"