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

Merge pull request #63 from hercules-ci/generalize-transposed-attrs

Generalize transposed attrs
This commit is contained in:
Robert Hensing 2022-10-27 17:38:47 +02:00 committed by GitHub
commit 0e101dbae7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 99 additions and 49 deletions

View file

@ -128,3 +128,13 @@ Why so many ways?
1. Flakes counterintuitively handles `system` by enumerating all of them in attribute sets. `flake-parts` does not impose this restriction, but does need to support it. 1. Flakes counterintuitively handles `system` by enumerating all of them in attribute sets. `flake-parts` does not impose this restriction, but does need to support it.
2. `flake-parts` provides an extensible structure that is richer than the flakes interface alone. 2. `flake-parts` provides an extensible structure that is richer than the flakes interface alone.
# How do I define my own flake output attribute?
Have a look at the [source](https://github.com/hercules-ci/flake-parts/tree/main/modules) for some examples.
Whether directly or indirectly, you'll be defining an attribute inside [the `flake` option](https://flake.parts/options.html#opt-flake).
If you want the attribute to be derived from [`perSystem`](https://flake.parts/options.html#opt-perSystem) you can start with [`packages.nix`](https://github.com/hercules-ci/flake-parts/blob/main/modules/packages.nix) as an example, or [`formatter.nix`](https://github.com/hercules-ci/flake-parts/blob/main/modules/formatter.nix) if you need to do some filtering.
If you really don't care about your attribute, you may temporarily use [`transposition.<name>.adHoc = true`](https://flake.parts/options.html#opt-transposition._name_.adHoc) to create and expose a `perSystem` option without merging support, type checking or documentation.

View file

@ -15,6 +15,7 @@
./modules/overlays.nix ./modules/overlays.nix
./modules/packages.nix ./modules/packages.nix
./modules/perSystem.nix ./modules/perSystem.nix
./modules/transposition.nix
./modules/withSystem.nix ./modules/withSystem.nix
]; ];
} }

View file

@ -81,15 +81,6 @@ in
}; };
config = { config = {
flake.apps = transposition.apps = { };
mapAttrs
(k: v: v.apps or { })
config.allSystems;
perInput = system: flake:
optionalAttrs (flake?apps.${system}) {
apps = flake.apps.${system};
};
}; };
} }

View file

@ -39,15 +39,6 @@ in
}; };
config = { config = {
flake.checks = transposition.checks = { };
mapAttrs
(k: v: v.checks)
config.allSystems;
perInput = system: flake:
optionalAttrs (flake?checks.${system}) {
checks = flake.checks.${system};
};
}; };
} }

View file

@ -40,14 +40,6 @@ in
}); });
}; };
config = { config = {
flake.devShells = transposition.devShells = { };
mapAttrs
(k: v: v.devShells)
config.allSystems;
perInput = system: flake:
optionalAttrs (flake?devShells.${system}) {
devShells = flake.devShells.${system};
};
}; };
} }

View file

@ -38,15 +38,6 @@ in
}; };
config = { config = {
flake.legacyPackages = transposition.legacyPackages = { };
mapAttrs
(k: v: v.legacyPackages)
config.allSystems;
perInput = system: flake:
optionalAttrs (flake?legacyPackages.${system}) {
legacyPackages = flake.legacyPackages.${system};
};
}; };
} }

View file

@ -40,15 +40,6 @@ in
}); });
}; };
config = { config = {
flake.packages = transposition.packages = { };
mapAttrs
(k: v: v.packages or { })
config.allSystems;
perInput = system: flake:
optionalAttrs (flake?packages.${system}) {
packages = flake.packages.${system};
};
}; };
} }

83
modules/transposition.nix Normal file
View file

@ -0,0 +1,83 @@
{ config, lib, flake-parts-lib, ... }:
let
inherit (lib)
filterAttrs
mapAttrs
mkOption
types
;
inherit (flake-parts-lib)
mkSubmoduleOptions
mkPerSystemOption
;
transpositionModule = {
options = {
adHoc = mkOption {
type = types.bool;
default = false;
description = ''
Whether to provide a stub option declaration for <option>perSystem.&lt;name></option>
The stub option declaration does not support merging and lacks
documentation, so you are recommended to declare the <option>perSystem.&lt;name></option>
option yourself and avoid <option>adHoc</option>.
'';
};
};
};
in
{
options = {
transposition = lib.mkOption {
description = ''
A helper that defines transposed attributes in the flake outputs.
Transposition is the operation that swaps the indices of a data structure.
Here it refers specifically to the transposition between
<literal>
perSystem: .''${system}.''${attribute}
outputs: .''${attribute}.''${system}
</literal>
It also defines the reverse operation in <option>perInput</option>.
'';
type =
types.lazyAttrsOf
(types.submoduleWith { modules = [ transpositionModule ]; });
};
};
config = {
flake =
lib.mapAttrs
(attrName: attrConfig:
mapAttrs
(system: v: v.${attrName})
config.allSystems
)
config.transposition;
perInput =
system: flake:
mapAttrs
(attrName: attrConfig: flake.${attrName}.${system})
(filterAttrs
(attrName: attrConfig: flake?${attrName}.${system})
config.transposition
);
perSystem = { ... }: {
options =
mapAttrs
(k: v: lib.mkOption { })
(filterAttrs
(k: v: v.adHoc)
config.transposition
);
};
};
}