summaryrefslogtreecommitdiffstats
path: root/nixos/modules/programs/xfs_quota.nix
blob: 640d28824762fc9f07ae9ca91e94b9872b9d8495 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Configuration for the xfs_quota command

{
  config,
  lib,
  pkgs,
  ...
}:

let

  cfg = config.programs.xfs_quota;

  limitOptions =
    opts:
    builtins.concatStringsSep " " [
      (lib.optionalString (opts.sizeSoftLimit != null) "bsoft=${opts.sizeSoftLimit}")
      (lib.optionalString (opts.sizeHardLimit != null) "bhard=${opts.sizeHardLimit}")
    ];

in

{

  ###### interface

  options = {

    programs.xfs_quota = {
      projects = lib.mkOption {
        default = { };
        type = lib.types.attrsOf (
          lib.types.submodule {
            options = {
              id = lib.mkOption {
                type = lib.types.int;
                description = "Project ID.";
              };

              fileSystem = lib.mkOption {
                type = lib.types.str;
                description = "XFS filesystem hosting the xfs_quota project.";
                default = "/";
              };

              path = lib.mkOption {
                type = lib.types.str;
                description = "Project directory.";
              };

              sizeSoftLimit = lib.mkOption {
                type = lib.types.nullOr lib.types.str;
                default = null;
                example = "30g";
                description = "Soft limit of the project size";
              };

              sizeHardLimit = lib.mkOption {
                type = lib.types.nullOr lib.types.str;
                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 = {
          projname = {
            id = 50;
            path = "/xfsprojects/projname";
            sizeHardLimit = "50g";
          };
        };
      };
    };

  };

  ###### implementation

  config = lib.mkIf (cfg.projects != { }) {

    environment.etc.projects.source = pkgs.writeText "etc-project" (
      builtins.concatStringsSep "\n" (
        lib.mapAttrsToList (name: opts: "${toString opts.id}:${opts.path}") cfg.projects
      )
    );

    environment.etc.projid.source = pkgs.writeText "etc-projid" (
      builtins.concatStringsSep "\n" (
        lib.mapAttrsToList (name: opts: "${name}:${toString opts.id}") cfg.projects
      )
    );

    systemd.services = lib.mapAttrs' (
      name: opts:
      lib.nameValuePair "xfs_quota-${name}" {
        description = "Setup xfs_quota for project ${name}";
        script = ''
          ${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}
        '';

        wantedBy = [ "multi-user.target" ];
        after = [ ((builtins.replaceStrings [ "/" ] [ "-" ] opts.fileSystem) + ".mount") ];

        restartTriggers = [ config.environment.etc.projects.source ];

        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = true;
        };
      }
    ) cfg.projects;

  };

}