summaryrefslogtreecommitdiff
path: root/modules/flake
diff options
context:
space:
mode:
authorseth <[email protected]>2023-10-07 17:07:44 -0400
committerseth <[email protected]>2023-10-08 13:55:52 -0400
commit003c15c82ca1554e62966c520e811132b071555e (patch)
tree23b52f1ad249b9b3c2a48b44a28a93a2bbb94afb /modules/flake
parent664fdadd482d1742e37bbd8b50410f86dc10dcbe (diff)
modules/githubWorkflowGenerator: init
Diffstat (limited to 'modules/flake')
-rw-r--r--modules/flake/githubWorkflow.nix166
1 files changed, 166 insertions, 0 deletions
diff --git a/modules/flake/githubWorkflow.nix b/modules/flake/githubWorkflow.nix
new file mode 100644
index 0000000..bb36bf7
--- /dev/null
+++ b/modules/flake/githubWorkflow.nix
@@ -0,0 +1,166 @@
+{
+ config,
+ lib,
+ self,
+ ...
+}: let
+ cfg = config.githubWorkflowGenerator;
+
+ inherit
+ (builtins)
+ attrNames
+ filter
+ ;
+
+ inherit
+ (lib)
+ elem
+ flatten
+ getAttrs
+ literalExpression
+ mapAttrsToList
+ mdDoc
+ mkOption
+ types
+ ;
+
+ supportedOutputs = [
+ "apps"
+ "checks"
+ "devShells"
+ "darwinConfigurations"
+ "homeConfigurations"
+ "nixosConfigurations"
+ "packages"
+ ];
+
+ platformMap = {
+ options = {
+ arch = mkOption {
+ description = mdDoc "the architecture of a system";
+ type = types.str;
+ default = null;
+ example = literalExpression "x86_64";
+ };
+
+ os = mkOption {
+ description = mdDoc "the name of an os supported by github runners";
+ type = types.str;
+ default = null;
+ example = literalExpression "ubuntu-latest";
+ };
+ };
+ };
+
+ overrides = {
+ options = {
+ systems = mkOption {
+ description = mdDoc "list of systems to build an output for";
+ type = types.listOf types.str;
+ default = builtins.attrNames cfg.platforms;
+ };
+ };
+ };
+
+ mkMatrix = {
+ output,
+ systems ? (attrNames cfg.platforms),
+ }:
+ if (lib.elem output ["nixosConfigurations" "darwinConfigurations"])
+ then
+ mkMatrixFlat {
+ inherit output;
+ suffix = ".config.system.build.toplevel";
+ }
+ else if (output == "homeConfigurations")
+ then
+ mkMatrixFlat {
+ inherit output;
+ suffix = ".activationPackage";
+ }
+ else
+ flatten (
+ mapAttrsToList
+ (
+ system:
+ mapAttrsToList (
+ attr: _: {
+ inherit (cfg.platforms.${system}) os arch;
+ attr = "${output}.${system}.${attr}";
+ }
+ )
+ )
+ (getAttrs systems self.${output})
+ );
+
+ mkMatrixFlat = {
+ output,
+ suffix ? "",
+ }:
+ mapAttrsToList (
+ attr: deriv: {
+ inherit (cfg.platforms.${deriv.pkgs.system}) os arch;
+ attr = "${output}.${attr}${suffix}";
+ }
+ )
+ self.${output};
+
+ jobs = let
+ self' = getAttrs cfg.outputs self;
+ in
+ flatten (
+ mapAttrsToList (
+ output: _:
+ mkMatrix ({inherit output;} // cfg.overrides.${output} or {})
+ )
+ self'
+ );
+in {
+ options = {
+ githubWorkflowGenerator = {
+ outputs = mkOption {
+ description = mdDoc "outputs to include in workflow";
+ type = types.listOf types.str;
+ default = filter (output: elem output supportedOutputs) (attrNames self);
+ };
+
+ platforms = mkOption {
+ description = mdDoc ''
+ an attrset that can map a nix system to an architecture and os supported by github
+ '';
+ type = types.attrsOf (types.submodule platformMap);
+ default = {
+ "x86_64-linux" = {
+ arch = "x86_64";
+ os = "ubuntu-latest";
+ };
+
+ "aarch64-linux" = {
+ arch = "aarch64";
+ os = "ubuntu-latest";
+ };
+
+ "x86_64-darwin" = {
+ arch = "x86_64";
+ os = "macos-latest";
+ };
+ };
+ };
+
+ overrides = mkOption {
+ description = mdDoc "overrides for mkMatrix args";
+ type = types.attrsOf (types.submodule overrides);
+ default = {};
+ example = literalExpression ''
+ {
+ githubworkflowGenerator.overrides = {
+ checks.systems = [ "x86_64-linux" ];
+ };
+ }
+ '';
+ };
+ };
+ };
+
+ config.flake.githubWorkflow = {matrix.include = jobs;};
+}