move ensureZfsMounts

This commit is contained in:
2025-08-11 16:18:10 -07:00
parent 80df89e9a1
commit 30421d96f0
5 changed files with 68 additions and 50 deletions

63
lib.nix
View File

@@ -9,58 +9,29 @@ inputs.nixpkgs.lib.extend (
lib = prev;
in
{
# 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 -x
serviceMountDeps =
serviceName: dirs:
{ pkgs, ... }:
{
systemd.services."${serviceName}_mounts" = {
wants = [ "zfs.target" ];
before = [ "${serviceName}.service" ];
if [[ "$#" -eq "0" ]]; then
echo "no arguments passed"
exit 1
fi
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${lib.getExe pkgs.ensureZfsMounts} ${lib.strings.concatStringsSep " " dirs}";
};
};
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
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" = {
wants = [ "zfs.target" ];
before = [ "${serviceName}.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${lib.getExe final.ensureZfsMounts} ${lib.strings.concatStringsSep " " dirs}";
systemd.services.${serviceName} = {
wants = [ "${serviceName}_mounts.service" ];
after = [ "${serviceName}_mounts.service" ];
requires = [ "${serviceName}_mounts.service" ];
};
};
systemd.services.${serviceName} = {
wants = [ "${serviceName}_mounts.service" ];
after = [ "${serviceName}_mounts.service" ];
requires = [ "${serviceName}_mounts.service" ];
};
};
# stolen from: https://stackoverflow.com/a/42398526
optimizeWithFlags =
pkg: flags: