| 
									
										
										
										
											2015-08-19 16:05:25 +02:00
										 |  |  | # Configuration for the xfs_quota command | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { config, lib, pkgs, ... }: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | with lib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cfg = config.programs.xfs_quota; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   limitOptions = opts: concatStringsSep " " [ | 
					
						
							|  |  |  |     (optionalString (opts.sizeSoftLimit != null) "bsoft=${opts.sizeSoftLimit}") | 
					
						
							|  |  |  |     (optionalString (opts.sizeHardLimit != null) "bhard=${opts.sizeHardLimit}") | 
					
						
							|  |  |  |   ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | in | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### interface | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   options = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     programs.xfs_quota = { | 
					
						
							|  |  |  |       projects = mkOption { | 
					
						
							| 
									
										
										
										
											2015-08-19 18:03:42 +02:00
										 |  |  |         default = {}; | 
					
						
							| 
									
										
										
										
											2015-08-19 16:05:25 +02:00
										 |  |  |         type = types.attrsOf (types.submodule { | 
					
						
							|  |  |  |           options = { | 
					
						
							|  |  |  |             id = mkOption { | 
					
						
							|  |  |  |               type = types.int; | 
					
						
							|  |  |  |               description = "Project ID."; | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             fileSystem = mkOption { | 
					
						
							| 
									
										
										
										
											2015-08-17 17:52:45 +00:00
										 |  |  |               type = types.str; | 
					
						
							| 
									
										
										
										
											2015-08-19 16:05:25 +02:00
										 |  |  |               description = "XFS filesystem hosting the xfs_quota project."; | 
					
						
							|  |  |  |               default = "/"; | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             path = mkOption { | 
					
						
							| 
									
										
										
										
											2015-08-17 17:52:45 +00:00
										 |  |  |               type = types.str; | 
					
						
							| 
									
										
										
										
											2015-08-19 16:05:25 +02:00
										 |  |  |               description = "Project directory."; | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             sizeSoftLimit = mkOption { | 
					
						
							| 
									
										
										
										
											2015-08-17 17:52:45 +00:00
										 |  |  |               type = types.nullOr types.str; | 
					
						
							| 
									
										
										
										
											2015-08-19 16:05:25 +02:00
										 |  |  |               default = null; | 
					
						
							|  |  |  |               example = "30g"; | 
					
						
							|  |  |  |               description = "Soft limit of the project size"; | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             sizeHardLimit = mkOption { | 
					
						
							| 
									
										
										
										
											2015-08-17 17:52:45 +00:00
										 |  |  |               type = types.nullOr types.str; | 
					
						
							| 
									
										
										
										
											2015-08-19 16:05:25 +02:00
										 |  |  |               default = null; | 
					
						
							|  |  |  |               example = "50g"; | 
					
						
							|  |  |  |               description = "Hard limit of the project size."; | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         description = "Setup of xfs_quota projects. Make sure the filesystem is mounted with the pquota option."; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         example = { | 
					
						
							| 
									
										
										
										
											2019-08-13 21:52:01 +00:00
										 |  |  |           projname = { | 
					
						
							| 
									
										
										
										
											2015-08-19 16:05:25 +02:00
										 |  |  |             id = 50; | 
					
						
							|  |  |  |             path = "/xfsprojects/projname"; | 
					
						
							|  |  |  |             sizeHardLimit = "50g"; | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### implementation | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   config = mkIf (cfg.projects != {}) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     environment.etc.projects.source = pkgs.writeText "etc-project" | 
					
						
							|  |  |  |       (concatStringsSep "\n" (mapAttrsToList | 
					
						
							|  |  |  |         (name: opts: "${toString opts.id}:${opts.path}") cfg.projects)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     environment.etc.projid.source = pkgs.writeText "etc-projid" | 
					
						
							|  |  |  |       (concatStringsSep "\n" (mapAttrsToList | 
					
						
							|  |  |  |         (name: opts: "${name}:${toString opts.id}") cfg.projects)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     systemd.services = mapAttrs' (name: opts: | 
					
						
							|  |  |  |       nameValuePair "xfs_quota-${name}" { | 
					
						
							|  |  |  |         description = "Setup xfs_quota for project ${name}"; | 
					
						
							|  |  |  |         script = ''
 | 
					
						
							| 
									
										
										
										
											2016-02-01 20:46:23 +02:00
										 |  |  |           ${pkgs.xfsprogs.bin}/bin/xfs_quota -x -c 'project -s ${name}' ${opts.fileSystem} | 
					
						
							|  |  |  |           ${pkgs.xfsprogs.bin}/bin/xfs_quota -x -c 'limit -p ${limitOptions opts} ${name}' ${opts.fileSystem} | 
					
						
							| 
									
										
										
										
											2015-08-19 16:05:25 +02:00
										 |  |  |         '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         wantedBy = [ "multi-user.target" ]; | 
					
						
							|  |  |  |         after = [ ((replaceChars [ "/" ] [ "-" ] opts.fileSystem) + ".mount") ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 20:04:08 +02:00
										 |  |  |         restartTriggers = [ config.environment.etc.projects.source ]; | 
					
						
							| 
									
										
										
										
											2015-08-19 16:05:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         serviceConfig = { | 
					
						
							|  |  |  |           Type = "oneshot"; | 
					
						
							|  |  |  |           RemainAfterExit = true; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ) cfg.projects; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |