-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
240 lines (224 loc) · 7.49 KB
/
flake.nix
File metadata and controls
240 lines (224 loc) · 7.49 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
{
description = "Standalone Cloudflare R2 flake (Phase 1 scaffold)";
nixConfig = {
extra-substituters = [
"https://nix-r2-cloudflare-flake.cachix.org"
"https://wrangler.cachix.org"
];
extra-trusted-public-keys = [
"nix-r2-cloudflare-flake.cachix.org-1:pmYucG85iBm6Y+8TxNwqU5j/lmY1UBReZxIXslMFntw="
"wrangler.cachix.org-1:N/FIcG2qBQcolSpklb2IMDbsfjZKWg+ctxx0mSMXdSs="
];
};
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
home-manager = {
url = "github:nix-community/home-manager";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-parts.url = "github:hercules-ci/flake-parts";
wrangler.url = "github:emrldnix/wrangler";
};
outputs =
inputs@{
self,
nixpkgs,
flake-parts,
wrangler,
...
}:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
flake = {
nixosModules = {
default = import ./modules/nixos;
r2-sync = import ./modules/nixos/r2-sync.nix;
r2-restic = import ./modules/nixos/r2-restic.nix;
git-annex = import ./modules/nixos/git-annex.nix;
};
homeManagerModules = {
default = import ./modules/home-manager;
r2-cli = import ./modules/home-manager/r2-cli.nix;
r2-credentials = import ./modules/home-manager/r2-credentials.nix;
rclone-config = import ./modules/home-manager/rclone-config.nix;
};
templates = {
minimal = {
path = ./templates/minimal;
description = "Minimal R2 scaffold template";
};
full = {
path = ./templates/full;
description = "Full R2 scaffold template";
};
};
lib =
let
r2Lib = import ./lib/r2.nix { inherit (nixpkgs) lib; };
versionLib = import ./lib/version.nix { inherit (nixpkgs) lib; };
in
r2Lib // versionLib;
};
perSystem =
{
pkgs,
system,
...
}:
let
optionalPkg = name: if builtins.hasAttr name pkgs then [ pkgs.${name} ] else [ ];
optionalNodePkg =
name:
if builtins.hasAttr "nodePackages" pkgs && builtins.hasAttr name pkgs.nodePackages then
[ pkgs.nodePackages.${name} ]
else
[ ];
wranglerPkg = wrangler.packages.${system}.default;
formatterPkg =
if builtins.hasAttr "treefmt" pkgs then
pkgs.writeShellApplication {
name = "treefmt-wrapper";
runtimeInputs =
optionalPkg "treefmt"
++ optionalPkg "nixfmt"
++ optionalPkg "shfmt"
++ optionalNodePkg "prettier"
++ optionalPkg "taplo"
++ optionalPkg "actionlint";
text = ''
set -euo pipefail
exec treefmt "$@"
'';
}
else
pkgs.nixfmt;
versionLib = import ./lib/version.nix { inherit (pkgs) lib; };
inherit (versionLib) releaseBase;
r2DerivationVersion = versionLib.mkDerivationVersion {
inherit releaseBase;
sourceInfo = self.sourceInfo or null;
rev = self.rev or null;
shortRev = self.shortRev or null;
dirtyRev = self.dirtyRev or null;
dirtyShortRev = self.dirtyShortRev or null;
src = ./.;
};
hookToolPackages =
optionalPkg "lefthook"
++ optionalPkg "deadnix"
++ optionalPkg "statix"
++ optionalPkg "treefmt"
++ optionalPkg "nixfmt"
++ optionalPkg "shfmt"
++ optionalNodePkg "prettier"
++ optionalPkg "taplo"
++ optionalPkg "actionlint"
++ optionalPkg "yamllint"
++ optionalPkg "ripsecrets"
++ [
self.packages.${system}.lefthook-treefmt
self.packages.${system}.lefthook-statix
];
hookShellSetup = ''
treefmt_cache="$(git rev-parse --git-path treefmt-cache/cache 2>/dev/null || echo "$PWD/.git/treefmt-cache/cache")"
mkdir -p "$treefmt_cache" 2>/dev/null || true
export TREEFMT_CACHE_DB="$treefmt_cache/eval-cache"
if command -v lefthook >/dev/null 2>&1; then
pre_commit_hook="$(git rev-parse --git-path hooks/pre-commit 2>/dev/null || echo ".git/hooks/pre-commit")"
if [ ! -f "$pre_commit_hook" ] || ! grep -q "lefthook" "$pre_commit_hook" 2>/dev/null; then
lefthook install
fi
fi
'';
r2Package = pkgs.callPackage ./packages/r2-cli.nix {
wrangler = wranglerPkg;
derivationVersion = r2DerivationVersion;
inherit releaseBase;
};
in
{
packages = {
r2 = r2Package;
lefthook-treefmt = pkgs.writeShellApplication {
name = "lefthook-treefmt";
runtimeInputs = [
pkgs.coreutils
]
++ optionalPkg "git"
++ optionalPkg "treefmt";
text = ''
set -euo pipefail
mapfile -t changed < <(
{
git diff --name-only HEAD --diff-filter=ACM 2>/dev/null || true
git ls-files --others --exclude-standard 2>/dev/null || true
} | sort -u
)
if [ "''${#changed[@]}" -eq 0 ]; then
exit 0
fi
treefmt --fail-on-change "''${changed[@]}"
'';
};
lefthook-statix = pkgs.writeShellApplication {
name = "lefthook-statix";
runtimeInputs = [
pkgs.coreutils
]
++ optionalPkg "statix";
text = ''
set -euo pipefail
if [ "$#" -eq 0 ]; then
statix check --format errfmt
exit 0
fi
status=0
for file in "$@"; do
if [ -f "$file" ]; then
statix check --format errfmt "$file" || status=$?
fi
done
exit "$status"
'';
};
default = pkgs.symlinkJoin {
name = "r2-cloud-tools";
paths = [
self.packages.${system}.r2
self.packages.${system}.lefthook-treefmt
self.packages.${system}.lefthook-statix
];
};
};
devShells.default = pkgs.mkShell {
packages = [
pkgs.rclone
pkgs.restic
pkgs.nodejs
pkgs.jq
pkgs.python3
]
++ optionalPkg "git-annex"
++ hookToolPackages
++ [ wranglerPkg ];
shellHook = hookShellSetup;
};
devShells.hooks = pkgs.mkShell {
packages = [
pkgs.coreutils
pkgs.git
pkgs.python3
]
++ hookToolPackages
++ optionalPkg "vulnix";
shellHook = hookShellSetup;
};
formatter = formatterPkg;
};
};
}