2021-10-27 09:05:52 +00:00
|
|
|
{ lib }:
|
|
|
|
let
|
|
|
|
inherit (lib)
|
|
|
|
mkOption
|
2022-05-25 16:09:17 +00:00
|
|
|
mkOptionType
|
|
|
|
defaultFunctor
|
|
|
|
isAttrs
|
|
|
|
isFunction
|
|
|
|
showOption
|
2021-10-27 09:05:52 +00:00
|
|
|
types
|
|
|
|
;
|
2022-05-25 16:09:17 +00:00
|
|
|
inherit (lib.types)
|
|
|
|
path
|
|
|
|
submoduleWith
|
|
|
|
;
|
2021-10-27 09:05:52 +00:00
|
|
|
|
2022-05-17 21:20:53 +00:00
|
|
|
# Polyfill functionTo to make sure it has type merging.
|
|
|
|
# Remove 2022-12
|
|
|
|
functionTo =
|
|
|
|
let sample = types.functionTo lib.types.str;
|
|
|
|
in
|
|
|
|
if sample.functor.wrapped._type or null == "option-type"
|
|
|
|
then types.functionTo
|
|
|
|
else
|
|
|
|
elemType: lib.mkOptionType {
|
|
|
|
name = "functionTo";
|
|
|
|
description = "function that evaluates to a(n) ${elemType.description}";
|
|
|
|
check = lib.isFunction;
|
|
|
|
merge = loc: defs:
|
|
|
|
fnArgs: (lib.mergeDefinitions (loc ++ [ "[function body]" ]) elemType (map (fn: { inherit (fn) file; value = fn.value fnArgs; }) defs)).mergedValue;
|
|
|
|
getSubOptions = prefix: elemType.getSubOptions (prefix ++ [ "[function body]" ]);
|
|
|
|
getSubModules = elemType.getSubModules;
|
|
|
|
substSubModules = m: functionTo (elemType.substSubModules m);
|
|
|
|
functor = (lib.defaultFunctor "functionTo") // { type = functionTo; wrapped = elemType; };
|
|
|
|
nestedTypes.elemType = elemType;
|
|
|
|
};
|
|
|
|
|
2022-05-25 16:09:17 +00:00
|
|
|
# Polyfill https://github.com/NixOS/nixpkgs/pull/163617
|
|
|
|
deferredModuleWith = lib.deferredModuleWith or (
|
|
|
|
attrs@{ staticModules ? [ ] }: mkOptionType {
|
|
|
|
name = "deferredModule";
|
|
|
|
description = "module";
|
|
|
|
check = x: isAttrs x || isFunction x || path.check x;
|
|
|
|
merge = loc: defs: staticModules ++ map (def: lib.setDefaultModuleLocation "${def.file}, via option ${showOption loc}" def.value) defs;
|
|
|
|
inherit (submoduleWith { modules = staticModules; })
|
|
|
|
getSubOptions
|
|
|
|
getSubModules;
|
|
|
|
substSubModules = m: deferredModuleWith (attrs // {
|
|
|
|
staticModules = m;
|
|
|
|
});
|
|
|
|
functor = defaultFunctor "deferredModuleWith" // {
|
|
|
|
type = deferredModuleWith;
|
|
|
|
payload = {
|
|
|
|
inherit staticModules;
|
|
|
|
};
|
|
|
|
binOp = lhs: rhs: {
|
|
|
|
staticModules = lhs.staticModules ++ rhs.staticModules;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2022-05-25 14:36:33 +00:00
|
|
|
flake-parts-lib = {
|
2021-10-27 09:05:52 +00:00
|
|
|
evalFlakeModule =
|
|
|
|
{ self
|
|
|
|
, specialArgs ? { }
|
|
|
|
}:
|
|
|
|
module:
|
|
|
|
|
|
|
|
lib.evalModules {
|
2022-05-25 14:36:33 +00:00
|
|
|
specialArgs = { inherit self flake-parts-lib; inherit (self) inputs; } // specialArgs;
|
2021-10-27 09:05:52 +00:00
|
|
|
modules = [ ./all-modules.nix module ];
|
|
|
|
};
|
2021-11-21 14:47:11 +00:00
|
|
|
|
2022-05-18 11:59:21 +00:00
|
|
|
mkFlake = args: module:
|
2022-05-25 14:36:33 +00:00
|
|
|
(flake-parts-lib.evalFlakeModule args module).config.flake;
|
2022-05-18 11:59:21 +00:00
|
|
|
|
2021-11-21 14:47:11 +00:00
|
|
|
# For extending options in an already declared submodule.
|
|
|
|
# Workaround for https://github.com/NixOS/nixpkgs/issues/146882
|
|
|
|
mkSubmoduleOptions =
|
|
|
|
options:
|
|
|
|
mkOption {
|
|
|
|
type = types.submoduleWith {
|
2022-05-17 08:28:03 +00:00
|
|
|
modules = [{ inherit options; }];
|
2021-11-21 14:47:11 +00:00
|
|
|
};
|
|
|
|
};
|
2022-05-17 08:28:03 +00:00
|
|
|
|
2022-05-13 08:14:10 +00:00
|
|
|
mkPerSystemType =
|
|
|
|
module:
|
2022-05-25 16:09:17 +00:00
|
|
|
deferredModuleWith {
|
|
|
|
staticModules = [ module ];
|
|
|
|
};
|
2022-05-13 08:14:10 +00:00
|
|
|
|
2022-05-13 08:15:21 +00:00
|
|
|
mkPerSystemOption =
|
2022-05-13 08:14:10 +00:00
|
|
|
module:
|
|
|
|
mkOption {
|
2022-05-25 14:36:33 +00:00
|
|
|
type = flake-parts-lib.mkPerSystemType module;
|
2022-05-13 08:14:10 +00:00
|
|
|
};
|
|
|
|
|
2022-10-30 10:52:28 +00:00
|
|
|
# Helper function for defining a per-system option that
|
|
|
|
# gets transposed by the usual flake system logic to a
|
|
|
|
# top-level flake attribute.
|
|
|
|
mkTransposedPerSystemModule = { name, option, file }: {
|
|
|
|
_file = file;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
flake = flake-parts-lib.mkSubmoduleOptions {
|
|
|
|
${name} = mkOption {
|
|
|
|
type = types.lazyAttrsOf option.type;
|
|
|
|
default = { };
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
See {option}`perSystem.${name}` for description and examples.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
perSystem = flake-parts-lib.mkPerSystemOption {
|
|
|
|
_file = file;
|
|
|
|
|
|
|
|
options.${name} = option;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
transposition.${name} = { };
|
|
|
|
};
|
|
|
|
};
|
2021-10-27 09:05:52 +00:00
|
|
|
};
|
2021-11-21 14:47:11 +00:00
|
|
|
|
2021-10-27 09:05:52 +00:00
|
|
|
in
|
2022-05-25 14:36:33 +00:00
|
|
|
flake-parts-lib
|