about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorFlorian Richer <florian.richer.97@outlook.com>2024-04-02 19:01:05 +0200
committerGitHub <noreply@github.com>2024-04-02 19:01:05 +0200
commit3b883d3cdf8942c56e5f232973b50fc1c9e941aa (patch)
treea25bda45439586ebdbb6332f6aa742cd88a6c19a /lib
parent9ce47d84cddede3ee9330f63f458e60e267f939c (diff)
downloadnixlib-3b883d3cdf8942c56e5f232973b50fc1c9e941aa.tar
nixlib-3b883d3cdf8942c56e5f232973b50fc1c9e941aa.tar.gz
nixlib-3b883d3cdf8942c56e5f232973b50fc1c9e941aa.tar.bz2
nixlib-3b883d3cdf8942c56e5f232973b50fc1c9e941aa.tar.lz
nixlib-3b883d3cdf8942c56e5f232973b50fc1c9e941aa.tar.xz
nixlib-3b883d3cdf8942c56e5f232973b50fc1c9e941aa.tar.zst
nixlib-3b883d3cdf8942c56e5f232973b50fc1c9e941aa.zip
lib/strings: Add makeIncludePath (#296237)
* Update strings.nix

* Fix typo in docs

* Update lib/strings.nix

Co-authored-by: lolbinarycat <dogedoge61+github@gmail.com>

* Update lib/strings.nix

Co-authored-by: lolbinarycat <dogedoge61+github@gmail.com>

* Add unit test with strings

* Move test to strings

* Add unit test with package structure

* testMakeIncludePathWithPkgs: use real pkgs

* Revert "testMakeIncludePathWithPkgs: use real pkgs"

This reverts commit fb1850c069cfb324f3a43323da740a27a11793f3.

* Update lib/tests/misc.nix

Co-authored-by: lolbinarycat <dogedoge61+github@gmail.com>

* Update lib/tests/misc.nix

Co-authored-by: Silvan Mosberger <github@infinisil.com>

---------

Co-authored-by: lolbinarycat <dogedoge61+github@gmail.com>
Co-authored-by: Silvan Mosberger <github@infinisil.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix2
-rw-r--r--lib/strings.nix12
-rw-r--r--lib/tests/misc.nix30
3 files changed, 43 insertions, 1 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 668c29640f9f..f6cb7932507a 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -97,7 +97,7 @@ let
     inherit (self.strings) concatStrings concatMapStrings concatImapStrings
       intersperse concatStringsSep concatMapStringsSep
       concatImapStringsSep concatLines makeSearchPath makeSearchPathOutput
-      makeLibraryPath makeBinPath optionalString
+      makeLibraryPath makeIncludePath makeBinPath optionalString
       hasInfix hasPrefix hasSuffix stringToCharacters stringAsChars escape
       escapeShellArg escapeShellArgs
       isStorePath isStringLike
diff --git a/lib/strings.nix b/lib/strings.nix
index 32efc9bdb70e..67bb669d04e0 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -206,6 +206,18 @@ rec {
   */
   makeLibraryPath = makeSearchPathOutput "lib" "lib";
 
+  /* Construct an include search path (such as C_INCLUDE_PATH) containing the
+     header files for a set of packages or paths.
+
+     Example:
+       makeIncludePath [ "/usr" "/usr/local" ]
+       => "/usr/include:/usr/local/include"
+       pkgs = import <nixpkgs> { }
+       makeIncludePath [ pkgs.openssl pkgs.zlib ]
+       => "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r-dev/include:/nix/store/wwh7mhwh269sfjkm6k5665b5kgp7jrk2-zlib-1.2.8-dev/include"
+  */
+  makeIncludePath = makeSearchPathOutput "dev" "include";
+
   /* Construct a binary search path (such as $PATH) containing the
      binaries for a set of packages.
 
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 6f1d9039db80..3cb96c1b68bc 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -64,6 +64,7 @@ let
     lists
     listToAttrs
     makeExtensible
+    makeIncludePath
     makeOverridable
     mapAttrs
     matchAttrs
@@ -296,6 +297,35 @@ runTests {
     expected = "a\nb\nc\n";
   };
 
+  testMakeIncludePathWithPkgs = {
+    expr = (makeIncludePath [
+      # makeIncludePath preferably selects the "dev" output
+      { dev.outPath = "/dev"; out.outPath = "/out"; outPath = "/default"; }
+      # "out" is used if "dev" is not found
+      { out.outPath = "/out"; outPath = "/default"; }
+      # And it returns the derivation directly if there's no "out" either
+      { outPath = "/default"; }
+      # Same if the output is specified explicitly, even if there's a "dev"
+      { dev.outPath = "/dev"; outPath = "/default"; outputSpecified = true; }
+    ]);
+    expected = "/dev/include:/out/include:/default/include:/default/include";
+  };
+
+  testMakeIncludePathWithEmptyList = {
+    expr = (makeIncludePath [ ]);
+    expected = "";
+  };
+
+  testMakeIncludePathWithOneString = {
+    expr = (makeIncludePath [ "/usr" ]);
+    expected = "/usr/include";
+  };
+
+  testMakeIncludePathWithManyString = {
+    expr = (makeIncludePath [ "/usr" "/usr/local" ]);
+    expected = "/usr/include:/usr/local/include";
+  };
+
   testReplicateString = {
     expr = strings.replicate 5 "hello";
     expected = "hellohellohellohellohello";