forked from yaxitech/ragenix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
381 lines (326 loc) · 13.5 KB
/
flake.nix
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
{
description = "A rust drop-in replacement for agenix";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
agenix = {
url = "github:NiXium-org/agenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, flake-utils, rust-overlay, crane, agenix }:
let
cargoTOML = lib.importTOML ./Cargo.toml;
name = cargoTOML.package.name;
lib = nixpkgs.lib;
# Recursively merge a list of attribute sets. Following elements take
# precedence over previous elements if they have conflicting keys.
recursiveMerge = with lib; foldl recursiveUpdate { };
eachSystem = systems: f: flake-utils.lib.eachSystem systems (system: f (pkgsFor system));
defaultSystems = flake-utils.lib.defaultSystems;
eachDefaultSystem = eachSystem defaultSystems;
eachLinuxSystem = eachSystem (lib.filter (lib.hasSuffix "-linux") flake-utils.lib.defaultSystems);
pkgsFor = system: import nixpkgs {
inherit system;
overlays = [
rust-overlay.overlays.default
self.overlays.default
(final: prev: {
rustToolchain = final.rust-bin.fromRustupToolchainFile ./rust-toolchain;
craneLib = (crane.mkLib prev).overrideToolchain final.rustToolchain;
})
];
config = { allowAliases = false; };
};
in
recursiveMerge [
#
# COMMON OUTPUTS FOR ALL SYSTEMS
#
(eachDefaultSystem (pkgs: rec {
# `nix build`
packages.${name} = pkgs.callPackage ./default.nix { };
packages.default = packages.${name};
defaultPackage = lib.warnIf (builtins.compareVersions builtins.nixVersion "2.7" >= 0)
"ragenix's flake output `defaultPackage` is deprecated in favor of `packages.default` for Nix >= 2.7"
packages.default;
# `nix run`
apps.${name} = flake-utils.lib.mkApp {
drv = packages.${name};
};
apps.default = apps.${name};
defaultApp = lib.warnIf (builtins.compareVersions builtins.nixVersion "2.7" >= 0)
"ragenix's flake output `defaultApp` is deprecated in favor of `apps.default` for Nix >= 2.7"
apps.default;
# Regenerate the roff and HTML manpages and commit the changes, if any
apps.update-manpage = flake-utils.lib.mkApp {
drv = pkgs.writeShellApplication {
name = "update-manpage";
runtimeInputs = with pkgs; [ ronn git ];
text = ''
ronn docs/ragenix.1.ronn
git diff --quiet -- docs/ragenix.1* || changes=1
git diff --staged --quiet -- docs/ragenix.1* || changes=1
if [[ -z "''${changes:-}" ]]; then
echo 'No changes to commit'
else
echo 'Committing changes'
git commit -m "docs: update manpage" docs/ragenix.1*
fi
'';
};
};
# nix `check`
checks.clippy = pkgs.craneLib.cargoClippy {
inherit (self.packages.${pkgs.stdenv.system}.${name}) src cargoArtifacts;
cargoClippyExtraArgs = "--all-targets --tests --examples -- -D clippy::pedantic -D warnings";
doInstallCargoArtifacts = false;
RAGENIX_NIX_BIN_PATH = lib.getExe pkgs.nix;
};
checks.fmt = pkgs.craneLib.cargoFmt {
inherit (self.packages.${pkgs.stdenv.system}.${name}) src cargoArtifacts;
doInstallCargoArtifacts = false;
RAGENIX_NIX_BIN_PATH = lib.getExe pkgs.nix;
};
checks.nixpkgs-fmt = pkgs.runCommand "check-nix-format" { } ''
${pkgs.nixpkgs-fmt}/bin/nixpkgs-fmt --check ${self}
mkdir $out #sucess
'';
checks.schema = pkgs.runCommand "emit-schema" { } ''
set -euo pipefail
${pkgs.ragenix}/bin/ragenix --schema > "$TMPDIR/agenix.schema.json"
${pkgs.diffutils}/bin/diff '${self}/src/ragenix/agenix.schema.json' "$TMPDIR/agenix.schema.json"
echo "Schema matches"
mkdir "$out"
'';
checks.agenix-symlink = pkgs.runCommand "check-agenix-symlink" { } ''
set -euo pipefail
agenix="$(readlink -f '${pkgs.ragenix}/bin/agenix')"
ragenix="$(readlink -f '${pkgs.ragenix}/bin/ragenix')"
if [[ "$agenix" == "$ragenix" ]]; then
echo "agenix symlinked to ragenix"
mkdir $out
else
echo "agenix doesn't resolve to ragenix"
echo "agenix: $agenix"
echo "ragenix: $ragenix"
exit 1
fi
'';
checks.shell-files = pkgs.runCommand "check-shell-files" { } ''
set -euo pipefail
if [[ ! -e "${pkgs.ragenix}/share/bash-completion" ]]; then
echo 'Failed to install bash completions'
elif [[ ! -e "${pkgs.ragenix}/share/zsh" ]]; then
echo 'Failed to install zsh completions'
elif [[ ! -e "${pkgs.ragenix}/share/fish" ]]; then
echo 'Failed to install fish completions'
elif [[ ! -e "${pkgs.ragenix}/share/man/man1/ragenix.1.gz" ]]; then
echo 'Failed to install manpage'
else
echo '${name} shell files installed successfully'
mkdir $out
exit 0
fi
exit 1
'';
checks.decrypt-with-age = pkgs.runCommand "decrypt-with-age" { } ''
set -euo pipefail
# Required to prevent a panic in the locale_config crate
# https://github.com/yaxitech/ragenix/issues/76
${lib.optionalString pkgs.stdenv.isDarwin ''export LANG="en_US.UTF-8"''}
files=('${self}/example/root.passwd.age' '${self}/example/github-runner.token.age')
for file in ''${files[@]}; do
rage_output="$(${pkgs.rage}/bin/rage -i '${self}/example/keys/id_ed25519' -d "$file")"
age_output="$(${pkgs.age}/bin/age -i '${self}/example/keys/id_ed25519' -d "$file")"
if [[ "$rage_output" != "$age_output" ]]; then
printf 'Decrypted plaintext for %s differs for rage and age' "$file"
exit 1
fi
done
echo "rage and age decryption of examples successful and equal"
mkdir $out
'';
checks.metadata = pkgs.runCommand "check-metadata" { } ''
set -euo pipefail
flakeDescription=${lib.escapeShellArg (import "${self}/flake.nix").description}
packageDescription=${lib.escapeShellArg cargoTOML.package.description}
if [[ "$flakeDescription" != "$packageDescription" ]]; then
echo 'The descriptions given in flake.nix and Cargo.toml do not match'
exit 1
fi
flakePackageName=${pkgs.ragenix.pname}
cargoName=${cargoTOML.package.name}
if [[ "$flakePackageName" != "$cargoName" ]]; then
echo 'The package name given in flake.nix and Cargo.toml do not match'
exit 1
fi
echo 'All metadata checks completed successfully'
mkdir $out # success
'';
# Make sure the roff and HTML manpages are up-to-date
checks.manpage = pkgs.runCommand "check-manpage"
{
buildInputs = with pkgs; [ ronn diffutils ];
} ''
set -euo pipefail
echo "Generate roff and HTML manpage"
ln -s ${self}/docs/ragenix.1.ronn .
ronn ragenix.1.ronn
echo "roff: strip date"
tail -n '+5' ${self}/docs/ragenix.1 > ragenix.1.old
tail -n '+5' ragenix.1 > ragenix.1.new
diff -u ragenix.1.{old,new} > diff \
|| (printf "roff: error, not up-to-date:\n\n%s\n" "$(cat diff)" >&2 && exit 1)
echo "html: strip date"
grep -v "<li class='tc'>" ${self}/docs/ragenix.1.html > ragenix.1.html.old
grep -v "<li class='tc'>" ragenix.1.html > ragenix.1.html.new
diff -u ragenix.1.html.{old,new} > diff \
|| (printf "html: error, not up-to-date:\n\n%s\n" "$(cat diff)" >&2 && exit 1)
echo 'Manpage is up-to-date'
mkdir -p $out
'';
# `nix develop`
devShell = pkgs.mkShell {
name = "${name}-dev-shell";
inputsFrom = [ self.packages."${pkgs.stdenv.system}".ragenix ];
nativeBuildInputs = with pkgs; [
ronn
rust-analyzer
];
RAGENIX_NIX_BIN_PATH = lib.getExe pkgs.nix;
RUST_SRC_PATH = "${pkgs.rustToolchain}/lib/rustlib/src/rust/library";
shellHook = ''
export PATH=$PWD/target/debug:$PATH
'';
};
})
)
#
# CHECKS SPECIFIC TO LINUX SYSTEMS
#
(eachLinuxSystem (pkgs: {
checks.nixos-module =
let
pythonTest = import ("${nixpkgs}/nixos/lib/testing-python.nix") {
inherit (pkgs.stdenv.hostPlatform) system;
};
secretsConfig = import "${self}/example/secrets-configuration.nix";
secretPath = "/run/agenix/github-runner.token";
ageIdentitiesConfig = { lib, ... }: {
# XXX: This is insecure and copies your private key plaintext to the Nix store
# NEVER DO THIS IN YOUR CONFIG!
age.identityPaths = lib.mkForce [ "${self}/example/keys/id_ed25519" ];
};
in
pythonTest.runTest {
name = "ragenix-nixos-module";
nodes.machine.imports = [
self.nixosModules.age
secretsConfig
ageIdentitiesConfig
];
testScript = ''
machine.start()
machine.wait_for_unit("multi-user.target")
machine.succeed('test -e "${secretPath}"')
machine.succeed(
'[[ "$(cat "${secretPath}")" == "wurzelpfropf!" ]] || exit 1'
)
machine.succeed(
'[[ "$(stat -c "%a" "${secretPath}")" == "400" ]] || exit 1'
)
machine.succeed(
'[[ "$(stat -c "%U" "${secretPath}")" == "root" ]] || exit 1'
)
machine.succeed(
'[[ "$(stat -c "%G" "${secretPath}")" == "root" ]] || exit 1'
)
'';
};
checks.tests-recursive-nix = (pkgs.ragenix.override {
enableRecursiveNixTests = true;
}).overrideAttrs (_: {
name = "tests-recursive-nix";
});
checks.rekey = pkgs.runCommand "run-rekey"
{
requiredSystemFeatures = [ "recursive-nix" ];
}
''
set -euo pipefail
cp -r '${self}/example/.' "$TMPDIR"
chmod 600 *.age
ln -s "${self}/example/keys" "$TMPDIR/.ssh"
export HOME="$TMPDIR"
${pkgs.ragenix}/bin/ragenix --rekey
${pkgs.agenix}/bin/agenix --rekey
mkdir "$out"
'';
checks.age-plugin =
let
rageExamplePlugin = pkgs.rage.overrideAttrs (_: rec {
pname = "age-plugin-unencrypted";
doCheck = false;
cargoBuildFlags = [ "--example" pname ];
installPhase = ''
set -euo pipefail
find target/**/release/examples -name ${pname} \
-exec install -D {} $out/bin/${pname} \;
# strip.sh uses unset variables
set +u
'';
});
plugins = [ rageExamplePlugin ];
ragenixWithPlugins = pkgs.ragenix.override { inherit plugins; };
pluginsSearchPath = lib.strings.makeBinPath plugins;
in
pkgs.runCommand "age-plugin"
{
buildInputs = [ pkgs.rage ragenixWithPlugins ];
requiredSystemFeatures = [ "recursive-nix" ];
}
''
set -euo pipefail
cp -r '${self}/example/.' "$TMPDIR"
cd "$TMPDIR"
# Encrypt with ragenix
echo 'wurzelpfropf' | ragenix --rules ./secrets-plugin.nix --editor - --edit unencrypted.age
# Decrypt with rage
decrypted="$(PATH="${pluginsSearchPath}:$PATH" rage -i '${self}/example/keys/example_plugin_key.txt' -d unencrypted.age)"
if [[ "$decrypted" != "wurzelpfropf" ]]; then
echo 'Unexpected value for decryption with plugin'
exit 1
fi
# Rekey
ragenix --rules ./secrets-plugin.nix -i '${self}/example/keys/example_plugin_key.txt' --rekey
mkdir $out # success
'';
})
)
#
# SYSTEM-INDEPENDENT OUTPUTS
#
{
# Passthrough the agenix NixOS, Darwin and Home Manager modules
inherit (agenix) nixosModules darwinModules homeManagerModules;
# Overlay to add ragenix and replace agenix
overlays.default = _final: prev: rec {
ragenix = self.packages.${prev.stdenv.hostPlatform.system}.ragenix;
agenix = ragenix;
};
overlay = lib.warnIf (builtins.compareVersions builtins.nixVersion "2.7" >= 0)
"ragenix's flake output `overlay` is deprecated in favor of `overlays.default` for Nix >= 2.7"
self.overlays.default;
}
];
}