|
6 | 6 | ... |
7 | 7 | }: |
8 | 8 | let |
9 | | - inherit (lib.attrsets) |
10 | | - filterAttrs |
11 | | - mapAttrs' |
12 | | - mapAttrsToList |
13 | | - nameValuePair |
14 | | - ; |
15 | | - inherit (lib.strings) concatStringsSep; |
16 | | - inherit (lib.trivial) pipe; |
17 | | - script = |
18 | | - name: script: extra_pkgs: execer: fake: |
19 | | - (pkgs.resholve.writeScript name { |
20 | | - interpreter = lib.getExe pkgs.bash; |
21 | | - inputs = with pkgs; [ coreutils ] ++ extra_pkgs; |
22 | | - inherit execer fake; |
23 | | - } script).outPath; |
24 | | - validMounts = |
25 | | - _pipe: |
26 | | - lib.mkIf (config'.mounts ? gocryptfs && config'.mounts.gocryptfs != null) ( |
27 | | - pipe config'.mounts.gocryptfs _pipe |
28 | | - ); |
29 | | - map_cmd = |
30 | | - cmd: |
31 | | - (validMounts [ |
32 | | - (mapAttrsToList cmd) |
33 | | - (concatStringsSep "\n") |
34 | | - ]).content; |
| 9 | + cfg = config'.mounts.gocryptfs or { }; |
| 10 | + enabled = cfg != { }; |
35 | 11 | in |
36 | 12 | { |
37 | | - home.packages = with pkgs; [ gocryptfs ]; |
| 13 | + home.packages = lib.optionals enabled [ pkgs.gocryptfs ]; |
38 | 14 |
|
39 | | - age.secrets = validMounts [ |
40 | | - (filterAttrs (n: v: v ? authentication && v.authentication)) |
41 | | - (mapAttrs' (n: v: nameValuePair n { file = ./. + "../../../../Config/${n}.age"; })) |
42 | | - ]; |
| 15 | + age.secrets = lib.mkIf enabled ( |
| 16 | + lib.mapAttrs' (n: _: { |
| 17 | + name = n; |
| 18 | + value.file = ./. + "../../../../Config/${n}.age"; |
| 19 | + }) (lib.filterAttrs (_: v: v.authentication or false) cfg) |
| 20 | + ); |
43 | 21 |
|
44 | | - systemd.user.services.gocryptfs = { |
45 | | - Service = { |
46 | | - Type = "forking"; |
47 | | - Restart = "on-failure"; |
48 | | - RestartSec = 5; |
49 | | - ExecStartPre = |
50 | | - script "gocryptfs-premount.sh" (map_cmd (n: v: "mkdir -p ${v.mountpoint}")) [ ] [ ] |
51 | | - { }; |
52 | | - |
53 | | - ExecStart = script "gocryptfs-mount.sh" (map_cmd ( |
54 | | - n: v: |
55 | | - "gocryptfs ${v.source} ${v.mountpoint} ${ |
56 | | - lib.optionalString ( |
57 | | - v ? authentication && v.authentication |
58 | | - ) "-passfile ${config.age.secrets.${n}.path}" |
59 | | - }" |
60 | | - )) [ pkgs.gocryptfs ] [ "cannot:${lib.getExe' pkgs.gocryptfs "gocryptfs"}" ] { }; |
61 | | - |
62 | | - ExecStop = |
63 | | - script "gocryptfs-unmount.sh" (map_cmd (n: v: "fusermount -u ${v.mountpoint}")) |
64 | | - [ |
65 | | - pkgs.fuse |
66 | | - ] |
67 | | - [ ] |
68 | | - { external = [ "fusermount" ]; }; |
69 | | - |
70 | | - # Force gocryptfs and others to use the fusermount wrapper in /run/wrappers/bin, otherwise permissions error occurs. |
71 | | - Environment = [ "PATH=/run/wrappers/bin" ]; |
72 | | - }; |
73 | | - Install.WantedBy = [ "default.target" ]; |
74 | | - }; |
| 22 | + systemd.user.services = lib.mkIf enabled ( |
| 23 | + lib.mapAttrs' (n: v: { |
| 24 | + name = "gocryptfs-${n}"; |
| 25 | + value = { |
| 26 | + Unit.Description = "Mount gocryptfs: ${n}"; |
| 27 | + Service = { |
| 28 | + Type = "simple"; |
| 29 | + Restart = "on-failure"; |
| 30 | + RestartSec = "5s"; |
| 31 | + Environment = [ "PATH=/run/wrappers/bin" ]; |
| 32 | + ExecStartPre = "${pkgs.coreutils}/bin/mkdir -p ${v.mountpoint}"; |
| 33 | + ExecStart = |
| 34 | + "${lib.getExe' pkgs.gocryptfs "gocryptfs"} -f " |
| 35 | + + lib.optionalString (v.authentication or false) "-passfile ${config.age.secrets.${n}.path} " |
| 36 | + + "${v.source} ${v.mountpoint}"; |
| 37 | + ExecStop = "fusermount -u -z ${v.mountpoint}"; |
| 38 | + }; |
| 39 | + Install.WantedBy = [ "default.target" ]; |
| 40 | + }; |
| 41 | + }) cfg |
| 42 | + ); |
75 | 43 | } |
0 commit comments