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.exports.source = exports; 92 93 boot.kernelModules = [ "nfsd" ]; 94 95 systemd.services.nfsd = 96 { description = "NFS Server"; 97 98 wantedBy = [ "multi-user.target" ]; 99 100 requires = [ "rpcbind.service" "mountd.service" ]; 101 after = [ "rpcbind.service" "mountd.service" "idmapd.service" ]; 102 before = [ "statd.service" ]; 103 104 path = [ pkgs.nfs-utils ]; 105 106 script = 107 '' 108 # Create a state directory required by NFSv4. 109 mkdir -p /var/lib/nfs/v4recovery 110 111 ${pkgs.procps}/sbin/sysctl -w fs.nfs.nlm_tcpport=${builtins.toString cfg.lockdPort} 112 ${pkgs.procps}/sbin/sysctl -w fs.nfs.nlm_udpport=${builtins.toString cfg.lockdPort} 113 114 rpc.nfsd \ 115 ${if cfg.hostName != null then "-H ${cfg.hostName}" else ""} \ 116 ${builtins.toString cfg.nproc} 117 ''; 118 119 postStop = "rpc.nfsd 0"; 120 121 serviceConfig.Type = "oneshot"; 122 serviceConfig.RemainAfterExit = true; 123 }; 124 125 systemd.services.mountd = 126 { description = "NFSv3 Mount Daemon"; 127 128 requires = [ "rpcbind.service" ]; 129 after = [ "rpcbind.service" ]; 130 131 path = [ pkgs.nfs-utils pkgs.sysvtools pkgs.utillinux ]; 132 133 preStart = 134 '' 135 mkdir -p /var/lib/nfs 136 touch /var/lib/nfs/rmtab 137 138 mountpoint -q /proc/fs/nfsd || mount -t nfsd none /proc/fs/nfsd 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 exportfs -rav 151 ''; 152 153 restartTriggers = [ exports ]; 154 155 serviceConfig.Type = "forking"; 156 serviceConfig.ExecStart = '' 157 @${pkgs.nfs-utils}/sbin/rpc.mountd rpc.mountd \ 158 ${if cfg.mountdPort != null then "-p ${toString cfg.mountdPort}" else ""} 159 ''; 160 serviceConfig.Restart = "always"; 161 }; 162 163 }; 164 165}