add LAN prefer to nfs

This commit is contained in:
Yan Lin 2025-10-25 15:35:16 +02:00
parent 73b137dc84
commit 475ab0df9b
4 changed files with 25 additions and 11 deletions

View file

@ -12,7 +12,7 @@ in
remoteHost = mkOption {
type = types.str;
description = "Remote NFS server hostname or IP";
description = "Primary remote NFS server hostname or IP";
};
remotePath = mkOption {
@ -24,15 +24,28 @@ in
type = types.str;
description = "Local mount point";
};
replicas = mkOption {
type = types.listOf types.str;
default = [];
description = "Replica server hostnames or IPs for failover (in order of preference)";
};
};
config = mkIf cfg.enable {
services.autofs = {
enable = true;
timeout = 300;
autoMaster = ''
${cfg.mountPoint} -fstype=nfs4,rw,soft,intr,noatime ${cfg.remoteHost}:${cfg.remotePath}
'';
autoMaster =
let
# Build server list: primary host followed by replicas
allHosts = [ cfg.remoteHost ] ++ cfg.replicas;
# Format as "host1:/path host2:/path host3:/path"
locations = concatStringsSep " " (map (host: "${host}:${cfg.remotePath}") allHosts);
in
''
${cfg.mountPoint} -fstype=nfs4,rw,soft,intr,noatime ${locations}
'';
};
systemd.tmpfiles.rules = [

View file

@ -15,10 +15,10 @@ in
description = "Path to export via NFS";
};
allowedNetwork = mkOption {
type = types.str;
default = "10.2.2.0/24";
description = "Network allowed to access the export (CIDR)";
allowedNetworks = mkOption {
type = types.listOf types.str;
default = [ "10.2.2.0/24" ];
description = "Networks allowed to access the export (CIDR)";
};
};
@ -26,7 +26,7 @@ in
services.nfs.server = {
enable = true;
exports = ''
${cfg.exportPath} ${cfg.allowedNetwork}(rw,sync,no_subtree_check,no_root_squash)
${cfg.exportPath} ${concatStringsSep " " (map (net: "${net}(rw,sync,no_subtree_check,no_root_squash)") cfg.allowedNetworks)}
'';
};
};