| 1 | { config, lib, pkgs, ... }: |
| 2 | let |
| 3 | workspace = builtins.fromJSON (builtins.readFile ../config/mac-studio-sandbox.json); |
| 4 | dataset = workspace.dataset; |
| 5 | mountPoint = workspace.mountPoint; |
| 6 | parent = builtins.dirOf dataset; |
| 7 | checkMount = "${pkgs.runtimeShell} ${../service/samba/check-mount.sh} ${mountPoint} ${dataset}"; |
| 8 | in |
| 9 | { |
| 10 | systemd.services.mac-studio-sandbox = { |
| 11 | wantedBy = [ "multi-user.target" ]; |
| 12 | after = [ "zfs-mount.service" ]; |
| 13 | before = [ "nfs-mountd.service" "nfs-server.service" ]; |
| 14 | requiredBy = [ "nfs-mountd.service" "nfs-server.service" ]; |
| 15 | path = [ pkgs.zfs pkgs.util-linux pkgs.coreutils pkgs.findutils pkgs.gawk ]; |
| 16 | serviceConfig = { Type = "oneshot"; RemainAfterExit = true; }; |
| 17 | script = '' |
| 18 | test "$(zfs get -H -o value keystatus ${parent})" = available |
| 19 | test "$(zfs get -H -o value encryptionroot ${parent})" = ${parent} |
| 20 | test "$(readlink -m ${mountPoint})" = ${mountPoint} |
| 21 | if mountpoint -q ${mountPoint}; then |
| 22 | ${checkMount} |
| 23 | else |
| 24 | test ! -e ${mountPoint} || { test -d ${mountPoint} && test -z "$(find ${mountPoint} -mindepth 1 -maxdepth 1 -print -quit)"; } |
| 25 | fi |
| 26 | fresh=false |
| 27 | if ! zfs list -H -o name ${dataset} >/dev/null 2>&1; then |
| 28 | mkdir -p ${builtins.dirOf mountPoint} |
| 29 | zfs create -u -o mountpoint=${mountPoint} -o casesensitivity=sensitive \ |
| 30 | -o compression=lz4 -o atime=off -o sync=standard -o acltype=posix -o xattr=sa ${dataset} |
| 31 | fresh=true |
| 32 | fi |
| 33 | test "$(zfs get -H -o value mountpoint ${dataset})" = ${mountPoint} |
| 34 | test "$(zfs get -H -o value encryptionroot ${dataset})" = ${parent} |
| 35 | test "$(zfs get -H -o value casesensitivity ${dataset})" = sensitive |
| 36 | test "$(zfs get -H -o value sync ${dataset})" = standard |
| 37 | test "$(zfs get -H -o value checksum ${dataset})" != off |
| 38 | if test "$(zfs get -H -o value mounted ${dataset})" != yes; then |
| 39 | zfs mount ${dataset} |
| 40 | fi |
| 41 | ${checkMount} |
| 42 | if $fresh; then |
| 43 | chown ${toString config.users.users.clo.uid}:${toString config.users.groups.chloe.gid} ${mountPoint} |
| 44 | chmod 2770 ${mountPoint} |
| 45 | fi |
| 46 | ''; |
| 47 | }; |
| 48 | |
| 49 | services.nfs.server = { |
| 50 | enable = true; |
| 51 | hostName = "10.0.0.1"; |
| 52 | exports = '' |
| 53 | ${mountPoint} ${workspace.clientAddress}(rw,sync,fsid=0,no_subtree_check,all_squash,anonuid=${toString config.users.users.clo.uid},anongid=${toString config.users.groups.chloe.gid},insecure) |
| 54 | ''; |
| 55 | }; |
| 56 | environment.etc."exports.d/snowglobe-work.exports".text = '' |
| 57 | ${mountPoint}/.work ${workspace.clientAddress}(rw,sync,fsid=42,no_subtree_check,all_squash,anonuid=${toString config.users.users.clo.uid},anongid=${toString config.users.groups.chloe.gid},insecure) |
| 58 | ''; |
| 59 | systemd.services.mac-work-share-check = { |
| 60 | requires = [ "zfs-import-work.service" ]; |
| 61 | after = [ "zfs-import-work.service" "zfs-mount.service" ]; |
| 62 | serviceConfig = { Type = "oneshot"; RemainAfterExit = true; }; |
| 63 | script = '' |
| 64 | test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv/work)" = work |
| 65 | ''; |
| 66 | }; |
| 67 | systemd.mounts = [{ |
| 68 | what = "/srv/work"; |
| 69 | where = "${mountPoint}/.work"; |
| 70 | type = "none"; |
| 71 | options = "bind"; |
| 72 | requires = [ "mac-work-share-check.service" "mac-studio-sandbox.service" ]; |
| 73 | after = [ "mac-work-share-check.service" "mac-studio-sandbox.service" ]; |
| 74 | before = [ "nfs-server.service" ]; |
| 75 | wantedBy = [ "multi-user.target" ]; |
| 76 | }]; |
| 77 | services.nfs.settings.nfsd = { |
| 78 | vers3 = false; |
| 79 | "vers4.0" = true; |
| 80 | udp = false; |
| 81 | tcp = true; |
| 82 | }; |
| 83 | |
| 84 | systemd.services.nfs-mountd = { |
| 85 | path = [ pkgs.gawk ]; |
| 86 | preStart = lib.mkBefore checkMount; |
| 87 | }; |
| 88 | systemd.services.nfs-server = { |
| 89 | path = [ pkgs.gawk ]; |
| 90 | preStart = lib.mkBefore checkMount; |
| 91 | }; |
| 92 | } |