-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub-ci.nix
More file actions
195 lines (181 loc) · 6.24 KB
/
Copy pathgithub-ci.nix
File metadata and controls
195 lines (181 loc) · 6.24 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
{
flake-parts-lib,
lib,
...
}: let
inherit (lib) mkOption types literalExpression;
# Import type definitions
workflowTypes = import ./types/workflow.nix {inherit lib;};
inherit (workflowTypes) workflowType;
actionTypes = import ./types/action.nix {inherit lib;};
inherit (actionTypes) actionType;
# Import conversion functions
converters = import ./converters.nix {inherit lib;};
inherit (converters) workflowToYaml actionToYaml;
in {
options.perSystem = flake-parts-lib.mkPerSystemOption (
psArgs @ {pkgs, ...}: let
cfg = psArgs.config.githubActions;
in {
options = {
githubActions = {
enable = mkOption {
type = types.bool;
default = false;
description = "Whether to generate GitHub Actions workflows from Nix configuration.";
};
workflows = mkOption {
type = types.attrsOf workflowType;
default = {};
description = ''
GitHub Actions workflows to generate. Keys are workflow file names
(without the .yml extension).
'';
example = literalExpression ''
{
ci = {
name = "CI";
on = ["push" "pull_request"];
jobs = {
build = {
runsOn = "ubuntu-latest";
steps = [
{
uses = "actions/checkout@v4";
}
{
name = "Build";
run = "npm run build";
}
];
};
};
};
}
'';
};
workflowsDir = mkOption {
type = types.package;
readOnly = true;
description = ''
Generated .github/workflows directory as a derivation.
Contains all workflow files defined in the configuration.
Only populated when `enable = true`.
'';
};
workflowFiles = mkOption {
type = types.attrsOf types.package;
readOnly = true;
description = ''
Individual workflow files as derivations.
Keys are workflow names (without .yml extension).
Only populated when `enable = true`.
'';
};
actions = mkOption {
type = types.attrsOf actionType;
default = {};
description = ''
GitHub actions to generate. Keys are action names;
each emits .github/actions/<name>/action.yml.
'';
example = literalExpression ''
{
setup-ci = {
name = "Setup CI";
description = "Checkout + toolchain setup";
runs.steps = [
{ uses = "actions/checkout@v4"; }
{ name = "Install"; run = "npm ci"; }
];
};
}
'';
};
actionFiles = mkOption {
type = types.attrsOf types.package;
readOnly = true;
description = ''
Individual composite action files as derivations.
Keys are "<name>/action.yml". Only populated when enable = true.
'';
};
actionsDir = mkOption {
type = types.package;
readOnly = true;
description = ''
Generated .github/actions directory as a derivation, containing
<name>/action.yml subdirectories. Only populated when enable = true.
'';
};
};
};
config = let
# Generate individual workflow files
workflowFiles =
lib.mapAttrs' (
name: workflow:
lib.nameValuePair "${name}.yml" (
pkgs.runCommandLocal "${name}.yml" {
nativeBuildInputs = [pkgs.yq-go];
json = builtins.toJSON (workflowToYaml workflow);
passAsFile = ["json"];
} ''
{
echo "# This file is automatically generated from Nix configuration. Do not edit directly."
echo ""
yq eval --prettyPrint '.' -P $jsonPath
} > $out
''
)
)
cfg.workflows;
# Generate individual composite action files (directory per action)
actionFiles =
lib.mapAttrs' (
name: action:
lib.nameValuePair "${name}/action.yml" (
pkgs.runCommandLocal "${name}-action.yml" {
nativeBuildInputs = [pkgs.yq-go];
json = builtins.toJSON (actionToYaml action);
passAsFile = ["json"];
} ''
{
echo "# This file is automatically generated from Nix configuration. Do not edit directly."
echo ""
yq eval --prettyPrint '.' -P $jsonPath
} > $out
''
)
)
cfg.actions;
in {
githubActions = {
workflowFiles = lib.mkIf cfg.enable workflowFiles;
workflowsDir = lib.mkIf cfg.enable (
# Create a directory with all workflow files
pkgs.runCommandLocal "github-workflows" {} ''
mkdir -p $out
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: file: ''
cp ${file} $out/${name}
'')
workflowFiles)}
''
);
actionFiles = lib.mkIf cfg.enable actionFiles;
actionsDir = lib.mkIf cfg.enable (
# Create a directory with all composite action subdirectories
pkgs.runCommandLocal "github-actions-composite" {} ''
mkdir -p $out
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: file: ''
mkdir -p "$out/$(dirname ${name})"
cp ${file} $out/${name}
'')
actionFiles)}
''
);
};
};
}
);
}