nixpkgs.lib.makeOverridable
(AttrSets -> a) -> AttrSets -> Overridable a
Overridable result = { override = AttrSets -> Overridable; overrideDerivation = AttrSets -> (Derivation -> Derivation) -> Derivation; result = result; }
関数にデフォルト引数を与えておいて、後から引数をオーバーライド可能な属性セットに変換する関数。デフォルト引数を与えた場合の結果はresulrキーに格納されており、overrideキーに属性セットを渡すことでオーバーライドできる。
code: customusation.nix
/* makeOverridable takes a function from attribute set to attribute set and
injects override attribute which can be used to override arguments of
the function.
nix-repl> x = {a, b}: { result = a + b; }
nix-repl> y = lib.makeOverridable x { a = 1; b = 2; }
nix-repl> y
{ override = «lambda»; overrideDerivation = «lambda»; result = 3; }
nix-repl> y.override { a = 10; }
{ override = «lambda»; overrideDerivation = «lambda»; result = 12; }
Please refer to "Nixpkgs Contributors Guide" section
"<pkg>.overrideDerivation" to learn about overrideDerivation and caveats
related to its use.
*/
makeOverridable = f: origArgs:
let
result = f origArgs;
# Creates a functor with the same arguments as f
copyArgs = g: lib.setFunctionArgs g (lib.functionArgs f);
# Changes the original arguments with (potentially a function that returns) a set of new attributes
overrideWith = newArgs: origArgs // (if lib.isFunction newArgs then newArgs origArgs else newArgs);
# Re-call the function but with different arguments
overrideArgs = copyArgs (newArgs: makeOverridable f (overrideWith newArgs));
# Change the result of the function call by applying g to it
overrideResult = g: makeOverridable (copyArgs (args: g (f args))) origArgs;
in
if builtins.isAttrs result then
result // {
override = overrideArgs;
overrideDerivation = fdrv: overrideResult (x: overrideDerivation x fdrv);
${if result ? overrideAttrs then "overrideAttrs" else null} = fdrv:
overrideResult (x: x.overrideAttrs fdrv);
}
else if lib.isFunction result then
# Transform the result into a functor while propagating its arguments
lib.setFunctionArgs result (lib.functionArgs result) // {
override = overrideArgs;
}
else result;