add testing infra

This commit is contained in:
2025-08-07 19:17:16 -07:00
parent 207722acb2
commit 2875d29293
5 changed files with 131 additions and 25 deletions

54
lib.nix
View File

@@ -9,34 +9,38 @@ inputs.nixpkgs.lib.extend (
lib = prev;
in
{
ensureZfsMounts =
dirs:
pkgs.writeShellApplication {
name = "zfslistmounted";
runtimeInputs = with pkgs; [
zfs
gnugrep
gawk
coreutils
];
# Should probably be moved to a package later instead of this
ensureZfsMounts = pkgs.writeShellApplication {
name = "zfsEnsureMounted";
runtimeInputs = with pkgs; [
zfs
gnugrep
gawk
coreutils
];
text = ''
#!/bin/sh
text = ''
#!/bin/sh -x
TARGETS=$(echo "${lib.strings.concatStringsSep "\n" dirs}" | sort | uniq)
MOUNTED=$(zfs list -o mountpoint,mounted -H | awk '$2 == "yes" {print $1}' | sort | uniq)
NUM_MATCHED=$(echo "$MOUNTED" | grep -Ec '${lib.strings.concatStringsSep "\|" dirs}')
if [[ "$NUM_MATCHED" -eq "${toString (lib.length dirs)}" ]]; then
exit 0
fi
if [[ "$#" -eq "0" ]]; then
echo "no arguments passed"
exit 1
fi
FOUND=$(printf "%s\n%s" "$TARGETS" "$MOUNTED" | sort | uniq -c | awk '$1 == "2" {print $2}' | sort)
MISSING=$(printf "%s\n%s" "$FOUND" "$TARGETS" | sort | uniq -u | sort)
TARGETS=$(echo "$@" | sort | uniq)
MOUNTED=$(zfs list -o mountpoint,mounted -H | awk '$2 == "yes" {print $1}' | sort | uniq)
NUM_MATCHED=$(echo "$MOUNTED" | grep -Ec "$(echo "$@" | tr ' ' '\|')") # does not properly handle paths with strings
if [[ "$NUM_MATCHED" -eq "$#" ]]; then
exit 0
fi
echo "FAILURE, missing: $MISSING" 1>&2
exit 1
'';
};
FOUND=$(printf "%s\n%s" "$TARGETS" "$MOUNTED" | sort | uniq -c | awk '$1 == "2" {print $2}' | sort)
MISSING=$(printf "%s\n%s" "$FOUND" "$TARGETS" | sort | uniq -u | sort)
echo "FAILURE, missing: $MISSING" 1>&2
exit 1
'';
};
serviceMountDeps = serviceName: dirs: {
systemd.services."${serviceName}_mounts" = {
@@ -46,7 +50,7 @@ inputs.nixpkgs.lib.extend (
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = lib.getExe (final.ensureZfsMounts dirs);
ExecStart = "${lib.getExe final.ensureZfsMounts} ${lib.strings.concatStringsSep " " dirs}";
};
};