nixos xfs_quota: add new module for managing xfs_quota projects

authored by Luca Bruno and committed by Luca Bruno d8b9521d da8b5696

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