This repository has no description
2.2 kB
104 lines
1{ config, lib, pkgs, ... }:
2
3#TODO: implement recursive mode maybe?
4# enable different mods for files and folders
5
6let
7 inherit (pkgs)
8 writeScript
9 ;
10
11 inherit (lib)
12 concatMapStringsSep
13 concatStringsSep
14 mkEnableOption
15 mkIf
16 mkOption
17 types
18 ;
19
20 cfg = config.lass.folderPerms;
21
22 out = {
23 options.lass.folderPerms = api;
24 config = mkIf cfg.enable imp;
25 };
26
27 api = {
28 enable = mkEnableOption "folder permissions";
29 permissions = mkOption {
30 type = with types; listOf (submodule ({
31 options = {
32 path = mkOption {
33 type = str;
34 };
35 permission = mkOption {
36 type = nullOr str;
37 example = "755";
38 description = ''
39 basically anything that chmod takes as permission
40 '';
41 default = null;
42 };
43 owner = mkOption {
44 type = nullOr str;
45 example = "root:root";
46 description = ''
47 basically anything that chown takes as owner
48 '';
49 default = null;
50 };
51 };
52 }));
53 };
54 };
55
56 imp = {
57 systemd.services.lass-folderPerms = {
58 description = "lass-folderPerms";
59 wantedBy = [ "multi-user.target" ];
60
61 path = with pkgs; [
62 coreutils
63 ];
64
65 restartIfChanged = true;
66
67 serviceConfig = {
68 type = "simple";
69 RemainAfterExit = true;
70 Restart = "always";
71 ExecStart = "@${startScript}";
72 };
73 };
74 };
75
76 startScript = writeScript "lass-folderPerms" ''
77 ${concatMapStringsSep "\n" writeCommand cfg.permissions}
78 '';
79
80 writeCommand = fperm:
81 concatStringsSep "\n" [
82 (buildPermission fperm)
83 (buildOwner fperm)
84 ];
85
86 buildPermission = perm:
87 #TODO: create folder maybe
88 #TODO: check if permission is valid
89 if (perm.permission == null) then
90 ""
91 else
92 "chmod ${perm.permission} ${perm.path}"
93 ;
94
95 buildOwner = perm:
96 #TODO: create folder maybe
97 #TODO: check if owner/group valid
98 if (perm.owner == null) then
99 ""
100 else
101 "chown ${perm.owner} ${perm.path}"
102 ;
103
104in out