summary refs log tree commit diff
path: root/lib/sandbox.nix
blob: 414bf36f779f4fb74d6a31b3e835d7437febc54e (plain) (blame)
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
with import ./strings.nix;

/* Helpers for creating lisp S-exprs for the Apple sandbox

lib.sandbox.allowFileRead [ "/usr/bin/file" ];
  # => "(allow file-read* (literal \"/usr/bin/file\"))";

lib.sandbox.allowFileRead {
  literal = [ "/usr/bin/file" ];
  subpath = [ "/usr/lib/system" ];
}
  # => "(allow file-read* (literal \"/usr/bin/file\") (subpath \"/usr/lib/system\"))"
*/

let

sexp = tokens: "(" + builtins.concatStringsSep " " tokens + ")";
generateFileList = files:
  if builtins.isList files
    then concatMapStringsSep " " (x: sexp [ "literal" ''"${x}"'' ]) files
    else if builtins.isString files
      then generateFileList [ files ]
      else concatStringsSep " " (
        (map (x: sexp [ "literal" ''"${x}"'' ]) (files.literal or [])) ++
        (map (x: sexp [ "subpath" ''"${x}"'' ]) (files.subpath or []))
      );
applyToFiles = f: act: files: f "${act} ${generateFileList files}";
genActions = actionName: let
  action = feature: sexp [ actionName feature ];
  self = {
    "${actionName}" = action;
    "${actionName}File" = applyToFiles action "file*";
    "${actionName}FileRead" = applyToFiles action "file-read*";
    "${actionName}FileReadMetadata" = applyToFiles action "file-read-metadata";
    "${actionName}DirectoryList" = self."${actionName}FileReadMetadata";
    "${actionName}FileWrite" = applyToFiles action "file-write*";
    "${actionName}FileWriteMetadata" = applyToFiles action "file-write-metadata";
  };
  in self;

in

genActions "allow" // genActions "deny" // {
  importProfile = derivation: ''
    (import "${derivation}")
  '';
}