| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | with lib; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cfg = config.services.nfs.server; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   exports = pkgs.writeText "exports" cfg.exports; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | in | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### interface | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   options = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     services.nfs = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       server = { | 
					
						
							|  |  |  |         enable = mkOption { | 
					
						
							|  |  |  |           default = false; | 
					
						
							|  |  |  |           description = ''
 | 
					
						
							|  |  |  |             Whether to enable the kernel's NFS server. | 
					
						
							|  |  |  |           '';
 | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         exports = mkOption { | 
					
						
							|  |  |  |           default = ""; | 
					
						
							|  |  |  |           description = ''
 | 
					
						
							|  |  |  |             Contents of the /etc/exports file.  See | 
					
						
							|  |  |  |             <citerefentry><refentrytitle>exports</refentrytitle> | 
					
						
							|  |  |  |             <manvolnum>5</manvolnum></citerefentry> for the format. | 
					
						
							|  |  |  |           '';
 | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         hostName = mkOption { | 
					
						
							|  |  |  |           default = null; | 
					
						
							|  |  |  |           description = ''
 | 
					
						
							|  |  |  |             Hostname or address on which NFS requests will be accepted. | 
					
						
							|  |  |  |             Default is all.  See the <option>-H</option> option in | 
					
						
							|  |  |  |             <citerefentry><refentrytitle>nfsd</refentrytitle> | 
					
						
							|  |  |  |             <manvolnum>8</manvolnum></citerefentry>. | 
					
						
							|  |  |  |           '';
 | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         nproc = mkOption { | 
					
						
							|  |  |  |           default = 8; | 
					
						
							|  |  |  |           description = ''
 | 
					
						
							|  |  |  |             Number of NFS server threads.  Defaults to the recommended value of 8. | 
					
						
							|  |  |  |           '';
 | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         createMountPoints = mkOption { | 
					
						
							|  |  |  |           default = false; | 
					
						
							|  |  |  |           description = "Whether to create the mount points in the exports file at startup time."; | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2014-07-30 23:47:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         mountdPort = mkOption { | 
					
						
							|  |  |  |           default = null; | 
					
						
							|  |  |  |           example = 4002; | 
					
						
							|  |  |  |           description = ''
 | 
					
						
							| 
									
										
										
										
											2014-12-30 03:31:03 +01:00
										 |  |  |             Use fixed port for rpc.mountd, useful if server is behind firewall. | 
					
						
							| 
									
										
										
										
											2014-07-30 23:47:52 +02:00
										 |  |  |           '';
 | 
					
						
							| 
									
										
										
										
											2014-10-15 12:00:20 +02:00
										 |  |  |         }; | 
					
						
							| 
									
										
										
										
											2014-10-15 10:56:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         lockdPort = mkOption { | 
					
						
							|  |  |  |           default = 0; | 
					
						
							|  |  |  |           description = ''
 | 
					
						
							|  |  |  |             Fix the lockd port number. This can help setting firewall rules for NFS. | 
					
						
							|  |  |  |           '';
 | 
					
						
							| 
									
										
										
										
											2014-07-30 23:47:52 +02:00
										 |  |  |         }; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### implementation | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   config = mkIf cfg.enable { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-21 20:37:37 +00:00
										 |  |  |     services.rpcbind.enable = true; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-22 12:24:23 +00:00
										 |  |  |     boot.supportedFilesystems = [ "nfs" ]; # needed for statd and idmapd | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-06 16:40:57 +01:00
										 |  |  |     environment.systemPackages = [ pkgs.nfs-utils ]; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     environment.etc = singleton | 
					
						
							|  |  |  |       { source = exports; | 
					
						
							|  |  |  |         target = "exports"; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     boot.kernelModules = [ "nfsd" ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-16 12:33:18 +01:00
										 |  |  |     systemd.services.nfsd = | 
					
						
							| 
									
										
										
										
											2012-10-24 18:10:58 +02:00
										 |  |  |       { description = "NFS Server"; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-24 18:10:58 +02:00
										 |  |  |         wantedBy = [ "multi-user.target" ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         requires = [ "rpcbind.service" "mountd.service" ]; | 
					
						
							| 
									
										
										
										
											2013-07-16 13:48:52 +02:00
										 |  |  |         after = [ "rpcbind.service" "mountd.service" "idmapd.service" ]; | 
					
						
							|  |  |  |         before = [ "statd.service" ]; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-06 16:40:57 +01:00
										 |  |  |         path = [ pkgs.nfs-utils ]; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-24 18:10:58 +02:00
										 |  |  |         script = | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  |           ''
 | 
					
						
							|  |  |  |             # Create a state directory required by NFSv4. | 
					
						
							|  |  |  |             mkdir -p /var/lib/nfs/v4recovery | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-15 10:56:41 +02:00
										 |  |  |             ${pkgs.procps}/sbin/sysctl -w fs.nfs.nlm_tcpport=${builtins.toString cfg.lockdPort} | 
					
						
							|  |  |  |             ${pkgs.procps}/sbin/sysctl -w fs.nfs.nlm_udpport=${builtins.toString cfg.lockdPort} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  |             rpc.nfsd \ | 
					
						
							|  |  |  |               ${if cfg.hostName != null then "-H ${cfg.hostName}" else ""} \ | 
					
						
							|  |  |  |               ${builtins.toString cfg.nproc} | 
					
						
							|  |  |  |           '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         postStop = "rpc.nfsd 0"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-24 18:10:58 +02:00
										 |  |  |         serviceConfig.Type = "oneshot"; | 
					
						
							|  |  |  |         serviceConfig.RemainAfterExit = true; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-16 12:33:18 +01:00
										 |  |  |     systemd.services.mountd = | 
					
						
							| 
									
										
										
										
											2012-10-24 18:10:58 +02:00
										 |  |  |       { description = "NFSv3 Mount Daemon"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         requires = [ "rpcbind.service" ]; | 
					
						
							|  |  |  |         after = [ "rpcbind.service" ]; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-06 16:40:57 +01:00
										 |  |  |         path = [ pkgs.nfs-utils pkgs.sysvtools pkgs.utillinux ]; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         preStart = | 
					
						
							|  |  |  |           ''
 | 
					
						
							|  |  |  |             mkdir -p /var/lib/nfs | 
					
						
							|  |  |  |             touch /var/lib/nfs/rmtab | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             mountpoint -q /proc/fs/nfsd || mount -t nfsd none /proc/fs/nfsd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ${optionalString cfg.createMountPoints | 
					
						
							|  |  |  |               ''
 | 
					
						
							|  |  |  |                 # create export directories: | 
					
						
							|  |  |  |                 # skip comments, take first col which may either be a quoted | 
					
						
							|  |  |  |                 # "foo bar" or just foo (-> man export) | 
					
						
							|  |  |  |                 sed '/^#.*/d;s/^"\([^"]*\)".*/\1/;t;s/[ ].*//' ${exports} \ | 
					
						
							|  |  |  |                 | xargs -d '\n' mkdir -p | 
					
						
							|  |  |  |               ''
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-16 13:48:52 +02:00
										 |  |  |             exportfs -rav | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  |           '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-24 18:10:58 +02:00
										 |  |  |         restartTriggers = [ exports ]; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-24 18:10:58 +02:00
										 |  |  |         serviceConfig.Type = "forking"; | 
					
						
							| 
									
										
										
										
											2014-07-30 23:47:52 +02:00
										 |  |  |         serviceConfig.ExecStart = ''
 | 
					
						
							| 
									
										
										
										
											2014-12-06 16:40:57 +01:00
										 |  |  |           @${pkgs.nfs-utils}/sbin/rpc.mountd rpc.mountd \ | 
					
						
							| 
									
										
										
										
											2014-07-30 23:47:52 +02:00
										 |  |  |               ${if cfg.mountdPort != null then "-p ${toString cfg.mountdPort}" else ""} | 
					
						
							|  |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2012-10-24 18:10:58 +02:00
										 |  |  |         serviceConfig.Restart = "always"; | 
					
						
							| 
									
										
										
										
											2012-03-16 20:41:49 +00:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |