From 3bdc986dd7e5e721089ab1a49981db52e3387272 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Mon, 4 Sep 2023 21:17:52 +0100 Subject: [PATCH] extract "mount filesystem" to module --- THOUGHTS.txt | 4 ++-- examples/arhcive.nix | 20 +++++----------- modules/mount/default.nix | 49 +++++++++++++++++++++++++++++++++++++++ modules/mount/service.nix | 18 ++++++++++++++ 4 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 modules/mount/default.nix create mode 100644 modules/mount/service.nix diff --git a/THOUGHTS.txt b/THOUGHTS.txt index 85a46b4b..c6c9f8cb 100644 --- a/THOUGHTS.txt +++ b/THOUGHTS.txt @@ -2141,8 +2141,8 @@ Thu Aug 31 23:53:54 BST 2023 - packet forwarding - and arhcive - rsync - - watchdog - - mount + - [done] watchdog + - [done] mount - nftables syntax error - tidy up the dependency handling in serviceDefn build (interface is fine, implementation is a bit brutal) diff --git a/examples/arhcive.nix b/examples/arhcive.nix index b5430a74..506b8bb8 100644 --- a/examples/arhcive.nix +++ b/examples/arhcive.nix @@ -30,6 +30,7 @@ in rec { ../modules/vlan ../modules/ssh ../modules/watchdog + ../modules/mount ]; hostname = "arhcive"; @@ -101,27 +102,18 @@ in rec { }; programs.busybox = { - applets = ["blkid" "lsusb" "findfs" "tar"]; + applets = ["lsusb" "tar"]; options = { FEATURE_LS_TIMESTAMPS = "y"; FEATURE_LS_SORTFILES = "y"; - FEATURE_BLKID_TYPE = "y"; - FEATURE_MOUNT_FLAGS = "y"; - FEATURE_MOUNT_LABEL = "y"; FEATURE_VOLUMEID_EXT = "y"; }; }; - services.mount_external_disk = oneshot { - name = "mount_external_disk"; - up = '' - while ! findfs LABEL=backup-disk; do - echo waiting for backup-disk - sleep 1 - done - mount -t ext4 LABEL=backup-disk /srv - ''; - down = "umount /srv"; + services.mount_external_disk = svc.mount.build { + device = "LABEL=backup-disk"; + mountpoint = "/srv"; + fstype = "ext4"; }; services.rsync = diff --git a/modules/mount/default.nix b/modules/mount/default.nix new file mode 100644 index 00000000..31d5c450 --- /dev/null +++ b/modules/mount/default.nix @@ -0,0 +1,49 @@ + +{ lib, pkgs, config, ...}: +let + inherit (lib) mkOption types; + inherit (pkgs) liminix; + mkBoolOption = description : mkOption { + type = types.bool; + inherit description; + default = true; + }; + +in { + options = { + system.service.mount = mkOption { + type = liminix.lib.types.serviceDefn; + }; + }; + config.system.service = { + mount = liminix.callService ./service.nix { + device = mkOption { + type = types.str; + example = "/dev/sda1"; + }; + mountpoint = mkOption { + type = types.str; + example = "/mnt/media"; + }; + options = mkOption { + type = types.listOf types.str; + default = []; + example = ["noatime" "ro" "sync"]; + }; + fstype = mkOption { + type = types.str; + default = "auto"; + example = "vfat"; + }; + }; + }; + config.programs.busybox = { + applets = ["blkid" "findfs"]; + options = { + FEATURE_BLKID_TYPE = "y"; + FEATURE_MOUNT_FLAGS = "y"; + FEATURE_MOUNT_LABEL = "y"; + FEATURE_VOLUMEID_EXT = "y"; + }; + }; +} diff --git a/modules/mount/service.nix b/modules/mount/service.nix new file mode 100644 index 00000000..67fa7682 --- /dev/null +++ b/modules/mount/service.nix @@ -0,0 +1,18 @@ +{ + liminix +, lib +}: +{ device, mountpoint, options, fstype }: +let + inherit (liminix.services) oneshot; +in oneshot { + name = "mount.${lib.escapeURL mountpoint}"; + up = '' + while ! findfs ${device}; do + echo waiting for device ${device} + sleep 1 + done + mount -t ${fstype} -o ${lib.concatStringsSep "," options} ${device} ${mountpoint} + ''; + down = "umount ${mountpoint}"; +}