From f2c32f5a427e23a79acc5d7bbaa2b84f3e32a544 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Sun, 14 Jan 2024 19:05:39 -0800 Subject: [PATCH] Add optCallWith type for optional calling optFunctionTo results in a function which needs to be called when using the option value. This is needed when the argument is not known when building types (such as with pkgs). When the args are known (for example, moduleArgs), this leads to more complex code than just calling the function and resulting in the target type. optCallWith does the latter. --- builtinModules/homeConfigurations.nix | 8 +++----- builtinModules/nixosConfigurations.nix | 8 +++----- default.nix | 3 +++ 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/builtinModules/homeConfigurations.nix b/builtinModules/homeConfigurations.nix index f120916..4bf8f2c 100644 --- a/builtinModules/homeConfigurations.nix +++ b/builtinModules/homeConfigurations.nix @@ -8,7 +8,7 @@ let inherit (lib) foldl mapAttrsToList mkOption mkIf recursiveUpdate; inherit (lib.types) attrs lazyAttrsOf; inherit (flakelight) selectAttr; - inherit (flakelight.types) optFunctionTo; + inherit (flakelight.types) optCallWith; isHome = x: x ? activationPackage; @@ -29,14 +29,12 @@ let ); configs = mapAttrs - (name: f: - let val = f moduleArgs; in - if isHome val then val else mkHome name val) + (name: cfg: if isHome cfg then cfg else mkHome name cfg) config.homeConfigurations; in { options.homeConfigurations = mkOption { - type = lazyAttrsOf (optFunctionTo attrs); + type = lazyAttrsOf (optCallWith moduleArgs attrs); default = { }; }; diff --git a/builtinModules/nixosConfigurations.nix b/builtinModules/nixosConfigurations.nix index 30b4bb6..96746cf 100644 --- a/builtinModules/nixosConfigurations.nix +++ b/builtinModules/nixosConfigurations.nix @@ -8,7 +8,7 @@ let inherit (lib) foldl mapAttrsToList mkIf mkOption recursiveUpdate; inherit (lib.types) attrs lazyAttrsOf; inherit (flakelight) selectAttr; - inherit (flakelight.types) optFunctionTo; + inherit (flakelight.types) optCallWith; # Avoid checking if toplevel is a derivation as it causes the nixos modules # to be evaluated. @@ -23,14 +23,12 @@ let }); configs = mapAttrs - (hostname: f: - let val = f moduleArgs; in - if isNixos val then val else mkNixos hostname val) + (hostname: cfg: if isNixos cfg then cfg else mkNixos hostname cfg) config.nixosConfigurations; in { options.nixosConfigurations = mkOption { - type = lazyAttrsOf (optFunctionTo attrs); + type = lazyAttrsOf (optCallWith moduleArgs attrs); default = { }; }; diff --git a/default.nix b/default.nix index 7df750a..d2a7995 100644 --- a/default.nix +++ b/default.nix @@ -92,6 +92,9 @@ let optFunctionTo = elemType: coercedTo elemType (x: _: x) (functionTo elemType); + + optCallWith = args: elemType: coercedTo (functionTo elemType) (x: x args) + elemType; }; supportedSystem = { lib, stdenv, ... }: