move ensureZfsMounts

This commit is contained in:
Simon Gardling 2025-08-11 16:18:10 -07:00
parent 80df89e9a1
commit 30421d96f0
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
5 changed files with 68 additions and 50 deletions

View File

@ -189,7 +189,10 @@
# get nix-minecraft working!
nix-minecraft.nixosModules.minecraft-servers
{
nixpkgs.overlays = [ nix-minecraft.overlay ];
nixpkgs.overlays = [
nix-minecraft.overlay
(import ./overlays.nix)
];
}
lanzaboote.nixosModules.lanzaboote

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:

33
overlays.nix Normal file
View File

@ -0,0 +1,33 @@
final: prev: {
ensureZfsMounts = prev.writeShellApplication {
name = "zfsEnsureMounted";
runtimeInputs = with prev; [
zfs
gnugrep
gawk
coreutils
];
text = ''
#!/bin/sh -x
if [[ "$#" -eq "0" ]]; then
echo "no arguments passed"
exit 1
fi
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
'';
};
}

View File

@ -10,7 +10,10 @@ let
testPkgs = import inputs.nixpkgs {
system = pkgs.system;
config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "minecraft-server" ];
overlays = [ inputs.nix-minecraft.overlay ];
overlays = [
inputs.nix-minecraft.overlay
(import ../overlays.nix)
];
};
# Create a wrapper module that imports the actual minecraft service

View File

@ -2,9 +2,17 @@
config,
lib,
pkgs,
inputs,
...
}:
pkgs.testers.runNixOSTest {
let
# Create pkgs with ensureZfsMounts overlay
testPkgs = import inputs.nixpkgs {
system = pkgs.system;
overlays = [ (import ../overlays.nix) ];
};
in
testPkgs.testers.runNixOSTest {
name = "zfs folder dependency and mounting test";
nodes.machine =
@ -35,7 +43,7 @@ pkgs.testers.runNixOSTest {
environment.systemPackages = [
pkgs.parted
lib.ensureZfsMounts
pkgs.ensureZfsMounts
];
systemd.services.foobar = {