mirror of
https://github.com/hercules-ci/flake-parts.git
synced 2024-12-14 11:47:31 +00:00
Add option for defining flake-parts modules for downstream flakes.
This commit is contained in:
parent
98bec08c58
commit
f3c79bef3b
4 changed files with 80 additions and 1 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
# 2022-12-25
|
||||||
|
|
||||||
|
- Added a new `flake.flakeModules` option so a flake can expose a module
|
||||||
|
to be used in a downstream flake's flake-parts usage. `.flakeModule` is
|
||||||
|
now an alias for `.flakeModules.default`.
|
||||||
|
|
||||||
|
Option only available if `flake-parts.flakeModules.flakeModules` is imported.
|
||||||
|
|
||||||
# 2022-12-17
|
# 2022-12-17
|
||||||
|
|
||||||
|
@ -30,7 +37,7 @@
|
||||||
- The `nixpkgs` input has been renamed to `nixpkgs-lib` to signify that the
|
- The `nixpkgs` input has been renamed to `nixpkgs-lib` to signify that the
|
||||||
only dependency is on the `lib` attribute, which can be provided by either
|
only dependency is on the `lib` attribute, which can be provided by either
|
||||||
the `nixpkgs?dir=lib` subflake or by the `nixpkgs` flake itself.
|
the `nixpkgs?dir=lib` subflake or by the `nixpkgs` flake itself.
|
||||||
|
|
||||||
- The templates now use the default, _fixed_ `nixpkgs?dir=lib` dependency instead
|
- The templates now use the default, _fixed_ `nixpkgs?dir=lib` dependency instead
|
||||||
of a _following_ `nixpkgs` dependency.
|
of a _following_ `nixpkgs` dependency.
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
outputs = { self, nixpkgs-lib, ... }: {
|
outputs = { self, nixpkgs-lib, ... }: {
|
||||||
lib = import ./lib.nix { inherit (nixpkgs-lib) lib; };
|
lib = import ./lib.nix { inherit (nixpkgs-lib) lib; };
|
||||||
|
flakeModules.flakeModules = ./modules/flakeModules.nix;
|
||||||
templates = {
|
templates = {
|
||||||
default = {
|
default = {
|
||||||
path = ./template/default;
|
path = ./template/default;
|
||||||
|
|
26
lib.nix
26
lib.nix
|
@ -10,7 +10,13 @@ let
|
||||||
throwIf
|
throwIf
|
||||||
types
|
types
|
||||||
warnIf
|
warnIf
|
||||||
|
getAttrFromPath
|
||||||
|
setAttrByPath
|
||||||
|
attrByPath
|
||||||
|
optionalAttrs
|
||||||
;
|
;
|
||||||
|
inherit (lib.modules)
|
||||||
|
mkAliasAndWrapDefsWithPriority;
|
||||||
inherit (lib.types)
|
inherit (lib.types)
|
||||||
path
|
path
|
||||||
submoduleWith
|
submoduleWith
|
||||||
|
@ -165,6 +171,26 @@ let
|
||||||
transposition.${name} = { };
|
transposition.${name} = { };
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Needed pending https://github.com/NixOS/nixpkgs/pull/198450
|
||||||
|
mkAliasOptionModule = from: to: { config, options, ... }:
|
||||||
|
let
|
||||||
|
fromOpt = getAttrFromPath from options;
|
||||||
|
toOf = attrByPath to
|
||||||
|
(abort "Renaming error: option `${showOption to}' does not exist.");
|
||||||
|
toType = let opt = attrByPath to { } options; in opt.type or (types.submodule { });
|
||||||
|
in
|
||||||
|
{
|
||||||
|
options = setAttrByPath from (mkOption
|
||||||
|
{
|
||||||
|
visible = true;
|
||||||
|
description = lib.mdDoc "Alias of {option}`${showOption to}`.";
|
||||||
|
apply = x: (toOf config);
|
||||||
|
} // optionalAttrs (toType != null) {
|
||||||
|
type = toType;
|
||||||
|
});
|
||||||
|
config = (mkAliasAndWrapDefsWithPriority (setAttrByPath to) fromOpt);
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
in
|
in
|
||||||
|
|
45
modules/flakeModules.nix
Normal file
45
modules/flakeModules.nix
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
{ config, self, lib, flake-parts-lib, ... }:
|
||||||
|
let
|
||||||
|
inherit (lib)
|
||||||
|
filterAttrs
|
||||||
|
mapAttrs
|
||||||
|
mkOption
|
||||||
|
optionalAttrs
|
||||||
|
types
|
||||||
|
;
|
||||||
|
inherit (flake-parts-lib)
|
||||||
|
mkAliasOptionModule
|
||||||
|
;
|
||||||
|
|
||||||
|
flakeModulesOption = mkOption {
|
||||||
|
type = types.lazyAttrsOf types.deferredModule;
|
||||||
|
default = { };
|
||||||
|
apply = mapAttrs (k: v: {
|
||||||
|
_file = "${toString self.outPath}/flake.nix#flakeModules.${k}";
|
||||||
|
imports = [ v ];
|
||||||
|
});
|
||||||
|
description = ''
|
||||||
|
flake-parts modules for use by other flakes.
|
||||||
|
|
||||||
|
If the flake defines only one module, it should be flakeModules.default.
|
||||||
|
|
||||||
|
You can not use this option in defining the flake's own `imports`. Instead, you can
|
||||||
|
put the module in question into its own file and reference it both in `imports` and
|
||||||
|
export it with this option.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
options = {
|
||||||
|
flake = mkOption {
|
||||||
|
type = types.submoduleWith {
|
||||||
|
modules = [
|
||||||
|
(mkAliasOptionModule [ "flakeModule" ] [ "flakeModules" "default" ])
|
||||||
|
{
|
||||||
|
options.flakeModules = flakeModulesOption;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in a new issue