devShellTools.{stringValue -> valueToString}

This commit is contained in:
Robert Hensing 2024-06-29 17:22:57 +02:00
parent 469039098b
commit 8398e087cd
4 changed files with 22 additions and 22 deletions

View File

@ -11,19 +11,19 @@ However, `nix-shell` is not the only way to create such environments, and even `
This library provides a set of functions that help create such environments.
## `devShellTools.stringValue` {#sec-devShellTools-stringValue}
## `devShellTools.valueToString` {#sec-devShellTools-valueToString}
Converts Nix values to strings in the way the [`derivation` built-in function](https://nix.dev/manual/nix/2.23/language/derivations) does.
:::{.example}
## `stringValue` usage examples
## `valueToString` usage examples
```nix
devShellTools.stringValue (builtins.toFile "foo" "bar")
devShellTools.valueToString (builtins.toFile "foo" "bar")
=> "/nix/store/...-foo"
```
```nix
devShellTools.stringValue false
devShellTools.valueToString false
=> ""
```

View File

@ -6,11 +6,11 @@ rec {
# This function closely mirrors what this Nix code does:
# https://github.com/NixOS/nix/blob/2.8.0/src/libexpr/primops.cc#L1102
# https://github.com/NixOS/nix/blob/2.8.0/src/libexpr/eval.cc#L1981-L2036
stringValue = value:
valueToString = value:
# We can't just use `toString` on all derivation attributes because that
# would not put path literals in the closure. So we explicitly copy
# those into the store here
if typeOf value == "path" then "${value}"
else if typeOf value == "list" then toString (map stringValue value)
else if typeOf value == "list" then toString (map valueToString value)
else toString value;
}

View File

@ -9,12 +9,12 @@ let
inherit (lib) escapeShellArg;
in
{
# nix-build -A tests.devShellTools.stringValue
stringValue =
let inherit (devShellTools) stringValue; in
# nix-build -A tests.devShellTools.valueToString
valueToString =
let inherit (devShellTools) valueToString; in
stdenv.mkDerivation {
name = "devShellTools-stringValue-built-tests";
name = "devShellTools-valueToString-built-tests";
# Test inputs
inherit emptyFile hello;
@ -30,15 +30,15 @@ in
buildCommand = ''
touch $out
( set -x
[[ "$one" = ${escapeShellArg (stringValue 1)} ]]
[[ "$boolTrue" = ${escapeShellArg (stringValue true)} ]]
[[ "$boolFalse" = ${escapeShellArg (stringValue false)} ]]
[[ "$foo" = ${escapeShellArg (stringValue "foo")} ]]
[[ "$hello" = ${escapeShellArg (stringValue hello)} ]]
[[ "$list" = ${escapeShellArg (stringValue [ 1 2 3 ])} ]]
[[ "$packages" = ${escapeShellArg (stringValue [ hello emptyFile ])} ]]
[[ "$pathDefaultNix" = ${escapeShellArg (stringValue ./default.nix)} ]]
[[ "$emptyFile" = ${escapeShellArg (stringValue emptyFile)} ]]
[[ "$one" = ${escapeShellArg (valueToString 1)} ]]
[[ "$boolTrue" = ${escapeShellArg (valueToString true)} ]]
[[ "$boolFalse" = ${escapeShellArg (valueToString false)} ]]
[[ "$foo" = ${escapeShellArg (valueToString "foo")} ]]
[[ "$hello" = ${escapeShellArg (valueToString hello)} ]]
[[ "$list" = ${escapeShellArg (valueToString [ 1 2 3 ])} ]]
[[ "$packages" = ${escapeShellArg (valueToString [ hello emptyFile ])} ]]
[[ "$pathDefaultNix" = ${escapeShellArg (valueToString ./default.nix)} ]]
[[ "$emptyFile" = ${escapeShellArg (valueToString emptyFile)} ]]
) >log 2>&1 || { cat log; exit 1; }
'';
};

View File

@ -51,7 +51,7 @@ let
;
inherit (devShellTools)
stringValue
valueToString
;
mkDbExtraCommand = contents:
@ -1146,7 +1146,7 @@ rec {
# A binary that calls the command to build the derivation
builder = writeShellScriptBin "buildDerivation" ''
exec ${lib.escapeShellArg (stringValue drv.drvAttrs.builder)} ${lib.escapeShellArgs (map stringValue drv.drvAttrs.args)}
exec ${lib.escapeShellArg (valueToString drv.drvAttrs.builder)} ${lib.escapeShellArgs (map valueToString drv.drvAttrs.args)}
'';
staticPath = "${dirOf shell}:${lib.makeBinPath [ builder ]}";
@ -1180,7 +1180,7 @@ rec {
# https://github.com/NixOS/nix/blob/2.8.0/src/libstore/build/local-derivation-goal.cc#L992-L1004
drvEnv = lib.mapAttrs' (name: value:
let str = stringValue value;
let str = valueToString value;
in if lib.elem name (drv.drvAttrs.passAsFile or [])
then lib.nameValuePair "${name}Path" (writeText "pass-as-text-${name}" str)
else lib.nameValuePair name str