aboutsummaryrefslogblamecommitdiff
path: root/modules/nas-client.nix
blob: fe0952e4520e7580a7d0837f95f8f7b4b2213488 (plain) (tree)



















































































                                                                                                                                                        
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.my.modules.nas-client;
in
{
  options.my.modules.nas-client = with lib; {
    enable = mkEnableOption "NAS client";

    volumes = mkOption {
      type = types.attrsOf (
        types.submodule {
          options = {
            server = mkOption {
              type = types.str;
              example = "nas";
              description = "Hostname of the server to connect to.";
            };
            remotePath = mkOption {
              type = types.str;
              example = "data";
              description = "Remote path on the NAS to mount.";
            };
            mountPoint = mkOption {
              type = types.str;
              description = "Local directory where the volume will be mounted.";
            };
            uid = mkOption {
              type = types.int;
              default = 1000;
              description = "User ID for mounted files.";
            };
            gid = mkOption {
              type = types.int;
              default = 1000;
              description = "Group ID for mounted files.";
            };
            options = mkOption {
              type = types.str;
              default = "rw";
              description = "Additional mount options.";
            };
          };
        }
      );
      default = { };
      description = "NAS volumes to mount.";
    };
  };

  config = lib.mkIf cfg.enable {
    boot.kernelModules = [
      "cifs"
      "cmac"
      "sha256"
    ];

    # this is required to get the credentials options to work
    environment.systemPackages = [ pkgs.cifs-utils ];

    systemd.mounts = lib.mapAttrsToList (name: volume: {
      description = "Mount for NAS volume ${name}";
      what = "//${volume.server}/${volume.remotePath}";
      where = volume.mountPoint;
      unitConfig = {
        # This ensures it uses mount.cifs
        Type = "cifs";
      };
      type = "cifs"; # Explicitly specify CIFS type otherwise we ran into issues when using the credentials file option
      options = "credentials=${config.age.secrets.nas_client_credentials.path},uid=${toString volume.uid},gid=${toString volume.gid},${volume.options}";
    }) cfg.volumes;

    systemd.automounts = lib.mapAttrsToList (name: volume: {
      description = "Automount for NAS volume ${name}";
      where = volume.mountPoint;
      wantedBy = [ "multi-user.target" ];
    }) cfg.volumes;
  };
}