1 { config, lib, pkgs, ... }:
7 cfg = config.services.nfs.server;
9 exports = pkgs.writeText "exports" cfg.exports;
15 (mkRenamedOptionModule [ "services" "nfs" "lockdPort" ] [ "services" "nfs" "server" "lockdPort" ])
16 (mkRenamedOptionModule [ "services" "nfs" "statdPort" ] [ "services" "nfs" "server" "statdPort" ])
29 description = lib.mdDoc ''
30 Whether to enable the kernel's NFS server.
34 extraNfsdConfig = mkOption {
37 description = lib.mdDoc ''
38 Extra configuration options for the [nfsd] section of /etc/nfs.conf.
45 description = lib.mdDoc ''
46 Contents of the /etc/exports file. See
47 {manpage}`exports(5)` for the format.
52 type = types.nullOr types.str;
54 description = lib.mdDoc ''
55 Hostname or address on which NFS requests will be accepted.
56 Default is all. See the {option}`-H` option in
64 description = lib.mdDoc ''
65 Number of NFS server threads. Defaults to the recommended value of 8.
69 createMountPoints = mkOption {
72 description = lib.mdDoc "Whether to create the mount points in the exports file at startup time.";
75 mountdPort = mkOption {
76 type = types.nullOr types.int;
79 description = lib.mdDoc ''
80 Use fixed port for rpc.mountd, useful if server is behind firewall.
84 lockdPort = mkOption {
85 type = types.nullOr types.int;
88 description = lib.mdDoc ''
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.
95 statdPort = mkOption {
96 type = types.nullOr types.int;
99 description = lib.mdDoc ''
100 Use a fixed port for {command}`rpc.statd`. This is
101 useful if the NFS server is behind a firewall.
112 ###### implementation
114 config = mkIf cfg.enable {
116 services.nfs.extraConfig = ''
118 threads=${toString cfg.nproc}
119 ${optionalString (cfg.hostName != null) "host=${cfg.hostName}"}
120 ${cfg.extraNfsdConfig}
123 ${optionalString (cfg.mountdPort != null) "port=${toString cfg.mountdPort}"}
126 ${optionalString (cfg.statdPort != null) "port=${toString cfg.statdPort}"}
129 ${optionalString (cfg.lockdPort != null) ''
130 port=${toString cfg.lockdPort}
131 udp-port=${toString cfg.lockdPort}
135 services.rpcbind.enable = true;
137 boot.supportedFilesystems = [ "nfs" ]; # needed for statd and idmapd
139 environment.etc.exports.source = exports;
141 systemd.services.nfs-server =
143 wantedBy = [ "multi-user.target" ];
147 mkdir -p /var/lib/nfs/v4recovery
151 systemd.services.nfs-mountd =
153 restartTriggers = [ exports ];
157 mkdir -p /var/lib/nfs
159 ${optionalString cfg.createMountPoints
161 # create export directories:
162 # skip comments, take first col which may either be a quoted
163 # "foo bar" or just foo (-> man export)
164 sed '/^#.*/d;s/^"\([^"]*\)".*/\1/;t;s/[ ].*//' ${exports} \
165 | xargs -d '\n' mkdir -p