nixos/local/lib/mkModules.nix

46 lines
1.9 KiB
Nix
Raw Normal View History

2023-06-24 22:00:28 +08:00
# Behaviors of these two NixOS modules would be different:
# { pkgs, ... }@inputs: { environment.systemPackages = [ pkgs.hello ]; }
# inputs: { environment.systemPackages = [ pkgs.hello ]; }
# The second one would failed to evaluate because nixpkgs would not pass pkgs to it.
# So that we wrote a wrapper to make it always works like the first one.
2023-06-24 23:00:02 +08:00
# Input a list of modules, allowed types are:
# * attribute set
# * file containing attribute set
# * file containing lambda, which takes inputs as argument
# * lambda, which takes inputs as argument
# * list, first member is a lambda,
2023-06-24 22:00:28 +08:00
moduleList: { pkgs, ... }@inputs:
{
imports = builtins.map
2023-06-24 23:00:02 +08:00
(
let
handle = { module, customArgs }:
if ( builtins.typeOf module ) == "list"
then handle { module = builtins.elemAt module 0; customArgs = builtins.elemAt module 1; }
else if ( builtins.typeOf module ) == "path"
then handle { module = import module; inherit customArgs; }
2023-07-08 15:56:39 +08:00
else if ( builtins.typeOf module ) == "lambda" && customArgs != null # deprecated
2023-06-24 23:00:02 +08:00
then handle { module = module customArgs; customArgs = null; }
2023-07-08 15:56:39 +08:00
else if ( builtins.typeOf module ) == "lambda" then module inputs # deprecated
2023-06-24 23:00:02 +08:00
else module;
caller = module: handle { inherit module; customArgs = null; };
in caller
) moduleList;
2023-06-24 22:00:28 +08:00
}
2023-07-08 16:26:12 +08:00
# Behaviors of these two NixOS modules would be different:
# { pkgs, ... }@inputs: { environment.systemPackages = [ pkgs.hello ]; }
# inputs: { environment.systemPackages = [ pkgs.hello ]; }
# The second one would failed to evaluate because nixpkgs would not pass pkgs to it.
# So that we wrote a wrapper to make it always works like the first one.
# moduleList: { pkgs, ... }@inputs:
# {
# imports = builtins.map
# (
# handle = module:
# if ( builtins.typeOf module ) == "path" then handle import module
# else if ( builtins.typeOf module ) == "lambda" then module inputs
# else module;
# ) moduleList;
# }