mirror of
https://github.com/CHN-beta/nixpkgs.git
synced 2026-01-12 02:40:31 +08:00
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.
This commit is contained in:
@@ -116,7 +116,7 @@ let
|
||||
inherit (self.customisation) overrideDerivation makeOverridable
|
||||
callPackageWith callPackagesWith extendDerivation hydraJob
|
||||
makeScope makeScopeWithSplicing makeScopeWithSplicing';
|
||||
inherit (self.derivations) lazyDerivation;
|
||||
inherit (self.derivations) lazyDerivation optionalDrvAttr;
|
||||
inherit (self.meta) addMetaAttrs dontDistribute setName updateName
|
||||
appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio
|
||||
hiPrioSet getLicenseFromSpdxId getExe getExe';
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1902,7 +1902,7 @@ runTests {
|
||||
expected = true;
|
||||
};
|
||||
|
||||
# lazyDerivation
|
||||
# DERIVATIONS
|
||||
|
||||
testLazyDerivationIsLazyInDerivationForAttrNames = {
|
||||
expr = attrNames (lazyDerivation {
|
||||
@@ -1955,6 +1955,24 @@ runTests {
|
||||
expected = derivation;
|
||||
};
|
||||
|
||||
testOptionalDrvAttr = let
|
||||
mkDerivation = args: derivation (args // {
|
||||
builder = "builder";
|
||||
system = "system";
|
||||
__ignoreNulls = true;
|
||||
});
|
||||
in {
|
||||
expr = (mkDerivation {
|
||||
name = "foo";
|
||||
x = optionalDrvAttr true 1;
|
||||
y = optionalDrvAttr false 1;
|
||||
}).drvPath;
|
||||
expected = (mkDerivation {
|
||||
name = "foo";
|
||||
x = 1;
|
||||
}).drvPath;
|
||||
};
|
||||
|
||||
testTypeDescriptionInt = {
|
||||
expr = (with types; int).description;
|
||||
expected = "signed integer";
|
||||
|
||||
Reference in New Issue
Block a user