1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.nfs.server;
8
9 exports = pkgs.writeText "exports" cfg.exports;
10
11in
12
13{
14 imports = [
15 (mkRenamedOptionModule [ "services" "nfs" "lockdPort" ] [ "services" "nfs" "server" "lockdPort" ])
16 (mkRenamedOptionModule [ "services" "nfs" "statdPort" ] [ "services" "nfs" "server" "statdPort" ])
17 ];
18
19 ###### interface
20
21 options = {
22
23 services.nfs = {
24
25 server = {
26 enable = mkOption {
27 type = types.bool;
28 default = false;
29 description = ''
30 Whether to enable the kernel's NFS server.
31 '';
32 };
33
34 extraNfsdConfig = mkOption {
35 type = types.str;
36 default = "";
37 description = ''
38 Extra configuration options for the [nfsd] section of /etc/nfs.conf.
39 '';
40 };
41
42 exports = mkOption {
43 type = types.lines;
44 default = "";
45 description = ''
46 Contents of the /etc/exports file. See
47 {manpage}`exports(5)` for the format.
48 '';
49 };
50
51 hostName = mkOption {
52 type = types.nullOr types.str;
53 default = null;
54 description = ''
55 Hostname or address on which NFS requests will be accepted.
56 Default is all. See the {option}`-H` option in
57 {manpage}`nfsd(8)`.
58 '';
59 };
60
61 nproc = mkOption {
62 type = types.int;
63 default = 8;
64 description = ''
65 Number of NFS server threads. Defaults to the recommended value of 8.
66 '';
67 };
68
69 createMountPoints = mkOption {
70 type = types.bool;
71 default = false;
72 description = "Whether to create the mount points in the exports file at startup time.";
73 };
74
75 mountdPort = mkOption {
76 type = types.nullOr types.int;
77 default = null;
78 example = 4002;
79 description = ''
80 Use fixed port for rpc.mountd, useful if server is behind firewall.
81 '';
82 };
83
84 lockdPort = mkOption {
85 type = types.nullOr types.int;
86 default = null;
87 example = 4001;
88 description = ''
89 Use a fixed port for the NFS lock manager kernel module
90 (`lockd/nlockmgr`). This is useful if the
91 NFS server is behind a firewall.
92 '';
93 };
94
95 statdPort = mkOption {
96 type = types.nullOr types.int;
97 default = null;
98 example = 4000;
99 description = ''
100 Use a fixed port for {command}`rpc.statd`. This is
101 useful if the NFS server is behind a firewall.
102 '';
103 };
104
105 };
106
107 };
108
109 };
110
111
112 ###### implementation
113
114 config = mkIf cfg.enable {
115
116 services.rpcbind.enable = true;
117
118 boot.supportedFilesystems = [ "nfs" ]; # needed for statd and idmapd
119
120 environment.etc.exports.source = exports;
121
122 systemd.services.nfs-server =
123 { enable = true;
124 wantedBy = [ "multi-user.target" ];
125
126 preStart =
127 ''
128 mkdir -p /var/lib/nfs/v4recovery
129 '';
130 };
131
132 systemd.services.nfs-mountd =
133 { enable = true;
134 restartTriggers = [ exports ];
135
136 preStart =
137 ''
138 mkdir -p /var/lib/nfs
139
140 ${optionalString cfg.createMountPoints
141 ''
142 # create export directories:
143 # skip comments, take first col which may either be a quoted
144 # "foo bar" or just foo (-> man export)
145 sed '/^#.*/d;s/^"\([^"]*\)".*/\1/;t;s/[ ].*//' ${exports} \
146 | xargs -d '\n' mkdir -p
147 ''
148 }
149 '';
150 };
151
152 };
153
154}