1
0
Fork 0
mirror of https://github.com/hercules-ci/flake-parts.git synced 2024-12-14 11:47:31 +00:00
flake-parts/modules/transposition.nix

88 lines
2.1 KiB
Nix
Raw Normal View History

2022-10-26 11:16:04 +00:00
{ config, lib, flake-parts-lib, ... }:
let
inherit (lib)
filterAttrs
mapAttrs
mkOption
types
;
2022-10-26 12:07:24 +00:00
transpositionModule = {
options = {
adHoc = mkOption {
type = types.bool;
default = false;
2022-11-11 06:39:25 +00:00
description = ''
2022-11-11 05:40:37 +00:00
Whether to provide a stub option declaration for {option}`perSystem.<name>`.
2022-10-26 12:07:24 +00:00
The stub option declaration does not support merging and lacks
2022-11-11 05:40:37 +00:00
documentation, so you are recommended to declare the {option}`perSystem.<name>`
option yourself and avoid {option}`adHoc`.
2022-10-26 12:07:24 +00:00
'';
};
};
};
2022-10-26 11:16:04 +00:00
in
{
options = {
transposition = lib.mkOption {
2022-11-11 06:39:25 +00:00
description = ''
2022-10-26 11:16:04 +00:00
A helper that defines transposed attributes in the flake outputs.
2023-10-01 12:34:27 +00:00
When you define `transposition.foo = { };`, definitions are added to the effect of (pseudo-code):
```nix
flake.foo.''${system} = (perSystem system).foo;
perInput = system: inputFlake: inputFlake.foo.''${system};
```
2022-10-26 11:16:04 +00:00
Transposition is the operation that swaps the indices of a data structure.
Here it refers specifically to the transposition between
2022-11-11 05:40:37 +00:00
```plain
perSystem: .''${system}.''${attribute}
outputs: .''${attribute}.''${system}
```
2022-10-26 11:16:04 +00:00
2022-11-11 05:40:37 +00:00
It also defines the reverse operation in [{option}`perInput`](#opt-perInput).
2022-10-26 11:16:04 +00:00
'';
type =
types.lazyAttrsOf
2022-10-26 12:07:24 +00:00
(types.submoduleWith { modules = [ transpositionModule ]; });
2022-10-26 11:16:04 +00:00
};
};
config = {
flake =
lib.mapAttrs
(attrName: attrConfig:
mapAttrs
(system: v: v.${attrName})
config.allSystems
)
config.transposition;
perInput =
system: flake:
mapAttrs
(attrName: attrConfig:
flake.${attrName}.${system} or (throw ''
Attemt to access non existent attribute ${attrName}.${system} of flake ${flake}.
'')
)
2023-05-29 17:39:48 +00:00
config.transposition;
2022-10-26 12:07:24 +00:00
2023-05-29 17:52:03 +00:00
perSystem = {
2022-10-26 12:07:24 +00:00
options =
mapAttrs
(k: v: lib.mkOption { })
(filterAttrs
(k: v: v.adHoc)
config.transposition
);
};
2022-10-26 11:16:04 +00:00
};
}