summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/bindfs.nix
blob: 60736710f59e36bfa9f028eb67c165e135d199ae (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{ config, pkgs, lib, ... }:
with lib;
let
  cfg = config.krebs.bindfs;
in {
  options.krebs.bindfs = mkOption {
    type = types.attrsOf (types.submodule ({ config, ... }: {
      options = {
        target = mkOption {
          description = ''
            destination where bindfs mounts to.
            second positional argument to bindfs.
          '';
          default = config._module.args.name;
          type = types.absolute-pathname;
        };
        source = mkOption {
          description = ''
            source folder where the mounted directory is originally.
            first positional argument to bindfs.
          '';
          type = types.absolute-pathname;
        };
        options = mkOption {
          description = ''
            additional arguments to bindfs
          '';
          type = types.listOf types.str;
          default = [];
        };
        clearTarget = mkOption {
          description = ''
            whether to clear the target folder before mounting
          '';
          type = types.bool;
          default = false;
        };
      };
    }));
    default = {};
  };

  config = mkIf (cfg != {}) {
    systemd.services = mapAttrs' (n: mount: let
      name = replaceStrings [ "/" ] [ "_" ] n;
    in nameValuePair "bindfs-${name}" {
      wantedBy = [ "local-fs.target" ];
      path = [ pkgs.coreutils ];
      serviceConfig = {
        ExecStartPre = pkgs.writeDash "bindfs-init-${name}" ''
          ${optionalString mount.clearTarget ''
            rm -rf '${mount.target}'
          ''}
          mkdir -p '${mount.source}'
          mkdir -p '${mount.target}'
        '';
        ExecStart = "${pkgs.bindfs}/bin/bindfs -f ${concatStringsSep " " mount.options} ${mount.source} ${mount.target}";
      };
    }) cfg;
  };
}