about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorRobert Hensing <roberth@users.noreply.github.com>2022-05-12 14:43:48 +0200
committerGitHub <noreply@github.com>2022-05-12 14:43:48 +0200
commitf771d397500b5138329bd206af2634086d51d108 (patch)
tree5220d7c2d213739cef7f2d62307ce2190e3d182c /lib
parentad874c4237765b1a61d3b1b766b03f1d878ffe21 (diff)
parent4d2ea62d8211be13575d62c6d1aa3858bf5c90e1 (diff)
Merge pull request #171946 from ncfavier/toShellVars-derivations
lib/strings/toShellVars: handle derivations as strings
Diffstat (limited to 'lib')
-rw-r--r--lib/strings.nix9
-rw-r--r--lib/tests/misc.nix12
2 files changed, 17 insertions, 4 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index 328f64cf1b61d..295d98900e994 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -339,9 +339,10 @@ rec {
 
   /* Translate a Nix value into a shell variable declaration, with proper escaping.
 
-     Supported value types are strings (mapped to regular variables), lists of strings
-     (mapped to Bash-style arrays) and attribute sets of strings (mapped to Bash-style
-     associative arrays). Note that "strings" include string-coercible values like paths.
+     The value can be a string (mapped to a regular variable), a list of strings
+     (mapped to a Bash-style array) or an attribute set of strings (mapped to a
+     Bash-style associative array). Note that "string" includes string-coercible
+     values like paths or derivations.
 
      Strings are translated into POSIX sh-compatible code; lists and attribute sets
      assume a shell that understands Bash syntax (e.g. Bash or ZSH).
@@ -356,7 +357,7 @@ rec {
   */
   toShellVar = name: value:
     lib.throwIfNot (isValidPosixName name) "toShellVar: ${name} is not a valid shell variable name" (
-    if isAttrs value then
+    if isAttrs value && ! isCoercibleToString value then
       "declare -A ${name}=(${
         concatStringsSep " " (lib.mapAttrsToList (n: v:
           "[${escapeShellArg n}]=${escapeShellArg v}"
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index faa7ee547f57a..c7010f41394a0 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -269,6 +269,15 @@ runTests {
           strings
           possibly newlines
         '';
+        drv = {
+          outPath = "/drv";
+          foo = "ignored attribute";
+        };
+        path = /path;
+        stringable = {
+          __toString = _: "hello toString";
+          bar = "ignored attribute";
+        };
       }}
     '';
     expected = ''
@@ -277,6 +286,9 @@ runTests {
       declare -A assoc=(['with some']='strings
       possibly newlines
       ')
+      drv='/drv'
+      path='/path'
+      stringable='hello toString'
     '';
   };