forked from mightyiam/files
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.nix
More file actions
183 lines (175 loc) · 5.81 KB
/
module.nix
File metadata and controls
183 lines (175 loc) · 5.81 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
{
flake-parts-lib,
lib,
self,
...
}:
{
options.perSystem = flake-parts-lib.mkPerSystemOption (
psArgs@{ pkgs, ... }:
let
cfg = psArgs.config.files;
in
{
options = {
files = {
gitToplevel = lib.mkOption {
type = lib.types.path;
default = self;
defaultText = lib.literalExpression "self";
description = ''
Each check is performed by copying the existing file into the store
and comparing its contents with the configured contents.
For that purpose a path to the file must be provided to Nix.
Configured file paths are relative to the Git top-level.
But Nix is oblivious to the Git top-level.
So file paths are resolved relative to the value of this option.
The default value is correct when the flake is at the Git top-level.
Otherwise the correct Git top-level must be provided.
'';
example = lib.literalExpression "../.";
};
files = lib.mkOption {
description = ''
Files to be written and checked for.
'';
default = [ ];
example =
lib.literalExpression
# nix
''
[
{
path_ = "README.md";
drv =
pkgs.writeText "README.md"
# markdown
'''
# Practical Project
Clear documentation
''';
}
{
path_ = ".gitignore";
drv = pkgs.writeText "gitignore" '''
result
''';
}
]
'';
type = lib.types.listOf (
lib.types.submodule {
options = {
path_ = lib.mkOption {
type = lib.types.str;
description = ''
File path relative to Git top-level.
'';
example = lib.literalExpression ''".github/workflows/check.yaml"'';
};
drv = lib.mkOption {
description = ''
Provide the file as a derivation.
The out path is expected to be a file.
Directory out path not supported;
pull request welcome!
'';
type = lib.types.package;
example =
lib.literalExpression
# nix
''
pkgs.writers.writeJSON "gh-actions-workflow-check.yaml" {
on.push = { };
jobs.check = {
runs-on = "ubuntu-latest";
steps = [
{ uses = "actions/checkout@v4"; }
{ uses = "DeterminateSystems/nix-installer-action@main"; }
{ uses = "DeterminateSystems/magic-nix-cache-action@main"; }
{ run = "nix flake check"; }
];
};
}
'';
};
};
}
);
};
writer = {
exeFilename = lib.mkOption {
type = lib.types.singleLineStr;
default = "write-files";
description = ''
The writer executable filename.
'';
example = lib.literalExpression ''"files-write"'';
};
drv = lib.mkOption {
description = ''
Provides an executable
that writes each configured file's contents to its path.
Missing parent directories are created.
Consider including this in the project's development shell.
'';
type = lib.types.package;
readOnly = true;
};
};
};
};
config = {
files.writer.drv = pkgs.writeShellApplication {
name = psArgs.config.files.writer.exeFilename;
runtimeInputs = [ pkgs.git ];
text = lib.pipe cfg.files [
(map (
{ path_, drv }:
''
dir=$(dirname ${path_})
mkdir -p "$dir"
cat ${drv} > ${lib.escapeShellArg path_}
''
))
(lib.concat [
''
toplevel="$(git rev-parse --show-toplevel)"
cd "$toplevel"
''
])
lib.concatLines
];
};
checks = lib.pipe cfg.files [
(map (
{ path_, drv }:
{
name = "files/${path_}";
value =
let
file =
lib.pipe
[ cfg.gitToplevel "/" path_ ]
[
lib.concatStrings
lib.readFile
(pkgs.writeText "flake-files-file")
];
in
pkgs.runCommand "flake-file-check"
{
nativeBuildInputs = [ pkgs.difftastic ];
}
''
difft --exit-code --display inline ${drv} ${file}
touch $out
'';
}
))
lib.listToAttrs
];
};
}
);
}