From 51d6cd005c6d1305a5b69ce1c613abd48013ff3b Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 23 Aug 2023 17:02:22 +0200 Subject: [PATCH] modules/external-storage: add strictMounts to systemd service options --- modules/external-storage/default.nix | 4 +++ modules/external-storage/filesystem-type.nix | 4 +++ modules/external-storage/strict-mounts.nix | 27 ++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 modules/external-storage/strict-mounts.nix diff --git a/modules/external-storage/default.nix b/modules/external-storage/default.nix index baa2cdf..ec23ff3 100644 --- a/modules/external-storage/default.nix +++ b/modules/external-storage/default.nix @@ -13,6 +13,10 @@ let in { + imports = [ + ./strict-mounts.nix + ]; + options = { services.external-storage = { fileSystems = lib.mkOption { diff --git a/modules/external-storage/filesystem-type.nix b/modules/external-storage/filesystem-type.nix index e35a68f..af401e5 100644 --- a/modules/external-storage/filesystem-type.nix +++ b/modules/external-storage/filesystem-type.nix @@ -26,5 +26,9 @@ with lib; type = types.str; default = "default"; }; + dependentServices = mkOption { + type = with types; listOf str; + default = []; + }; }; } diff --git a/modules/external-storage/strict-mounts.nix b/modules/external-storage/strict-mounts.nix new file mode 100644 index 0000000..b304ac0 --- /dev/null +++ b/modules/external-storage/strict-mounts.nix @@ -0,0 +1,27 @@ +{ config, lib, ... }: + +let + cfg = config.services.external-storage; +in + +with lib; +{ + options.systemd.services = mkOption { + type = with types; attrsOf (submodule ({ config, ... }: { + options.strictMounts = mkOption { + type = with types; listOf path; + default = []; + }; + config = mkIf (config.strictMounts != []) (let + findFilesystemsFor = mount: pipe cfg.fileSystems [ + (filterAttrs (_: fs: hasPrefix "${fs.mountpoint}/" "${mount}/")) + (mapAttrsToList (_: fs: "${fs.unitName}.service")) + ]; + services = flatten (map findFilesystemsFor config.strictMounts); + in { + after = services; + bindsTo = services; + }); + })); + }; +}