aboutsummaryrefslogtreecommitdiff
path: root/nixos/serverModules/nfs.nix
blob: f8186b6de43072556d0cbf0530aa776d223a948e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{ config, lib, ... }:
with lib;
let
  cfg = config.nfs;
in
{
  options = {
    nfs.server = mkOption {
      description = ''
          NFS server configuration.
        '';
      default = { enable = false; };
      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);
  };
}