about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorSomeone Serge2024-07-14 13:54:08 +0000
committerSomeone Serge2024-07-21 11:29:23 +0000
commit744305bce4cf59d9bec124b6170db925bf71ac63 (patch)
tree777f924e5c67e3182468e123dad1315050b3d8fb /lib
parentf7a2522fcb47c553c5dd35c12dbbd732c8ca37a0 (diff)
lib: add getOutput', a nix-lang counterpart of _overrideFirst
Diffstat (limited to 'lib')
-rw-r--r--lib/attrsets.nix73
-rw-r--r--lib/default.nix4
2 files changed, 74 insertions, 3 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix
index c0ac6eeb41bc..ea802e551e0a 100644
--- a/lib/attrsets.nix
+++ b/lib/attrsets.nix
@@ -1799,6 +1799,48 @@ rec {
       else pkg;
 
   /**
+    Like `getOutput` but with a list of fallback output names.
+    This function is alligned with `_overrideFirst()` from the `multiple-outputs.sh` setup hook.
+
+    # Inputs
+
+    `outputs`
+
+    : 1\. Function argument
+
+    `pkg`
+
+    : 2\. Function argument
+
+    # Type
+
+    ```
+    getFirstOutput :: [String] -> Derivation -> String
+    ```
+
+    # Examples
+    :::{.example}
+    ## `lib.attrsets.getFirstOutput` usage example
+
+    ```nix
+    getFirstOutput [ "include" "dev" ] pkgs.openssl
+    => "/nix/store/00000000000000000000000000000000-openssl-1.0.1r-dev"
+    ```
+
+    :::
+  */
+  getFirstOutput =
+    candidates: pkg:
+    let
+      outputs = builtins.filter (name: hasAttr name pkg) candidates;
+      output = builtins.head outputs;
+    in
+    if pkg.outputSpecified or false || outputs == [ ] then
+      pkg
+    else
+      pkg.${output};
+
+  /**
     Get a package's `bin` output.
     If the output does not exist, fallback to `.out` and then to the default.
 
@@ -1820,7 +1862,7 @@ rec {
 
     ```nix
     getBin pkgs.openssl
-    => "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r"
+    => "/nix/store/00000000000000000000000000000000-openssl-1.0.1r"
     ```
 
     :::
@@ -1887,6 +1929,35 @@ rec {
   */
   getDev = getOutput "dev";
 
+  /**
+    Get a package's `include` output.
+    If the output does not exist, fallback to `.dev`, then to `.out`, and then to the default.
+
+    # Inputs
+
+    `pkg`
+
+    : The package whose `include` output will be retrieved.
+
+    # Type
+
+    ```
+    getInclude :: Derivation -> String
+    ```
+
+    # Examples
+    :::{.example}
+    ## `lib.attrsets.getInclude` usage example
+
+    ```nix
+    getInclude pkgs.openssl
+    => "/nix/store/00000000000000000000000000000000-openssl-1.0.1r-dev"
+    ```
+
+    :::
+  */
+  getInclude = getFirstOutput [ "include" "dev" "out" ];
+
 
   /**
     Get a package's `man` output.
diff --git a/lib/default.nix b/lib/default.nix
index 2605da47679e..adc7caa6bb9e 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -86,8 +86,8 @@ let
       mapAttrs' mapAttrsToList attrsToList concatMapAttrs mapAttrsRecursive
       mapAttrsRecursiveCond genAttrs isDerivation toDerivation optionalAttrs
       zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil
-      recursiveUpdate matchAttrs mergeAttrsList overrideExisting showAttrPath getOutput
-      getBin getLib getDev getMan chooseDevOutputs zipWithNames zip
+      recursiveUpdate matchAttrs mergeAttrsList overrideExisting showAttrPath getOutput getFirstOutput
+      getBin getLib getDev getInclude getMan chooseDevOutputs zipWithNames zip
       recurseIntoAttrs dontRecurseIntoAttrs cartesianProduct cartesianProductOfSets
       mapCartesianProduct updateManyAttrsByPath listToAttrs hasAttr getAttr isAttrs intersectAttrs removeAttrs;
     inherit (self.lists) singleton forEach map foldr fold foldl foldl' imap0 imap1