summaryrefslogtreecommitdiffstats
path: root/lass/3modules/folderPerms.nix
blob: bb032032706a854ab411c195fc4b6e7e984d3646 (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
{ config, lib, pkgs, ... }:

#TODO: implement recursive mode maybe?
# enable different mods for files and folders

let
  inherit (pkgs)
    writeScript
  ;

  inherit (lib)
    concatMapStringsSep
    concatStringsSep
    mkEnableOption
    mkIf
    mkOption
    types
  ;

  cfg = config.lass.folderPerms;

  out = {
    options.lass.folderPerms = api;
    config = mkIf cfg.enable imp;
  };

  api = {
    enable = mkEnableOption "folder permissions";
    permissions = mkOption {
      type = with types; listOf (submodule ({
        options = {
          path = mkOption {
            type = str;
          };
          permission = mkOption {
            type = nullOr str;
            example = "755";
            description = ''
              basically anything that chmod takes as permission
            '';
            default = null;
          };
          owner = mkOption {
            type = nullOr str;
            example = "root:root";
            description = ''
              basically anything that chown takes as owner
            '';
            default = null;
          };
        };
      }));
    };
  };

  imp = {
    systemd.services.lass-folderPerms = {
      description = "lass-folderPerms";
      wantedBy = [ "multi-user.target" ];

      path = with pkgs; [
        coreutils
      ];

      restartIfChanged = true;

      serviceConfig = {
        type = "simple";
        RemainAfterExit = true;
        Restart = "always";
        ExecStart = "@${startScript}";
      };
    };
  };

  startScript = writeScript "lass-folderPerms" ''
    ${concatMapStringsSep "\n" writeCommand cfg.permissions}
  '';

  writeCommand = fperm:
    concatStringsSep "\n" [
      (buildPermission fperm)
      (buildOwner fperm)
    ];

  buildPermission = perm:
    #TODO: create folder maybe
    #TODO: check if permission is valid
    if (perm.permission == null) then
      ""
    else
      "chmod ${perm.permission} ${perm.path}"
  ;

  buildOwner = perm:
    #TODO: create folder maybe
    #TODO: check if owner/group valid
    if (perm.owner == null) then
      ""
    else
      "chown ${perm.owner} ${perm.path}"
  ;

in out