at 24.11-pre 3.0 kB view raw
1# Configuration for the xfs_quota command 2 3{ config, lib, pkgs, ... }: 4 5let 6 7 cfg = config.programs.xfs_quota; 8 9 limitOptions = opts: builtins.concatStringsSep " " [ 10 (lib.optionalString (opts.sizeSoftLimit != null) "bsoft=${opts.sizeSoftLimit}") 11 (lib.optionalString (opts.sizeHardLimit != null) "bhard=${opts.sizeHardLimit}") 12 ]; 13 14in 15 16{ 17 18 ###### interface 19 20 options = { 21 22 programs.xfs_quota = { 23 projects = lib.mkOption { 24 default = {}; 25 type = lib.types.attrsOf (lib.types.submodule { 26 options = { 27 id = lib.mkOption { 28 type = lib.types.int; 29 description = "Project ID."; 30 }; 31 32 fileSystem = lib.mkOption { 33 type = lib.types.str; 34 description = "XFS filesystem hosting the xfs_quota project."; 35 default = "/"; 36 }; 37 38 path = lib.mkOption { 39 type = lib.types.str; 40 description = "Project directory."; 41 }; 42 43 sizeSoftLimit = lib.mkOption { 44 type = lib.types.nullOr lib.types.str; 45 default = null; 46 example = "30g"; 47 description = "Soft limit of the project size"; 48 }; 49 50 sizeHardLimit = lib.mkOption { 51 type = lib.types.nullOr lib.types.str; 52 default = null; 53 example = "50g"; 54 description = "Hard limit of the project size."; 55 }; 56 }; 57 }); 58 59 description = "Setup of xfs_quota projects. Make sure the filesystem is mounted with the pquota option."; 60 61 example = { 62 projname = { 63 id = 50; 64 path = "/xfsprojects/projname"; 65 sizeHardLimit = "50g"; 66 }; 67 }; 68 }; 69 }; 70 71 }; 72 73 74 ###### implementation 75 76 config = lib.mkIf (cfg.projects != {}) { 77 78 environment.etc.projects.source = pkgs.writeText "etc-project" 79 (builtins.concatStringsSep "\n" (lib.mapAttrsToList 80 (name: opts: "${builtins.toString opts.id}:${opts.path}") cfg.projects)); 81 82 environment.etc.projid.source = pkgs.writeText "etc-projid" 83 (builtins.concatStringsSep "\n" (lib.mapAttrsToList 84 (name: opts: "${name}:${builtins.toString opts.id}") cfg.projects)); 85 86 systemd.services = lib.mapAttrs' (name: opts: 87 lib.nameValuePair "xfs_quota-${name}" { 88 description = "Setup xfs_quota for project ${name}"; 89 script = '' 90 ${pkgs.xfsprogs.bin}/bin/xfs_quota -x -c 'project -s ${name}' ${opts.fileSystem} 91 ${pkgs.xfsprogs.bin}/bin/xfs_quota -x -c 'limit -p ${limitOptions opts} ${name}' ${opts.fileSystem} 92 ''; 93 94 wantedBy = [ "multi-user.target" ]; 95 after = [ ((builtins.replaceStrings [ "/" ] [ "-" ] opts.fileSystem) + ".mount") ]; 96 97 restartTriggers = [ config.environment.etc.projects.source ]; 98 99 serviceConfig = { 100 Type = "oneshot"; 101 RemainAfterExit = true; 102 }; 103 } 104 ) cfg.projects; 105 106 }; 107 108}