forked from mirrors/nixpkgs
lib/strings/toShellVars: handle derivations as strings
This commit is contained in:
parent
7dbfd6b997
commit
4d2ea62d82
|
@ -339,9 +339,10 @@ rec {
|
||||||
|
|
||||||
/* Translate a Nix value into a shell variable declaration, with proper escaping.
|
/* Translate a Nix value into a shell variable declaration, with proper escaping.
|
||||||
|
|
||||||
Supported value types are strings (mapped to regular variables), lists of strings
|
The value can be a string (mapped to a regular variable), a list of strings
|
||||||
(mapped to Bash-style arrays) and attribute sets of strings (mapped to Bash-style
|
(mapped to a Bash-style array) or an attribute set of strings (mapped to a
|
||||||
associative arrays). Note that "strings" include string-coercible values like paths.
|
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
|
Strings are translated into POSIX sh-compatible code; lists and attribute sets
|
||||||
assume a shell that understands Bash syntax (e.g. Bash or ZSH).
|
assume a shell that understands Bash syntax (e.g. Bash or ZSH).
|
||||||
|
@ -356,7 +357,7 @@ rec {
|
||||||
*/
|
*/
|
||||||
toShellVar = name: value:
|
toShellVar = name: value:
|
||||||
lib.throwIfNot (isValidPosixName name) "toShellVar: ${name} is not a valid shell variable name" (
|
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}=(${
|
"declare -A ${name}=(${
|
||||||
concatStringsSep " " (lib.mapAttrsToList (n: v:
|
concatStringsSep " " (lib.mapAttrsToList (n: v:
|
||||||
"[${escapeShellArg n}]=${escapeShellArg v}"
|
"[${escapeShellArg n}]=${escapeShellArg v}"
|
||||||
|
|
|
@ -269,6 +269,15 @@ runTests {
|
||||||
strings
|
strings
|
||||||
possibly newlines
|
possibly newlines
|
||||||
'';
|
'';
|
||||||
|
drv = {
|
||||||
|
outPath = "/drv";
|
||||||
|
foo = "ignored attribute";
|
||||||
|
};
|
||||||
|
path = /path;
|
||||||
|
stringable = {
|
||||||
|
__toString = _: "hello toString";
|
||||||
|
bar = "ignored attribute";
|
||||||
|
};
|
||||||
}}
|
}}
|
||||||
'';
|
'';
|
||||||
expected = ''
|
expected = ''
|
||||||
|
@ -277,6 +286,9 @@ runTests {
|
||||||
declare -A assoc=(['with some']='strings
|
declare -A assoc=(['with some']='strings
|
||||||
possibly newlines
|
possibly newlines
|
||||||
')
|
')
|
||||||
|
drv='/drv'
|
||||||
|
path='/path'
|
||||||
|
stringable='hello toString'
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue