summaryrefslogtreecommitdiff
path: root/modules/flake/githubWorkflow.nix
blob: bb36bf72e75b9098ab41efcacad4d2b500819632 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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;};
}