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 15 ###### interface 16 17 options = { 18 19 services.nfs = { 20 21 server = { 22 enable = mkOption { 23 default = false; 24 description = '' 25 Whether to enable the kernel's NFS server. 26 ''; 27 }; 28 29 exports = mkOption { 30 default = ""; 31 description = '' 32 Contents of the /etc/exports file. See 33 <citerefentry><refentrytitle>exports</refentrytitle> 34 <manvolnum>5</manvolnum></citerefentry> for the format. 35 ''; 36 }; 37 38 hostName = mkOption { 39 default = null; 40 description = '' 41 Hostname or address on which NFS requests will be accepted. 42 Default is all. See the <option>-H</option> option in 43 <citerefentry><refentrytitle>nfsd</refentrytitle> 44 <manvolnum>8</manvolnum></citerefentry>. 45 ''; 46 }; 47 48 nproc = mkOption { 49 default = 8; 50 description = '' 51 Number of NFS server threads. Defaults to the recommended value of 8. 52 ''; 53 }; 54 55 createMountPoints = mkOption { 56 default = false; 57 description = "Whether to create the mount points in the exports file at startup time."; 58 }; 59 60 mountdPort = mkOption { 61 default = null; 62 example = 4002; 63 description = '' 64 Use fixed port for rpc.mountd, useful if server is behind firewall. 65 ''; 66 }; 67 68 lockdPort = mkOption { 69 default = 0; 70 description = '' 71 Fix the lockd port number. This can help setting firewall rules for NFS. 72 ''; 73 }; 74 }; 75 76 }; 77 78 }; 79 80 81 ###### implementation 82 83 config = mkIf cfg.enable { 84 85 services.rpcbind.enable = true; 86 87 boot.supportedFilesystems = [ "nfs" ]; # needed for statd and idmapd 88 89 environment.systemPackages = [ pkgs.nfs-utils ]; 90 91 environment.etc = singleton 92 { source = exports; 93 target = "exports"; 94 }; 95 96 boot.kernelModules = [ "nfsd" ]; 97 98 systemd.services.nfsd = 99 { description = "NFS Server"; 100 101 wantedBy = [ "multi-user.target" ]; 102 103 requires = [ "rpcbind.service" "mountd.service" ]; 104 after = [ "rpcbind.service" "mountd.service" "idmapd.service" ]; 105 before = [ "statd.service" ]; 106 107 path = [ pkgs.nfs-utils ]; 108 109 script = 110 '' 111 # Create a state directory required by NFSv4. 112 mkdir -p /var/lib/nfs/v4recovery 113 114 ${pkgs.procps}/sbin/sysctl -w fs.nfs.nlm_tcpport=${builtins.toString cfg.lockdPort} 115 ${pkgs.procps}/sbin/sysctl -w fs.nfs.nlm_udpport=${builtins.toString cfg.lockdPort} 116 117 rpc.nfsd \ 118 ${if cfg.hostName != null then "-H ${cfg.hostName}" else ""} \ 119 ${builtins.toString cfg.nproc} 120 ''; 121 122 postStop = "rpc.nfsd 0"; 123 124 serviceConfig.Type = "oneshot"; 125 serviceConfig.RemainAfterExit = true; 126 }; 127 128 systemd.services.mountd = 129 { description = "NFSv3 Mount Daemon"; 130 131 requires = [ "rpcbind.service" ]; 132 after = [ "rpcbind.service" ]; 133 134 path = [ pkgs.nfs-utils pkgs.sysvtools pkgs.utillinux ]; 135 136 preStart = 137 '' 138 mkdir -p /var/lib/nfs 139 touch /var/lib/nfs/rmtab 140 141 mountpoint -q /proc/fs/nfsd || mount -t nfsd none /proc/fs/nfsd 142 143 ${optionalString cfg.createMountPoints 144 '' 145 # create export directories: 146 # skip comments, take first col which may either be a quoted 147 # "foo bar" or just foo (-> man export) 148 sed '/^#.*/d;s/^"\([^"]*\)".*/\1/;t;s/[ ].*//' ${exports} \ 149 | xargs -d '\n' mkdir -p 150 '' 151 } 152 153 exportfs -rav 154 ''; 155 156 restartTriggers = [ exports ]; 157 158 serviceConfig.Type = "forking"; 159 serviceConfig.ExecStart = '' 160 @${pkgs.nfs-utils}/sbin/rpc.mountd rpc.mountd \ 161 ${if cfg.mountdPort != null then "-p ${toString cfg.mountdPort}" else ""} 162 ''; 163 serviceConfig.Restart = "always"; 164 }; 165 166 }; 167 168}