about summary refs log tree commit diff
path: root/pkgs/build-support
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2024-06-29 17:07:17 +0200
committerRobert Hensing <robert@roberthensing.nl>2024-06-29 17:21:01 +0200
commit469039098bfb7f3c9b1c6229139f22e4ef89d704 (patch)
tree9c85aab2110279b066d5cbf11a1afa1a4990e42a /pkgs/build-support
parent091d8370a215b384b6cdb5448e5c7110ada9d32b (diff)
devShellTools.stringValue: init
Diffstat (limited to 'pkgs/build-support')
-rw-r--r--pkgs/build-support/dev-shell-tools/default.nix18
-rw-r--r--pkgs/build-support/dev-shell-tools/tests/default.nix44
-rw-r--r--pkgs/build-support/docker/default.nix16
3 files changed, 62 insertions, 16 deletions
diff --git a/pkgs/build-support/dev-shell-tools/default.nix b/pkgs/build-support/dev-shell-tools/default.nix
index b61813bb4eb62..f0f180dc19054 100644
--- a/pkgs/build-support/dev-shell-tools/default.nix
+++ b/pkgs/build-support/dev-shell-tools/default.nix
@@ -1,4 +1,16 @@
-{ }:
-
-{
+{ lib }:
+let
+  inherit (builtins) typeOf;
+in
+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:
+    # 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 toString value;
 }
diff --git a/pkgs/build-support/dev-shell-tools/tests/default.nix b/pkgs/build-support/dev-shell-tools/tests/default.nix
index d8147ea851180..d4b0872dd6b89 100644
--- a/pkgs/build-support/dev-shell-tools/tests/default.nix
+++ b/pkgs/build-support/dev-shell-tools/tests/default.nix
@@ -1,5 +1,45 @@
-{ ... }:
-
 {
+  devShellTools,
+  emptyFile,
+  lib,
+  stdenv,
+  hello,
+}:
+let
+  inherit (lib) escapeShellArg;
+in
+{
+  # nix-build -A tests.devShellTools.stringValue
+  stringValue =
+    let inherit (devShellTools) stringValue; in
+
+    stdenv.mkDerivation {
+      name = "devShellTools-stringValue-built-tests";
+
+      # Test inputs
+      inherit emptyFile hello;
+      one = 1;
+      boolTrue = true;
+      boolFalse = false;
+      foo = "foo";
+      list = [ 1 2 3 ];
+      pathDefaultNix = ./default.nix;
+      packages = [ hello emptyFile ];
+      # TODO: nested lists
 
+      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)} ]]
+        ) >log 2>&1 || { cat log; exit 1; }
+      '';
+    };
 }
diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix
index 1d1989d27fbb2..033969a7fc88e 100644
--- a/pkgs/build-support/docker/default.nix
+++ b/pkgs/build-support/docker/default.nix
@@ -4,6 +4,7 @@
 , callPackage
 , closureInfo
 , coreutils
+, devShellTools
 , e2fsprogs
 , proot
 , fakeNss
@@ -49,6 +50,10 @@ let
     toList
     ;
 
+  inherit (devShellTools)
+    stringValue
+    ;
+
   mkDbExtraCommand = contents:
     let
       contentsList = if builtins.isList contents then contents else [ contents ];
@@ -1173,17 +1178,6 @@ rec {
         # https://github.com/NixOS/nix/blob/2.8.0/src/libstore/globals.hh#L464-L465
         sandboxBuildDir = "/build";
 
-        # 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:
-          # 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 builtins.typeOf value == "path" then "${value}"
-          else if builtins.typeOf value == "list" then toString (map stringValue value)
-          else toString value;
-
         # 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;