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