liminix/modules/mount/service.nix

32 lines
896 B
Nix
Raw Normal View History

2023-09-04 22:17:52 +02:00
{
liminix
, lib
}:
{ device, mountpoint, options, fstype }:
let
inherit (liminix.services) longrun oneshot;
options_string =
if options == [] then "" else "-o ${lib.concatStringsSep "," options}";
mount_service = oneshot {
name = "mount.${lib.escapeURL mountpoint}";
timeout-up = 3600;
up = "mount -t ${fstype} ${options_string} ${device} ${mountpoint}";
down = "umount ${mountpoint}";
};
in longrun {
name = "watch-mount.${lib.strings.sanitizeDerivationName mountpoint}";
isTrigger = true;
buildInputs = [ mount_service ];
# This accommodates bringing the service up when the device appears.
# It doesn't bring it down on unplug because unmount will probably
# fail anyway (so don't do that)
run = ''
2023-09-04 22:17:52 +02:00
while ! findfs ${device}; do
echo waiting for device ${device}
sleep 1
done
s6-rc -b -u change ${mount_service.name}
2023-09-04 22:17:52 +02:00
'';
}