From 69f37ce6dca756d9e95be8f3194c790ebbdafc7a Mon Sep 17 00:00:00 2001 From: Chuang Zhu Date: Thu, 7 Nov 2024 17:53:02 +0800 Subject: [PATCH] nixos/environment: make {sessionV,v}ariables items nullable --- nixos/modules/config/shells-environment.nix | 8 ++++++-- nixos/modules/config/system-environment.nix | 3 +++ nixos/tests/env.nix | 12 ++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/nixos/modules/config/shells-environment.nix b/nixos/modules/config/shells-environment.nix index 9289eced109f..7897e20d53fb 100644 --- a/nixos/modules/config/shells-environment.nix +++ b/nixos/modules/config/shells-environment.nix @@ -34,14 +34,18 @@ in description = '' A set of environment variables used in the global environment. These variables will be set on shell initialisation (e.g. in /etc/profile). + The value of each variable can be either a string or a list of strings. The latter is concatenated, interspersed with colon characters. + + Setting a variable to `null` does nothing. You can override a + variable set by another module to `null` to unset it. ''; - type = with lib.types; attrsOf (oneOf [ (listOf (oneOf [ int str path ])) int str path ]); + type = with lib.types; attrsOf (nullOr (oneOf [ (listOf (oneOf [ int str path ])) int str path ])); apply = let toStr = v: if lib.isPath v then "${v}" else toString v; - in lib.mapAttrs (n: v: if lib.isList v then lib.concatMapStringsSep ":" toStr v else toStr v); + in attrs: lib.mapAttrs (n: v: if lib.isList v then lib.concatMapStringsSep ":" toStr v else toStr v) (lib.filterAttrs (n: v: v != null) attrs); }; environment.profiles = lib.mkOption { diff --git a/nixos/modules/config/system-environment.nix b/nixos/modules/config/system-environment.nix index ef49701fb492..d5020d14f7d1 100644 --- a/nixos/modules/config/system-environment.nix +++ b/nixos/modules/config/system-environment.nix @@ -21,6 +21,9 @@ in list of strings. The latter is concatenated, interspersed with colon characters. + Setting a variable to `null` does nothing. You can override a + variable set by another module to `null` to unset it. + Note, due to limitations in the PAM format values may not contain the `"` character. diff --git a/nixos/tests/env.nix b/nixos/tests/env.nix index dec17b6b565a..cf526e5aa011 100644 --- a/nixos/tests/env.nix +++ b/nixos/tests/env.nix @@ -4,7 +4,7 @@ import ./make-test-python.nix ({ pkgs, ...} : { maintainers = [ nequissimus ]; }; - nodes.machine = { pkgs, ... }: + nodes.machine = { pkgs, lib, ... }: lib.mkMerge [ { boot.kernelPackages = pkgs.linuxPackages; environment.etc.plainFile.text = '' @@ -17,8 +17,15 @@ import ./make-test-python.nix ({ pkgs, ...} : { environment.sessionVariables = { TERMINFO_DIRS = "/run/current-system/sw/share/terminfo"; NIXCON = "awesome"; + SHOULD_NOT_BE_SET = "oops"; }; - }; + } + { + environment.sessionVariables = { + SHOULD_NOT_BE_SET = lib.mkForce null; + }; + } + ]; testScript = '' machine.succeed('[ -L "/etc/plainFile" ]') @@ -32,5 +39,6 @@ import ./make-test-python.nix ({ pkgs, ...} : { "echo ''${TERMINFO_DIRS}" ) assert "awesome" in machine.succeed("echo ''${NIXCON}") + machine.fail("printenv SHOULD_NOT_BE_SET") ''; })