-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.nix
More file actions
146 lines (144 loc) · 4.5 KB
/
Copy pathcodegen.nix
File metadata and controls
146 lines (144 loc) · 4.5 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
# codegen flake module simplifies vendoring code generation in your repo
# Copyright (C) 2026 Anterior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, version 3 of the
# License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
{
inputs,
self,
flake-parts-lib,
...
}:
{
options.perSystem = flake-parts-lib.mkPerSystemOption (
{
pkgs,
lib,
system,
config,
...
}:
let
inherit (lib) types;
cfg = config.codegen;
entry =
{
lib,
config,
options,
name,
...
}:
{
options = {
source = lib.mkOption {
type = types.pathInStore;
description = "File or directory to treat as the source for this target";
};
text = lib.mkOption {
type = types.nullOr types.str;
default = null;
description = "Verbatim content for this file as a string";
};
};
config = {
source = lib.mkIf (config.text != null) (lib.mkDerivedConfig options.text (pkgs.writeText name));
};
};
in
{
options.codegen = {
enable = lib.mkEnableOption "Enable management of auto generated files";
files = lib.mkOption {
type = lib.types.lazyAttrsOf (types.submodule entry);
description = ''
Mapping of local path to derivation output for that path
If the source is a directory and you want to copy its *contents*,
not the directory itself, make sure to terminate it with a /.
'';
default = { };
example = lib.literalExpression ''
{
".github/workflows".source = "''${github-actions}/";
"src/schemas".source = "''${ourschemas}/schemas/";
}
'';
};
root = lib.mkOption {
type = lib.types.path;
description = "Root to which the paths in the schema are relative";
example = lib.literalExpression "./.";
};
workingDirPrefix = lib.mkOption {
type = lib.types.str;
default = "";
example = ''"$PRJ_ROOT"/'';
description = "Path prefix for syncing generated files to the working directory.";
};
};
config = lib.mkIf cfg.enable {
packages.codegen = pkgs.runCommand "codegen-build" { } (
''
mkdir -p $out
''
+ lib.concatLines (
lib.mapAttrsToList (
k: v:
let
target = "$out/${lib.escapeShellArg k}";
in
''
mkdir -p ${target}
rm -rf ${target}
cp -r ${v.source} ${target}
''
) cfg.files
)
);
checks.codegen = pkgs.runCommand "codegen-check" { nativeBuildInputs = [ pkgs.diffutils ]; } (
lib.concatLines (
lib.mapAttrsToList (
k: v:
let
# Construct separately to avoid copying over the entire working
# directory to the store.
storePath = cfg.root + "/${lib.escapeShellArg k}";
in
''
diff --recursive --brief ${v.source} ${storePath}
touch $out
''
) cfg.files
)
);
apps.codegen = {
type = "app";
program =
let
pkg = pkgs.writeShellApplication {
name = "codegen-sync";
runtimeInputs = [ pkgs.rsync ];
text = lib.concatLines (
lib.mapAttrsToList (k: v: ''
rsync -ah --delete --checksum --chmod=u+w ${v.source} ${cfg.workingDirPrefix}${lib.escapeShellArg k}
'') cfg.files
);
};
in
lib.getExe pkg;
meta.description = "synchronize the autogenerated files in the working directory";
};
};
}
);
}