about summary refs log tree commit diff
path: root/lib/derivations.nix
diff options
context:
space:
mode:
authorShea Levy <shea@shealevy.com>2024-01-30 17:29:47 -0500
committerShea Levy <shea@shealevy.com>2024-02-02 16:27:30 -0500
commitca1262a4832b290d9f35c4b9c5f67f93144c7f2a (patch)
tree4f6bea9f271666da958a0531adb7ede78df05d0b /lib/derivations.nix
parent5b5e6f990070ec0c5c343ff9160554866ecd23c2 (diff)
lib: Add optionalDrvAttr to conditionally set drv attributes.
This allows for adding new, conditionally set, derivation attributes
to an existing derivation without changing any output paths in the
case where the condition is not met.
Diffstat (limited to 'lib/derivations.nix')
-rw-r--r--lib/derivations.nix26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/derivations.nix b/lib/derivations.nix
index 5b7ed1868e866..44b727ee31cc2 100644
--- a/lib/derivations.nix
+++ b/lib/derivations.nix
@@ -98,4 +98,30 @@ in
       # `lazyDerivation` caller knew a shortcut, be taken from there.
       meta = args.meta or checked.meta;
     } // passthru;
+
+  /* Conditionally set a derivation attribute.
+
+     Because `mkDerivation` sets `__ignoreNulls = true`, a derivation
+     attribute set to `null` will not impact the derivation output hash.
+     Thus, this function passes through its `value` argument if the `cond`
+     is `true`, but returns `null` if not.
+
+     Type: optionalDrvAttr :: Bool -> a -> a | Null
+
+     Example:
+       (stdenv.mkDerivation {
+         name = "foo";
+         x = optionalDrvAttr true 1;
+         y = optionalDrvAttr false 1;
+       }).drvPath == (stdenv.mkDerivation {
+         name = "foo";
+         x = 1;
+       }).drvPath
+       => true
+  */
+  optionalDrvAttr =
+    # Condition
+    cond:
+    # Attribute value
+    value: if cond then value else null;
 }