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 = lib.mdDoc '' 30 Whether to enable the kernel's NFS server. 31 ''; 32 }; 33 34 extraNfsdConfig = mkOption { 35 type = types.str; 36 default = ""; 37 description = lib.mdDoc '' 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 = lib.mdDoc '' 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 = lib.mdDoc '' 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 = lib.mdDoc '' 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 = lib.mdDoc "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 = lib.mdDoc '' 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 = 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. 92 ''; 93 }; 94 95 statdPort = mkOption { 96 type = types.nullOr types.int; 97 default = null; 98 example = 4000; 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. 102 ''; 103 }; 104 105 }; 106 107 }; 108 109 }; 110 111 112 ###### implementation 113 114 config = mkIf cfg.enable { 115 116 services.nfs.extraConfig = '' 117 [nfsd] 118 threads=${toString cfg.nproc} 119 ${optionalString (cfg.hostName != null) "host=${cfg.hostName}"} 120 ${cfg.extraNfsdConfig} 121 122 [mountd] 123 ${optionalString (cfg.mountdPort != null) "port=${toString cfg.mountdPort}"} 124 125 [statd] 126 ${optionalString (cfg.statdPort != null) "port=${toString cfg.statdPort}"} 127 128 [lockd] 129 ${optionalString (cfg.lockdPort != null) '' 130 port=${toString cfg.lockdPort} 131 udp-port=${toString cfg.lockdPort} 132 ''} 133 ''; 134 135 services.rpcbind.enable = true; 136 137 boot.supportedFilesystems = [ "nfs" ]; # needed for statd and idmapd 138 139 environment.etc.exports.source = exports; 140 141 systemd.services.nfs-server = 142 { enable = true; 143 wantedBy = [ "multi-user.target" ]; 144 145 preStart = 146 '' 147 mkdir -p /var/lib/nfs/v4recovery 148 ''; 149 }; 150 151 systemd.services.nfs-mountd = 152 { enable = true; 153 restartTriggers = [ exports ]; 154 155 preStart = 156 '' 157 mkdir -p /var/lib/nfs 158 159 ${optionalString cfg.createMountPoints 160 '' 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 166 '' 167 } 168 ''; 169 }; 170 171 }; 172 173}