1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 inherit (config.security) wrapperDir;
9 cfg = config.services.kbfs;
10
11in
12{
13
14 ###### interface
15
16 options = {
17
18 services.kbfs = {
19
20 enable = lib.mkOption {
21 type = lib.types.bool;
22 default = false;
23 description = "Whether to mount the Keybase filesystem.";
24 };
25
26 enableRedirector = lib.mkOption {
27 type = lib.types.bool;
28 default = false;
29 description = ''
30 Whether to enable the Keybase root redirector service, allowing
31 any user to access KBFS files via `/keybase`,
32 which will show different contents depending on the requester.
33 '';
34 };
35
36 mountPoint = lib.mkOption {
37 type = lib.types.str;
38 default = "%h/keybase";
39 example = "/keybase";
40 description = "Mountpoint for the Keybase filesystem.";
41 };
42
43 extraFlags = lib.mkOption {
44 type = lib.types.listOf lib.types.str;
45 default = [ ];
46 example = [
47 "-label kbfs"
48 "-mount-type normal"
49 ];
50 description = ''
51 Additional flags to pass to the Keybase filesystem on launch.
52 '';
53 };
54
55 };
56 };
57
58 ###### implementation
59
60 config = lib.mkIf cfg.enable (
61 lib.mkMerge [
62 {
63 # Upstream: https://github.com/keybase/client/blob/master/packaging/linux/systemd/kbfs.service
64 systemd.user.services.kbfs = {
65 description = "Keybase File System";
66
67 # Note that the "Requires" directive will cause a unit to be restarted whenever its dependency is restarted.
68 # Do not issue a hard dependency on keybase, because kbfs can reconnect to a restarted service.
69 # Do not issue a hard dependency on keybase-redirector, because it's ok if it fails (e.g., if it is disabled).
70 wants = [ "keybase.service" ] ++ lib.optional cfg.enableRedirector "keybase-redirector.service";
71 path = [ "/run/wrappers" ];
72 unitConfig.ConditionUser = "!@system";
73
74 serviceConfig = {
75 Type = "notify";
76 # Keybase notifies from a forked process
77 EnvironmentFile = [
78 "-%E/keybase/keybase.autogen.env"
79 "-%E/keybase/keybase.env"
80 ];
81 ExecStartPre = [
82 "${pkgs.coreutils}/bin/mkdir -p \"${cfg.mountPoint}\""
83 "-${wrapperDir}/fusermount -uz \"${cfg.mountPoint}\""
84 ];
85 ExecStart = "${pkgs.kbfs}/bin/kbfsfuse ${toString cfg.extraFlags} \"${cfg.mountPoint}\"";
86 ExecStop = "${wrapperDir}/fusermount -uz \"${cfg.mountPoint}\"";
87 Restart = "on-failure";
88 PrivateTmp = true;
89 };
90 wantedBy = [ "default.target" ];
91 };
92
93 services.keybase.enable = true;
94
95 environment.systemPackages = [ pkgs.kbfs ];
96 }
97
98 (lib.mkIf cfg.enableRedirector {
99 security.wrappers."keybase-redirector".source = "${pkgs.kbfs}/bin/redirector";
100
101 systemd.tmpfiles.settings."10-kbfs"."/keybase".d = {
102 user = "root";
103 group = "root";
104 mode = "0755";
105 age = "0";
106 };
107
108 # Upstream: https://github.com/keybase/client/blob/master/packaging/linux/systemd/keybase-redirector.service
109 systemd.user.services.keybase-redirector = {
110 description = "Keybase Root Redirector for KBFS";
111 wants = [ "keybase.service" ];
112 unitConfig.ConditionUser = "!@system";
113
114 serviceConfig = {
115 EnvironmentFile = [
116 "-%E/keybase/keybase.autogen.env"
117 "-%E/keybase/keybase.env"
118 ];
119 # Note: The /keybase mount point is not currently configurable upstream.
120 ExecStart = "${wrapperDir}/keybase-redirector /keybase";
121 Restart = "on-failure";
122 PrivateTmp = true;
123 };
124
125 wantedBy = [ "default.target" ];
126 };
127 })
128 ]
129 );
130}