aboutsummaryrefslogtreecommitdiff
path: root/nixos/serverModules/nfs.nix
diff options
context:
space:
mode:
authorspl3g <spleefer6@yandex.ru>2025-08-22 22:24:50 +0500
committerspl3g <spleefer6@yandex.ru>2025-08-22 22:38:35 +0500
commitc087d476f03b9e94a879ab1fa752ffe90de3e7f9 (patch)
tree1b41316e76715648442654bd97b0f7cbca9714b5 /nixos/serverModules/nfs.nix
parentc7117141d2f42f5be5006d07e6d6476238fb96e6 (diff)
feat: add server modules
Diffstat (limited to 'nixos/serverModules/nfs.nix')
-rw-r--r--nixos/serverModules/nfs.nix113
1 files changed, 113 insertions, 0 deletions
diff --git a/nixos/serverModules/nfs.nix b/nixos/serverModules/nfs.nix
new file mode 100644
index 0000000..5a88697
--- /dev/null
+++ b/nixos/serverModules/nfs.nix
@@ -0,0 +1,113 @@
+{ config, lib, ... }:
+with lib;
+let
+ cfg = config.nfs;
+in
+{
+ options = {
+ nfs = {
+ server = mkOption {
+ description = ''
+ NFS server configuration.
+ '';
+ type = types.submodule {
+ options = {
+ enable = mkEnableOption "Enable nfs server";
+ exportsPath = mkOption {
+ type = types.str;
+ default = "/export";
+ description = ''
+ A path to the dir, where exports will be binded.
+ '';
+ };
+
+ defaultExportIps = mkOption {
+ type = types.listOf (types.str);
+ description = ''
+ A list of ip addresses, that will be used as default in exportDirs
+ '';
+ };
+
+ defaultExportParams = mkOption {
+ type = types.str;
+ default = "rw,nohide,insecure,no_subtree_check";
+ description = ''
+ Params, that will be used as default in exportDirs
+ '';
+ };
+
+ exportDirs = mkOption {
+ description = ''
+ A list of directories to export.
+ '';
+ type = types.listOf (types.submodule {
+ options = {
+ path = mkOption {
+ type = types.str;
+ description = ''
+ A path to the directory to export.
+ '';
+ };
+ exportPath = mkOption {
+ type = types.str;
+ default = "";
+ description = ''
+ A path that will be binded to the export directory in the exportsPath.
+ '';
+ };
+ ips = mkOption {
+ type = types.listOf (types.str);
+ default = cfg.server.defaultExportIps;
+ description = ''
+ A list of ip addresses to export the dir to.
+ '';
+ };
+ params = mkOption {
+ type = types.str;
+ default = cfg.server.defaultExportParams;
+ description = ''
+ Params for the ip addresses.
+ '';
+ };
+ };
+ });
+ };
+ };
+ };
+ };
+ };
+ };
+
+ config = mkIf cfg.server.enable {
+ services.nfs.server = {
+ enable = true;
+ exports = "${cfg.server.exportsPath} ${concatMapStrings (ip: "${ip}(rw,fsid=0,no_subtree_check) ") cfg.server.defaultExportIps}\n"
+ + concatMapStrings
+ (dir:
+ let
+ ips = concatMapStrings (ip: "${ip}(${dir.params}) ") dir.ips;
+ exportPath = if dir.exportPath != "" then dir.exportPath else
+ baseNameOf dir.path;
+ in "${cfg.server.exportsPath}/${exportPath} ${ips}\n")
+ cfg.server.exportDirs;
+ };
+
+ systemd.tmpfiles.rules = [
+ "d ${cfg.server.exportsPath} 0744 nobody nogroup"
+ ];
+
+ fileSystems = listToAttrs (map (exportDir:
+ let
+ exportPath = if exportDir.exportPath != "" then exportDir.exportPath else
+ baseNameOf exportDir.path;
+ fullExportPath = "${cfg.server.exportsPath}/${exportPath}";
+ in
+ {
+ name = fullExportPath;
+ value = {
+ device = exportDir.path;
+ options = ["bind"];
+ };
+ }) cfg.server.exportDirs);
+ };
+}